source: pyyaml/trunk/tests/test_resolver.py @ 222

Revision 222, 3.1 KB checked in by xi, 8 years ago (diff)

Subclass all base classes from object.

Hold references to the objects being represented (should fix #22).

The value of a mapping node is represented as a list of pairs (key, value)
now.

Sort dictionary items (fix #23).

Recursive structures are now loaded and dumped correctly, including complex
structures like recursive tuples (fix #5). Thanks Peter Murphy for the patches.
To make it possible, representer functions are allowed to be generators.
In this case, the first generated value is an object. Other values produced
by the representer are ignored.

Make Representer not try to guess !!pairs when a list is represented.
You need to construct a !!pairs node explicitly now.

Do not check for duplicate mapping keys as it didn't work correctly anyway.

RevLine 
[55]1
2import test_appliance
3
[136]4from yaml import *
[55]5
[137]6class MyLoader(Loader):
7    pass
[55]8
[137]9class MyDumper(Dumper):
10    pass
11
12add_path_resolver(u'!root', [],
13        Loader=MyLoader, Dumper=MyDumper)
14
15add_path_resolver(u'!root/scalar', [], str,
16        Loader=MyLoader, Dumper=MyDumper)
17
18add_path_resolver(u'!root/key11/key12/*', ['key11', 'key12'],
19        Loader=MyLoader, Dumper=MyDumper)
20
21add_path_resolver(u'!root/key21/1/*', ['key21', 1],
22        Loader=MyLoader, Dumper=MyDumper)
23
[166]24add_path_resolver(u'!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict,
[137]25        Loader=MyLoader, Dumper=MyDumper)
26
27class TestResolver(test_appliance.TestAppliance):
28
29    def _testImplicitResolver(self, test_name, data_filename, detect_filename):
[55]30        node = None
31        correct_tag = None
32        try:
33            correct_tag = file(detect_filename, 'rb').read().strip()
[136]34            node = compose(file(data_filename, 'rb'))
[55]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
48
[137]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))
[55]55
[137]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))
64
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 = []
[222]75            for key, item in node.value:
[137]76                value.append((self._convert(key), self._convert(item)))
77            value.sort()
78            return node.tag, value
79
80TestResolver.add_tests('testImplicitResolver', '.data', '.detect')
81TestResolver.add_tests('testPathResolverLoader', '.data', '.path')
82TestResolver.add_tests('testPathResolverDumper', '.data', '.path')
83
Note: See TracBrowser for help on using the repository browser.