Remove y/n from the boolean constants.
[pyyaml/python3.git] / tests / test_constructor.py
blob794d3e4e220c726eb7953944ed308d57335dc279
2 import test_appliance
3 try:
4 import datetime
5 except ImportError:
6 pass
7 try:
8 set
9 except NameError:
10 from sets import Set as set
12 from yaml import *
14 class MyConstructor(Constructor):
15 pass
17 class MyTestClass1:
19 def __init__(self, x, y=0, z=0):
20 self.x = x
21 self.y = y
22 self.z = z
24 def __eq__(self, other):
25 return self.__class__, self.__dict__ == other.__class__, other.__dict__
27 def construct1(constructor, node):
28 mapping = constructor.construct_mapping(node)
29 return MyTestClass1(**mapping)
31 MyConstructor.add_constructor("!tag1", construct1)
33 class MyTestClass2(MyTestClass1, YAMLObject):
35 yaml_constructor = MyConstructor
36 yaml_tag = "!tag2"
38 def from_yaml(cls, constructor, node):
39 x = constructor.construct_yaml_int(node)
40 return cls(x=x)
41 from_yaml = classmethod(from_yaml)
43 class MyTestClass3(MyTestClass2):
45 yaml_tag = "!tag3"
47 def from_yaml(cls, constructor, node):
48 mapping = constructor.construct_mapping(node)
49 if '=' in mapping:
50 x = mapping['=']
51 del mapping['=']
52 mapping['x'] = x
53 return cls(**mapping)
54 from_yaml = classmethod(from_yaml)
56 class TestTypes(test_appliance.TestAppliance):
58 def _testTypes(self, test_name, data_filename, code_filename):
59 natives1 = None
60 natives2 = None
61 try:
62 constructor1 = MyConstructor(Resolver(Composer(Parser(Scanner(Reader(file(data_filename, 'rb')))))))
63 natives1 = list(iter(constructor1))
64 if len(natives1) == 1:
65 natives1 = natives1[0]
66 natives2 = eval(file(code_filename, 'rb').read())
67 try:
68 self.failUnlessEqual(natives1, natives2)
69 except AssertionError:
70 if isinstance(natives1, dict):
71 natives1 = natives1.items()
72 natives1.sort()
73 natives1 = repr(natives1)
74 natives2 = natives2.items()
75 natives2.sort()
76 natives2 = repr(natives2)
77 if natives1 != natives2:
78 raise
79 except:
80 print
81 print "DATA:"
82 print file(data_filename, 'rb').read()
83 print "CODE:"
84 print file(code_filename, 'rb').read()
85 print "NATIVES1:", natives1
86 print "NATIVES2:", natives2
87 raise
89 TestTypes.add_tests('testTypes', '.data', '.code')