| 4 | | from yaml import * |
| | 5 | def test_implicit_resolver(data_filename, detect_filename, verbose=False): |
| | 6 | correct_tag = None |
| | 7 | node = None |
| | 8 | try: |
| | 9 | correct_tag = open(detect_filename, 'rb').read().strip() |
| | 10 | node = yaml.compose(open(data_filename, 'rb')) |
| | 11 | assert isinstance(node, yaml.SequenceNode), node |
| | 12 | for scalar in node.value: |
| | 13 | assert isinstance(scalar, yaml.ScalarNode), scalar |
| | 14 | assert scalar.tag == correct_tag, (scalar.tag, correct_tag) |
| | 15 | finally: |
| | 16 | if verbose: |
| | 17 | print "CORRECT TAG:", correct_tag |
| | 18 | if hasattr(node, 'value'): |
| | 19 | print "CHILDREN:" |
| | 20 | pprint.pprint(node.value) |
| 15 | | add_path_resolver(u'!root/scalar', [], str, |
| 16 | | Loader=MyLoader, Dumper=MyDumper) |
| | 32 | yaml.add_path_resolver(u'!root', [], |
| | 33 | Loader=MyLoader, Dumper=MyDumper) |
| | 34 | yaml.add_path_resolver(u'!root/scalar', [], str, |
| | 35 | Loader=MyLoader, Dumper=MyDumper) |
| | 36 | yaml.add_path_resolver(u'!root/key11/key12/*', ['key11', 'key12'], |
| | 37 | Loader=MyLoader, Dumper=MyDumper) |
| | 38 | yaml.add_path_resolver(u'!root/key21/1/*', ['key21', 1], |
| | 39 | Loader=MyLoader, Dumper=MyDumper) |
| | 40 | yaml.add_path_resolver(u'!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict, |
| | 41 | Loader=MyLoader, Dumper=MyDumper) |
| 21 | | add_path_resolver(u'!root/key21/1/*', ['key21', 1], |
| 22 | | Loader=MyLoader, Dumper=MyDumper) |
| | 45 | def _convert_node(node): |
| | 46 | if isinstance(node, yaml.ScalarNode): |
| | 47 | return (node.tag, node.value) |
| | 48 | elif isinstance(node, yaml.SequenceNode): |
| | 49 | value = [] |
| | 50 | for item in node.value: |
| | 51 | value.append(_convert_node(item)) |
| | 52 | return (node.tag, value) |
| | 53 | elif isinstance(node, yaml.MappingNode): |
| | 54 | value = [] |
| | 55 | for key, item in node.value: |
| | 56 | value.append((_convert_node(key), _convert_node(item))) |
| | 57 | return (node.tag, value) |
| 24 | | add_path_resolver(u'!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], dict, |
| 25 | | Loader=MyLoader, Dumper=MyDumper) |
| | 59 | def test_path_resolver_loader(data_filename, path_filename, verbose=False): |
| | 60 | _make_path_loader_and_dumper() |
| | 61 | nodes1 = list(yaml.compose_all(open(data_filename, 'rb').read(), Loader=MyLoader)) |
| | 62 | nodes2 = list(yaml.compose_all(open(path_filename, 'rb').read())) |
| | 63 | try: |
| | 64 | for node1, node2 in zip(nodes1, nodes2): |
| | 65 | data1 = _convert_node(node1) |
| | 66 | data2 = _convert_node(node2) |
| | 67 | assert data1 == data2, (data1, data2) |
| | 68 | finally: |
| | 69 | if verbose: |
| | 70 | print yaml.serialize_all(nodes1) |
| 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 |
| | 74 | def test_path_resolver_dumper(data_filename, path_filename, verbose=False): |
| | 75 | _make_path_loader_and_dumper() |
| | 76 | for filename in [data_filename, path_filename]: |
| | 77 | output = yaml.serialize_all(yaml.compose_all(open(filename, 'rb')), Dumper=MyDumper) |
| | 78 | if verbose: |
| | 79 | print output |
| | 80 | nodes1 = yaml.compose_all(output) |
| | 81 | nodes2 = yaml.compose_all(open(data_filename, 'rb')) |
| | 82 | for node1, node2 in zip(nodes1, nodes2): |
| | 83 | data1 = _convert_node(node1) |
| | 84 | data2 = _convert_node(node2) |
| | 85 | assert data1 == data2, (data1, data2) |
| 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)) |
| | 87 | test_path_resolver_dumper.unittest = ['.data', '.path'] |
| 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)) |
| | 89 | if __name__ == '__main__': |
| | 90 | import test_appliance |
| | 91 | test_appliance.run(globals()) |
| 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 |
| 79 | | |
| 80 | | TestResolver.add_tests('testImplicitResolver', '.data', '.detect') |
| 81 | | TestResolver.add_tests('testPathResolverLoader', '.data', '.path') |
| 82 | | TestResolver.add_tests('testPathResolverDumper', '.data', '.path') |
| 83 | | |