Changeset 144
- Timestamp:
- 04/18/06 15:33:30 (2 years ago)
- Files:
-
- pyyaml/trunk/tests/data/construct-python-bool.code (added)
- pyyaml/trunk/tests/data/construct-python-bool.data (added)
- pyyaml/trunk/tests/data/construct-python-complex.code (added)
- pyyaml/trunk/tests/data/construct-python-complex.data (added)
- pyyaml/trunk/tests/data/construct-python-float.code (added)
- pyyaml/trunk/tests/data/construct-python-float.data (added)
- pyyaml/trunk/tests/data/construct-python-int.code (added)
- pyyaml/trunk/tests/data/construct-python-int.data (added)
- pyyaml/trunk/tests/data/construct-python-long-short.code (added)
- pyyaml/trunk/tests/data/construct-python-long-short.data (added)
- pyyaml/trunk/tests/data/construct-python-name-module.code (added)
- pyyaml/trunk/tests/data/construct-python-name-module.data (added)
- pyyaml/trunk/tests/data/construct-python-none.code (added)
- pyyaml/trunk/tests/data/construct-python-none.data (added)
- pyyaml/trunk/tests/data/construct-python-str-ascii.code (added)
- pyyaml/trunk/tests/data/construct-python-str-ascii.data (added)
- pyyaml/trunk/tests/data/construct-python-str-utf8.code (added)
- pyyaml/trunk/tests/data/construct-python-str-utf8.data (added)
- pyyaml/trunk/tests/data/construct-python-tuple-list-dict.code (added)
- pyyaml/trunk/tests/data/construct-python-tuple-list-dict.data (added)
- pyyaml/trunk/tests/data/construct-python-unicode-ascii.code (added)
- pyyaml/trunk/tests/data/construct-python-unicode-ascii.data (added)
- pyyaml/trunk/tests/data/construct-python-unicode-utf8.code (added)
- pyyaml/trunk/tests/data/construct-python-unicode-utf8.data (added)
- pyyaml/trunk/tests/data/construct-str-ascii.code (added)
- pyyaml/trunk/tests/data/construct-str-ascii.data (added)
- pyyaml/trunk/tests/data/construct-str-utf8.code (added)
- pyyaml/trunk/tests/data/construct-str-utf8.data (added)
- pyyaml/trunk/tests/test_constructor.py (modified) (7 diffs)
- pyyaml/trunk/tests/test_representer.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
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 pyyaml/trunk/tests/test_representer.py
r136 r144 1 1 2 2 import test_appliance 3 4 try: 5 import datetime 6 except ImportError: 7 pass 8 try: 9 set 10 except NameError: 11 from sets import Set as set 3 from test_constructor import * 12 4 13 5 from yaml import * 14 6 15 class MyLoader(Loader): 16 pass 17 class MyDumper(Dumper): 18 pass 19 20 class 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 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__) 38 39 class MyTestClass2(MyTestClass1, YAMLObject): 40 41 yaml_loader = MyLoader 42 yaml_dumper = MyDumper 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 54 class 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 71 MyLoader.add_constructor("!tag1", construct1) 72 MyDumper.add_representer(MyTestClass1, represent1) 73 74 class 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 90 class TestTypeRepresenter(test_appliance.TestAppliance): 7 class TestRepresenterTypes(test_appliance.TestAppliance): 91 8 92 9 def _testTypes(self, test_name, data_filename, code_filename): … … 97 14 output = dump(data1, Dumper=MyDumper) 98 15 data2 = load(output, Loader=MyLoader) 16 self.failUnlessEqual(type(data1), type(data2)) 99 17 try: 100 18 self.failUnlessEqual(data1, data2) … … 117 35 raise 118 36 119 Test TypeRepresenter.add_tests('testTypes', '.data', '.code')37 TestRepresenterTypes.add_tests('testTypes', '.data', '.code') 120 38
