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')
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
)
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
)
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
):
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
)
185 print file(data_filename
, 'rb').read()
187 print file(canonical_filename
, 'rb').read()
188 print "NATIVES1:", data1
189 print "NATIVES2:", data2
192 TestConstructor
.add_tests('testConstructor', '.data', '.canonical')
194 class TestParserOnCanonical(test_appliance
.TestAppliance
):
196 def _testParserOnCanonical(self
, test_name
, canonical_filename
):
200 events1
= list(parse(file(canonical_filename
, 'rb')))
201 events2
= list(test_appliance
.canonical_parse(file(canonical_filename
, 'rb')))
202 self
._compare
(events1
, events2
)
206 print file(canonical_filename
, 'rb').read()
207 print "EVENTS1:", events1
208 print "EVENTS2:", events2
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')