summaryrefslogtreecommitdiff
path: root/tests/lib
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/test_input_output.py116
-rw-r--r--tests/lib/test_yaml.py1
-rw-r--r--tests/lib/test_yaml_ext.py4
3 files changed, 119 insertions, 2 deletions
diff --git a/tests/lib/test_input_output.py b/tests/lib/test_input_output.py
new file mode 100644
index 0000000..91e45df
--- /dev/null
+++ b/tests/lib/test_input_output.py
@@ -0,0 +1,116 @@
+
+import yaml
+import codecs, StringIO
+
+def _unicode_open(file, encoding, errors='strict'):
+ info = codecs.lookup(encoding)
+ srw = codecs.StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
+ srw.encoding = encoding
+ return srw
+
+def test_unicode_input(unicode_filename, verbose=False):
+ data = open(unicode_filename, 'rb').read().decode('utf-8')
+ value = ' '.join(data.split())
+ output = yaml.load(_unicode_open(StringIO.StringIO(data.encode('utf-8')), 'utf-8'))
+ assert output == value, (output, value)
+ for input in [data, data.encode('utf-8'),
+ codecs.BOM_UTF8+data.encode('utf-8'),
+ codecs.BOM_UTF16_BE+data.encode('utf-16-be'),
+ codecs.BOM_UTF16_LE+data.encode('utf-16-le')]:
+ if verbose:
+ print "INPUT:", repr(input[:10]), "..."
+ output = yaml.load(input)
+ assert output == value, (output, value)
+ output = yaml.load(StringIO.StringIO(input))
+ assert output == value, (output, value)
+
+test_unicode_input.unittest = ['.unicode']
+
+def test_unicode_input_errors(unicode_filename, verbose=False):
+ data = open(unicode_filename, 'rb').read().decode('utf-8')
+ for input in [data.encode('latin1', 'ignore'),
+ data.encode('utf-16-be'), data.encode('utf-16-le'),
+ codecs.BOM_UTF8+data.encode('utf-16-be'),
+ codecs.BOM_UTF16_BE+data.encode('utf-16-le'),
+ codecs.BOM_UTF16_LE+data.encode('utf-8')+'!']:
+ try:
+ yaml.load(input)
+ except yaml.YAMLError, exc:
+ if verbose:
+ print exc
+ else:
+ raise AssertionError("expected an exception")
+ try:
+ yaml.load(StringIO.StringIO(input))
+ except yaml.YAMLError, exc:
+ if verbose:
+ print exc
+ else:
+ raise AssertionError("expected an exception")
+
+test_unicode_input_errors.unittest = ['.unicode']
+
+def test_unicode_output(unicode_filename, verbose=False):
+ data = open(unicode_filename, 'rb').read().decode('utf-8')
+ value = ' '.join(data.split())
+ for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']:
+ for allow_unicode in [False, True]:
+ data1 = yaml.dump(value, allow_unicode=allow_unicode)
+ stream = StringIO.StringIO()
+ yaml.dump(value, _unicode_open(stream, 'utf-8'), encoding=encoding, allow_unicode=allow_unicode)
+ data2 = stream.getvalue()
+ data3 = yaml.dump(value, encoding=encoding, allow_unicode=allow_unicode)
+ stream = StringIO.StringIO()
+ yaml.dump(value, stream, encoding=encoding, allow_unicode=allow_unicode)
+ data4 = stream.getvalue()
+ for copy in [data1, data2, data3, data4]:
+ if allow_unicode:
+ try:
+ copy[4:].encode('ascii')
+ except (UnicodeDecodeError, UnicodeEncodeError), exc:
+ if verbose:
+ print exc
+ else:
+ raise AssertionError("expected an exception")
+ else:
+ copy[4:].encode('ascii')
+ 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)
+
+test_unicode_output.unittest = ['.unicode']
+
+def test_unicode_transfer(unicode_filename, verbose=False):
+ data = open(unicode_filename, 'rb').read().decode('utf-8')
+ for encoding in [None, 'utf-8', 'utf-16-be', 'utf-16-le']:
+ input = data
+ if encoding is not None:
+ input = (u'\ufeff'+input).encode(encoding)
+ output1 = yaml.emit(yaml.parse(input), allow_unicode=True)
+ stream = StringIO.StringIO()
+ yaml.emit(yaml.parse(input), _unicode_open(stream, 'utf-8'),
+ allow_unicode=True)
+ output2 = stream.getvalue()
+ if encoding is None:
+ assert isinstance(output1, unicode), (type(output1), encoding)
+ else:
+ assert isinstance(output1, str), (type(output1), encoding)
+ output1.decode(encoding)
+ assert isinstance(output2, str), (type(output2), encoding)
+ output2.decode('utf-8')
+
+test_unicode_transfer.unittest = ['.unicode']
+
+if __name__ == '__main__':
+ import test_appliance
+ test_appliance.run(globals())
+
diff --git a/tests/lib/test_yaml.py b/tests/lib/test_yaml.py
index d195e1a..0927368 100644
--- a/tests/lib/test_yaml.py
+++ b/tests/lib/test_yaml.py
@@ -10,6 +10,7 @@ from test_constructor import *
from test_emitter import *
from test_representer import *
from test_recursive import *
+from test_input_output import *
if __name__ == '__main__':
import test_appliance
diff --git a/tests/lib/test_yaml_ext.py b/tests/lib/test_yaml_ext.py
index e18becf..bdfda3e 100644
--- a/tests/lib/test_yaml_ext.py
+++ b/tests/lib/test_yaml_ext.py
@@ -267,9 +267,9 @@ def wrap_ext(collections):
globals()[function.unittest_name] = function
import test_tokens, test_structure, test_errors, test_resolver, test_constructor, \
- test_emitter, test_representer, test_recursive
+ test_emitter, test_representer, test_recursive, test_input_output
wrap_ext([test_tokens, test_structure, test_errors, test_resolver, test_constructor,
- test_emitter, test_representer, test_recursive])
+ test_emitter, test_representer, test_recursive, test_input_output])
if __name__ == '__main__':
import test_appliance