Changeset 322 for pyyaml/trunk/tests/test_representer.py
- Timestamp:
- 12/28/08 15:16:50 (4 years ago)
- File:
-
- 1 edited
-
pyyaml/trunk/tests/test_representer.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
pyyaml/trunk/tests/test_representer.py
r234 r322 1 1 2 import test_appliance 3 from test_constructor import * 2 import yaml 3 import test_constructor 4 import pprint 4 5 5 from yaml import * 6 def test_representer_types(code_filename, verbose=False): 7 test_constructor._make_objects() 8 for allow_unicode in [False, True]: 9 native1 = test_constructor._load_code(open(code_filename, 'rb').read()) 10 native2 = None 11 try: 12 output = yaml.dump(native1, Dumper=test_constructor.MyDumper, 13 allow_unicode=allow_unicode) 14 native2 = yaml.load(output, Loader=test_constructor.MyLoader) 15 try: 16 if native1 == native2: 17 continue 18 except TypeError: 19 pass 20 value1 = test_constructor._serialize_value(native1) 21 value2 = test_constructor._serialize_value(native2) 22 if verbose: 23 print "SERIALIZED NATIVE1:" 24 print value1 25 print "SERIALIZED NATIVE2:" 26 print value2 27 assert value1 == value2, (native1, native2) 28 finally: 29 if verbose: 30 print "NATIVE1:" 31 pprint.pprint(native1) 32 print "NATIVE2:" 33 pprint.pprint(native2) 34 print "OUTPUT:" 35 print output 6 36 7 class TestRepresenterTypes(test_appliance.TestAppliance): 37 test_representer_types.unittest = ['.code'] 8 38 9 def _testTypesUnicode(self, test_name, data_filename, code_filename): 10 return self._testTypes(test_name, data_filename, code_filename, allow_unicode=True) 39 if __name__ == '__main__': 40 import test_appliance 41 test_appliance.run(globals()) 11 42 12 def _testTypes(self, test_name, data_filename, code_filename, allow_unicode=False):13 data1 = eval(file(code_filename, 'rb').read())14 data2 = None15 output = None16 try:17 output = dump(data1, Dumper=MyDumper, allow_unicode=allow_unicode)18 data2 = load(output, Loader=MyLoader)19 self.failUnlessEqual(type(data1), type(data2))20 try:21 self.failUnlessEqual(data1, data2)22 except (AssertionError, TypeError):23 if isinstance(data1, dict):24 data1 = [(repr(key), value) for key, value in data1.items()]25 data1.sort()26 data1 = repr(data1)27 data2 = [(repr(key), value) for key, value in data2.items()]28 data2.sort()29 data2 = repr(data2)30 if data1 != data2:31 raise32 elif isinstance(data1, list):33 self.failUnlessEqual(type(data1), type(data2))34 self.failUnlessEqual(len(data1), len(data2))35 for item1, item2 in zip(data1, data2):36 if (item1 != item1 or (item1 == 0.0 and item1 == 1.0)) and \37 (item2 != item2 or (item2 == 0.0 and item2 == 1.0)):38 continue39 if isinstance(item1, datetime.datetime) \40 and isinstance(item2, datetime.datetime):41 self.failUnlessEqual(item1.microsecond,42 item2.microsecond)43 if isinstance(item1, datetime.datetime):44 item1 = item1.utctimetuple()45 if isinstance(item2, datetime.datetime):46 item2 = item2.utctimetuple()47 self.failUnlessEqual(item1, item2)48 else:49 raise50 except:51 print52 print "OUTPUT:"53 print output54 print "NATIVES1:", data155 print "NATIVES2:", data256 raise57 58 TestRepresenterTypes.add_tests('testTypes', '.data', '.code')59 TestRepresenterTypes.add_tests('testTypesUnicode', '.data', '.code')60
Note: See TracChangeset
for help on using the changeset viewer.
