Debian Codegraph @

tests/unit/test_textutil.py

raw ◀ ▶
1 Debian patch touch this file
1 # -*- coding: UTF-8 -*-
2 """
3 Unit tests for :mod:`behave.textutil`.
4 """
5
6 from __future__ import absolute_import, print_function
7 from behave.textutil import text, is_ascii_encoding, select_best_encoding
8 import pytest
9 import codecs
10 import six
11
12 # -----------------------------------------------------------------------------
13 # TEST SUPPORT:
14 # -----------------------------------------------------------------------------
15 class ConvertableToUnicode(object):
16 """Class that can be converted into a unicode string.
17 If parameter is a bytes-string, it is converted into unicode.
18
19 .. code-block:: python
20
21 obj1 = ConvertableToUnicode(u"Ärgernis")
22 obj2 = ConvertableToUnicode(u"Ärgernis".encode("latin-1")
23
24 # -- CASE Python2: string is a bytes-string
25 text_value21 = unicode(obj1)
26 text_value22 = unicode(obj2)
27
28 # -- CASE Python3: string is a unicode-string
29 text_value31 = str(obj1)
30 text_value32 = str(obj2)
31 """
32 encoding = "utf-8"
33
34 def __init__(self, text, encoding=None):
35 self.text = text
36 self.encoding = encoding or self.__class__.encoding
37
38 def __str__(self):
39 """Convert into a unicode string."""
40 text = self.text
41 if isinstance(text, six.binary_type):
42 text = codecs.decode(text, self.encoding)
43 return text
44
45 if six.PY2:
46 __unicode__ = __str__
47 __str__ = lambda self: self.__unicode__().encode(self.encoding)
48
49 class ConvertableToString(object):
50 encoding = "utf-8"
51
52 def __init__(self, text, encoding=None):
53 self.text = text
54 self.encoding = encoding or self.__class__.encoding
55
56 def __str__(self):
57 text = self.text
58 if isinstance(text, six.binary_type):
59 text = codecs.decode(text, self.encoding)
60 return text
61
62 if six.PY2:
63 __unicode__ = __str__
64
65 def __str__(self):
66 # MAYBE: self.__unicode__().encode(self.encoding)
67 text = self.text
68 if isinstance(text, six.text_type):
69 text = codecs.encode(text, self.encoding)
70 return text
71
72 class ConvertableToPy2String(object):
73 encoding = "utf-8"
74
75 def __init__(self, text, encoding=None):
76 self.text = text
77 self.encoding = encoding or self.__class__.encoding
78
79 def __str__(self):
80 text = self.text
81 if six.PY2:
82 if isinstance(text, six.text_type):
83 text = codecs.encode(text, self.encoding)
84 else:
85 if isinstance(text, six.bytes_type):
86 text = codecs.decode(text, self.encoding)
87 return text
88
89 if six.PY2:
90 __unicode__ = __str__
91
92 def __str__(self):
93 # MAYBE: self.__unicode__().encode(self.encoding)
94 text = self.text
95 if isinstance(text, six.text_type):
96 text = codecs.encode(text, self.encoding)
97 return text
98
99 # def raise_exception(exception_class, *args):
100 # raise exception_class(*args)
101 #
102 # def catch_raised_exception_and_return_as_text(exception_class, *args, **kwargs):
103 # encoding = kwargs.pop("encoding", None)
104 # try:
105 # raise_exception(*args)
106 # except Exception as e:
107 # return text(e, encoding=encoding)
108
109
110 # -----------------------------------------------------------------------------
111 # TEST SUITE
112 # -----------------------------------------------------------------------------
113 xfail = pytest.mark.xfail
114 requires_python2 = pytest.mark.skipif(not six.PY2, reason="requires python2")
115
116 UNICODE_TEXT_VALUES1 = [u"Alice", u"Bob"]
117 UNICODE_TEXT_VALUES = [u"Café", u"100€ (Euro)", u"Frühaufsteher"]
118 BYTES_TEXT_TUPLES_WITH_UTF8_ENCODING = [
119 (codecs.encode(_text, "utf-8"), _text) for _text in UNICODE_TEXT_VALUES
120 ]
121
122
123 class TestTextConversion(object):
124 """Unit tests for the :func:`behave.textutil.text()` function."""
125
126 @pytest.mark.parametrize("value", UNICODE_TEXT_VALUES)
127 def test_text__with_unicode_value(self, value):
128 value_id = id(value)
129 actual_text = text(value)
130 assert actual_text == value
131 assert id(actual_text) == value_id # PARANOID check w/ unicode copy.
132
133 @pytest.mark.parametrize("bytes_value, expected_text",
134 BYTES_TEXT_TUPLES_WITH_UTF8_ENCODING)
135 def test_text__with_bytes_value(self, bytes_value, expected_text):
136 actual_text = text(bytes_value)
137 assert actual_text == expected_text
138
139
140 @pytest.mark.parametrize("text_value, encoding", [
141 (u"Ärgernis", "UTF-8"),
142 (u"Übermut", "UTF-8"),
143 ])
144 def test_text__with_bytes_value_and_encoding(self, text_value, encoding):
145 bytes_value = text_value.encode(encoding)
146 assert isinstance(bytes_value, bytes)
147
148 actual = text(bytes_value, encoding)
149 assert isinstance(actual, six.text_type)
150 assert actual == text_value
151
152 def test_text__with_exception_traceback(self): pass
153
154 @pytest.mark.parametrize("text_value", UNICODE_TEXT_VALUES)
155 def test_text__with_object_convertable_to_unicode(self, text_value):
156 obj = ConvertableToUnicode(text_value)
157 actual_text = text(obj)
158 assert actual_text == text_value
159 assert isinstance(actual_text, six.text_type)
160
161 # @pytest.mark.parametrize("expected, text_value, encoding", [
162 # (u"Ärgernis", u"Ärgernis".encode("UTF-8"), "UTF-8"),
163 # (u"Übermut", u"Übermut".encode("latin-1"), "latin-1"),
164 # ])
165 # def test_text__with_object_convertable_to_unicode2(self, expected,
166 # text_value, encoding):
167 # obj = ConvertableToUnicode(text_value, encoding)
168 # actual_text = text(obj)
169 # assert actual_text == expected
170 # assert isinstance(actual_text, six.text_type)
171
172 @pytest.mark.parametrize("text_value", UNICODE_TEXT_VALUES)
173 def test_text__with_object_convertable_to_string(self, text_value):
174 obj = ConvertableToString(text_value)
175 actual_text = text(obj)
176 assert actual_text == text_value
177 assert isinstance(actual_text, six.text_type)
178
179 @xfail
180 @requires_python2
181 @pytest.mark.parametrize("text_value", UNICODE_TEXT_VALUES)
182 def test_text__with_object_convertable_to_py2string_only(self, text_value):
183 class ConvertableToPy2String(object):
184 """Lacks feature: convertable-to-unicode (only: to-string)"""
185 def __init__(self, message=""):
186 self.message = message or ""
187 if self.message and isinstance(self.message, six.text_type):
188 self.message = self.message.encode("UTF-8")
189
190 def __str__(self):
191 # assert isinstance(self.message, str)
192 return self.message
193
194 obj = ConvertableToPy2String(text_value.encode("UTF-8"))
195 actual = text(obj)
196 print(u"actual: %s" % actual)
197 print(u"text_value: %s" % text_value)
198 assert actual == text_value
199
200
201 class TestObjectToTextConversion(object):
202 """Unit tests for the :func:`behave.textutil.text()` function.
203 Explore case with object-to-text conversion.
204 """
205 ENCODING = "UTF-8"
206
207 # -- CASE: object=exception
208 @pytest.mark.parametrize("message", [
209 u"Ärgernis", u"Übermütig"
210 ])
211 def test_text__with_assert_failed_and_unicode_message(self, message):
212 with pytest.raises(AssertionError) as e:
213 assert False, message
214
•215 # -- FOR: pytest < 5.0
216 # expected = u"AssertionError: %s" % message
217 text2 = text(e.value)
218 assert u"AssertionError" in text(e)
219 assert message in text2, "OOPS: text=%r" % text2
220
221 @requires_python2
222 @pytest.mark.parametrize("message", [
223 u"Ärgernis", u"Übermütig"
224 ])
225 def test_text__with_assert_failed_and_bytes_message(self, message):
226 # -- ONLY PYTHON2: Use case makes no sense for Python 3.
227 bytes_message = message.encode(self.ENCODING)
228 with pytest.raises(AssertionError) as e:
229 assert False, bytes_message
230
231 text2 = text(e)
232 expected = u"AssertionError: %s" % message
233 assert text2.endswith(expected)
234
235 @pytest.mark.parametrize("exception_class, message", [
236 (AssertionError, u"Ärgernis"),
237 (RuntimeError, u"Übermütig"),
238 ])
239 def test_text__with_raised_exception_and_unicode_message(self,
240 exception_class,
241 message):
242 with pytest.raises(exception_class) as e:
243 raise exception_class(message)
244
•245 # -- FOR: pytest < 5.0
246 # expected = u"AssertionError: %s" % message
247 text2 = text(e.value)
248 expected = u"%s: %s" % (exception_class.__name__, message)
249 assert isinstance(text2, six.text_type)
•250 assert exception_class.__name__ in str(e)
251 assert message in text2, "OOPS: text=%r" % text2
252
253 @requires_python2
254 @pytest.mark.parametrize("exception_class, message", [
255 (AssertionError, u"Ärgernis"),
256 (RuntimeError, u"Übermütig"),
257 ])
258 def test_text__with_raised_exception_and_bytes_message(self, exception_class,
259 message):
260 # -- ONLY PYTHON2: Use case makes no sense for Python 3.
261 bytes_message = message.encode(self.ENCODING)
262 with pytest.raises(exception_class) as e:
263 raise exception_class(bytes_message)
264
•265 text2 = text(e.value)
266 unicode_message = bytes_message.decode(self.ENCODING)
267 expected = u"%s: %s" % (exception_class.__name__, unicode_message)
268 assert isinstance(text2, six.text_type)
269 assert text2.endswith(expected)
270 # -- DIAGNOSTICS:
271 print(u"text2: "+ text2)
272 print(u"expected: " + expected)