From 18687a8c291c5179b32efa4811aff08559920dc4 Mon Sep 17 00:00:00 2001 From: xi Date: Tue, 18 Apr 2006 19:33:30 +0000 Subject: [PATCH] Add more unit tests. git-svn-id: http://svn.pyyaml.org/pyyaml/trunk@144 18f92427-320e-0410-9341-c67f048884a3 --- tests/data/construct-python-bool.code | 1 + tests/data/construct-python-bool.data | 1 + tests/data/construct-python-complex.code | 1 + tests/data/construct-python-complex.data | 8 ++ tests/data/construct-python-float.code | 1 + tests/data/construct-python-float.data | 1 + tests/data/construct-python-int.code | 1 + tests/data/construct-python-int.data | 1 + tests/data/construct-python-long-short.code | 1 + tests/data/construct-python-long-short.data | 1 + tests/data/construct-python-name-module.code | 1 + tests/data/construct-python-name-module.data | 5 + tests/data/construct-python-none.code | 1 + tests/data/construct-python-none.data | 1 + tests/data/construct-python-str-ascii.code | 1 + tests/data/construct-python-str-ascii.data | 1 + tests/data/construct-python-str-utf8.code | 1 + tests/data/construct-python-str-utf8.data | 1 + tests/data/construct-python-tuple-list-dict.code | 6 + tests/data/construct-python-tuple-list-dict.data | 8 ++ tests/data/construct-python-unicode-ascii.code | 1 + tests/data/construct-python-unicode-ascii.data | 1 + tests/data/construct-python-unicode-utf8.code | 1 + tests/data/construct-python-unicode-utf8.data | 1 + tests/data/construct-str-ascii.code | 1 + tests/data/construct-str-ascii.data | 1 + tests/data/construct-str-utf8.code | 1 + tests/data/construct-str-utf8.data | 1 + tests/test_constructor.py | 55 +++++++- tests/test_representer.py | 158 ++++++----------------- 30 files changed, 140 insertions(+), 124 deletions(-) create mode 100644 tests/data/construct-python-bool.code create mode 100644 tests/data/construct-python-bool.data create mode 100644 tests/data/construct-python-complex.code create mode 100644 tests/data/construct-python-complex.data create mode 100644 tests/data/construct-python-float.code create mode 100644 tests/data/construct-python-float.data create mode 100644 tests/data/construct-python-int.code create mode 100644 tests/data/construct-python-int.data create mode 100644 tests/data/construct-python-long-short.code create mode 100644 tests/data/construct-python-long-short.data create mode 100644 tests/data/construct-python-name-module.code create mode 100644 tests/data/construct-python-name-module.data create mode 100644 tests/data/construct-python-none.code create mode 100644 tests/data/construct-python-none.data create mode 100644 tests/data/construct-python-str-ascii.code create mode 100644 tests/data/construct-python-str-ascii.data create mode 100644 tests/data/construct-python-str-utf8.code create mode 100644 tests/data/construct-python-str-utf8.data create mode 100644 tests/data/construct-python-tuple-list-dict.code create mode 100644 tests/data/construct-python-tuple-list-dict.data create mode 100644 tests/data/construct-python-unicode-ascii.code create mode 100644 tests/data/construct-python-unicode-ascii.data create mode 100644 tests/data/construct-python-unicode-utf8.code create mode 100644 tests/data/construct-python-unicode-utf8.data create mode 100644 tests/data/construct-str-ascii.code create mode 100644 tests/data/construct-str-ascii.data create mode 100644 tests/data/construct-str-utf8.code create mode 100644 tests/data/construct-str-utf8.data rewrite tests/test_representer.py (71%) diff --git a/tests/data/construct-python-bool.code b/tests/data/construct-python-bool.code new file mode 100644 index 0000000..170da01 --- /dev/null +++ b/tests/data/construct-python-bool.code @@ -0,0 +1 @@ +[ True, False ] diff --git a/tests/data/construct-python-bool.data b/tests/data/construct-python-bool.data new file mode 100644 index 0000000..0068869 --- /dev/null +++ b/tests/data/construct-python-bool.data @@ -0,0 +1 @@ +[ !!python/bool True, !!python/bool False ] diff --git a/tests/data/construct-python-complex.code b/tests/data/construct-python-complex.code new file mode 100644 index 0000000..e582dff --- /dev/null +++ b/tests/data/construct-python-complex.code @@ -0,0 +1 @@ +[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] diff --git a/tests/data/construct-python-complex.data b/tests/data/construct-python-complex.data new file mode 100644 index 0000000..17ebad4 --- /dev/null +++ b/tests/data/construct-python-complex.data @@ -0,0 +1,8 @@ +- !!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 diff --git a/tests/data/construct-python-float.code b/tests/data/construct-python-float.code new file mode 100644 index 0000000..d5910a0 --- /dev/null +++ b/tests/data/construct-python-float.code @@ -0,0 +1 @@ +123.456 diff --git a/tests/data/construct-python-float.data b/tests/data/construct-python-float.data new file mode 100644 index 0000000..b460eb8 --- /dev/null +++ b/tests/data/construct-python-float.data @@ -0,0 +1 @@ +!!python/float 123.456 diff --git a/tests/data/construct-python-int.code b/tests/data/construct-python-int.code new file mode 100644 index 0000000..190a180 --- /dev/null +++ b/tests/data/construct-python-int.code @@ -0,0 +1 @@ +123 diff --git a/tests/data/construct-python-int.data b/tests/data/construct-python-int.data new file mode 100644 index 0000000..741d669 --- /dev/null +++ b/tests/data/construct-python-int.data @@ -0,0 +1 @@ +!!python/int 123 diff --git a/tests/data/construct-python-long-short.code b/tests/data/construct-python-long-short.code new file mode 100644 index 0000000..fafc3f1 --- /dev/null +++ b/tests/data/construct-python-long-short.code @@ -0,0 +1 @@ +123L diff --git a/tests/data/construct-python-long-short.data b/tests/data/construct-python-long-short.data new file mode 100644 index 0000000..4bd5dc2 --- /dev/null +++ b/tests/data/construct-python-long-short.data @@ -0,0 +1 @@ +!!python/long 123 diff --git a/tests/data/construct-python-name-module.code b/tests/data/construct-python-name-module.code new file mode 100644 index 0000000..fa17f15 --- /dev/null +++ b/tests/data/construct-python-name-module.code @@ -0,0 +1 @@ +[file, Loader, dump, abs, xml.parsers] diff --git a/tests/data/construct-python-name-module.data b/tests/data/construct-python-name-module.data new file mode 100644 index 0000000..709dcda --- /dev/null +++ b/tests/data/construct-python-name-module.data @@ -0,0 +1,5 @@ +- !!python/name:file +- !!python/name:yaml.Loader +- !!python/name:yaml.dump +- !!python/name:abs +- !!python/module:xml.parsers diff --git a/tests/data/construct-python-none.code b/tests/data/construct-python-none.code new file mode 100644 index 0000000..b0047fa --- /dev/null +++ b/tests/data/construct-python-none.code @@ -0,0 +1 @@ +None diff --git a/tests/data/construct-python-none.data b/tests/data/construct-python-none.data new file mode 100644 index 0000000..7907ec3 --- /dev/null +++ b/tests/data/construct-python-none.data @@ -0,0 +1 @@ +!!python/none diff --git a/tests/data/construct-python-str-ascii.code b/tests/data/construct-python-str-ascii.code new file mode 100644 index 0000000..d9d62f6 --- /dev/null +++ b/tests/data/construct-python-str-ascii.code @@ -0,0 +1 @@ +"ascii string" diff --git a/tests/data/construct-python-str-ascii.data b/tests/data/construct-python-str-ascii.data new file mode 100644 index 0000000..a83349e --- /dev/null +++ b/tests/data/construct-python-str-ascii.data @@ -0,0 +1 @@ +--- !!python/str "ascii string" diff --git a/tests/data/construct-python-str-utf8.code b/tests/data/construct-python-str-utf8.code new file mode 100644 index 0000000..47b28ab --- /dev/null +++ b/tests/data/construct-python-str-utf8.code @@ -0,0 +1 @@ +u'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430'.encode('utf-8') diff --git a/tests/data/construct-python-str-utf8.data b/tests/data/construct-python-str-utf8.data new file mode 100644 index 0000000..9ef2c72 --- /dev/null +++ b/tests/data/construct-python-str-utf8.data @@ -0,0 +1 @@ +--- !!python/str "Это уникодная строка" diff --git a/tests/data/construct-python-tuple-list-dict.code b/tests/data/construct-python-tuple-list-dict.code new file mode 100644 index 0000000..20ced98 --- /dev/null +++ b/tests/data/construct-python-tuple-list-dict.code @@ -0,0 +1,6 @@ +[ + [1, 2, 3, 4], + (1, 2, 3, 4), + {1: 2, 3: 4}, + {(0,0): 0, (0,1): 1, (1,0): 1, (1,1): 0}, +] diff --git a/tests/data/construct-python-tuple-list-dict.data b/tests/data/construct-python-tuple-list-dict.data new file mode 100644 index 0000000..c56159b --- /dev/null +++ b/tests/data/construct-python-tuple-list-dict.data @@ -0,0 +1,8 @@ +- !!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 diff --git a/tests/data/construct-python-unicode-ascii.code b/tests/data/construct-python-unicode-ascii.code new file mode 100644 index 0000000..d4cd82c --- /dev/null +++ b/tests/data/construct-python-unicode-ascii.code @@ -0,0 +1 @@ +u"ascii string" diff --git a/tests/data/construct-python-unicode-ascii.data b/tests/data/construct-python-unicode-ascii.data new file mode 100644 index 0000000..3a0647b --- /dev/null +++ b/tests/data/construct-python-unicode-ascii.data @@ -0,0 +1 @@ +--- !!python/unicode "ascii string" diff --git a/tests/data/construct-python-unicode-utf8.code b/tests/data/construct-python-unicode-utf8.code new file mode 100644 index 0000000..2793ac7 --- /dev/null +++ b/tests/data/construct-python-unicode-utf8.code @@ -0,0 +1 @@ +u'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430' diff --git a/tests/data/construct-python-unicode-utf8.data b/tests/data/construct-python-unicode-utf8.data new file mode 100644 index 0000000..5a980ea --- /dev/null +++ b/tests/data/construct-python-unicode-utf8.data @@ -0,0 +1 @@ +--- !!python/unicode "Это уникодная строка" diff --git a/tests/data/construct-str-ascii.code b/tests/data/construct-str-ascii.code new file mode 100644 index 0000000..d9d62f6 --- /dev/null +++ b/tests/data/construct-str-ascii.code @@ -0,0 +1 @@ +"ascii string" diff --git a/tests/data/construct-str-ascii.data b/tests/data/construct-str-ascii.data new file mode 100644 index 0000000..0d93013 --- /dev/null +++ b/tests/data/construct-str-ascii.data @@ -0,0 +1 @@ +--- !!str "ascii string" diff --git a/tests/data/construct-str-utf8.code b/tests/data/construct-str-utf8.code new file mode 100644 index 0000000..2793ac7 --- /dev/null +++ b/tests/data/construct-str-utf8.code @@ -0,0 +1 @@ +u'\u042d\u0442\u043e \u0443\u043d\u0438\u043a\u043e\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430' diff --git a/tests/data/construct-str-utf8.data b/tests/data/construct-str-utf8.data new file mode 100644 index 0000000..e355f18 --- /dev/null +++ b/tests/data/construct-str-utf8.data @@ -0,0 +1 @@ +--- !!str "Это уникодная строка" diff --git a/tests/test_constructor.py b/tests/test_constructor.py index 1c666f1..16579c0 100644 --- a/tests/test_constructor.py +++ b/tests/test_constructor.py @@ -11,8 +11,12 @@ except NameError: from yaml import * +import xml.parsers + class MyLoader(Loader): pass +class MyDumper(Dumper): + pass class MyTestClass1: @@ -22,17 +26,24 @@ 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): @@ -40,6 +51,10 @@ class MyTestClass2(MyTestClass1, YAMLObject): 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" @@ -53,8 +68,14 @@ class MyTestClass3(MyTestClass2): 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): @@ -67,7 +88,32 @@ class YAMLObject1(YAMLObject): 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 @@ -77,6 +123,7 @@ class TestTypes(test_appliance.TestAppliance): 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: @@ -99,5 +146,5 @@ class TestTypes(test_appliance.TestAppliance): print "NATIVES2:", data2 raise -TestTypes.add_tests('testTypes', '.data', '.code') +TestConstructorTypes.add_tests('testTypes', '.data', '.code') diff --git a/tests/test_representer.py b/tests/test_representer.py dissimilarity index 71% index 82ad46e..343d7f5 100644 --- a/tests/test_representer.py +++ b/tests/test_representer.py @@ -1,120 +1,38 @@ - -import test_appliance - -try: - import datetime -except ImportError: - pass -try: - set -except NameError: - from sets import Set as set - -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): - - def _testTypes(self, test_name, data_filename, code_filename): - data1 = eval(file(code_filename, 'rb').read()) - data2 = None - output = None - try: - output = dump(data1, Dumper=MyDumper) - data2 = load(output, Loader=MyLoader) - try: - self.failUnlessEqual(data1, data2) - except AssertionError: - if isinstance(data1, dict): - data1 = data1.items() - data1.sort() - data1 = repr(data1) - data2 = data2.items() - data2.sort() - data2 = repr(data2) - if data1 != data2: - raise - except: - print - print "OUTPUT:" - print output - print "NATIVES1:", data1 - print "NATIVES2:", data2 - raise - -TestTypeRepresenter.add_tests('testTypes', '.data', '.code') - + +import test_appliance +from test_constructor import * + +from yaml import * + +class TestRepresenterTypes(test_appliance.TestAppliance): + + def _testTypes(self, test_name, data_filename, code_filename): + data1 = eval(file(code_filename, 'rb').read()) + data2 = None + output = None + try: + output = dump(data1, Dumper=MyDumper) + data2 = load(output, Loader=MyLoader) + self.failUnlessEqual(type(data1), type(data2)) + try: + self.failUnlessEqual(data1, data2) + except AssertionError: + if isinstance(data1, dict): + data1 = data1.items() + data1.sort() + data1 = repr(data1) + data2 = data2.items() + data2.sort() + data2 = repr(data2) + if data1 != data2: + raise + except: + print + print "OUTPUT:" + print output + print "NATIVES1:", data1 + print "NATIVES2:", data2 + raise + +TestRepresenterTypes.add_tests('testTypes', '.data', '.code') + -- 2.11.4.GIT