| [133] | 1 | |
|---|
| 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 |
|---|
| 12 | |
|---|
| 13 | from yaml import * |
|---|
| 14 | |
|---|
| 15 | class MyConstructor(Constructor): |
|---|
| 16 | pass |
|---|
| 17 | class MyRepresenter(Representer): |
|---|
| 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_constructor = MyConstructor |
|---|
| 42 | yaml_tag = "!tag2" |
|---|
| 43 | |
|---|
| 44 | def from_yaml(cls, constructor, node): |
|---|
| 45 | x = constructor.construct_yaml_int(node) |
|---|
| 46 | return cls(x=x) |
|---|
| 47 | from_yaml = classmethod(from_yaml) |
|---|
| 48 | |
|---|
| 49 | def to_yaml(cls, representer, native): |
|---|
| 50 | return representer.represent_scalar(cls.yaml_tag, str(native.x)) |
|---|
| 51 | to_yaml = classmethod(to_yaml) |
|---|
| 52 | |
|---|
| 53 | class MyTestClass3(MyTestClass2): |
|---|
| 54 | |
|---|
| 55 | yaml_tag = "!tag3" |
|---|
| 56 | |
|---|
| 57 | def from_yaml(cls, constructor, node): |
|---|
| 58 | mapping = constructor.construct_mapping(node) |
|---|
| 59 | if '=' in mapping: |
|---|
| 60 | x = mapping['='] |
|---|
| 61 | del mapping['='] |
|---|
| 62 | mapping['x'] = x |
|---|
| 63 | return cls(**mapping) |
|---|
| 64 | from_yaml = classmethod(from_yaml) |
|---|
| 65 | |
|---|
| 66 | def to_yaml(cls, representer, native): |
|---|
| 67 | return representer.represent_mapping(cls.yaml_tag, native.__dict__) |
|---|
| 68 | to_yaml = classmethod(to_yaml) |
|---|
| 69 | |
|---|
| 70 | MyConstructor.add_constructor("!tag1", construct1) |
|---|
| 71 | MyRepresenter.add_representer(MyTestClass1, represent1) |
|---|
| 72 | |
|---|
| 73 | class TestTypeRepresenter(test_appliance.TestAppliance): |
|---|
| 74 | |
|---|
| 75 | def _testTypes(self, test_name, data_filename, code_filename): |
|---|
| 76 | natives1 = eval(file(code_filename, 'rb').read()) |
|---|
| 77 | natives2 = None |
|---|
| 78 | output = None |
|---|
| 79 | try: |
|---|
| 80 | output = dump(natives1, Representer=MyRepresenter) |
|---|
| 81 | natives2 = load(output, Constructor=MyConstructor) |
|---|
| 82 | try: |
|---|
| 83 | self.failUnlessEqual(natives1, natives2) |
|---|
| 84 | except AssertionError: |
|---|
| 85 | if isinstance(natives1, dict): |
|---|
| 86 | natives1 = natives1.items() |
|---|
| 87 | natives1.sort() |
|---|
| 88 | natives1 = repr(natives1) |
|---|
| 89 | natives2 = natives2.items() |
|---|
| 90 | natives2.sort() |
|---|
| 91 | natives2 = repr(natives2) |
|---|
| 92 | if natives1 != natives2: |
|---|
| 93 | raise |
|---|
| 94 | except: |
|---|
| 95 | print |
|---|
| 96 | print "OUTPUT:" |
|---|
| 97 | print output |
|---|
| 98 | print "NATIVES1:", natives1 |
|---|
| 99 | print "NATIVES2:", natives2 |
|---|
| 100 | raise |
|---|
| 101 | |
|---|
| 102 | TestTypeRepresenter.add_tests('testTypes', '.data', '.code') |
|---|
| 103 | |
|---|