| [55] | 1 | |
|---|
| 2 | import test_appliance |
|---|
| 3 | |
|---|
| [136] | 4 | from yaml import * |
|---|
| [55] | 5 | |
|---|
| [137] | 6 | class MyLoader(Loader): |
|---|
| 7 | pass |
|---|
| [55] | 8 | |
|---|
| [137] | 9 | class MyDumper(Dumper): |
|---|
| 10 | pass |
|---|
| 11 | |
|---|
| 12 | add_path_resolver(u'!root', [], |
|---|
| 13 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| 14 | |
|---|
| 15 | add_path_resolver(u'!root/scalar', [], str, |
|---|
| 16 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| 17 | |
|---|
| 18 | add_path_resolver(u'!root/key11/key12/*', ['key11', 'key12'], |
|---|
| 19 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| 20 | |
|---|
| 21 | add_path_resolver(u'!root/key21/1/*', ['key21', 1], |
|---|
| 22 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| 23 | |
|---|
| 24 | add_path_resolver(u'!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], map, |
|---|
| 25 | Loader=MyLoader, Dumper=MyDumper) |
|---|
| 26 | |
|---|
| 27 | class 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 = [] |
|---|
| 75 | for key in node.value: |
|---|
| 76 | item = node.value[key] |
|---|
| 77 | value.append((self._convert(key), self._convert(item))) |
|---|
| 78 | value.sort() |
|---|
| 79 | return node.tag, value |
|---|
| 80 | |
|---|
| 81 | TestResolver.add_tests('testImplicitResolver', '.data', '.detect') |
|---|
| 82 | TestResolver.add_tests('testPathResolverLoader', '.data', '.path') |
|---|
| 83 | TestResolver.add_tests('testPathResolverDumper', '.data', '.path') |
|---|
| 84 | |
|---|