Newer
Older

Karine PARRA
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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)