6 class TestStructure(test_appliance
.TestAppliance
):
8 def _testStructure(self
, test_name
, data_filename
, structure_filename
):
10 node2
= eval(file(structure_filename
, 'rb').read())
12 parser
= Parser(Scanner(Reader(file(data_filename
, 'rb'))))
14 while not parser
.check(StreamEndEvent
):
15 if not parser
.check(StreamStartEvent
, DocumentStartEvent
, DocumentEndEvent
):
16 node1
.append(self
._convert
(parser
))
22 self
.failUnlessEqual(node1
, node2
)
26 print file(data_filename
, 'rb').read()
31 def _convert(self
, parser
):
32 if parser
.check(ScalarEvent
):
34 if event
.tag
or event
.anchor
or event
.value
:
38 elif parser
.check(SequenceEvent
):
41 while not parser
.check(CollectionEndEvent
):
42 sequence
.append(self
._convert
(parser
))
45 elif parser
.check(MappingEvent
):
48 while not parser
.check(CollectionEndEvent
):
49 key
= self
._convert
(parser
)
50 value
= self
._convert
(parser
)
51 mapping
.append((key
, value
))
54 elif parser
.check(AliasEvent
):
61 TestStructure
.add_tests('testStructure', '.data', '.structure')
63 class TestParser(test_appliance
.TestAppliance
):
65 def _testParser(self
, test_name
, data_filename
, canonical_filename
):
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
)
77 print file(data_filename
, 'rb').read()
79 print file(canonical_filename
, 'rb').read()
80 print "EVENTS1:", events1
81 print "EVENTS2:", events2
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)
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)
100 TestParser
.add_tests('testParser', '.data', '.canonical')
102 class TestResolver(test_appliance
.TestAppliance
):
104 def _testResolver(self
, test_name
, data_filename
, canonical_filename
):
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())
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
)
120 print file(data_filename
, 'rb').read()
122 print file(canonical_filename
, 'rb').read()
123 print "NODES1:", nodes1
124 print "NODES2:", nodes2
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
)
160 MyConstructor
.add_constructor(None, MyConstructor
.construct_scalar
)
162 class TestConstructor(test_appliance
.TestAppliance
):
164 def _testConstructor(self
, test_name
, data_filename
, canonical_filename
):
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())
172 constructor2
= MyConstructor(Resolver(Composer(canonical
)))
173 natives2
= list(iter(constructor2
))
174 self
.failUnlessEqual(natives1
, natives2
)
178 print file(data_filename
, 'rb').read()
180 print file(canonical_filename
, 'rb').read()
181 print "NATIVES1:", natives1
182 print "NATIVES2:", natives2
185 TestConstructor
.add_tests('testConstructor', '.data', '.canonical')
187 class TestParserOnCanonical(test_appliance
.TestAppliance
):
189 def _testParserOnCanonical(self
, test_name
, canonical_filename
):
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
)
201 print file(canonical_filename
, 'rb').read()
202 print "EVENTS1:", events1
203 print "EVENTS2:", events2
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')