diff options
-rw-r--r-- | pom_parser/__init__.py | 14 | ||||
-rw-r--r-- | tests/__init__.py | 11 | ||||
-rw-r--r-- | tests/interpretation.py | 39 |
3 files changed, 60 insertions, 4 deletions
diff --git a/pom_parser/__init__.py b/pom_parser/__init__.py index 3f1cce2..8c86e7b 100644 --- a/pom_parser/__init__.py +++ b/pom_parser/__init__.py @@ -1,3 +1,6 @@ +# TODO: +# - clean up read_conf example +# - add all_functions example r'''Configuration for the [POM configuration file format](https://www.pom.computer). \mainpage pom_parser @@ -93,6 +96,8 @@ Attributes return None if not all(c in '0123456789' for c in s): return None + if s == '': + return None value = int(s) if value >> 53: return None @@ -120,7 +125,10 @@ Attributes if c == '.' and (i == 0 or i == len(value)-1 or \ not value[i+1].isdigit() or not value[i-1].isdigit()): return None - return float(value) + try: + return float(value) + except ValueError: + return None def _parse_bool(self) -> Optional[bool]: value = self.value @@ -136,7 +144,7 @@ Attributes entry: list[str] = [] while (c := next(chars, '')): if c == ',': - list_.append(''.join(entry).strip(' \t')) + list_.append(''.join(entry).strip(' \t\n')) entry = [] elif c == '\\': c = next(chars, '') @@ -145,7 +153,7 @@ Attributes entry.append(c) else: entry.append(c) - last_entry = ''.join(entry).strip(' \t') + last_entry = ''.join(entry).strip(' \t\n') if last_entry: list_.append(last_entry) return list_ diff --git a/tests/__init__.py b/tests/__init__.py index 5616c23..8bfbe38 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,6 +1,7 @@ import unittest import os -from tests import parsing, errors, location +from tests import parsing, errors, location, interpretation +import sys class TestParsing(unittest.TestCase): def test_all(self) -> None: @@ -26,6 +27,14 @@ class TestLocation(unittest.TestCase): with self.subTest(file): location.test_path(self, f'{test_dir}/{file}') +class TestInterpretation(unittest.TestCase): + def test_all(self) -> None: + test_dir = '../tests/interpretation' + for file in os.listdir(test_dir): + if not file.endswith('.pom'): continue + with self.subTest(file): + interpretation.test_path(self, f'{test_dir}/{file}') + if __name__ == '__main__': unittest.main() diff --git a/tests/interpretation.py b/tests/interpretation.py new file mode 100644 index 0000000..6841a49 --- /dev/null +++ b/tests/interpretation.py @@ -0,0 +1,39 @@ +import pom_parser +import unittest + +def test_list(tester: unittest.TestCase, path: str) -> None: + conf = pom_parser.load_path(path) + for key in conf.keys(): + section = conf.section(key) + sep = section.get('sep') + list_ = section.get_list('list') + assert sep is not None + sep_list = sep.split(';') + sep_list.pop() + tester.assertEqual(sep_list, list_, f'List values for {key} disagree.') + +def test_path(tester: unittest.TestCase, path: str) -> None: + if 'list' in path: + test_list(tester, path) + return + conf = pom_parser.load_path(path) + getters = [ + ('uint', pom_parser.Configuration.get_uint), + ('int', pom_parser.Configuration.get_int), + ('float', pom_parser.Configuration.get_float), + ('bool', pom_parser.Configuration.get_bool), + ] + (name, getter) = next((name, g) for (name, g) in getters if path.endswith(f'{name}.pom')) + good = conf.section('good') + bad = conf.section('bad') + for key in good.keys(): + section = good.section(key) + value_a = getter(section, 'a') + value_b = getter(section, 'b') + tester.assertEqual(value_a, value_b, f'Values for {key} disagree.') + for key in bad.keys(): + try: + getter(bad, key) + tester.fail(f'Parsing {key} as a {name} should have failed.') + except pom_parser.Error: + pass |