Pablo Santiago Blum de Aguiar | 2c225b2 | 2015-04-04 13:25:24 -0300 | [diff] [blame^] | 1 | |
| 2 | import yaml |
| 3 | import codecs, io, tempfile, os, os.path |
| 4 | |
| 5 | def test_unicode_input(unicode_filename, verbose=False): |
| 6 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
| 7 | value = ' '.join(data.split()) |
| 8 | output = yaml.load(data) |
| 9 | assert output == value, (output, value) |
| 10 | output = yaml.load(io.StringIO(data)) |
| 11 | assert output == value, (output, value) |
| 12 | for input in [data.encode('utf-8'), |
| 13 | codecs.BOM_UTF8+data.encode('utf-8'), |
| 14 | codecs.BOM_UTF16_BE+data.encode('utf-16-be'), |
| 15 | codecs.BOM_UTF16_LE+data.encode('utf-16-le')]: |
| 16 | if verbose: |
| 17 | print("INPUT:", repr(input[:10]), "...") |
| 18 | output = yaml.load(input) |
| 19 | assert output == value, (output, value) |
| 20 | output = yaml.load(io.BytesIO(input)) |
| 21 | assert output == value, (output, value) |
| 22 | |
| 23 | test_unicode_input.unittest = ['.unicode'] |
| 24 | |
| 25 | def test_unicode_input_errors(unicode_filename, verbose=False): |
| 26 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
| 27 | for input in [data.encode('latin1', 'ignore'), |
| 28 | data.encode('utf-16-be'), data.encode('utf-16-le'), |
| 29 | codecs.BOM_UTF8+data.encode('utf-16-be'), |
| 30 | codecs.BOM_UTF16_BE+data.encode('utf-16-le'), |
| 31 | codecs.BOM_UTF16_LE+data.encode('utf-8')+b'!']: |
| 32 | try: |
| 33 | yaml.load(input) |
| 34 | except yaml.YAMLError as exc: |
| 35 | if verbose: |
| 36 | print(exc) |
| 37 | else: |
| 38 | raise AssertionError("expected an exception") |
| 39 | try: |
| 40 | yaml.load(io.BytesIO(input)) |
| 41 | except yaml.YAMLError as exc: |
| 42 | if verbose: |
| 43 | print(exc) |
| 44 | else: |
| 45 | raise AssertionError("expected an exception") |
| 46 | |
| 47 | test_unicode_input_errors.unittest = ['.unicode'] |
| 48 | |
| 49 | def test_unicode_output(unicode_filename, verbose=False): |
| 50 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
| 51 | value = ' '.join(data.split()) |
| 52 | for allow_unicode in [False, True]: |
| 53 | data1 = yaml.dump(value, allow_unicode=allow_unicode) |
| 54 | for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: |
| 55 | stream = io.StringIO() |
| 56 | yaml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode) |
| 57 | data2 = stream.getvalue() |
| 58 | data3 = yaml.dump(value, encoding=encoding, allow_unicode=allow_unicode) |
| 59 | if encoding is not None: |
| 60 | assert isinstance(data3, bytes) |
| 61 | data3 = data3.decode(encoding) |
| 62 | stream = io.BytesIO() |
| 63 | if encoding is None: |
| 64 | try: |
| 65 | yaml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode) |
| 66 | except TypeError as exc: |
| 67 | if verbose: |
| 68 | print(exc) |
| 69 | data4 = None |
| 70 | else: |
| 71 | raise AssertionError("expected an exception") |
| 72 | else: |
| 73 | yaml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode) |
| 74 | data4 = stream.getvalue() |
| 75 | if verbose: |
| 76 | print("BYTES:", data4[:50]) |
| 77 | data4 = data4.decode(encoding) |
| 78 | for copy in [data1, data2, data3, data4]: |
| 79 | if copy is None: |
| 80 | continue |
| 81 | assert isinstance(copy, str) |
| 82 | if allow_unicode: |
| 83 | try: |
| 84 | copy[4:].encode('ascii') |
| 85 | except UnicodeEncodeError as exc: |
| 86 | if verbose: |
| 87 | print(exc) |
| 88 | else: |
| 89 | raise AssertionError("expected an exception") |
| 90 | else: |
| 91 | copy[4:].encode('ascii') |
| 92 | assert isinstance(data1, str), (type(data1), encoding) |
| 93 | assert isinstance(data2, str), (type(data2), encoding) |
| 94 | |
| 95 | test_unicode_output.unittest = ['.unicode'] |
| 96 | |
| 97 | def test_file_output(unicode_filename, verbose=False): |
| 98 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
| 99 | handle, filename = tempfile.mkstemp() |
| 100 | os.close(handle) |
| 101 | try: |
| 102 | stream = io.StringIO() |
| 103 | yaml.dump(data, stream, allow_unicode=True) |
| 104 | data1 = stream.getvalue() |
| 105 | stream = io.BytesIO() |
| 106 | yaml.dump(data, stream, encoding='utf-16-le', allow_unicode=True) |
| 107 | data2 = stream.getvalue().decode('utf-16-le')[1:] |
| 108 | stream = open(filename, 'w', encoding='utf-16-le') |
| 109 | yaml.dump(data, stream, allow_unicode=True) |
| 110 | stream.close() |
| 111 | data3 = open(filename, 'r', encoding='utf-16-le').read() |
| 112 | stream = open(filename, 'wb') |
| 113 | yaml.dump(data, stream, encoding='utf-8', allow_unicode=True) |
| 114 | stream.close() |
| 115 | data4 = open(filename, 'r', encoding='utf-8').read() |
| 116 | assert data1 == data2, (data1, data2) |
| 117 | assert data1 == data3, (data1, data3) |
| 118 | assert data1 == data4, (data1, data4) |
| 119 | finally: |
| 120 | if os.path.exists(filename): |
| 121 | os.unlink(filename) |
| 122 | |
| 123 | test_file_output.unittest = ['.unicode'] |
| 124 | |
| 125 | def test_unicode_transfer(unicode_filename, verbose=False): |
| 126 | data = open(unicode_filename, 'rb').read().decode('utf-8') |
| 127 | for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']: |
| 128 | input = data |
| 129 | if encoding is not None: |
| 130 | input = ('\ufeff'+input).encode(encoding) |
| 131 | output1 = yaml.emit(yaml.parse(input), allow_unicode=True) |
| 132 | if encoding is None: |
| 133 | stream = io.StringIO() |
| 134 | else: |
| 135 | stream = io.BytesIO() |
| 136 | yaml.emit(yaml.parse(input), stream, allow_unicode=True) |
| 137 | output2 = stream.getvalue() |
| 138 | assert isinstance(output1, str), (type(output1), encoding) |
| 139 | if encoding is None: |
| 140 | assert isinstance(output2, str), (type(output1), encoding) |
| 141 | else: |
| 142 | assert isinstance(output2, bytes), (type(output1), encoding) |
| 143 | output2.decode(encoding) |
| 144 | |
| 145 | test_unicode_transfer.unittest = ['.unicode'] |
| 146 | |
| 147 | if __name__ == '__main__': |
| 148 | import test_appliance |
| 149 | test_appliance.run(globals()) |
| 150 | |