Fix #4 (Thanks to murphy(at)rubychan.de)
[pyyaml/python3.git] / tests / test_structure.py
blobb1fd91cb6d2e798bfb0f759ca929276e31983e77
2 import test_appliance
4 from yaml import *
6 class TestStructure(test_appliance.TestAppliance):
8 def _testStructure(self, test_name, data_filename, structure_filename):
9 node1 = None
10 node2 = eval(file(structure_filename, 'rb').read())
11 try:
12 loader = Loader(file(data_filename, 'rb'))
13 node1 = []
14 while not loader.check_event(StreamEndEvent):
15 if not loader.check_event(StreamStartEvent, DocumentStartEvent, DocumentEndEvent):
16 node1.append(self._convert(loader))
17 else:
18 loader.get_event()
19 loader.get_event()
20 if len(node1) == 1:
21 node1 = node1[0]
22 self.failUnlessEqual(node1, node2)
23 except:
24 print
25 print "DATA:"
26 print file(data_filename, 'rb').read()
27 print "NODE1:", node1
28 print "NODE2:", node2
29 raise
31 def _convert(self, loader):
32 if loader.check_event(ScalarEvent):
33 event = loader.get_event()
34 if event.tag or event.anchor or event.value:
35 return True
36 else:
37 return None
38 elif loader.check_event(SequenceStartEvent):
39 loader.get_event()
40 sequence = []
41 while not loader.check_event(SequenceEndEvent):
42 sequence.append(self._convert(loader))
43 loader.get_event()
44 return sequence
45 elif loader.check_event(MappingStartEvent):
46 loader.get_event()
47 mapping = []
48 while not loader.check_event(MappingEndEvent):
49 key = self._convert(loader)
50 value = self._convert(loader)
51 mapping.append((key, value))
52 loader.get_event()
53 return mapping
54 elif loader.check_event(AliasEvent):
55 loader.get_event()
56 return '*'
57 else:
58 loader.get_event()
59 return '?'
61 TestStructure.add_tests('testStructure', '.data', '.structure')
63 class TestParser(test_appliance.TestAppliance):
65 def _testParser(self, test_name, data_filename, canonical_filename):
66 events1 = None
67 events2 = None
68 try:
69 events1 = list(parse(file(data_filename, 'rb')))
70 events2 = list(test_appliance.canonical_parse(file(canonical_filename, 'rb')))
71 self._compare(events1, events2)
72 except:
73 print
74 print "DATA1:"
75 print file(data_filename, 'rb').read()
76 print "DATA2:"
77 print file(canonical_filename, 'rb').read()
78 print "EVENTS1:", events1
79 print "EVENTS2:", events2
80 raise
82 def _compare(self, events1, events2):
83 self.failUnlessEqual(len(events1), len(events2))
84 for event1, event2 in zip(events1, events2):
85 self.failUnlessEqual(event1.__class__, event2.__class__)
86 if isinstance(event1, AliasEvent):
87 #self.failUnlessEqual(event1.name, event2.name)
88 pass
89 elif isinstance(event1, ScalarEvent):
90 #self.failUnlessEqual(event1.anchor, event2.anchor)
91 #self.failUnlessEqual(event1.tag, event2.tag)
92 self.failUnlessEqual(event1.value, event2.value)
93 if isinstance(event1, CollectionStartEvent):
94 #self.failUnlessEqual(event1.anchor, event2.anchor)
95 #self.failUnlessEqual(event1.tag, event2.tag)
96 pass
98 TestParser.add_tests('testParser', '.data', '.canonical')
100 class TestResolver(test_appliance.TestAppliance):
102 def _testResolver(self, test_name, data_filename, canonical_filename):
103 nodes1 = None
104 nodes2 = None
105 try:
106 nodes1 = list(compose_all(file(data_filename, 'rb')))
107 nodes2 = list(test_appliance.canonical_compose_all(file(canonical_filename, 'rb')))
108 self.failUnlessEqual(len(nodes1), len(nodes2))
109 for node1, node2 in zip(nodes1, nodes2):
110 self._compare(node1, node2)
111 except:
112 print
113 print "DATA1:"
114 print file(data_filename, 'rb').read()
115 print "DATA2:"
116 print file(canonical_filename, 'rb').read()
117 print "NODES1:", nodes1
118 print "NODES2:", nodes2
119 raise
121 def _compare(self, node1, node2):
122 self.failUnlessEqual(node1.__class__, node2.__class__)
123 if isinstance(node1, ScalarNode):
124 #self.failUnlessEqual(node1.tag, node2.tag)
125 self.failUnlessEqual(node1.value, node2.value)
126 elif isinstance(node1, SequenceNode):
127 self.failUnlessEqual(len(node1.value), len(node2.value))
128 for item1, item2 in zip(node1.value, node2.value):
129 self._compare(item1, item2)
130 elif isinstance(node1, MappingNode):
131 self.failUnlessEqual(len(node1.value), len(node2.value))
132 items1 = node1.value.items()
133 items1.sort(lambda (k1,v1), (k2,v2): cmp((k1.tag,k1.value,v1.tag,v1.value),
134 (k2.tag,k2.value,v2.tag,v2.value)))
135 items2 = node2.value.items()
136 items2.sort(lambda (k1,v1), (k2,v2): cmp((k1.tag,k1.value,v1.tag,v1.value),
137 (k2.tag,k2.value,v2.tag,v2.value)))
138 for (key1, value1), (key2, value2) in zip(items1, items2):
139 self._compare(key1, key2)
140 self._compare(value1, value2)
142 TestResolver.add_tests('testResolver', '.data', '.canonical')
144 class MyConstructor:
146 def construct_sequence(self, node):
147 return tuple(Constructor.construct_sequence(self, node))
149 def construct_mapping(self, node):
150 pairs = self.construct_pairs(node)
151 pairs.sort()
152 return pairs
154 def construct_undefined(self, node):
155 return self.construct_scalar(node)
157 class MyLoader(MyConstructor, Loader):
158 pass
159 MyLoader.add_constructor(None, MyLoader.construct_undefined)
161 class MyCanonicalLoader(MyConstructor, test_appliance.CanonicalLoader):
162 pass
163 MyCanonicalLoader.add_constructor(None, MyCanonicalLoader.construct_undefined)
165 class TestConstructor(test_appliance.TestAppliance):
167 def _testConstructor(self, test_name, data_filename, canonical_filename):
168 data1 = None
169 data2 = None
170 try:
171 data1 = list(load_all(file(data_filename, 'rb'), Loader=MyLoader))
172 data2 = list(load_all(file(canonical_filename, 'rb'), Loader=MyCanonicalLoader))
173 self.failUnlessEqual(data1, data2)
174 except:
175 print
176 print "DATA1:"
177 print file(data_filename, 'rb').read()
178 print "DATA2:"
179 print file(canonical_filename, 'rb').read()
180 print "NATIVES1:", data1
181 print "NATIVES2:", data2
182 raise
184 TestConstructor.add_tests('testConstructor', '.data', '.canonical')
186 class TestParserOnCanonical(test_appliance.TestAppliance):
188 def _testParserOnCanonical(self, test_name, canonical_filename):
189 events1 = None
190 events2 = None
191 try:
192 events1 = list(parse(file(canonical_filename, 'rb')))
193 events2 = list(test_appliance.canonical_parse(file(canonical_filename, 'rb')))
194 self._compare(events1, events2)
195 except:
196 print
197 print "DATA:"
198 print file(canonical_filename, 'rb').read()
199 print "EVENTS1:", events1
200 print "EVENTS2:", events2
201 raise
203 def _compare(self, events1, events2):
204 self.failUnlessEqual(len(events1), len(events2))
205 for event1, event2 in zip(events1, events2):
206 self.failUnlessEqual(event1.__class__, event2.__class__)
207 if isinstance(event1, AliasEvent):
208 self.failUnlessEqual(event1.anchor, event2.anchor)
209 elif isinstance(event1, ScalarEvent):
210 self.failUnlessEqual(event1.anchor, event2.anchor)
211 self.failUnlessEqual(event1.tag, event2.tag)
212 self.failUnlessEqual(event1.value, event2.value)
213 if isinstance(event1, CollectionStartEvent):
214 self.failUnlessEqual(event1.anchor, event2.anchor)
215 self.failUnlessEqual(event1.tag, event2.tag)
217 TestParserOnCanonical.add_tests('testParserOnCanonical', '.canonical')