|
|
|
@ -1,7 +1,6 @@
|
|
|
|
|
import pytest
|
|
|
|
|
from mock import call
|
|
|
|
|
from calculate.utils.calculateini import CalculateIniParser
|
|
|
|
|
Define = CalculateIniParser.Define
|
|
|
|
|
from calculate.vars.vars_loader import CalculateIniParser, Define
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.vars
|
|
|
|
@ -13,118 +12,112 @@ class TestCalculateIni:
|
|
|
|
|
# assert ns.varPath is None
|
|
|
|
|
|
|
|
|
|
def test_section_values(self, mocker):
|
|
|
|
|
cip = CalculateIniParser()
|
|
|
|
|
spy_section = mocker.spy(cip, 'start_section')
|
|
|
|
|
spy_def_key = mocker.spy(cip, 'define_key')
|
|
|
|
|
spy_error = mocker.spy(cip, 'parse_error')
|
|
|
|
|
ini_parser = CalculateIniParser()
|
|
|
|
|
|
|
|
|
|
cip.parse("[section]\n"
|
|
|
|
|
"varval1 = value1\n")
|
|
|
|
|
input_text = ("[section]\n"
|
|
|
|
|
"varval1 = value1\n")
|
|
|
|
|
|
|
|
|
|
spy_error.assert_not_called()
|
|
|
|
|
spy_section.assert_has_calls([call(['section'])])
|
|
|
|
|
spy_def_key.assert_has_calls([
|
|
|
|
|
call(['section'], 'varval1', 'value1', Define.assign),
|
|
|
|
|
])
|
|
|
|
|
parsed_lines = [{'start_section': (['section'],)},
|
|
|
|
|
{'define_key': (['section'], 'varval1',
|
|
|
|
|
'value1', Define.assign)},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
parse_result = list(ini_parser.parse(input_text))
|
|
|
|
|
for parsed_line, result_line in zip(parsed_lines, parse_result):
|
|
|
|
|
assert parsed_line == result_line
|
|
|
|
|
|
|
|
|
|
def test_simple_calculate_ini_with_comments(self, mocker):
|
|
|
|
|
cip = CalculateIniParser()
|
|
|
|
|
spy_section = mocker.spy(cip, 'start_section')
|
|
|
|
|
spy_def_key = mocker.spy(cip, 'define_key')
|
|
|
|
|
spy_error = mocker.spy(cip, 'parse_error')
|
|
|
|
|
|
|
|
|
|
cip.parse("[section]\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"# some comment\n"
|
|
|
|
|
"varval2 += value2\n"
|
|
|
|
|
"varval3 -= value3\n")
|
|
|
|
|
spy_error.assert_not_called()
|
|
|
|
|
spy_section.assert_has_calls([call(['section'])])
|
|
|
|
|
spy_def_key.assert_has_calls([
|
|
|
|
|
call(['section'], 'varval1', 'value1', Define.assign),
|
|
|
|
|
call(['section'], 'varval2', 'value2', Define.append),
|
|
|
|
|
call(['section'], 'varval3', 'value3', Define.remove),
|
|
|
|
|
])
|
|
|
|
|
ini_parser = CalculateIniParser()
|
|
|
|
|
|
|
|
|
|
input_text = ("[section]\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"# some comment\n"
|
|
|
|
|
"varval2 += value2\n"
|
|
|
|
|
"varval3 -= value3\n")
|
|
|
|
|
|
|
|
|
|
parsed_lines = [{'start_section': (['section'],)},
|
|
|
|
|
{'define_key': (['section'], 'varval1',
|
|
|
|
|
'value1', Define.assign)},
|
|
|
|
|
{'define_key': (['section'], 'varval2',
|
|
|
|
|
'value2', Define.append)},
|
|
|
|
|
{'define_key': (['section'], 'varval3',
|
|
|
|
|
'value3', Define.remove)},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
parse_result = list(ini_parser.parse(input_text))
|
|
|
|
|
for parsed_line, result_line in zip(parsed_lines, parse_result):
|
|
|
|
|
assert parsed_line == result_line
|
|
|
|
|
|
|
|
|
|
def test_some_complex_section_calculate_ini(self, mocker):
|
|
|
|
|
cip = CalculateIniParser()
|
|
|
|
|
spy_section = mocker.spy(cip, 'start_section')
|
|
|
|
|
spy_def_key = mocker.spy(cip, 'define_key')
|
|
|
|
|
spy_error = mocker.spy(cip, 'parse_error')
|
|
|
|
|
|
|
|
|
|
cip.parse("[section][sub]\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"varval2 = value2\n"
|
|
|
|
|
"[section][sub2]\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"[section2]\n"
|
|
|
|
|
"varval1 = value1\n")
|
|
|
|
|
|
|
|
|
|
spy_error.assert_not_called()
|
|
|
|
|
spy_section.assert_has_calls([call(['section', 'sub'])])
|
|
|
|
|
spy_section.assert_has_calls([call(['section', 'sub2'])])
|
|
|
|
|
spy_section.assert_has_calls([call(['section2'])])
|
|
|
|
|
spy_def_key.assert_has_calls([
|
|
|
|
|
call(['section', 'sub'], 'varval1', 'value1', Define.assign),
|
|
|
|
|
call(['section', 'sub'], 'varval2', 'value2', Define.assign),
|
|
|
|
|
call(['section', 'sub2'], 'varval1', 'value1', Define.assign),
|
|
|
|
|
call(['section2'], 'varval1', 'value1', Define.assign),
|
|
|
|
|
])
|
|
|
|
|
ini_parser = CalculateIniParser()
|
|
|
|
|
|
|
|
|
|
input_text = ("[section][sub]\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"varval2 = value2\n"
|
|
|
|
|
"[section][sub2]\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"[section2]\n"
|
|
|
|
|
"varval1 = value1\n")
|
|
|
|
|
|
|
|
|
|
parsed_lines = [{'start_section': (['section', 'sub'],)},
|
|
|
|
|
{'define_key': (['section', 'sub'],
|
|
|
|
|
'varval1', 'value1', Define.assign)},
|
|
|
|
|
{'define_key': (['section', 'sub'],
|
|
|
|
|
'varval2', 'value2', Define.assign)},
|
|
|
|
|
{'start_section': (['section', 'sub2'],)},
|
|
|
|
|
{'define_key': (['section', 'sub2'],
|
|
|
|
|
'varval1', 'value1', Define.assign)},
|
|
|
|
|
{'start_section': (['section2'],)},
|
|
|
|
|
{'define_key': (['section2'], 'varval1',
|
|
|
|
|
'value1', Define.assign)}]
|
|
|
|
|
|
|
|
|
|
parse_result = list(ini_parser.parse(input_text))
|
|
|
|
|
for parsed_line, result_line in zip(parsed_lines, parse_result):
|
|
|
|
|
assert parsed_line == result_line
|
|
|
|
|
|
|
|
|
|
def test_error(self, mocker):
|
|
|
|
|
cip = CalculateIniParser()
|
|
|
|
|
spy_section = mocker.spy(cip, 'start_section')
|
|
|
|
|
spy_def_key = mocker.spy(cip, 'define_key')
|
|
|
|
|
spy_error = mocker.spy(cip, 'parse_error')
|
|
|
|
|
|
|
|
|
|
cip.parse("[section\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"varval2 = value2\n"
|
|
|
|
|
"[section][sub2]\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"eee\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"[section2\n"
|
|
|
|
|
"varval4 = value4\n"
|
|
|
|
|
"[section][]\n"
|
|
|
|
|
"[section][][sub]\n"
|
|
|
|
|
"[section][][sub][]\n")
|
|
|
|
|
|
|
|
|
|
# проверяем, что расозналась только одна секция
|
|
|
|
|
spy_section.assert_has_calls([call(['section', 'sub2'])])
|
|
|
|
|
# проверяем, что значение из криво определённой section2 попало
|
|
|
|
|
# в section.sub2
|
|
|
|
|
spy_def_key.assert_has_calls([
|
|
|
|
|
call(['section', 'sub2'], 'varval1', 'value1', Define.assign),
|
|
|
|
|
call(['section', 'sub2'], 'varval4', 'value4', Define.assign),
|
|
|
|
|
])
|
|
|
|
|
# проверяем, все нераспознанные строки попали в ошибки
|
|
|
|
|
# криво объявленная первая секция
|
|
|
|
|
# её переменные
|
|
|
|
|
# кривая переменная eee
|
|
|
|
|
# криво объявленная section2
|
|
|
|
|
spy_error.assert_has_calls([
|
|
|
|
|
call('[section', 1, 1),
|
|
|
|
|
call('varval1 = value1', 2, 1),
|
|
|
|
|
call('varval2 = value2', 3, 1),
|
|
|
|
|
call('eee', 6, 1),
|
|
|
|
|
call('[section2', 8, 1),
|
|
|
|
|
call('[section][][sub]', 11, 1),
|
|
|
|
|
call('[section][][sub][]', 12, 1),
|
|
|
|
|
])
|
|
|
|
|
ini_parser = CalculateIniParser()
|
|
|
|
|
|
|
|
|
|
input_text = ("[section\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"varval2 = value2\n"
|
|
|
|
|
"[section][sub2]\n"
|
|
|
|
|
"varval1 = value1\n"
|
|
|
|
|
"eee\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"[section2\n"
|
|
|
|
|
"varval4 = value4\n"
|
|
|
|
|
"[section][]\n"
|
|
|
|
|
"[section][][sub]\n"
|
|
|
|
|
"[section][][sub][]\n")
|
|
|
|
|
|
|
|
|
|
parsed_lines = [{'start_section': (['section', 'sub2'],)},
|
|
|
|
|
{'define_key': (['section', 'sub2'],
|
|
|
|
|
'varval1', 'value1', Define.assign)},
|
|
|
|
|
{'define_key': (['section', 'sub2'],
|
|
|
|
|
'varval4', 'value4', Define.assign)},
|
|
|
|
|
{'clear_section': (['section'],)}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
errors = [('[section', 1, 1),
|
|
|
|
|
('varval1 = value1', 2, 1),
|
|
|
|
|
('varval2 = value2', 3, 1),
|
|
|
|
|
('eee', 6, 1),
|
|
|
|
|
('[section2', 8, 1),
|
|
|
|
|
('[section][][sub]', 11, 1),
|
|
|
|
|
('[section][][sub][]', 12, 1),
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
parse_result = list(ini_parser.parse(input_text))
|
|
|
|
|
|
|
|
|
|
for error, parsed_error in zip(errors, ini_parser.errors):
|
|
|
|
|
assert parsed_error == error
|
|
|
|
|
for parsed_line, result_line in zip(parsed_lines, parse_result):
|
|
|
|
|
assert parsed_line == result_line
|
|
|
|
|
|
|
|
|
|
def test_clear_section(self, mocker):
|
|
|
|
|
cip = CalculateIniParser()
|
|
|
|
|
spy_section = mocker.spy(cip, 'start_section')
|
|
|
|
|
spy_def_key = mocker.spy(cip, 'define_key')
|
|
|
|
|
spy_error = mocker.spy(cip, 'parse_error')
|
|
|
|
|
spy_clear_sec = mocker.spy(cip, 'clear_section')
|
|
|
|
|
|
|
|
|
|
cip.parse("[section][test][]\n")
|
|
|
|
|
|
|
|
|
|
spy_error.assert_not_called()
|
|
|
|
|
spy_def_key.assert_not_called()
|
|
|
|
|
spy_section.assert_not_called()
|
|
|
|
|
ini_parser = CalculateIniParser()
|
|
|
|
|
|
|
|
|
|
spy_clear_sec.assert_has_calls([call(['section', 'test'])])
|
|
|
|
|
parse_result = next(ini_parser.parse("[section][test][]\n"))
|
|
|
|
|
assert parse_result == {'clear_section': (['section', 'test'],)}
|
|
|
|
|