| 1 | |
|---|
| 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 |
|---|
| 11 | |
|---|
| 12 | from yaml import * |
|---|
| 13 | |
|---|
| 14 | class MyLoader(Loader): |
|---|
| 15 | pass |
|---|
| 16 | |
|---|
| 17 | class MyTestClass1: |
|---|
| 18 | |
|---|
| 19 | def __init__(self, x, y=0, z=0): |
|---|
| 20 | self.x = x |
|---|
| 21 | self.y = y |
|---|
| 22 | self.z = z |
|---|
| 23 | |
|---|
| 24 | def __eq__(self, other): |
|---|
| 25 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
|---|
| 26 | |
|---|
| 27 | def construct1(constructor, node): |
|---|
| 28 | mapping = constructor.construct_mapping(node) |
|---|
| 29 | return MyTestClass1(**mapping) |
|---|
| 30 | |
|---|
| 31 | MyLoader.add_constructor("!tag1", construct1) |
|---|
| 32 | |
|---|
| 33 | class MyTestClass2(MyTestClass1, YAMLObject): |
|---|
| 34 | |
|---|
| 35 | yaml_loader = MyLoader |
|---|
| 36 | yaml_tag = "!tag2" |
|---|
| 37 | |
|---|
| 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) |
|---|
| 42 | |
|---|
| 43 | class MyTestClass3(MyTestClass2): |
|---|
| 44 | |
|---|
| 45 | yaml_tag = "!tag3" |
|---|
| 46 | |
|---|
| 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) |
|---|
| 55 | |
|---|
| 56 | class YAMLObject1(YAMLObject): |
|---|
| 57 | yaml_loader = MyLoader |
|---|
| 58 | yaml_tag = '!foo' |
|---|
| 59 | |
|---|
| 60 | def __init__(self, my_parameter=None, my_another_parameter=None): |
|---|
| 61 | self.my_parameter = my_parameter |
|---|
| 62 | self.my_another_parameter = my_another_parameter |
|---|
| 63 | |
|---|
| 64 | def __eq__(self, other): |
|---|
| 65 | if isinstance(other, YAMLObject1): |
|---|
| 66 | return self.__class__, self.__dict__ == other.__class__, other.__dict__ |
|---|
| 67 | else: |
|---|
| 68 | return False |
|---|
| 69 | |
|---|
| 70 | class TestTypes(test_appliance.TestAppliance): |
|---|
| 71 | |
|---|
| 72 | def _testTypes(self, test_name, data_filename, code_filename): |
|---|
| 73 | data1 = None |
|---|
| 74 | data2 = None |
|---|
| 75 | try: |
|---|
| 76 | data1 = list(load_all(file(data_filename, 'rb'), Loader=MyLoader)) |
|---|
| 77 | if len(data1) == 1: |
|---|
| 78 | data1 = data1[0] |
|---|
| 79 | data2 = eval(file(code_filename, 'rb').read()) |
|---|
| 80 | try: |
|---|
| 81 | self.failUnlessEqual(data1, data2) |
|---|
| 82 | except AssertionError: |
|---|
| 83 | if isinstance(data1, dict): |
|---|
| 84 | data1 = data1.items() |
|---|
| 85 | data1.sort() |
|---|
| 86 | data1 = repr(data1) |
|---|
| 87 | data2 = data2.items() |
|---|
| 88 | data2.sort() |
|---|
| 89 | data2 = repr(data2) |
|---|
| 90 | if data1 != data2: |
|---|
| 91 | raise |
|---|
| 92 | except: |
|---|
| 93 | print |
|---|
| 94 | print "DATA:" |
|---|
| 95 | print file(data_filename, 'rb').read() |
|---|
| 96 | print "CODE:" |
|---|
| 97 | print file(code_filename, 'rb').read() |
|---|
| 98 | print "NATIVES1:", data1 |
|---|
| 99 | print "NATIVES2:", data2 |
|---|
| 100 | raise |
|---|
| 101 | |
|---|
| 102 | TestTypes.add_tests('testTypes', '.data', '.code') |
|---|
| 103 | |
|---|