Add the tests for checking the libyaml bug.
[pyyaml/python3.git] / tests / test_resolver.py
bloba1845d81309bca26d18d9a5dde833fd9a2315b7b
2 import test_appliance
4 from yaml import *
6 class MyLoader(Loader):
7 pass
9 class MyDumper(Dumper):
10 pass
12 add_path_resolver(u'!root', [],
13 Loader=MyLoader, Dumper=MyDumper)
15 add_path_resolver(u'!root/scalar', [], str,
16 Loader=MyLoader, Dumper=MyDumper)
18 add_path_resolver(u'!root/key11/key12/*', ['key11', 'key12'],
19 Loader=MyLoader, Dumper=MyDumper)
21 add_path_resolver(u'!root/key21/1/*', ['key21', 1],
22 Loader=MyLoader, Dumper=MyDumper)
24 add_path_resolver(u'!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict,
25 Loader=MyLoader, Dumper=MyDumper)
27 class TestResolver(test_appliance.TestAppliance):
29 def _testImplicitResolver(self, test_name, data_filename, detect_filename):
30 node = None
31 correct_tag = None
32 try:
33 correct_tag = file(detect_filename, 'rb').read().strip()
34 node = compose(file(data_filename, 'rb'))
35 self.failUnless(isinstance(node, SequenceNode))
36 for scalar in node.value:
37 self.failUnless(isinstance(scalar, ScalarNode))
38 self.failUnlessEqual(scalar.tag, correct_tag)
39 except:
40 print
41 print "DATA:"
42 print file(data_filename, 'rb').read()
43 print "CORRECT_TAG:"
44 print file(detect_filename, 'rb').read()
45 print "ROOT NODE:", node
46 print "SCALAR NODES:", node.value
47 raise
49 def _testPathResolverLoader(self, test_name, data_filename, path_filename):
50 #print serialize_all(compose_all(file(data_filename, 'rb').read(), Loader=MyLoader))
51 nodes1 = compose_all(file(data_filename, 'rb').read(), Loader=MyLoader)
52 nodes2 = compose_all(file(path_filename, 'rb').read())
53 for node1, node2 in zip(nodes1, nodes2):
54 self.failUnlessEqual(self._convert(node1), self._convert(node2))
56 def _testPathResolverDumper(self, test_name, data_filename, path_filename):
57 for filename in [data_filename, path_filename]:
58 output = serialize_all(compose_all(file(filename, 'rb').read()), Dumper=MyDumper)
59 #print output
60 nodes1 = compose_all(output)
61 nodes2 = compose_all(file(data_filename, 'rb').read())
62 for node1, node2 in zip(nodes1, nodes2):
63 self.failUnlessEqual(self._convert(node1), self._convert(node2))
65 def _convert(self, node):
66 if isinstance(node, ScalarNode):
67 return node.tag, node.value
68 elif isinstance(node, SequenceNode):
69 value = []
70 for item in node.value:
71 value.append(self._convert(item))
72 return node.tag, value
73 elif isinstance(node, MappingNode):
74 value = []
75 for key, item in node.value:
76 value.append((self._convert(key), self._convert(item)))
77 value.sort()
78 return node.tag, value
80 TestResolver.add_tests('testImplicitResolver', '.data', '.detect')
81 TestResolver.add_tests('testPathResolverLoader', '.data', '.path')
82 TestResolver.add_tests('testPathResolverDumper', '.data', '.path')