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 loader
= Loader(file(data_filename
, 'rb'))
14 while not loader
.check_event(StreamEndEvent
):
15 if not loader
.check_event(StreamStartEvent
, DocumentStartEvent
, DocumentEndEvent
):
16 node1
.append(self
._convert
(loader
))
22 self
.failUnlessEqual(node1
, node2
)
26 print file(data_filename
, 'rb').read()
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
:
38 elif loader
.check_event(SequenceStartEvent
):
41 while not loader
.check_event(SequenceEndEvent
):
42 sequence
.append(self
._convert
(loader
))
45 elif loader
.check_event(MappingStartEvent
):
48 while not loader
.check_event(MappingEndEvent
):
49 key
= self
._convert
(loader
)
50 value
= self
._convert
(loader
)
51 mapping
.append((key
, value
))
54 elif loader
.check_event(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 events1
= list(parse(file(data_filename
, 'rb')))
70 events2
= list(test_appliance
.canonical_parse(file(canonical_filename
, 'rb')))
71 self
._compare
(events1
, events2
)
75 print file(data_filename
, 'rb').read()
77 print file(canonical_filename
, 'rb').read()
78 print "EVENTS1:", events1
79 print "EVENTS2:", events2
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)
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)
98 TestParser
.add_tests('testParser', '.data', '.canonical')
100 class TestResolver(test_appliance
.TestAppliance
):
102 def _testResolver(self
, test_name
, data_filename
, canonical_filename
):
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
)
114 print file(data_filename
, 'rb').read()
116 print file(canonical_filename
, 'rb').read()
117 print "NODES1:", nodes1
118 print "NODES2:", nodes2
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')
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
)
154 def construct_undefined(self
, node
):
155 return self
.construct_scalar(node
)
157 class MyLoader(MyConstructor
, Loader
):
159 MyLoader
.add_constructor(None, MyLoader
.construct_undefined
)
161 class MyCanonicalLoader(MyConstructor
, test_appliance
.CanonicalLoader
):
163 MyCanonicalLoader
.add_constructor(None, MyCanonicalLoader
.construct_undefined
)
165 class TestConstructor(test_appliance
.TestAppliance
):
167 def _testConstructor(self
, test_name
, data_filename
, canonical_filename
):
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
)
177 print file(data_filename
, 'rb').read()
179 print file(canonical_filename
, 'rb').read()
180 print "NATIVES1:", data1
181 print "NATIVES2:", data2
184 TestConstructor
.add_tests('testConstructor', '.data', '.canonical')
186 class TestParserOnCanonical(test_appliance
.TestAppliance
):
188 def _testParserOnCanonical(self
, test_name
, canonical_filename
):
192 events1
= list(parse(file(canonical_filename
, 'rb')))
193 events2
= list(test_appliance
.canonical_parse(file(canonical_filename
, 'rb')))
194 self
._compare
(events1
, events2
)
198 print file(canonical_filename
, 'rb').read()
199 print "EVENTS1:", events1
200 print "EVENTS2:", events2
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')