4 from yaml
.reader
import Reader
5 from yaml
.scanner
import Scanner
6 from yaml
.parser
import *
8 class TestStructure(test_appliance
.TestAppliance
):
10 def _testStructure(self
, test_name
, data_filename
, structure_filename
):
12 node2
= eval(file(structure_filename
, 'rb').read())
14 parser
= Parser(Scanner(Reader(file(data_filename
, 'rb'))))
16 while not parser
.check(StreamEndEvent
):
17 node1
.append(self
._convert
(parser
))
21 self
.failUnlessEqual(node1
, node2
)
25 print file(data_filename
, 'rb').read()
30 def _convert(self
, parser
):
31 if parser
.check(ScalarEvent
):
33 if event
.tag
or event
.anchor
or event
.value
:
37 elif parser
.check(SequenceEvent
):
40 while not parser
.check(CollectionEndEvent
):
41 sequence
.append(self
._convert
(parser
))
44 elif parser
.check(MappingEvent
):
47 while not parser
.check(CollectionEndEvent
):
48 key
= self
._convert
(parser
)
49 value
= self
._convert
(parser
)
50 mapping
.append((key
, value
))
53 elif parser
.check(AliasEvent
):
60 TestStructure
.add_tests('testStructure', '.data', '.structure')
62 class TestParser(test_appliance
.TestAppliance
):
64 def _testParser(self
, test_name
, data_filename
, canonical_filename
):
68 parser
= Parser(Scanner(Reader(file(data_filename
, 'rb'))))
69 events1
= list(iter(parser
))
70 canonical
= test_appliance
.CanonicalParser(file(canonical_filename
, 'rb').read())
71 events2
= canonical
.parse()
72 self
._compare
(events1
, events2
)
76 print file(data_filename
, 'rb').read()
78 print file(canonical_filename
, 'rb').read()
79 print "EVENTS1:", events1
80 print "EVENTS2:", events2
83 def _compare(self
, events1
, events2
):
84 self
.failUnlessEqual(len(events1
), len(events2
))
85 for event1
, event2
in zip(events1
, events2
):
86 self
.failUnlessEqual(event1
.__class__
, event2
.__class__
)
87 if isinstance(event1
, AliasEvent
):
88 #self.failUnlessEqual(event1.name, event2.name)
90 elif isinstance(event1
, ScalarEvent
):
91 #self.failUnlessEqual(event1.anchor, event2.anchor)
92 #self.failUnlessEqual(event1.tag, event2.tag)
93 self
.failUnlessEqual(event1
.value
, event2
.value
)
94 if isinstance(event1
, CollectionEvent
):
95 #self.failUnlessEqual(event1.anchor, event2.anchor)
96 #self.failUnlessEqual(event1.tag, event2.tag)
100 TestParser
.add_tests('testParser', '.data', '.canonical')
102 class TestParserOnCanonical(test_appliance
.TestAppliance
):
104 def _testParserOnCanonical(self
, test_name
, canonical_filename
):
108 parser
= Parser(Scanner(Reader(file(canonical_filename
, 'rb'))))
109 events1
= list(iter(parser
))
110 canonical
= test_appliance
.CanonicalParser(file(canonical_filename
, 'rb').read())
111 events2
= canonical
.parse()
112 self
._compare
(events1
, events2
)
116 print file(canonical_filename
, 'rb').read()
117 print "EVENTS1:", events1
118 print "EVENTS2:", events2
121 def _compare(self
, events1
, events2
):
122 self
.failUnlessEqual(len(events1
), len(events2
))
123 for event1
, event2
in zip(events1
, events2
):
124 self
.failUnlessEqual(event1
.__class__
, event2
.__class__
)
125 if isinstance(event1
, AliasEvent
):
126 self
.failUnlessEqual(event1
.name
, event2
.name
)
127 elif isinstance(event1
, ScalarEvent
):
128 self
.failUnlessEqual(event1
.anchor
, event2
.anchor
)
129 self
.failUnlessEqual(event1
.tag
, event2
.tag
)
130 self
.failUnlessEqual(event1
.value
, event2
.value
)
131 if isinstance(event1
, CollectionEvent
):
132 self
.failUnlessEqual(event1
.anchor
, event2
.anchor
)
133 self
.failUnlessEqual(event1
.tag
, event2
.tag
)
135 TestParserOnCanonical
.add_tests('testParserOnCanonical', '.canonical')