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

Revision 223, 7.8 KB checked in by xi, 8 years ago (diff)

Completely rewrite the libyaml bindings.

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