* Add the token STREAM-START.
[pyyaml/python3.git] / tests / test_structure.py
blobca3cb60a334236aae63d6d3e81196d1ec3266faf
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 parser = Parser(Scanner(Reader(file(data_filename, 'rb'))))
13 node1 = []
14 while not parser.check(StreamEndEvent):
15 if not parser.check(StreamStartEvent, DocumentStartEvent, DocumentEndEvent):
16 node1.append(self._convert(parser))
17 else:
18 parser.get()
19 parser.get()
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, parser):
32 if parser.check(ScalarEvent):
33 event = parser.get()
34 if event.tag or event.anchor or event.value:
35 return True
36 else:
37 return None
38 elif parser.check(SequenceEvent):
39 parser.get()
40 sequence = []
41 while not parser.check(CollectionEndEvent):
42 sequence.append(self._convert(parser))
43 parser.get()
44 return sequence
45 elif parser.check(MappingEvent):
46 parser.get()
47 mapping = []
48 while not parser.check(CollectionEndEvent):
49 key = self._convert(parser)
50 value = self._convert(parser)
51 mapping.append((key, value))
52 parser.get()
53 return mapping
54 elif parser.check(AliasEvent):
55 parser.get()
56 return '*'
57 else:
58 parser.get()
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 parser = Parser(Scanner(Reader(file(data_filename, 'rb'))))
70 events1 = list(iter(parser))
71 canonical = test_appliance.CanonicalParser(file(canonical_filename, 'rb').read())
72 events2 = canonical.parse()
73 self._compare(events1, events2)
74 except:
75 print
76 print "DATA1:"
77 print file(data_filename, 'rb').read()
78 print "DATA2:"
79 print file(canonical_filename, 'rb').read()
80 print "EVENTS1:", events1
81 print "EVENTS2:", events2
82 raise
84 def _compare(self, events1, events2):
85 self.failUnlessEqual(len(events1), len(events2))
86 for event1, event2 in zip(events1, events2):
87 self.failUnlessEqual(event1.__class__, event2.__class__)
88 if isinstance(event1, AliasEvent):
89 #self.failUnlessEqual(event1.name, event2.name)
90 pass
91 elif isinstance(event1, ScalarEvent):
92 #self.failUnlessEqual(event1.anchor, event2.anchor)
93 #self.failUnlessEqual(event1.tag, event2.tag)
94 self.failUnlessEqual(event1.value, event2.value)
95 if isinstance(event1, CollectionEvent):
96 #self.failUnlessEqual(event1.anchor, event2.anchor)
97 #self.failUnlessEqual(event1.tag, event2.tag)
98 pass
100 TestParser.add_tests('testParser', '.data', '.canonical')
102 class TestResolver(test_appliance.TestAppliance):
104 def _testResolver(self, test_name, data_filename, canonical_filename):
105 nodes1 = None
106 nodes2 = None
107 try:
108 resolver1 = Resolver(Composer(Parser(Scanner(Reader(file(data_filename, 'rb'))))))
109 nodes1 = list(iter(resolver1))
110 canonical = test_appliance.CanonicalParser(file(canonical_filename, 'rb').read())
111 canonical.parse()
112 resolver2 = Resolver(Composer(canonical))
113 nodes2 = list(iter(resolver2))
114 self.failUnlessEqual(len(nodes1), len(nodes2))
115 for node1, node2 in zip(nodes1, nodes2):
116 self._compare(node1, node2)
117 except:
118 print
119 print "DATA1:"
120 print file(data_filename, 'rb').read()
121 print "DATA2:"
122 print file(canonical_filename, 'rb').read()
123 print "NODES1:", nodes1
124 print "NODES2:", nodes2
125 raise
127 def _compare(self, node1, node2):
128 self.failUnlessEqual(node1.__class__, node2.__class__)
129 if isinstance(node1, ScalarNode):
130 #self.failUnlessEqual(node1.tag, node2.tag)
131 self.failUnlessEqual(node1.value, node2.value)
132 elif isinstance(node1, SequenceNode):
133 self.failUnlessEqual(len(node1.value), len(node2.value))
134 for item1, item2 in zip(node1.value, node2.value):
135 self._compare(item1, item2)
136 elif isinstance(node1, MappingNode):
137 self.failUnlessEqual(len(node1.value), len(node2.value))
138 items1 = node1.value.items()
139 items1.sort(lambda (k1,v1), (k2,v2): cmp((k1.tag,k1.value,v1.tag,v1.value),
140 (k2.tag,k2.value,v2.tag,v2.value)))
141 items2 = node2.value.items()
142 items2.sort(lambda (k1,v1), (k2,v2): cmp((k1.tag,k1.value,v1.tag,v1.value),
143 (k2.tag,k2.value,v2.tag,v2.value)))
144 for (key1, value1), (key2, value2) in zip(items1, items2):
145 self._compare(key1, key2)
146 self._compare(value1, value2)
148 TestResolver.add_tests('testResolver', '.data', '.canonical')
150 class MyConstructor(Constructor):
152 def construct_sequence(self, node):
153 return tuple(Constructor.construct_sequence(self, node))
155 def construct_mapping(self, node):
156 pairs = self.construct_pairs(node)
157 pairs.sort()
158 return pairs
160 MyConstructor.add_constructor(None, MyConstructor.construct_scalar)
162 class TestConstructor(test_appliance.TestAppliance):
164 def _testConstructor(self, test_name, data_filename, canonical_filename):
165 natives1 = None
166 natives2 = None
167 try:
168 constructor1 = MyConstructor(Resolver(Composer(Parser(Scanner(Reader(file(data_filename, 'rb')))))))
169 natives1 = list(iter(constructor1))
170 canonical = test_appliance.CanonicalParser(file(canonical_filename, 'rb').read())
171 canonical.parse()
172 constructor2 = MyConstructor(Resolver(Composer(canonical)))
173 natives2 = list(iter(constructor2))
174 self.failUnlessEqual(natives1, natives2)
175 except:
176 print
177 print "DATA1:"
178 print file(data_filename, 'rb').read()
179 print "DATA2:"
180 print file(canonical_filename, 'rb').read()
181 print "NATIVES1:", natives1
182 print "NATIVES2:", natives2
183 raise
185 TestConstructor.add_tests('testConstructor', '.data', '.canonical')
187 class TestParserOnCanonical(test_appliance.TestAppliance):
189 def _testParserOnCanonical(self, test_name, canonical_filename):
190 events1 = None
191 events2 = None
192 try:
193 parser = Parser(Scanner(Reader(file(canonical_filename, 'rb'))))
194 events1 = list(iter(parser))
195 canonical = test_appliance.CanonicalParser(file(canonical_filename, 'rb').read())
196 events2 = canonical.parse()
197 self._compare(events1, events2)
198 except:
199 print
200 print "DATA:"
201 print file(canonical_filename, 'rb').read()
202 print "EVENTS1:", events1
203 print "EVENTS2:", events2
204 raise
206 def _compare(self, events1, events2):
207 self.failUnlessEqual(len(events1), len(events2))
208 for event1, event2 in zip(events1, events2):
209 self.failUnlessEqual(event1.__class__, event2.__class__)
210 if isinstance(event1, AliasEvent):
211 self.failUnlessEqual(event1.anchor, event2.anchor)
212 elif isinstance(event1, ScalarEvent):
213 self.failUnlessEqual(event1.anchor, event2.anchor)
214 self.failUnlessEqual(event1.tag, event2.tag)
215 self.failUnlessEqual(event1.value, event2.value)
216 if isinstance(event1, CollectionEvent):
217 self.failUnlessEqual(event1.anchor, event2.anchor)
218 self.failUnlessEqual(event1.tag, event2.tag)
220 TestParserOnCanonical.add_tests('testParserOnCanonical', '.canonical')