Ignore:
Timestamp:
04/16/06 19:45:21 (8 years ago)
Author:
xi
Message:

Refactor resolver.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • pyyaml/trunk/tests/test_resolver.py

    r136 r137  
    44from yaml import * 
    55 
    6 class TestDetector(test_appliance.TestAppliance): 
     6class MyLoader(Loader): 
     7    pass 
    78 
    8     def _testDetector(self, test_name, data_filename, detect_filename): 
     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 
     24add_path_resolver(u'!root/key31/*/*/key14/map', ['key31', None, None, 'key14'], map, 
     25        Loader=MyLoader, Dumper=MyDumper) 
     26 
     27class TestResolver(test_appliance.TestAppliance): 
     28 
     29    def _testImplicitResolver(self, test_name, data_filename, detect_filename): 
    930        node = None 
    1031        correct_tag = None 
     
    2647            raise 
    2748 
    28 TestDetector.add_tests('testDetector', '.data', '.detect') 
     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)) 
    2955 
     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 
     81TestResolver.add_tests('testImplicitResolver', '.data', '.detect') 
     82TestResolver.add_tests('testPathResolverLoader', '.data', '.path') 
     83TestResolver.add_tests('testPathResolverDumper', '.data', '.path') 
     84 
Note: See TracChangeset for help on using the changeset viewer.