Fix a bug when a block scalar is incorrectly emitted in the simple key context.
[pyyaml/python3.git] / tests / test_yaml_ext.py
blobc92cc29e4a6334ae62cd648fadd2122be373b029
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 _testExtScannerFileInput(self, test_name, data_filename, canonical_filename):
14 self._testExtScanner(test_name, data_filename, canonical_filename, True)
16 def _testExtScanner(self, test_name, data_filename, canonical_filename, file_input=False):
17 if file_input:
18 data = file(data_filename, 'r')
19 else:
20 data = file(data_filename, 'r').read()
21 tokens = list(yaml.scan(data))
22 ext_tokens = []
23 try:
24 if file_input:
25 data = file(data_filename, 'r')
26 for token in yaml.scan(data, Loader=yaml.ExtLoader):
27 ext_tokens.append(token)
28 self.failUnlessEqual(len(tokens), len(ext_tokens))
29 for token, ext_token in zip(tokens, ext_tokens):
30 self.failUnlessEqual(token.__class__, ext_token.__class__)
31 self.failUnlessEqual((token.start_mark.index, token.start_mark.line, token.start_mark.column),
32 (ext_token.start_mark.index, ext_token.start_mark.line, ext_token.start_mark.column))
33 self.failUnlessEqual((token.end_mark.index, token.end_mark.line, token.end_mark.column),
34 (ext_token.end_mark.index, ext_token.end_mark.line, ext_token.end_mark.column))
35 if hasattr(token, 'value'):
36 self.failUnlessEqual(token.value, ext_token.value)
37 except:
38 print
39 print "DATA:"
40 print file(data_filename, 'rb').read()
41 print "TOKENS:", tokens
42 print "EXT_TOKENS:", ext_tokens
43 raise
45 def _testExtParser(self, test_name, data_filename, canonical_filename):
46 data = file(data_filename, 'r').read()
47 events = list(yaml.parse(data))
48 ext_events = []
49 try:
50 for event in yaml.parse(data, Loader=yaml.ExtLoader):
51 ext_events.append(event)
52 #print "EVENT:", event
53 self.failUnlessEqual(len(events), len(ext_events))
54 for event, ext_event in zip(events, ext_events):
55 self.failUnlessEqual(event.__class__, ext_event.__class__)
56 if hasattr(event, 'anchor'):
57 self.failUnlessEqual(event.anchor, ext_event.anchor)
58 if hasattr(event, 'tag'):
59 self.failUnlessEqual(event.tag, ext_event.tag)
60 if hasattr(event, 'implicit'):
61 self.failUnlessEqual(event.implicit, ext_event.implicit)
62 if hasattr(event, 'value'):
63 self.failUnlessEqual(event.value, ext_event.value)
64 except:
65 print
66 print "DATA:"
67 print file(data_filename, 'rb').read()
68 print "EVENTS:", events
69 print "EXT_EVENTS:", ext_events
70 raise
72 TestExtLoader.add_tests('testExtScanner', '.data', '.canonical')
73 TestExtLoader.add_tests('testExtScannerFileInput', '.data', '.canonical')
74 TestExtLoader.add_tests('testExtParser', '.data', '.canonical')
76 def main(module='__main__'):
77 unittest.main(module)
79 if __name__ == '__main__':
80 main()