Changeset 144 for pyyaml/trunk/tests/test_constructor.py
- Timestamp:
- 04/18/06 15:33:30 (7 years ago)
- File:
-
- 1 edited
-
pyyaml/trunk/tests/test_constructor.py (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pyyaml/trunk/tests/test_constructor.py
r136 r144 12 12 from yaml import * 13 13 14 import xml.parsers 15 14 16 class MyLoader(Loader): 17 pass 18 class MyDumper(Dumper): 15 19 pass 16 20 … … 23 27 24 28 def __eq__(self, other): 25 return self.__class__, self.__dict__ == other.__class__, other.__dict__ 29 if isinstance(other, MyTestClass1): 30 return self.__class__, self.__dict__ == other.__class__, other.__dict__ 31 else: 32 return False 26 33 27 34 def construct1(constructor, node): 28 35 mapping = constructor.construct_mapping(node) 29 36 return MyTestClass1(**mapping) 37 def represent1(representer, native): 38 return representer.represent_mapping("!tag1", native.__dict__) 30 39 31 MyLoader.add_constructor("!tag1", construct1) 40 add_constructor("!tag1", construct1, Loader=MyLoader) 41 add_representer(MyTestClass1, represent1, Dumper=MyDumper) 32 42 33 43 class MyTestClass2(MyTestClass1, YAMLObject): 34 44 35 45 yaml_loader = MyLoader 46 yaml_dumper = MyDumper 36 47 yaml_tag = "!tag2" 37 48 … … 40 51 return cls(x=x) 41 52 from_yaml = classmethod(from_yaml) 53 54 def to_yaml(cls, representer, native): 55 return representer.represent_scalar(cls.yaml_tag, str(native.x)) 56 to_yaml = classmethod(to_yaml) 42 57 43 58 class MyTestClass3(MyTestClass2): … … 54 69 from_yaml = classmethod(from_yaml) 55 70 71 def to_yaml(cls, representer, native): 72 return representer.represent_mapping(cls.yaml_tag, native.__dict__) 73 to_yaml = classmethod(to_yaml) 74 56 75 class YAMLObject1(YAMLObject): 76 57 77 yaml_loader = MyLoader 78 yaml_dumper = MyDumper 58 79 yaml_tag = '!foo' 59 80 … … 68 89 return False 69 90 70 class TestTypes(test_appliance.TestAppliance): 91 class YAMLObject2(YAMLObject): 92 93 yaml_loader = MyLoader 94 yaml_dumper = MyDumper 95 yaml_tag = '!bar' 96 97 def __init__(self, foo=1, bar=2, baz=3): 98 self.foo = foo 99 self.bar = bar 100 self.baz = baz 101 102 def __getstate__(self): 103 return {1: self.foo, 2: self.bar, 3: self.baz} 104 105 def __setstate__(self, state): 106 self.foo = state[1] 107 self.bar = state[2] 108 self.baz = state[3] 109 110 def __eq__(self, other): 111 if isinstance(other, YAMLObject2): 112 return self.__class__, self.__dict__ == other.__class__, other.__dict__ 113 else: 114 return False 115 116 class TestConstructorTypes(test_appliance.TestAppliance): 71 117 72 118 def _testTypes(self, test_name, data_filename, code_filename): … … 78 124 data1 = data1[0] 79 125 data2 = eval(file(code_filename, 'rb').read()) 126 self.failUnlessEqual(type(data1), type(data2)) 80 127 try: 81 128 self.failUnlessEqual(data1, data2) … … 100 147 raise 101 148 102 Test Types.add_tests('testTypes', '.data', '.code')149 TestConstructorTypes.add_tests('testTypes', '.data', '.code') 103 150
Note: See TracChangeset
for help on using the changeset viewer.
