source: pyyaml/trunk/tests/test_yaml_ext.py @ 312

Revision 312, 7.9 KB checked in by xi, 6 years ago (diff)

Fixed test errors for LibYAML bindings; added a test on emitting nodes in all possible styles.

RevLine 
[195]1
2import unittest, test_appliance
3
4import _yaml, yaml
5
[312]6test_appliance.TestAppliance.SKIP_EXT = '.skip-ext'
7
[223]8class TestCVersion(unittest.TestCase):
[195]9
[223]10    def testCVersion(self):
[195]11        self.failUnlessEqual("%s.%s.%s" % _yaml.get_version(), _yaml.get_version_string())
12
[223]13class TestCLoader(test_appliance.TestAppliance):
[195]14
[223]15    def _testCScannerFileInput(self, test_name, data_filename, canonical_filename):
16        self._testCScanner(test_name, data_filename, canonical_filename, True)
[209]17
[223]18    def _testCScanner(self, test_name, data_filename, canonical_filename, file_input=False, Loader=yaml.Loader):
[209]19        if file_input:
20            data = file(data_filename, 'r')
21        else:
22            data = file(data_filename, 'r').read()
[223]23        tokens = list(yaml.scan(data, Loader=Loader))
[195]24        ext_tokens = []
25        try:
[209]26            if file_input:
27                data = file(data_filename, 'r')
[223]28            for token in yaml.scan(data, Loader=yaml.CLoader):
[195]29                ext_tokens.append(token)
30            self.failUnlessEqual(len(tokens), len(ext_tokens))
31            for token, ext_token in zip(tokens, ext_tokens):
32                self.failUnlessEqual(token.__class__, ext_token.__class__)
[311]33                if not isinstance(token, yaml.StreamEndToken):
34                    self.failUnlessEqual((token.start_mark.index, token.start_mark.line, token.start_mark.column),
35                            (ext_token.start_mark.index, ext_token.start_mark.line, ext_token.start_mark.column))
36                    self.failUnlessEqual((token.end_mark.index, token.end_mark.line, token.end_mark.column),
37                            (ext_token.end_mark.index, ext_token.end_mark.line, ext_token.end_mark.column))
[195]38                if hasattr(token, 'value'):
39                    self.failUnlessEqual(token.value, ext_token.value)
40        except:
41            print
42            print "DATA:"
43            print file(data_filename, 'rb').read()
44            print "TOKENS:", tokens
45            print "EXT_TOKENS:", ext_tokens
46            raise
47
[223]48    def _testCParser(self, test_name, data_filename, canonical_filename, Loader=yaml.Loader):
[205]49        data = file(data_filename, 'r').read()
[223]50        events = list(yaml.parse(data, Loader=Loader))
[205]51        ext_events = []
52        try:
[223]53            for event in yaml.parse(data, Loader=yaml.CLoader):
[205]54                ext_events.append(event)
55                #print "EVENT:", event
56            self.failUnlessEqual(len(events), len(ext_events))
57            for event, ext_event in zip(events, ext_events):
58                self.failUnlessEqual(event.__class__, ext_event.__class__)
59                if hasattr(event, 'anchor'):
60                    self.failUnlessEqual(event.anchor, ext_event.anchor)
61                if hasattr(event, 'tag'):
62                    self.failUnlessEqual(event.tag, ext_event.tag)
63                if hasattr(event, 'implicit'):
64                    self.failUnlessEqual(event.implicit, ext_event.implicit)
65                if hasattr(event, 'value'):
66                    self.failUnlessEqual(event.value, ext_event.value)
[223]67                if hasattr(event, 'explicit'):
68                    self.failUnlessEqual(event.explicit, ext_event.explicit)
69                if hasattr(event, 'version'):
70                    self.failUnlessEqual(event.version, ext_event.version)
71                if hasattr(event, 'tags'):
72                    self.failUnlessEqual(event.tags, ext_event.tags)
[205]73        except:
74            print
75            print "DATA:"
76            print file(data_filename, 'rb').read()
77            print "EVENTS:", events
78            print "EXT_EVENTS:", ext_events
79            raise
[195]80
[223]81TestCLoader.add_tests('testCScanner', '.data', '.canonical')
82TestCLoader.add_tests('testCScannerFileInput', '.data', '.canonical')
83TestCLoader.add_tests('testCParser', '.data', '.canonical')
[205]84
[223]85class TestCEmitter(test_appliance.TestAppliance):
86
87    def _testCEmitter(self, test_name, data_filename, canonical_filename, Loader=yaml.Loader):
88        data1 = file(data_filename, 'r').read()
89        events = list(yaml.parse(data1, Loader=Loader))
90        data2 = yaml.emit(events, Dumper=yaml.CDumper)
91        ext_events = []
92        try:
93            for event in yaml.parse(data2):
94                ext_events.append(event)
95            self.failUnlessEqual(len(events), len(ext_events))
96            for event, ext_event in zip(events, ext_events):
97                self.failUnlessEqual(event.__class__, ext_event.__class__)
98                if hasattr(event, 'anchor'):
99                    self.failUnlessEqual(event.anchor, ext_event.anchor)
100                if hasattr(event, 'tag'):
101                    if not (event.tag in ['!', None] and ext_event.tag in ['!', None]):
102                        self.failUnlessEqual(event.tag, ext_event.tag)
103                if hasattr(event, 'implicit'):
104                    self.failUnlessEqual(event.implicit, ext_event.implicit)
105                if hasattr(event, 'value'):
106                    self.failUnlessEqual(event.value, ext_event.value)
[312]107                if hasattr(event, 'explicit') and event.explicit:
[223]108                    self.failUnlessEqual(event.explicit, ext_event.explicit)
109                if hasattr(event, 'version'):
110                    self.failUnlessEqual(event.version, ext_event.version)
111                if hasattr(event, 'tags'):
112                    self.failUnlessEqual(event.tags, ext_event.tags)
113        except:
114            print
115            print "DATA1:"
116            print data1
117            print "DATA2:"
118            print data2
119            print "EVENTS:", events
120            print "EXT_EVENTS:", ext_events
121            raise
122
123TestCEmitter.add_tests('testCEmitter', '.data', '.canonical')
124
125yaml.BaseLoader = yaml.CBaseLoader
126yaml.SafeLoader = yaml.CSafeLoader
127yaml.Loader = yaml.CLoader
128yaml.BaseDumper = yaml.CBaseDumper
129yaml.SafeDumper = yaml.CSafeDumper
130yaml.Dumper = yaml.CDumper
131old_scan = yaml.scan
132def scan(stream, Loader=yaml.CLoader):
133    return old_scan(stream, Loader)
134yaml.scan = scan
135old_parse = yaml.parse
136def parse(stream, Loader=yaml.CLoader):
137    return old_parse(stream, Loader)
138yaml.parse = parse
139old_compose = yaml.compose
140def compose(stream, Loader=yaml.CLoader):
141    return old_compose(stream, Loader)
142yaml.compose = compose
143old_compose_all = yaml.compose_all
144def compose_all(stream, Loader=yaml.CLoader):
145    return old_compose_all(stream, Loader)
146yaml.compose_all = compose_all
147old_load_all = yaml.load_all
148def load_all(stream, Loader=yaml.CLoader):
149    return old_load_all(stream, Loader)
150yaml.load_all = load_all
151old_load = yaml.load
152def load(stream, Loader=yaml.CLoader):
153    return old_load(stream, Loader)
154yaml.load = load
155def safe_load_all(stream):
156    return yaml.load_all(stream, yaml.CSafeLoader)
157yaml.safe_load_all = safe_load_all
158def safe_load(stream):
159    return yaml.load(stream, yaml.CSafeLoader)
160yaml.safe_load = safe_load
161old_emit = yaml.emit
162def emit(events, stream=None, Dumper=yaml.CDumper, **kwds):
163    return old_emit(events, stream, Dumper, **kwds)
164yaml.emit = emit
165old_serialize_all = yaml.serialize_all
166def serialize_all(nodes, stream=None, Dumper=yaml.CDumper, **kwds):
167    return old_serialize_all(nodes, stream, Dumper, **kwds)
168yaml.serialize_all = serialize_all
169old_serialize = yaml.serialize
170def serialize(node, stream, Dumper=yaml.CDumper, **kwds):
171    return old_serialize(node, stream, Dumper, **kwds)
172yaml.serialize = serialize
173old_dump_all = yaml.dump_all
174def dump_all(documents, stream=None, Dumper=yaml.CDumper, **kwds):
175    return old_dump_all(documents, stream, Dumper, **kwds)
176yaml.dump_all = dump_all
177old_dump = yaml.dump
178def dump(data, stream=None, Dumper=yaml.CDumper, **kwds):
179    return old_dump(data, stream, Dumper, **kwds)
180yaml.dump = dump
181def safe_dump_all(documents, stream=None, **kwds):
182    return yaml.dump_all(documents, stream, yaml.CSafeDumper, **kwds)
183yaml.safe_dump_all = safe_dump_all
184def safe_dump(data, stream=None, **kwds):
185    return yaml.dump(data, stream, yaml.CSafeDumper, **kwds)
186yaml.safe_dump = safe_dump
187
188from test_yaml import *
189
[195]190def main(module='__main__'):
191    unittest.main(module)
192
193if __name__ == '__main__':
194    main()
195
Note: See TracBrowser for help on using the repository browser.