from src.config_parser.ini import INIConfiguration, INIConfigurationGroup from src.config_parser.exceptions import INISyntaxError def test_generating(): config = INIConfiguration() config.test_group = INIConfigurationGroup('test_group') config.test_group.key1 = 'value1' config.test_group.key2 = 'value2' assert config.to_string() == '''[test_group] key1=value1 key2=value2''' def test_parsing(): # Test deserializer config_string = ''' property1 = 42 property2=123 [configuration_group1] hello=world key1=value1 [group2] world=hel#lo key2 = "val#ue2" [] hello2=world2 invalid_line1 invalid_line2 # comment''' assert dict(INIConfiguration.from_string(config_string, ignore_errors=True)) == {'configuration_group1': {'hello': 'world', 'key1': 'value1'}, 'group2': {'world': 'hel', 'key2': 'val#ue2', 'hello2': 'world2'}} try: INIConfiguration.from_string('key="value') assert False, 'Expected INISyntaxError for unterminated literal' except INISyntaxError: pass try: INIConfiguration.from_string('key=value"') assert False, 'Expected INISyntaxError for unterminated literal' except INISyntaxError: pass