• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2import yaml
3
4def _compare_events(events1, events2):
5    assert len(events1) == len(events2), (events1, events2)
6    for event1, event2 in zip(events1, events2):
7        assert event1.__class__ == event2.__class__, (event1, event2)
8        if isinstance(event1, yaml.NodeEvent):
9            assert event1.anchor == event2.anchor, (event1, event2)
10        if isinstance(event1, yaml.CollectionStartEvent):
11            assert event1.tag == event2.tag, (event1, event2)
12        if isinstance(event1, yaml.ScalarEvent):
13            if True not in event1.implicit+event2.implicit:
14                assert event1.tag == event2.tag, (event1, event2)
15            assert event1.value == event2.value, (event1, event2)
16
17def test_emitter_on_data(data_filename, canonical_filename, verbose=False):
18    with open(data_filename, 'rb') as file:
19        events = list(yaml.parse(file))
20    output = yaml.emit(events)
21    if verbose:
22        print("OUTPUT:")
23        print(output)
24    new_events = list(yaml.parse(output))
25    _compare_events(events, new_events)
26
27test_emitter_on_data.unittest = ['.data', '.canonical']
28
29def test_emitter_on_canonical(canonical_filename, verbose=False):
30    with open(canonical_filename, 'rb') as file:
31        events = list(yaml.parse(file))
32    for canonical in [False, True]:
33        output = yaml.emit(events, canonical=canonical)
34        if verbose:
35            print("OUTPUT (canonical=%s):" % canonical)
36            print(output)
37        new_events = list(yaml.parse(output))
38        _compare_events(events, new_events)
39
40test_emitter_on_canonical.unittest = ['.canonical']
41
42def test_emitter_styles(data_filename, canonical_filename, verbose=False):
43    for filename in [data_filename, canonical_filename]:
44        with open(filename, 'rb') as file:
45            events = list(yaml.parse(file))
46        for flow_style in [False, True]:
47            for style in ['|', '>', '"', '\'', '']:
48                styled_events = []
49                for event in events:
50                    if isinstance(event, yaml.ScalarEvent):
51                        event = yaml.ScalarEvent(event.anchor, event.tag,
52                                event.implicit, event.value, style=style)
53                    elif isinstance(event, yaml.SequenceStartEvent):
54                        event = yaml.SequenceStartEvent(event.anchor, event.tag,
55                                event.implicit, flow_style=flow_style)
56                    elif isinstance(event, yaml.MappingStartEvent):
57                        event = yaml.MappingStartEvent(event.anchor, event.tag,
58                                event.implicit, flow_style=flow_style)
59                    styled_events.append(event)
60                output = yaml.emit(styled_events)
61                if verbose:
62                    print("OUTPUT (filename=%r, flow_style=%r, style=%r)" % (filename, flow_style, style))
63                    print(output)
64                new_events = list(yaml.parse(output))
65                _compare_events(events, new_events)
66
67test_emitter_styles.unittest = ['.data', '.canonical']
68
69class EventsLoader(yaml.Loader):
70
71    def construct_event(self, node):
72        if isinstance(node, yaml.ScalarNode):
73            mapping = {}
74        else:
75            mapping = self.construct_mapping(node)
76        class_name = str(node.tag[1:])+'Event'
77        if class_name in ['AliasEvent', 'ScalarEvent', 'SequenceStartEvent', 'MappingStartEvent']:
78            mapping.setdefault('anchor', None)
79        if class_name in ['ScalarEvent', 'SequenceStartEvent', 'MappingStartEvent']:
80            mapping.setdefault('tag', None)
81        if class_name in ['SequenceStartEvent', 'MappingStartEvent']:
82            mapping.setdefault('implicit', True)
83        if class_name == 'ScalarEvent':
84            mapping.setdefault('implicit', (False, True))
85            mapping.setdefault('value', '')
86        value = getattr(yaml, class_name)(**mapping)
87        return value
88
89EventsLoader.add_constructor(None, EventsLoader.construct_event)
90
91def test_emitter_events(events_filename, verbose=False):
92    with open(events_filename, 'rb') as file:
93        events = list(yaml.load(file, Loader=EventsLoader))
94    output = yaml.emit(events)
95    if verbose:
96        print("OUTPUT:")
97        print(output)
98    new_events = list(yaml.parse(output))
99    _compare_events(events, new_events)
100
101if __name__ == '__main__':
102    import test_appliance
103    test_appliance.run(globals())
104
105