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