source: pyyaml/trunk/tests3/test_emitter.py @ 328

Revision 328, 4.2 KB checked in by xi, 6 years ago (diff)

Added basic support for Python 3 (Thanks idadesub(at)users(dot)sourceforge(dot)net).

RevLine 
[122]1
[129]2import yaml
[122]3
[322]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)
[122]16
[322]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:
[328]21        print("OUTPUT:")
22        print(output)
[322]23    new_events = list(yaml.parse(output))
24    _compare_events(events, new_events)
[132]25
[322]26test_emitter_on_data.unittest = ['.data', '.canonical']
[132]27
[322]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:
[328]33            print("OUTPUT (canonical=%s):" % canonical)
34            print(output)
[322]35        new_events = list(yaml.parse(output))
36        _compare_events(events, new_events)
[132]37
[322]38test_emitter_on_canonical.unittest = ['.canonical']
[122]39
[322]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:
[328]59                    print("OUTPUT (filename=%r, flow_style=%r, style=%r)" % (filename, flow_style, style))
60                    print(output)
[322]61                new_events = list(yaml.parse(output))
62                _compare_events(events, new_events)
[312]63
[322]64test_emitter_styles.unittest = ['.data', '.canonical']
[312]65
[322]66class EventsLoader(yaml.Loader):
[122]67
[129]68    def construct_event(self, node):
[322]69        if isinstance(node, yaml.ScalarNode):
[129]70            mapping = {}
71        else:
72            mapping = self.construct_mapping(node)
73        class_name = str(node.tag[1:])+'Event'
[130]74        if class_name in ['AliasEvent', 'ScalarEvent', 'SequenceStartEvent', 'MappingStartEvent']:
[129]75            mapping.setdefault('anchor', None)
[130]76        if class_name in ['ScalarEvent', 'SequenceStartEvent', 'MappingStartEvent']:
[129]77            mapping.setdefault('tag', None)
[137]78        if class_name in ['SequenceStartEvent', 'MappingStartEvent']:
79            mapping.setdefault('implicit', True)
[129]80        if class_name == 'ScalarEvent':
[137]81            mapping.setdefault('implicit', (False, True))
[129]82            mapping.setdefault('value', '')
83        value = getattr(yaml, class_name)(**mapping)
84        return value
85
[136]86EventsLoader.add_constructor(None, EventsLoader.construct_event)
[129]87
[322]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:
[328]92        print("OUTPUT:")
93        print(output)
[322]94    new_events = list(yaml.parse(output))
95    _compare_events(events, new_events)
[129]96
[322]97if __name__ == '__main__':
98    import test_appliance
99    test_appliance.run(globals())
100
Note: See TracBrowser for help on using the repository browser.