Parser is done. Add iterator interfaces for Scanner and Parser.
[pyyaml/python3.git] / tests / test_structure.py
blob0bffbcd8f9f31933ea6d5b98dd0a6b27327702ba
2 import test_appliance
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):
11 node1 = None
12 node2 = eval(file(structure_filename, 'rb').read())
13 try:
14 parser = Parser(Scanner(Reader(file(data_filename, 'rb'))))
15 node1 = []
16 while not parser.check(StreamEndEvent):
17 node1.append(self._convert(parser))
18 parser.get()
19 if len(node1) == 1:
20 node1 = node1[0]
21 self.failUnlessEqual(node1, node2)
22 except:
23 print
24 print "DATA:"
25 print file(data_filename, 'rb').read()
26 print "NODE1:", node1
27 print "NODE2:", node2
28 raise
30 def _convert(self, parser):
31 if parser.check(ScalarEvent):
32 event = parser.get()
33 if event.tag or event.anchor or event.value:
34 return True
35 else:
36 return None
37 elif parser.check(SequenceEvent):
38 parser.get()
39 sequence = []
40 while not parser.check(CollectionEndEvent):
41 sequence.append(self._convert(parser))
42 parser.get()
43 return sequence
44 elif parser.check(MappingEvent):
45 parser.get()
46 mapping = []
47 while not parser.check(CollectionEndEvent):
48 key = self._convert(parser)
49 value = self._convert(parser)
50 mapping.append((key, value))
51 parser.get()
52 return mapping
53 elif parser.check(AliasEvent):
54 parser.get()
55 return '*'
56 else:
57 parser.get()
58 return '?'
60 TestStructure.add_tests('testStructure', '.data', '.structure')
62 class TestParser(test_appliance.TestAppliance):
64 def _testParser(self, test_name, data_filename, canonical_filename):
65 events1 = None
66 events2 = None
67 try:
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)
73 except:
74 print
75 print "DATA1:"
76 print file(data_filename, 'rb').read()
77 print "DATA2:"
78 print file(canonical_filename, 'rb').read()
79 print "EVENTS1:", events1
80 print "EVENTS2:", events2
81 raise
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)
89 pass
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)
97 pass
100 TestParser.add_tests('testParser', '.data', '.canonical')
102 class TestParserOnCanonical(test_appliance.TestAppliance):
104 def _testParserOnCanonical(self, test_name, canonical_filename):
105 events1 = None
106 events2 = None
107 try:
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)
113 except:
114 print
115 print "DATA:"
116 print file(canonical_filename, 'rb').read()
117 print "EVENTS1:", events1
118 print "EVENTS2:", events2
119 raise
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')