Fix a bug when a block scalar is incorrectly emitted in the simple key context.
[pyyaml/python3.git] / tests / test_structure.py
blob0573512ce06eb2e2b3051103c556d552a51bb583
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 MyLoader(Loader):
145 def construct_sequence(self, node):
146 return tuple(Loader.construct_sequence(self, node))
148 def construct_mapping(self, node):
149 pairs = self.construct_pairs(node)
150 pairs.sort()
151 return pairs
153 def construct_undefined(self, node):
154 return self.construct_scalar(node)
156 MyLoader.add_constructor(None, MyLoader.construct_undefined)
158 class MyCanonicalLoader(test_appliance.CanonicalLoader):
160 def construct_sequence(self, node):
161 return tuple(test_appliance.CanonicalLoader.construct_sequence(self, node))
163 def construct_mapping(self, node):
164 pairs = self.construct_pairs(node)
165 pairs.sort()
166 return pairs
168 def construct_undefined(self, node):
169 return self.construct_scalar(node)
171 MyCanonicalLoader.add_constructor(None, MyCanonicalLoader.construct_undefined)
173 class TestConstructor(test_appliance.TestAppliance):
175 def _testConstructor(self, test_name, data_filename, canonical_filename):
176 data1 = None
177 data2 = None
178 try:
179 data1 = list(load_all(file(data_filename, 'rb'), Loader=MyLoader))
180 data2 = list(load_all(file(canonical_filename, 'rb'), Loader=MyCanonicalLoader))
181 self.failUnlessEqual(data1, data2)
182 except:
183 print
184 print "DATA1:"
185 print file(data_filename, 'rb').read()
186 print "DATA2:"
187 print file(canonical_filename, 'rb').read()
188 print "NATIVES1:", data1
189 print "NATIVES2:", data2
190 raise
192 TestConstructor.add_tests('testConstructor', '.data', '.canonical')
194 class TestParserOnCanonical(test_appliance.TestAppliance):
196 def _testParserOnCanonical(self, test_name, canonical_filename):
197 events1 = None
198 events2 = None
199 try:
200 events1 = list(parse(file(canonical_filename, 'rb')))
201 events2 = list(test_appliance.canonical_parse(file(canonical_filename, 'rb')))
202 self._compare(events1, events2)
203 except:
204 print
205 print "DATA:"
206 print file(canonical_filename, 'rb').read()
207 print "EVENTS1:", events1
208 print "EVENTS2:", events2
209 raise
211 def _compare(self, events1, events2):
212 self.failUnlessEqual(len(events1), len(events2))
213 for event1, event2 in zip(events1, events2):
214 self.failUnlessEqual(event1.__class__, event2.__class__)
215 if isinstance(event1, AliasEvent):
216 self.failUnlessEqual(event1.anchor, event2.anchor)
217 elif isinstance(event1, ScalarEvent):
218 self.failUnlessEqual(event1.anchor, event2.anchor)
219 self.failUnlessEqual(event1.tag, event2.tag)
220 self.failUnlessEqual(event1.value, event2.value)
221 if isinstance(event1, CollectionStartEvent):
222 self.failUnlessEqual(event1.anchor, event2.anchor)
223 self.failUnlessEqual(event1.tag, event2.tag)
225 TestParserOnCanonical.add_tests('testParserOnCanonical', '.canonical')