Fix bugs in Resolver and add more tests.
[pyyaml/python3.git] / tests / test_structure.py
blob15f485a723a8b6ba099fd495eb23c3d8932e86e3
2 import test_appliance
4 from yaml.reader import Reader
5 from yaml.scanner import Scanner
6 from yaml.parser import *
7 from yaml.composer import *
8 from yaml.resolver import *
9 from yaml.nodes import *
11 class TestStructure(test_appliance.TestAppliance):
13 def _testStructure(self, test_name, data_filename, structure_filename):
14 node1 = None
15 node2 = eval(file(structure_filename, 'rb').read())
16 try:
17 parser = Parser(Scanner(Reader(file(data_filename, 'rb'))))
18 node1 = []
19 while not parser.check(StreamEndEvent):
20 node1.append(self._convert(parser))
21 parser.get()
22 if len(node1) == 1:
23 node1 = node1[0]
24 self.failUnlessEqual(node1, node2)
25 except:
26 print
27 print "DATA:"
28 print file(data_filename, 'rb').read()
29 print "NODE1:", node1
30 print "NODE2:", node2
31 raise
33 def _convert(self, parser):
34 if parser.check(ScalarEvent):
35 event = parser.get()
36 if event.tag or event.anchor or event.value:
37 return True
38 else:
39 return None
40 elif parser.check(SequenceEvent):
41 parser.get()
42 sequence = []
43 while not parser.check(CollectionEndEvent):
44 sequence.append(self._convert(parser))
45 parser.get()
46 return sequence
47 elif parser.check(MappingEvent):
48 parser.get()
49 mapping = []
50 while not parser.check(CollectionEndEvent):
51 key = self._convert(parser)
52 value = self._convert(parser)
53 mapping.append((key, value))
54 parser.get()
55 return mapping
56 elif parser.check(AliasEvent):
57 parser.get()
58 return '*'
59 else:
60 parser.get()
61 return '?'
63 TestStructure.add_tests('testStructure', '.data', '.structure')
65 class TestParser(test_appliance.TestAppliance):
67 def _testParser(self, test_name, data_filename, canonical_filename):
68 events1 = None
69 events2 = None
70 try:
71 parser = Parser(Scanner(Reader(file(data_filename, 'rb'))))
72 events1 = list(iter(parser))
73 canonical = test_appliance.CanonicalParser(file(canonical_filename, 'rb').read())
74 events2 = canonical.parse()
75 self._compare(events1, events2)
76 except:
77 print
78 print "DATA1:"
79 print file(data_filename, 'rb').read()
80 print "DATA2:"
81 print file(canonical_filename, 'rb').read()
82 print "EVENTS1:", events1
83 print "EVENTS2:", events2
84 raise
86 def _compare(self, events1, events2):
87 self.failUnlessEqual(len(events1), len(events2))
88 for event1, event2 in zip(events1, events2):
89 self.failUnlessEqual(event1.__class__, event2.__class__)
90 if isinstance(event1, AliasEvent):
91 #self.failUnlessEqual(event1.name, event2.name)
92 pass
93 elif isinstance(event1, ScalarEvent):
94 #self.failUnlessEqual(event1.anchor, event2.anchor)
95 #self.failUnlessEqual(event1.tag, event2.tag)
96 self.failUnlessEqual(event1.value, event2.value)
97 if isinstance(event1, CollectionEvent):
98 #self.failUnlessEqual(event1.anchor, event2.anchor)
99 #self.failUnlessEqual(event1.tag, event2.tag)
100 pass
102 TestParser.add_tests('testParser', '.data', '.canonical')
104 class TestResolver(test_appliance.TestAppliance):
106 def _testResolver(self, test_name, data_filename, canonical_filename):
107 nodes1 = None
108 nodes2 = None
109 try:
110 resolver1 = Resolver(Composer(Parser(Scanner(Reader(file(data_filename, 'rb'))))))
111 nodes1 = list(iter(resolver1))
112 canonical = test_appliance.CanonicalParser(file(canonical_filename, 'rb').read())
113 canonical.parse()
114 resolver2 = Resolver(Composer(canonical))
115 nodes2 = list(iter(resolver2))
116 self.failUnlessEqual(len(nodes1), len(nodes2))
117 for node1, node2 in zip(nodes1, nodes2):
118 self._compare(node1, node2)
119 except:
120 print
121 print "DATA1:"
122 print file(data_filename, 'rb').read()
123 print "DATA2:"
124 print file(canonical_filename, 'rb').read()
125 print "NODES1:", nodes1
126 print "NODES2:", nodes2
127 raise
129 def _compare(self, node1, node2):
130 self.failUnlessEqual(node1.__class__, node2.__class__)
131 if isinstance(node1, ScalarNode):
132 #self.failUnlessEqual(node1.tag, node2.tag)
133 self.failUnlessEqual(node1.value, node2.value)
134 elif isinstance(node1, SequenceNode):
135 self.failUnlessEqual(len(node1.value), len(node2.value))
136 for item1, item2 in zip(node1.value, node2.value):
137 self._compare(item1, item2)
138 elif isinstance(node1, MappingNode):
139 self.failUnlessEqual(len(node1.value), len(node2.value))
140 for (key1, value1), (key2, value2) in zip(node1.value, node2.value):
141 self._compare(key1, key2)
142 self._compare(value1, value2)
144 TestResolver.add_tests('testResolver', '.data', '.canonical')
146 class TestParserOnCanonical(test_appliance.TestAppliance):
148 def _testParserOnCanonical(self, test_name, canonical_filename):
149 events1 = None
150 events2 = None
151 try:
152 parser = Parser(Scanner(Reader(file(canonical_filename, 'rb'))))
153 events1 = list(iter(parser))
154 canonical = test_appliance.CanonicalParser(file(canonical_filename, 'rb').read())
155 events2 = canonical.parse()
156 self._compare(events1, events2)
157 except:
158 print
159 print "DATA:"
160 print file(canonical_filename, 'rb').read()
161 print "EVENTS1:", events1
162 print "EVENTS2:", events2
163 raise
165 def _compare(self, events1, events2):
166 self.failUnlessEqual(len(events1), len(events2))
167 for event1, event2 in zip(events1, events2):
168 self.failUnlessEqual(event1.__class__, event2.__class__)
169 if isinstance(event1, AliasEvent):
170 self.failUnlessEqual(event1.anchor, event2.anchor)
171 elif isinstance(event1, ScalarEvent):
172 self.failUnlessEqual(event1.anchor, event2.anchor)
173 self.failUnlessEqual(event1.tag, event2.tag)
174 self.failUnlessEqual(event1.value, event2.value)
175 if isinstance(event1, CollectionEvent):
176 self.failUnlessEqual(event1.anchor, event2.anchor)
177 self.failUnlessEqual(event1.tag, event2.tag)
179 TestParserOnCanonical.add_tests('testParserOnCanonical', '.canonical')