def test_file_output(unicode_filename, verbose=False): data = open(unicode_filename, 'rb').read().decode('utf-8') handle, filename = tempfile.mkstemp() os.close(handle) try: stream = StringIO.StringIO() hughml.dump(data, stream, allow_unicode=True) data1 = stream.getvalue() stream = open(filename, 'wb') hughml.dump(data, stream, allow_unicode=True) stream.close() data2 = open(filename, 'rb').read() stream = open(filename, 'wb') hughml.dump(data, stream, encoding='utf-16-le', allow_unicode=True) stream.close() data3 = open(filename, 'rb').read().decode('utf-16-le')[1:].encode('utf-8') stream = _unicode_open(open(filename, 'wb'), 'utf-8') hughml.dump(data, stream, allow_unicode=True) stream.close() data4 = open(filename, 'rb').read() assert data1 == data2, (data1, data2) assert data1 == data3, (data1, data3) assert data1 == data4, (data1, data4) finally: if os.path.exists(filename): os.unlink(filename)
def test_representer_types(code_filename, verbose=False): test_constructor._make_objects() for allow_unicode in [False, True]: for encoding in ['utf-8', 'utf-16-be', 'utf-16-le']: native1 = test_constructor._load_code(open(code_filename, 'rb').read()) native2 = None try: output = hughml.dump(native1, Dumper=test_constructor.MyDumper, allow_unicode=allow_unicode, encoding=encoding) native2 = hughml.load(output, Loader=test_constructor.MyLoader) try: if native1 == native2: continue except TypeError: pass value1 = test_constructor._serialize_value(native1) value2 = test_constructor._serialize_value(native2) if verbose: print("SERIALIZED NATIVE1:") print(value1) print("SERIALIZED NATIVE2:") print(value2) assert value1 == value2, (native1, native2) finally: if verbose: print("NATIVE1:") pprint.pprint(native1) print("NATIVE2:") pprint.pprint(native2) print("OUTPUT:") print(output)
def test_sort_keys(input_filename, sorted_filename, verbose=False): input = open(input_filename, 'rb').read().decode('utf-8') sorted = open(sorted_filename, 'rb').read().decode('utf-8') data = hughml.load(input, Loader=hughml.FullLoader) dump_sorted = hughml.dump(data, default_flow_style=False, sort_keys=True) dump_unsorted = hughml.dump(data, default_flow_style=False, sort_keys=False) dump_unsorted = hughml.dump(data, default_flow_style=False, sort_keys=False, Dumper=hughml.SafeDumper) if verbose: print("INPUT:") print(input) print("DATA:") print(data) assert dump_sorted == sorted
def test_recursive(recursive_filename, verbose=False): exec open(recursive_filename, 'rb').read() value1 = value output1 = None value2 = None output2 = None try: output1 = hughml.dump(value1) value2 = hughml.load(output1, hughml.FullLoader) output2 = hughml.dump(value2) assert output1 == output2, (output1, output2) finally: if verbose: #print "VALUE1:", value1 #print "VALUE2:", value2 print "OUTPUT1:" print output1 print "OUTPUT2:" print output2
def test_recursive(recursive_filename, verbose=False): context = globals().copy() exec(open(recursive_filename, 'rb').read(), context) value1 = context['value'] output1 = None value2 = None output2 = None try: output1 = hughml.dump(value1) value2 = hughml.full_load(output1) output2 = hughml.dump(value2) assert output1 == output2, (output1, output2) finally: if verbose: print("VALUE1:", value1) print("VALUE2:", value2) print("OUTPUT1:") print(output1) print("OUTPUT2:") print(output2)
def test_unicode_output(unicode_filename, verbose=False): data = open(unicode_filename, 'rb').read().decode('utf-8') value = ' '.join(data.split()) for allow_unicode in [False, True]: data1 = hughml.dump(value, allow_unicode=allow_unicode) for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: stream = StringIO.StringIO() hughml.dump(value, _unicode_open(stream, 'utf-8'), encoding=encoding, allow_unicode=allow_unicode) data2 = stream.getvalue() data3 = hughml.dump(value, encoding=encoding, allow_unicode=allow_unicode) stream = StringIO.StringIO() hughml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode) data4 = stream.getvalue() assert isinstance(data1, str), (type(data1), encoding) data1.decode('utf-8') assert isinstance(data2, str), (type(data2), encoding) data2.decode('utf-8') if encoding is None: assert isinstance(data3, unicode), (type(data3), encoding) assert isinstance(data4, unicode), (type(data4), encoding) else: assert isinstance(data3, str), (type(data3), encoding) data3.decode(encoding) assert isinstance(data4, str), (type(data4), encoding) data4.decode(encoding)
def test_unicode_output(unicode_filename, verbose=False): data = open(unicode_filename, 'rb').read().decode('utf-8') value = ' '.join(data.split()) for allow_unicode in [False, True]: data1 = hughml.dump(value, allow_unicode=allow_unicode) for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: stream = io.StringIO() hughml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode) data2 = stream.getvalue() data3 = hughml.dump(value, encoding=encoding, allow_unicode=allow_unicode) if encoding is not None: assert isinstance(data3, bytes) data3 = data3.decode(encoding) stream = io.BytesIO() if encoding is None: try: hughml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode) except TypeError as exc: if verbose: print(exc) data4 = None else: raise AssertionError("expected an exception") else: hughml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode) data4 = stream.getvalue() if verbose: print("BYTES:", data4[:50]) data4 = data4.decode(encoding) assert isinstance(data1, str), (type(data1), encoding) assert isinstance(data2, str), (type(data2), encoding)