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

Revision 311, 7.9 KB checked in by xi, 5 years ago (diff)

Minor fixes in the test subsystem to prevent failures in LibYAML bindings tests.

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