Source

pyyaml / tests / test_resolver.py

Diff from to

tests/test_resolver.py

 
-import test_appliance
+import yaml
+import pprint
 
-from yaml import *
+def test_implicit_resolver(data_filename, detect_filename, verbose=False):
+    correct_tag = None
+    node = None
+    try:
+        correct_tag = open(detect_filename, 'rb').read().strip()
+        node = yaml.compose(open(data_filename, 'rb'))
+        assert isinstance(node, yaml.SequenceNode), node
+        for scalar in node.value:
+            assert isinstance(scalar, yaml.ScalarNode), scalar
+            assert scalar.tag == correct_tag, (scalar.tag, correct_tag)
+    finally:
+        if verbose:
+            print "CORRECT TAG:", correct_tag
+            if hasattr(node, 'value'):
+                print "CHILDREN:"
+                pprint.pprint(node.value)
 
-class MyLoader(Loader):
-    pass
+test_implicit_resolver.unittest = ['.data', '.detect']
 
-class MyDumper(Dumper):
-    pass
+def _make_path_loader_and_dumper():
+    global MyLoader, MyDumper
 
-add_path_resolver(u'!root', [],
-        Loader=MyLoader, Dumper=MyDumper)
+    class MyLoader(yaml.Loader):
+        pass
+    class MyDumper(yaml.Dumper):
+        pass
 
-add_path_resolver(u'!root/scalar', [], str,
-        Loader=MyLoader, Dumper=MyDumper)
+    yaml.add_path_resolver(u'!root', [],
+            Loader=MyLoader, Dumper=MyDumper)
+    yaml.add_path_resolver(u'!root/scalar', [], str,
+            Loader=MyLoader, Dumper=MyDumper)
+    yaml.add_path_resolver(u'!root/key11/key12/*', ['key11', 'key12'],
+            Loader=MyLoader, Dumper=MyDumper)
+    yaml.add_path_resolver(u'!root/key21/1/*', ['key21', 1],
+            Loader=MyLoader, Dumper=MyDumper)
+    yaml.add_path_resolver(u'!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict,
+            Loader=MyLoader, Dumper=MyDumper)
 
-add_path_resolver(u'!root/key11/key12/*', ['key11', 'key12'],
-        Loader=MyLoader, Dumper=MyDumper)
+    return MyLoader, MyDumper
 
-add_path_resolver(u'!root/key21/1/*', ['key21', 1],
-        Loader=MyLoader, Dumper=MyDumper)
+def _convert_node(node):
+    if isinstance(node, yaml.ScalarNode):
+        return (node.tag, node.value)
+    elif isinstance(node, yaml.SequenceNode):
+        value = []
+        for item in node.value:
+            value.append(_convert_node(item))
+        return (node.tag, value)
+    elif isinstance(node, yaml.MappingNode):
+        value = []
+        for key, item in node.value:
+            value.append((_convert_node(key), _convert_node(item)))
+        return (node.tag, value)
 
-add_path_resolver(u'!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict,
-        Loader=MyLoader, Dumper=MyDumper)
+def test_path_resolver_loader(data_filename, path_filename, verbose=False):
+    _make_path_loader_and_dumper()
+    nodes1 = list(yaml.compose_all(open(data_filename, 'rb').read(), Loader=MyLoader))
+    nodes2 = list(yaml.compose_all(open(path_filename, 'rb').read()))
+    try:
+        for node1, node2 in zip(nodes1, nodes2):
+            data1 = _convert_node(node1)
+            data2 = _convert_node(node2)
+            assert data1 == data2, (data1, data2)
+    finally:
+        if verbose:
+            print yaml.serialize_all(nodes1)
 
-class TestResolver(test_appliance.TestAppliance):
+test_path_resolver_loader.unittest = ['.data', '.path']
 
-    def _testImplicitResolver(self, test_name, data_filename, detect_filename):
-        node = None
-        correct_tag = None
-        try:
-            correct_tag = file(detect_filename, 'rb').read().strip()
-            node = compose(file(data_filename, 'rb'))
-            self.failUnless(isinstance(node, SequenceNode))
-            for scalar in node.value:
-                self.failUnless(isinstance(scalar, ScalarNode))
-                self.failUnlessEqual(scalar.tag, correct_tag)
-        except:
-            print
-            print "DATA:"
-            print file(data_filename, 'rb').read()
-            print "CORRECT_TAG:"
-            print file(detect_filename, 'rb').read()
-            print "ROOT NODE:", node
-            print "SCALAR NODES:", node.value
-            raise
+def test_path_resolver_dumper(data_filename, path_filename, verbose=False):
+    _make_path_loader_and_dumper()
+    for filename in [data_filename, path_filename]:
+        output = yaml.serialize_all(yaml.compose_all(open(filename, 'rb')), Dumper=MyDumper)
+        if verbose:
+            print output
+        nodes1 = yaml.compose_all(output)
+        nodes2 = yaml.compose_all(open(data_filename, 'rb'))
+        for node1, node2 in zip(nodes1, nodes2):
+            data1 = _convert_node(node1)
+            data2 = _convert_node(node2)
+            assert data1 == data2, (data1, data2)
 
-    def _testPathResolverLoader(self, test_name, data_filename, path_filename):
-        #print serialize_all(compose_all(file(data_filename, 'rb').read(), Loader=MyLoader))
-        nodes1 = compose_all(file(data_filename, 'rb').read(), Loader=MyLoader)
-        nodes2 = compose_all(file(path_filename, 'rb').read())
-        for node1, node2 in zip(nodes1, nodes2):
-            self.failUnlessEqual(self._convert(node1), self._convert(node2))
+test_path_resolver_dumper.unittest = ['.data', '.path']
 
-    def _testPathResolverDumper(self, test_name, data_filename, path_filename):
-        for filename in [data_filename, path_filename]:
-            output = serialize_all(compose_all(file(filename, 'rb').read()), Dumper=MyDumper)
-            #print output
-            nodes1 = compose_all(output)
-            nodes2 = compose_all(file(data_filename, 'rb').read())
-            for node1, node2 in zip(nodes1, nodes2):
-                self.failUnlessEqual(self._convert(node1), self._convert(node2))
+if __name__ == '__main__':
+    import test_appliance
+    test_appliance.run(globals())
 
-    def _convert(self, node):
-        if isinstance(node, ScalarNode):
-            return node.tag, node.value
-        elif isinstance(node, SequenceNode):
-            value = []
-            for item in node.value:
-                value.append(self._convert(item))
-            return node.tag, value
-        elif isinstance(node, MappingNode):
-            value = []
-            for key, item in node.value:
-                value.append((self._convert(key), self._convert(item)))
-            value.sort()
-            return node.tag, value
-
-TestResolver.add_tests('testImplicitResolver', '.data', '.detect')
-TestResolver.add_tests('testPathResolverLoader', '.data', '.path')
-TestResolver.add_tests('testPathResolverDumper', '.data', '.path')
-