• 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    events = list(yaml.parse(open(data_filename, 'rb')))
19    output = yaml.emit(events)
20    if verbose:
21        print("OUTPUT:")
22        print(output)
23    new_events = list(yaml.parse(output))
24    _compare_events(events, new_events)
25
26test_emitter_on_data.unittest = ['.data', '.canonical']
27
28def test_emitter_on_canonical(canonical_filename, verbose=False):
29    events = list(yaml.parse(open(canonical_filename, 'rb')))
30    for canonical in [False, True]:
31        output = yaml.emit(events, canonical=canonical)
32        if verbose:
33            print("OUTPUT (canonical=%s):" % canonical)
34            print(output)
35        new_events = list(yaml.parse(output))
36        _compare_events(events, new_events)
37
38test_emitter_on_canonical.unittest = ['.canonical']
39
40def test_emitter_styles(data_filename, canonical_filename, verbose=False):
41    for filename in [data_filename, canonical_filename]:
42        events = list(yaml.parse(open(filename, 'rb')))
43        for flow_style in [False, True]:
44            for style in ['|', '>', '"', '\'', '']:
45                styled_events = []
46                for event in events:
47                    if isinstance(event, yaml.ScalarEvent):
48                        event = yaml.ScalarEvent(event.anchor, event.tag,
49                                event.implicit, event.value, style=style)
50                    elif isinstance(event, yaml.SequenceStartEvent):
51                        event = yaml.SequenceStartEvent(event.anchor, event.tag,
52                                event.implicit, flow_style=flow_style)
53                    elif isinstance(event, yaml.MappingStartEvent):
54                        event = yaml.MappingStartEvent(event.anchor, event.tag,
55                                event.implicit, flow_style=flow_style)
56                    styled_events.append(event)
57                output = yaml.emit(styled_events)
58                if verbose:
59                    print("OUTPUT (filename=%r, flow_style=%r, style=%r)" % (filename, flow_style, style))
60                    print(output)
61                new_events = list(yaml.parse(output))
62                _compare_events(events, new_events)
63
64test_emitter_styles.unittest = ['.data', '.canonical']
65
66class EventsLoader(yaml.Loader):
67
68    def construct_event(self, node):
69        if isinstance(node, yaml.ScalarNode):
70            mapping = {}
71        else:
72            mapping = self.construct_mapping(node)
73        class_name = str(node.tag[1:])+'Event'
74        if class_name in ['AliasEvent', 'ScalarEvent', 'SequenceStartEvent', 'MappingStartEvent']:
75            mapping.setdefault('anchor', None)
76        if class_name in ['ScalarEvent', 'SequenceStartEvent', 'MappingStartEvent']:
77            mapping.setdefault('tag', None)
78        if class_name in ['SequenceStartEvent', 'MappingStartEvent']:
79            mapping.setdefault('implicit', True)
80        if class_name == 'ScalarEvent':
81            mapping.setdefault('implicit', (False, True))
82            mapping.setdefault('value', '')
83        value = getattr(yaml, class_name)(**mapping)
84        return value
85
86EventsLoader.add_constructor(None, EventsLoader.construct_event)
87
88def test_emitter_events(events_filename, verbose=False):
89    events = list(yaml.load(open(events_filename, 'rb'), Loader=EventsLoader))
90    output = yaml.emit(events)
91    if verbose:
92        print("OUTPUT:")
93        print(output)
94    new_events = list(yaml.parse(output))
95    _compare_events(events, new_events)
96
97if __name__ == '__main__':
98    import test_appliance
99    test_appliance.run(globals())
100
101