Kirill Simonov avatar Kirill Simonov committed 5a069ae

Add more unit tests.

Comments (0)

Files changed (30)

tests/data/construct-python-bool.code

+[ True, False ]

tests/data/construct-python-bool.data

+[ !!python/bool True, !!python/bool False ]

tests/data/construct-python-complex.code

+[0.5+0j, 0.5+0.5j, 0.5j, -0.5+0.5j, -0.5+0j, -0.5-0.5j, -0.5j, 0.5-0.5j]

tests/data/construct-python-complex.data

+- !!python/complex 0.5+0j
+- !!python/complex 0.5+0.5j
+- !!python/complex 0.5j
+- !!python/complex -0.5+0.5j
+- !!python/complex -0.5+0j
+- !!python/complex -0.5-0.5j
+- !!python/complex -0.5j
+- !!python/complex 0.5-0.5j

tests/data/construct-python-float.code

+123.456

tests/data/construct-python-float.data

+!!python/float 123.456

tests/data/construct-python-int.code

+123

tests/data/construct-python-int.data

+!!python/int 123

tests/data/construct-python-long-short.code

+123L

tests/data/construct-python-long-short.data

+!!python/long 123

tests/data/construct-python-name-module.code

+[file, Loader, dump, abs, xml.parsers]

tests/data/construct-python-name-module.data

+- !!python/name:file
+- !!python/name:yaml.Loader
+- !!python/name:yaml.dump
+- !!python/name:abs
+- !!python/module:xml.parsers

tests/data/construct-python-none.code

+None

tests/data/construct-python-none.data

+!!python/none

tests/data/construct-python-str-ascii.code

+"ascii string"

tests/data/construct-python-str-ascii.data

+--- !!python/str "ascii string"

tests/data/construct-python-str-utf8.code

+u'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'.encode('utf-8')

tests/data/construct-python-str-utf8.data

+--- !!python/str "Это уникодная строка"

tests/data/construct-python-tuple-list-dict.code

+[
+    [1, 2, 3, 4],
+    (1, 2, 3, 4),
+    {1: 2, 3: 4},
+    {(0,0): 0, (0,1): 1, (1,0): 1, (1,1): 0},
+]

tests/data/construct-python-tuple-list-dict.data

+- !!python/list [1, 2, 3, 4]
+- !!python/tuple [1, 2, 3, 4]
+- !!python/dict {1: 2, 3: 4}
+- !!python/dict
+    !!python/tuple [0,0]: 0
+    !!python/tuple [0,1]: 1
+    !!python/tuple [1,0]: 1
+    !!python/tuple [1,1]: 0

tests/data/construct-python-unicode-ascii.code

+u"ascii string"

tests/data/construct-python-unicode-ascii.data

+--- !!python/unicode "ascii string"

tests/data/construct-python-unicode-utf8.code

+u'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'

tests/data/construct-python-unicode-utf8.data

+--- !!python/unicode "Это уникодная строка"

tests/data/construct-str-ascii.code

+"ascii string"

tests/data/construct-str-ascii.data

+--- !!str "ascii string"

tests/data/construct-str-utf8.code

+u'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'

tests/data/construct-str-utf8.data

+--- !!str "Это уникодная строка"

tests/test_constructor.py

 
 from yaml import *
 
+import xml.parsers
+
 class MyLoader(Loader):
     pass
+class MyDumper(Dumper):
+    pass
 
 class MyTestClass1:
 
         self.z = z
 
     def __eq__(self, other):
-        return self.__class__, self.__dict__ == other.__class__, other.__dict__
+        if isinstance(other, MyTestClass1):
+            return self.__class__, self.__dict__ == other.__class__, other.__dict__
+        else:
+            return False
 
 def construct1(constructor, node):
     mapping = constructor.construct_mapping(node)
     return MyTestClass1(**mapping)
+def represent1(representer, native):
+    return representer.represent_mapping("!tag1", native.__dict__)
 
-MyLoader.add_constructor("!tag1", construct1)
+add_constructor("!tag1", construct1, Loader=MyLoader)
+add_representer(MyTestClass1, represent1, Dumper=MyDumper)
 
 class MyTestClass2(MyTestClass1, YAMLObject):
 
     yaml_loader = MyLoader
+    yaml_dumper = MyDumper
     yaml_tag = "!tag2"
 
     def from_yaml(cls, constructor, node):
         return cls(x=x)
     from_yaml = classmethod(from_yaml)
 
+    def to_yaml(cls, representer, native):
+        return representer.represent_scalar(cls.yaml_tag, str(native.x))
+    to_yaml = classmethod(to_yaml)
+
 class MyTestClass3(MyTestClass2):
 
     yaml_tag = "!tag3"
         return cls(**mapping)
     from_yaml = classmethod(from_yaml)
 
+    def to_yaml(cls, representer, native):
+        return representer.represent_mapping(cls.yaml_tag, native.__dict__)
+    to_yaml = classmethod(to_yaml)
+
 class YAMLObject1(YAMLObject):
+
     yaml_loader = MyLoader
+    yaml_dumper = MyDumper
     yaml_tag = '!foo'
 
     def __init__(self, my_parameter=None, my_another_parameter=None):
         else:
             return False
 
-class TestTypes(test_appliance.TestAppliance):
+class YAMLObject2(YAMLObject):
+
+    yaml_loader = MyLoader
+    yaml_dumper = MyDumper
+    yaml_tag = '!bar'
+
+    def __init__(self, foo=1, bar=2, baz=3):
+        self.foo = foo
+        self.bar = bar
+        self.baz = baz
+
+    def __getstate__(self):
+        return {1: self.foo, 2: self.bar, 3: self.baz}
+
+    def __setstate__(self, state):
+        self.foo = state[1]
+        self.bar = state[2]
+        self.baz = state[3]
+
+    def __eq__(self, other):
+        if isinstance(other, YAMLObject2):
+            return self.__class__, self.__dict__ == other.__class__, other.__dict__
+        else:
+            return False
+
+class TestConstructorTypes(test_appliance.TestAppliance):
 
     def _testTypes(self, test_name, data_filename, code_filename):
         data1 = None
             if len(data1) == 1:
                 data1 = data1[0]
             data2 = eval(file(code_filename, 'rb').read())
+            self.failUnlessEqual(type(data1), type(data2))
             try:
                 self.failUnlessEqual(data1, data2)
             except AssertionError:
             print "NATIVES2:", data2
             raise
 
-TestTypes.add_tests('testTypes', '.data', '.code')
+TestConstructorTypes.add_tests('testTypes', '.data', '.code')
 

tests/test_representer.py

 
 import test_appliance
-
-try:
-    import datetime
-except ImportError:
-    pass
-try:
-    set
-except NameError:
-    from sets import Set as set
+from test_constructor import *
 
 from yaml import *
 
-class MyLoader(Loader):
-    pass
-class MyDumper(Dumper):
-    pass
-
-class MyTestClass1(object):
-
-    def __init__(self, x, y=0, z=0):
-        self.x = x
-        self.y = y
-        self.z = z
-
-    def __eq__(self, other):
-        if isinstance(other, MyTestClass1):
-            return self.__class__, self.__dict__ == other.__class__, other.__dict__
-        else:
-            return False
-
-def construct1(constructor, node):
-    mapping = constructor.construct_mapping(node)
-    return MyTestClass1(**mapping)
-def represent1(representer, native):
-    return representer.represent_mapping("!tag1", native.__dict__)
-
-class MyTestClass2(MyTestClass1, YAMLObject):
-
-    yaml_loader = MyLoader
-    yaml_dumper = MyDumper
-    yaml_tag = "!tag2"
-
-    def from_yaml(cls, constructor, node):
-        x = constructor.construct_yaml_int(node)
-        return cls(x=x)
-    from_yaml = classmethod(from_yaml)
-
-    def to_yaml(cls, representer, native):
-        return representer.represent_scalar(cls.yaml_tag, str(native.x))
-    to_yaml = classmethod(to_yaml)
-
-class MyTestClass3(MyTestClass2):
-
-    yaml_tag = "!tag3"
-
-    def from_yaml(cls, constructor, node):
-        mapping = constructor.construct_mapping(node)
-        if '=' in mapping:
-            x = mapping['=']
-            del mapping['=']
-            mapping['x'] = x
-        return cls(**mapping)
-    from_yaml = classmethod(from_yaml)
-
-    def to_yaml(cls, representer, native):
-        return representer.represent_mapping(cls.yaml_tag, native.__dict__)
-    to_yaml = classmethod(to_yaml)
-
-MyLoader.add_constructor("!tag1", construct1)
-MyDumper.add_representer(MyTestClass1, represent1)
-
-class YAMLObject1(YAMLObject):
-    yaml_loader = MyLoader
-    yaml_dumper = MyDumper
-    yaml_tag = '!foo'
-    yaml_flow_style = True
-
-    def __init__(self, my_parameter=None, my_another_parameter=None):
-        self.my_parameter = my_parameter
-        self.my_another_parameter = my_another_parameter
-
-    def __eq__(self, other):
-        if isinstance(other, YAMLObject1):
-            return self.__class__, self.__dict__ == other.__class__, other.__dict__
-        else:
-            return False
-
-class TestTypeRepresenter(test_appliance.TestAppliance):
+class TestRepresenterTypes(test_appliance.TestAppliance):
 
     def _testTypes(self, test_name, data_filename, code_filename):
         data1 = eval(file(code_filename, 'rb').read())
         try:
             output = dump(data1, Dumper=MyDumper)
             data2 = load(output, Loader=MyLoader)
+            self.failUnlessEqual(type(data1), type(data2))
             try:
                 self.failUnlessEqual(data1, data2)
             except AssertionError:
             print "NATIVES2:", data2
             raise
 
-TestTypeRepresenter.add_tests('testTypes', '.data', '.code')
+TestRepresenterTypes.add_tests('testTypes', '.data', '.code')
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.