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
):
15 node2
= eval(file(structure_filename
, 'rb').read())
17 parser
= Parser(Scanner(Reader(file(data_filename
, 'rb'))))
19 while not parser
.check(StreamEndEvent
):
20 node1
.append(self
._convert
(parser
))
24 self
.failUnlessEqual(node1
, node2
)
28 print file(data_filename
, 'rb').read()
33 def _convert(self
, parser
):
34 if parser
.check(ScalarEvent
):
36 if event
.tag
or event
.anchor
or event
.value
:
40 elif parser
.check(SequenceEvent
):
43 while not parser
.check(CollectionEndEvent
):
44 sequence
.append(self
._convert
(parser
))
47 elif parser
.check(MappingEvent
):
50 while not parser
.check(CollectionEndEvent
):
51 key
= self
._convert
(parser
)
52 value
= self
._convert
(parser
)
53 mapping
.append((key
, value
))
56 elif parser
.check(AliasEvent
):
63 TestStructure
.add_tests('testStructure', '.data', '.structure')
65 class TestParser(test_appliance
.TestAppliance
):
67 def _testParser(self
, test_name
, data_filename
, canonical_filename
):
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
)
79 print file(data_filename
, 'rb').read()
81 print file(canonical_filename
, 'rb').read()
82 print "EVENTS1:", events1
83 print "EVENTS2:", events2
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)
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)
102 TestParser
.add_tests('testParser', '.data', '.canonical')
104 class TestResolver(test_appliance
.TestAppliance
):
106 def _testResolver(self
, test_name
, data_filename
, canonical_filename
):
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())
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
)
122 print file(data_filename
, 'rb').read()
124 print file(canonical_filename
, 'rb').read()
125 print "NODES1:", nodes1
126 print "NODES2:", nodes2
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
):
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
)
160 print file(canonical_filename
, 'rb').read()
161 print "EVENTS1:", events1
162 print "EVENTS2:", events2
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')