Skip to content
Snippets Groups Projects
test_steering_file.py 2.66 KiB
Newer Older
import pytest
from source.steering_file import *


@pytest.fixture
def steering_file():
    """Fixture to create a sample SteeringFile object for testing."""
    return SteeringFile(
        filename="test_file.txt",
        content='parameter1 = "value1"\nparameter2 = "value2"\n'
    )


def test_find_in_steering_file_success(steering_file):
    regex = r'parameter1\s*=\s*"(?P<content>[^"]+)"'
    result = steering_file.find_in_steering_file(regex)
    assert result == "value1"


def test_find_in_steering_file_success_with_single_quotes(steering_file):
    steering_file.content = "parameter1 = 'value1'"
    regex = r'parameter1\s*=\s*\'(?P<content>[^\']+)\''
    result = steering_file.find_in_steering_file(regex)
    assert result == "value1"


def test_find_in_steering_file_not_found(steering_file):
    regex = r'parameter3\s*=\s*"(?P<content>[^"]+)"'
    with pytest.raises(ValueError, match="Unable to find"):
        steering_file.find_in_steering_file(regex)


def test_update_steering_file_success(steering_file):
    regex = r'parameter1\s*=\s*"(?P<content>[^"]+)"'
    new_content = "new_value1"
    steering_file.update_steering_file(regex, new_content)
    assert steering_file.content == 'parameter1 = "new_value1"\nparameter2 = "value2"\n'


def test_update_steering_file_not_found(steering_file):
    regex = r'parameter3\s*=\s*"(?P<content>[^"]+)"'
    new_content = "new_value3"
    with pytest.raises(ValueError, match="Unable to find"):
        steering_file.update_steering_file(regex, new_content)


def test_update_steering_file_with_single_quotes(steering_file):
    steering_file.content = "parameter1 = 'value1'\nparameter2 = 'value2'"
    regex = r'parameter1\s*=\s*\'(?P<content>[^\']+)\''
    new_content = "new_value1"
    steering_file.update_steering_file(regex, new_content)
    assert steering_file.content == "parameter1 = 'new_value1'\nparameter2 = 'value2'"


def test_update_steering_file_multiple_occurrences(steering_file):
    steering_file.content = 'parameter1 = "value1"\nparameter1 = "value2"\n'
    regex = r'parameter1\s*=\s*"(?P<content>[^"]+)"'
    new_content = "new_value1"
    steering_file.update_steering_file(regex, new_content)
    assert steering_file.content == 'parameter1 = "new_value1"\nparameter1 = "value2"\n'


def test_find_in_steering_file_with_no_named_group(steering_file):
    regex = r'parameter1\s*=\s*"(.*?)"'  # No named group
    with pytest.raises(IndexError):
        steering_file.find_in_steering_file(regex)


def test_find_in_steering_file_with_no_match(steering_file):
    regex = r'parameter3\s*=\s*"(.*?)"'  # No match
    with pytest.raises(ValueError, match="Unable to find"):
        steering_file.find_in_steering_file(regex)