Changeset 222 for pyyaml/trunk/tests

Show
Ignore:
Timestamp:
08/03/06 12:07:29 (6 years ago)
Author:
xi
Message:

Subclass all base classes from object.

Hold references to the objects being represented (should fix #22).

The value of a mapping node is represented as a list of pairs (key, value)
now.

Sort dictionary items (fix #23).

Recursive structures are now loaded and dumped correctly, including complex
structures like recursive tuples (fix #5). Thanks Peter Murphy for the patches.
To make it possible, representer functions are allowed to be generators.
In this case, the first generated value is an object. Other values produced
by the representer are ignored.

Make Representer not try to guess !!pairs when a list is represented.
You need to construct a !!pairs node explicitly now.

Do not check for duplicate mapping keys as it didn't work correctly anyway.

Location:
pyyaml/trunk/tests
Files:
9 added
4 modified
6 moved

Legend:

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

    r173 r222  
    240240    def __eq__(self, other): 
    241241        return type(self) is type(other) and dict(self) == dict(other) 
     242 
     243def execute(code): 
     244    exec code 
     245    return value 
    242246 
    243247class TestConstructorTypes(test_appliance.TestAppliance): 
  • pyyaml/trunk/tests/test_recursive.py

    r142 r222  
    11 
    2 import unittest 
     2import test_appliance 
     3 
    34from yaml import * 
    45 
    5 RECURSIVE = """ 
    6 --- &A 
    7 - *A: *A 
    8 """ 
     6class AnInstance: 
    97 
    10 class TestRecursive(unittest.TestCase): 
     8    def __init__(self, foo, bar): 
     9        self.foo = foo 
     10        self.bar = bar 
    1111 
    12     def testRecursive(self): 
    13         node = compose(RECURSIVE) 
    14         self._check(node) 
    15         document = serialize(node) 
    16         node = compose(document) 
    17         self._check(node) 
     12    def __repr__(self): 
     13        try: 
     14            return "%s(foo=%r, bar=%r)" % (self.__class__.__name__, 
     15                    self.foo, self.bar) 
     16        except RuntimeError: 
     17            return "%s(foo=..., bar=...)" % self.__class__.__name__ 
    1818 
    19     def _check(self, node): 
    20         self.failUnless(node in node.value[0].value) 
    21         self.failUnless(node.value[0].value[node] is node) 
     19class AnInstanceWithState(AnInstance): 
    2220 
     21    def __getstate__(self): 
     22        return {'attributes': [self.foo, self.bar]} 
     23 
     24    def __setstate__(self, state): 
     25        self.foo, self.bar = state['attributes'] 
     26 
     27class TestRecursive(test_appliance.TestAppliance): 
     28 
     29    def _testRecursive(self, test_name, recursive_filename): 
     30        exec file(recursive_filename, 'r').read() 
     31        value1 = value 
     32        output1 = None 
     33        value2 = None 
     34        output2 = None 
     35        try: 
     36            output1 = dump(value1) 
     37            #print "OUTPUT %s:" % test_name 
     38            #print output1 
     39            value2 = load(output1) 
     40            output2 = dump(value2) 
     41            self.failUnlessEqual(output1, output2) 
     42        except: 
     43            print "VALUE1:", value1 
     44            print "VALUE2:", value2 
     45            print "OUTPUT1:" 
     46            print output1 
     47            print "OUTPUT2:" 
     48            print output2 
     49            raise 
     50 
     51TestRecursive.add_tests('testRecursive', '.recursive') 
     52 
  • pyyaml/trunk/tests/test_resolver.py

    r166 r222  
    7373        elif isinstance(node, MappingNode): 
    7474            value = [] 
    75             for key in node.value: 
    76                 item = node.value[key] 
     75            for key, item in node.value: 
    7776                value.append((self._convert(key), self._convert(item))) 
    7877            value.sort() 
  • pyyaml/trunk/tests/test_structure.py

    r146 r222  
    154154        return self.construct_scalar(node) 
    155155 
     156MyLoader.add_constructor(u'tag:yaml.org,2002:map', MyLoader.construct_mapping) 
    156157MyLoader.add_constructor(None, MyLoader.construct_undefined) 
    157158 
     
    169170        return self.construct_scalar(node) 
    170171 
     172MyCanonicalLoader.add_constructor(u'tag:yaml.org,2002:map', MyCanonicalLoader.construct_mapping) 
    171173MyCanonicalLoader.add_constructor(None, MyCanonicalLoader.construct_undefined) 
    172174