Update PyRex based bindings to libyaml to include Parser functions.
[pyyaml/python3.git] / tests / test_yaml_ext.py
blobcd3e2043c26a5f6492385991efb241216e39488c
2 import unittest, test_appliance
4 import _yaml, yaml
6 class TestExtVersion(unittest.TestCase):
8 def testExtVersion(self):
9 self.failUnlessEqual("%s.%s.%s" % _yaml.get_version(), _yaml.get_version_string())
11 class TestExtLoader(test_appliance.TestAppliance):
13 def _testExtScanner(self, test_name, data_filename, canonical_filename):
14 data = file(data_filename, 'r').read()
15 tokens = list(yaml.scan(data))
16 ext_tokens = []
17 try:
18 for token in yaml.scan(data, Loader=yaml.ExtLoader):
19 ext_tokens.append(token)
20 self.failUnlessEqual(len(tokens), len(ext_tokens))
21 for token, ext_token in zip(tokens, ext_tokens):
22 self.failUnlessEqual(token.__class__, ext_token.__class__)
23 self.failUnlessEqual((token.start_mark.index, token.start_mark.line, token.start_mark.column),
24 (ext_token.start_mark.index, ext_token.start_mark.line, ext_token.start_mark.column))
25 self.failUnlessEqual((token.end_mark.index, token.end_mark.line, token.end_mark.column),
26 (ext_token.end_mark.index, ext_token.end_mark.line, ext_token.end_mark.column))
27 if hasattr(token, 'value'):
28 self.failUnlessEqual(token.value, ext_token.value)
29 except:
30 print
31 print "DATA:"
32 print file(data_filename, 'rb').read()
33 print "TOKENS:", tokens
34 print "EXT_TOKENS:", ext_tokens
35 raise
37 def _testExtParser(self, test_name, data_filename, canonical_filename):
38 data = file(data_filename, 'r').read()
39 events = list(yaml.parse(data))
40 ext_events = []
41 try:
42 for event in yaml.parse(data, Loader=yaml.ExtLoader):
43 ext_events.append(event)
44 #print "EVENT:", event
45 self.failUnlessEqual(len(events), len(ext_events))
46 for event, ext_event in zip(events, ext_events):
47 self.failUnlessEqual(event.__class__, ext_event.__class__)
48 if hasattr(event, 'anchor'):
49 self.failUnlessEqual(event.anchor, ext_event.anchor)
50 if hasattr(event, 'tag'):
51 self.failUnlessEqual(event.tag, ext_event.tag)
52 if hasattr(event, 'implicit'):
53 self.failUnlessEqual(event.implicit, ext_event.implicit)
54 if hasattr(event, 'value'):
55 self.failUnlessEqual(event.value, ext_event.value)
56 except:
57 print
58 print "DATA:"
59 print file(data_filename, 'rb').read()
60 print "EVENTS:", events
61 print "EXT_EVENTS:", ext_events
62 raise
64 TestExtLoader.add_tests('testExtScanner', '.data', '.canonical')
65 TestExtLoader.add_tests('testExtParser', '.data', '.canonical')
67 def main(module='__main__'):
68 unittest.main(module)
70 if __name__ == '__main__':
71 main()