source: pyyaml/trunk/tests/test_representer.py @ 136

Revision 136, 3.2 KB checked in by xi, 9 years ago (diff)

Major refactoring.

RevLine 
[133]1
2import test_appliance
3
4try:
5    import datetime
6except ImportError:
7    pass
8try:
9    set
10except NameError:
11    from sets import Set as set
12
13from yaml import *
14
[136]15class MyLoader(Loader):
[133]16    pass
[136]17class MyDumper(Dumper):
[133]18    pass
19
20class MyTestClass1(object):
21
22    def __init__(self, x, y=0, z=0):
23        self.x = x
24        self.y = y
25        self.z = z
26
27    def __eq__(self, other):
28        if isinstance(other, MyTestClass1):
29            return self.__class__, self.__dict__ == other.__class__, other.__dict__
30        else:
31            return False
32
33def construct1(constructor, node):
34    mapping = constructor.construct_mapping(node)
35    return MyTestClass1(**mapping)
36def represent1(representer, native):
37    return representer.represent_mapping("!tag1", native.__dict__)
38
39class MyTestClass2(MyTestClass1, YAMLObject):
40
[136]41    yaml_loader = MyLoader
42    yaml_dumper = MyDumper
[133]43    yaml_tag = "!tag2"
44
45    def from_yaml(cls, constructor, node):
46        x = constructor.construct_yaml_int(node)
47        return cls(x=x)
48    from_yaml = classmethod(from_yaml)
49
50    def to_yaml(cls, representer, native):
51        return representer.represent_scalar(cls.yaml_tag, str(native.x))
52    to_yaml = classmethod(to_yaml)
53
54class MyTestClass3(MyTestClass2):
55
56    yaml_tag = "!tag3"
57
58    def from_yaml(cls, constructor, node):
59        mapping = constructor.construct_mapping(node)
60        if '=' in mapping:
61            x = mapping['=']
62            del mapping['=']
63            mapping['x'] = x
64        return cls(**mapping)
65    from_yaml = classmethod(from_yaml)
66
67    def to_yaml(cls, representer, native):
68        return representer.represent_mapping(cls.yaml_tag, native.__dict__)
69    to_yaml = classmethod(to_yaml)
70
[136]71MyLoader.add_constructor("!tag1", construct1)
72MyDumper.add_representer(MyTestClass1, represent1)
[133]73
[136]74class YAMLObject1(YAMLObject):
75    yaml_loader = MyLoader
76    yaml_dumper = MyDumper
77    yaml_tag = '!foo'
78    yaml_flow_style = True
79
80    def __init__(self, my_parameter=None, my_another_parameter=None):
81        self.my_parameter = my_parameter
82        self.my_another_parameter = my_another_parameter
83
84    def __eq__(self, other):
85        if isinstance(other, YAMLObject1):
86            return self.__class__, self.__dict__ == other.__class__, other.__dict__
87        else:
88            return False
89
[133]90class TestTypeRepresenter(test_appliance.TestAppliance):
91
92    def _testTypes(self, test_name, data_filename, code_filename):
[136]93        data1 = eval(file(code_filename, 'rb').read())
94        data2 = None
[133]95        output = None
96        try:
[136]97            output = dump(data1, Dumper=MyDumper)
98            data2 = load(output, Loader=MyLoader)
[133]99            try:
[136]100                self.failUnlessEqual(data1, data2)
[133]101            except AssertionError:
[136]102                if isinstance(data1, dict):
103                    data1 = data1.items()
104                    data1.sort()
105                    data1 = repr(data1)
106                    data2 = data2.items()
107                    data2.sort()
108                    data2 = repr(data2)
109                if data1 != data2:
[133]110                    raise
111        except:
112            print
113            print "OUTPUT:"
114            print output
[136]115            print "NATIVES1:", data1
116            print "NATIVES2:", data2
[133]117            raise
118
119TestTypeRepresenter.add_tests('testTypes', '.data', '.code')
120
Note: See TracBrowser for help on using the repository browser.