Add more unit tests for error messages.
[pyyaml/python3.git] / tests / test_representer.py
blob82ad46ecfbc4dd29f48d33dab04e0614f85d5e35
2 import test_appliance
4 try:
5 import datetime
6 except ImportError:
7 pass
8 try:
9 set
10 except NameError:
11 from sets import Set as set
13 from yaml import *
15 class MyLoader(Loader):
16 pass
17 class MyDumper(Dumper):
18 pass
20 class MyTestClass1(object):
22 def __init__(self, x, y=0, z=0):
23 self.x = x
24 self.y = y
25 self.z = z
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
33 def construct1(constructor, node):
34 mapping = constructor.construct_mapping(node)
35 return MyTestClass1(**mapping)
36 def represent1(representer, native):
37 return representer.represent_mapping("!tag1", native.__dict__)
39 class MyTestClass2(MyTestClass1, YAMLObject):
41 yaml_loader = MyLoader
42 yaml_dumper = MyDumper
43 yaml_tag = "!tag2"
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)
50 def to_yaml(cls, representer, native):
51 return representer.represent_scalar(cls.yaml_tag, str(native.x))
52 to_yaml = classmethod(to_yaml)
54 class MyTestClass3(MyTestClass2):
56 yaml_tag = "!tag3"
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)
67 def to_yaml(cls, representer, native):
68 return representer.represent_mapping(cls.yaml_tag, native.__dict__)
69 to_yaml = classmethod(to_yaml)
71 MyLoader.add_constructor("!tag1", construct1)
72 MyDumper.add_representer(MyTestClass1, represent1)
74 class YAMLObject1(YAMLObject):
75 yaml_loader = MyLoader
76 yaml_dumper = MyDumper
77 yaml_tag = '!foo'
78 yaml_flow_style = True
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
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
90 class TestTypeRepresenter(test_appliance.TestAppliance):
92 def _testTypes(self, test_name, data_filename, code_filename):
93 data1 = eval(file(code_filename, 'rb').read())
94 data2 = None
95 output = None
96 try:
97 output = dump(data1, Dumper=MyDumper)
98 data2 = load(output, Loader=MyLoader)
99 try:
100 self.failUnlessEqual(data1, data2)
101 except AssertionError:
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:
110 raise
111 except:
112 print
113 print "OUTPUT:"
114 print output
115 print "NATIVES1:", data1
116 print "NATIVES2:", data2
117 raise
119 TestTypeRepresenter.add_tests('testTypes', '.data', '.code')