6 class MyLoader(Loader
):
9 class MyDumper(Dumper
):
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
):
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
)
42 print file(data_filename
, 'rb').read()
44 print file(detect_filename
, 'rb').read()
45 print "ROOT NODE:", node
46 print "SCALAR NODES:", node
.value
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
)
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
):
70 for item
in node
.value
:
71 value
.append(self
._convert
(item
))
72 return node
.tag
, value
73 elif isinstance(node
, MappingNode
):
75 for key
, item
in node
.value
:
76 value
.append((self
._convert
(key
), self
._convert
(item
)))
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')