|
2 | 2 | import os |
3 | 3 | import tempfile |
4 | 4 | import shutil |
| 5 | +import six |
| 6 | +import pytest |
5 | 7 |
|
6 | 8 | from awsshell.utils import FSLayer |
7 | 9 | from awsshell.utils import InMemoryFSLayer |
8 | 10 | from awsshell.utils import FileReadError |
9 | 11 | from awsshell.utils import temporary_file |
| 12 | +from awsshell.utils import force_unicode |
10 | 13 |
|
11 | 14 |
|
12 | 15 | class TestFSLayer(unittest.TestCase): |
@@ -93,3 +96,34 @@ def test_can_open_in_read(self): |
93 | 96 | f.seek(0) |
94 | 97 | assert f.read() == "foobar" |
95 | 98 | self.assertFalse(os.path.isfile(filename)) |
| 99 | + |
| 100 | + |
| 101 | +strings = ['', u'', 'some text', u'some text', 'yu\u3086', u'yu\u3086'] |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.parametrize('text', strings) |
| 105 | +def test_force_unicode(text): |
| 106 | + unicode_text = force_unicode(text) |
| 107 | + assert unicode_text == text |
| 108 | + assert isinstance(unicode_text, six.text_type) |
| 109 | + |
| 110 | + |
| 111 | +def test_force_unicode_binary(): |
| 112 | + # should fail decoding and remain unchanged |
| 113 | + blob = '\x81' |
| 114 | + result = force_unicode(blob) |
| 115 | + assert result is blob |
| 116 | + |
| 117 | + |
| 118 | +def test_force_unicode_recursion(): |
| 119 | + obj = { |
| 120 | + 'a': ['string'], |
| 121 | + 'b': {'str': 'yu\u3086'}, |
| 122 | + 'c': '\x81', |
| 123 | + } |
| 124 | + |
| 125 | + clean_obj = force_unicode(obj) |
| 126 | + assert isinstance(clean_obj['a'][0], six.text_type) |
| 127 | + assert isinstance(clean_obj['b']['str'], six.text_type) |
| 128 | + assert clean_obj['c'] is obj['c'] |
| 129 | + assert obj == clean_obj |
0 commit comments