diff options
Diffstat (limited to '_test/lib/test_structure.py')
-rw-r--r-- | _test/lib/test_structure.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/_test/lib/test_structure.py b/_test/lib/test_structure.py index 4f65926..a23dac8 100644 --- a/_test/lib/test_structure.py +++ b/_test/lib/test_structure.py @@ -2,10 +2,11 @@ from __future__ import absolute_import from __future__ import print_function import ruamel.yaml as yaml -import canonical +import canonical # NOQA import pprint from ruamel.yaml.compat import text_type, PY3 + def _convert_structure(loader): if loader.check_event(yaml.ScalarEvent): event = loader.get_event() @@ -36,6 +37,7 @@ def _convert_structure(loader): loader.get_event() return '?' + def test_structure(data_filename, structure_filename, verbose=False): nodes1 = [] with open(structure_filename, 'r' if PY3 else 'rb') as fp: @@ -45,7 +47,7 @@ def test_structure(data_filename, structure_filename, verbose=False): loader = yaml.Loader(fp) while loader.check_event(): if loader.check_event(yaml.StreamStartEvent, yaml.StreamEndEvent, - yaml.DocumentStartEvent, yaml.DocumentEndEvent): + yaml.DocumentStartEvent, yaml.DocumentEndEvent): loader.get_event() continue nodes1.append(_convert_structure(loader)) @@ -61,6 +63,7 @@ def test_structure(data_filename, structure_filename, verbose=False): test_structure.unittest = ['.data', '.structure'] + def _compare_events(events1, events2, full=False): assert len(events1) == len(events2), (len(events1), len(events2)) for event1, event2 in zip(events1, events2): @@ -73,6 +76,7 @@ def _compare_events(events1, events2, full=False): if isinstance(event1, yaml.ScalarEvent): assert event1.value == event2.value, (event1, event2) + def test_parser(data_filename, canonical_filename, verbose=False): events1 = None events2 = None @@ -91,6 +95,7 @@ def test_parser(data_filename, canonical_filename, verbose=False): test_parser.unittest = ['.data', '.canonical'] + def test_parser_on_canonical(canonical_filename, verbose=False): events1 = None events2 = None @@ -109,6 +114,7 @@ def test_parser_on_canonical(canonical_filename, verbose=False): test_parser_on_canonical.unittest = ['.canonical'] + def _compare_nodes(node1, node2): assert node1.__class__ == node2.__class__, (node1, node2) assert node1.tag == node2.tag, (node1, node2) @@ -123,6 +129,7 @@ def _compare_nodes(node1, node2): for subnode1, subnode2 in zip(item1, item2): _compare_nodes(subnode1, subnode2) + def test_composer(data_filename, canonical_filename, verbose=False): nodes1 = None nodes2 = None @@ -143,38 +150,46 @@ def test_composer(data_filename, canonical_filename, verbose=False): test_composer.unittest = ['.data', '.canonical'] + def _make_loader(): global MyLoader class MyLoader(yaml.Loader): def construct_sequence(self, node): return tuple(yaml.Loader.construct_sequence(self, node)) + def construct_mapping(self, node): pairs = self.construct_pairs(node) pairs.sort(key=(lambda i: text_type(i))) return pairs + def construct_undefined(self, node): return self.construct_scalar(node) MyLoader.add_constructor(u'tag:yaml.org,2002:map', MyLoader.construct_mapping) MyLoader.add_constructor(None, MyLoader.construct_undefined) + def _make_canonical_loader(): global MyCanonicalLoader class MyCanonicalLoader(yaml.CanonicalLoader): def construct_sequence(self, node): return tuple(yaml.CanonicalLoader.construct_sequence(self, node)) + def construct_mapping(self, node): pairs = self.construct_pairs(node) pairs.sort(key=(lambda i: text_type(i))) return pairs + def construct_undefined(self, node): return self.construct_scalar(node) - MyCanonicalLoader.add_constructor(u'tag:yaml.org,2002:map', MyCanonicalLoader.construct_mapping) + MyCanonicalLoader.add_constructor(u'tag:yaml.org,2002:map', + MyCanonicalLoader.construct_mapping) MyCanonicalLoader.add_constructor(None, MyCanonicalLoader.construct_undefined) + def test_constructor(data_filename, canonical_filename, verbose=False): _make_loader() _make_canonical_loader() @@ -198,4 +213,3 @@ test_constructor.unittest = ['.data', '.canonical'] if __name__ == '__main__': import test_appliance test_appliance.run(globals()) - |