Ignore:
Timestamp:
04/18/06 15:33:30 (8 years ago)
Author:
xi
Message:

Add more unit tests.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pyyaml/trunk/tests/test_representer.py

    r136 r144  
    11 
    22import 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 
     3from test_constructor import * 
    124 
    135from yaml import * 
    146 
    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): 
     7class TestRepresenterTypes(test_appliance.TestAppliance): 
    918 
    929    def _testTypes(self, test_name, data_filename, code_filename): 
     
    9714            output = dump(data1, Dumper=MyDumper) 
    9815            data2 = load(output, Loader=MyLoader) 
     16            self.failUnlessEqual(type(data1), type(data2)) 
    9917            try: 
    10018                self.failUnlessEqual(data1, data2) 
     
    11735            raise 
    11836 
    119 TestTypeRepresenter.add_tests('testTypes', '.data', '.code') 
     37TestRepresenterTypes.add_tests('testTypes', '.data', '.code') 
    12038 
Note: See TracChangeset for help on using the changeset viewer.