| 1 |
import pytest |
| 2 |
|
| 3 |
from DisplayCAL.util_str import ( |
| 4 |
asciize, |
| 5 |
ellipsis_, |
| 6 |
make_ascii_printable, |
| 7 |
make_filename_safe, |
| 8 |
safe_basestring, |
| 9 |
safe_str, |
| 10 |
strtr, |
| 11 |
) |
| 12 |
from DisplayCAL.icc_profile import TextDescriptionType |
| 13 |
|
| 14 |
|
| 15 |
def test_asciize_working_properly_1(): |
| 16 |
"""testing if the ``asciize`` function is working properly str input""" |
| 17 |
tag_data = b"\0" * 8 + b"\x00\x00\x00\x04" + b"test" + b"\0" * 12 |
| 18 |
description_type = TextDescriptionType(tagData=tag_data, tagSignature="desc") |
| 19 |
description = str(description_type) |
| 20 |
result = asciize(description) |
| 21 |
expected_result = b"test" |
| 22 |
assert result == expected_result |
| 23 |
|
| 24 |
|
| 25 |
def test_asciize_working_properly_2(): |
| 26 |
"""testing if the ``asciize`` function is working properly Exception input""" |
| 27 |
tag_data = b"\0" * 8 + b"\x00\x00\x00\x04" + b"te\x80t" + b"\0" * 12 |
| 28 |
description_type = TextDescriptionType(tagData=tag_data, tagSignature="desc") |
| 29 |
result = str(description_type) |
| 30 |
expected_result = "te�t" # TODO: I'm not sure if this is the expected result |
| 31 |
assert result == expected_result |
| 32 |
|
| 33 |
|
| 34 |
def test_safe_basestring_1(): |
| 35 |
"""testing if safe_basestring() is working properly""" |
| 36 |
env_error = EnvironmentError("test the Environment is not being correct") |
| 37 |
assert safe_basestring(env_error) == "test the Environment is not being correct" |
| 38 |
|
| 39 |
|
| 40 |
def test_safe_basestring_2(): |
| 41 |
"""testing if safe_basestring() is working properly""" |
| 42 |
env_error = EnvironmentError() |
| 43 |
env_error.reason = "test the Environment is not being correct" |
| 44 |
assert safe_basestring(env_error) == "test the Environment is not being correct" |
| 45 |
|
| 46 |
|
| 47 |
def test_safe_basestring_3(): |
| 48 |
"""testing if safe_basestring() is working properly""" |
| 49 |
env_error = EnvironmentError() |
| 50 |
env_error.reason = b"test the Environment is not being correct" |
| 51 |
assert safe_basestring(env_error) == "test the Environment is not being correct" |
| 52 |
|
| 53 |
|
| 54 |
def test_safe_basestring_4(): |
| 55 |
"""testing if safe_basestring() is working properly""" |
| 56 |
env_error = EnvironmentError() |
| 57 |
env_error.reason = ["test the Environment is not being correct"] |
| 58 |
assert safe_basestring(env_error) == "test the Environment is not being correct" |
| 59 |
|
| 60 |
|
| 61 |
def test_safe_basestring_5(): |
| 62 |
"""testing if safe_basestring() is working properly""" |
| 63 |
env_error = EnvironmentError() |
| 64 |
env_error.reason = [b"test the Environment is not being correct"] |
| 65 |
assert safe_basestring(env_error) == "test the Environment is not being correct" |
| 66 |
|
| 67 |
|
| 68 |
def test_safe_basestring_6(): |
| 69 |
"""testing if safe_basestring() is working properly""" |
| 70 |
env_error = EnvironmentError() |
| 71 |
env_error.reason = [123] |
| 72 |
assert safe_basestring(env_error) == "123" |
| 73 |
|
| 74 |
|
| 75 |
def test_safe_basestring_7(): |
| 76 |
"""testing if safe_basestring() is working properly""" |
| 77 |
|
| 78 |
class TestObj: |
| 79 |
def __init__(self): |
| 80 |
self.filename = "TestValue.ti3" |
| 81 |
|
| 82 |
env_error = EnvironmentError() |
| 83 |
test_obj = TestObj() |
| 84 |
env_error.reason = [test_obj] |
| 85 |
assert safe_basestring(env_error) == test_obj.__repr__() |
| 86 |
|
| 87 |
|
| 88 |
def test_safe_basestring_8(): |
| 89 |
"""testing if safe_basestring() is working properly""" |
| 90 |
key_error = KeyError("test some key values not being correct") |
| 91 |
assert ( |
| 92 |
safe_basestring(key_error) |
| 93 |
== "Key does not exist: 'test some key values not being correct'" |
| 94 |
) |
| 95 |
|
| 96 |
|
| 97 |
def test_safe_basestring_9(): |
| 98 |
"""testing if safe_basestring() is working properly""" |
| 99 |
from _socket import gaierror |
| 100 |
|
| 101 |
gai_error = gaierror("test some key values not being correct") |
| 102 |
assert safe_basestring(gai_error) == "test some key values not being correct" |
| 103 |
|
| 104 |
|
| 105 |
def test_safe_basestring_10(): |
| 106 |
"""testing if safe_basestring() is working properly""" |
| 107 |
gai_error = OSError("test some key values not being correct") |
| 108 |
assert safe_basestring(gai_error) == "test some key values not being correct" |
| 109 |
|
| 110 |
|
| 111 |
def test_safe_string_1(): |
| 112 |
"""testing if safe_string() is working properly""" |
| 113 |
obj = "Test value" |
| 114 |
assert safe_str(obj) == "Test value" |
| 115 |
|
| 116 |
|
| 117 |
def test_safe_string_2(): |
| 118 |
"""testing if safe_string() is working properly""" |
| 119 |
obj = b"Test value" |
| 120 |
assert safe_str(obj) == "Test value" |
| 121 |
|
| 122 |
|
| 123 |
def test_strtr_1(): |
| 124 |
"""testing if the strtr() is working properly""" |
| 125 |
test_value = b"test1, test2, test3" |
| 126 |
replacements = [[b"test1", b"value1"], [b"test2", b"value2"], [b"test3", b"value3"]] |
| 127 |
result = strtr(test_value, replacements) |
| 128 |
expected_result = b"value1, value2, value3" |
| 129 |
assert result == expected_result |
| 130 |
|
| 131 |
|
| 132 |
def test_strtr_2(): |
| 133 |
"""testing if the strtr() is working properly""" |
| 134 |
test_value = "test1, test2, test3" |
| 135 |
replacements = [["test1", "value1"], ["test2", "value2"], ["test3", "value3"]] |
| 136 |
result = strtr(test_value, replacements) |
| 137 |
expected_result = "value1, value2, value3" |
| 138 |
assert result == expected_result |
| 139 |
|
| 140 |
|
| 141 |
def test_strtr_3(): |
| 142 |
"""testing if the strtr() is working properly""" |
| 143 |
test_value = b"test1, test2, test3" |
| 144 |
replacements = {b"test1": b"value1", b"test2": b"value2", b"test3": b"value3"} |
| 145 |
result = strtr(test_value, replacements) |
| 146 |
expected_result = b"value1, value2, value3" |
| 147 |
assert result == expected_result |
| 148 |
|
| 149 |
|
| 150 |
def test_strtr_4(): |
| 151 |
"""testing if the strtr() is working properly""" |
| 152 |
test_value = "test1, test2, test3" |
| 153 |
replacements = {"test1": "value1", "test2": "value2", "test3": "value3"} |
| 154 |
result = strtr(test_value, replacements) |
| 155 |
expected_result = "value1, value2, value3" |
| 156 |
assert result == expected_result |
| 157 |
|
| 158 |
|
| 159 |
def test_ellipsis_1(): |
| 160 |
"""testing ellipsis with str input""" |
| 161 |
test_value = "some long str" |
| 162 |
expected_result = "some long\u2026" |
| 163 |
result = ellipsis_(test_value, 10) |
| 164 |
assert result == expected_result |
| 165 |
|
| 166 |
|
| 167 |
def test_ellipsis_2(): |
| 168 |
"""testing ellipsis with bytes input""" |
| 169 |
test_value = b"some long bytes sequence" |
| 170 |
expected_result = b"some longu\xc2\x826" |
| 171 |
result = ellipsis_(test_value, 10) |
| 172 |
assert result == expected_result |
| 173 |
|
| 174 |
|
| 175 |
def test_make_filename_safe_1(): |
| 176 |
"""testing make_filename_safe with str input""" |
| 177 |
test_value = "some proper values that is suitable for filesystem already" |
| 178 |
result = make_filename_safe(test_value) |
| 179 |
expected_result = "some proper values that is suitable for filesystem already" |
| 180 |
assert result == expected_result |
| 181 |
|
| 182 |
|
| 183 |
def test_make_filename_safe_2(): |
| 184 |
"""testing make_filename_safe with str input""" |
| 185 |
test_value = "this/is not:suitable*for?filesystem \\" |
| 186 |
result = make_filename_safe(test_value) |
| 187 |
expected_result = "this_is not_suitable_for_filesystem _" |
| 188 |
assert result == expected_result |
| 189 |
|
| 190 |
|
| 191 |
def test_make_filename_safe_3(): |
| 192 |
"""testing make_filename_safe with str input and substitute (str)""" |
| 193 |
test_value = "this/is not:suitable*for?filesystem \\" |
| 194 |
result = make_filename_safe(test_value, substitute="-") |
| 195 |
expected_result = "this-is not-suitable-for-filesystem -" |
| 196 |
assert result == expected_result |
| 197 |
|
| 198 |
|
| 199 |
def test_make_filename_safe_4(): |
| 200 |
"""testing make_filename_safe with str input and substitute (bytes)""" |
| 201 |
test_value = "this/is not:suitable*for?filesystem \\" |
| 202 |
result = make_filename_safe(test_value, substitute=b"-") |
| 203 |
expected_result = "this-is not-suitable-for-filesystem -" |
| 204 |
assert result == expected_result |
| 205 |
|
| 206 |
|
| 207 |
def test_make_filename_safe_5(): |
| 208 |
"""testing make_filename_safe with str input""" |
| 209 |
test_value = b"some proper values that is suitable for filesystem already" |
| 210 |
result = make_filename_safe(test_value) |
| 211 |
expected_result = b"some proper values that is suitable for filesystem already" |
| 212 |
assert result == expected_result |
| 213 |
|
| 214 |
|
| 215 |
def test_make_filename_safe_6(): |
| 216 |
"""testing make_filename_safe with str input""" |
| 217 |
test_value = b"this/is not:suitable*for?filesystem \\" |
| 218 |
result = make_filename_safe(test_value) |
| 219 |
expected_result = b"this_is not_suitable_for_filesystem _" |
| 220 |
assert result == expected_result |
| 221 |
|
| 222 |
|
| 223 |
def test_make_filename_safe_7(): |
| 224 |
"""testing make_filename_safe with str input and substitute (str)""" |
| 225 |
test_value = b"this/is not:suitable*for?filesystem \\" |
| 226 |
result = make_filename_safe(test_value, substitute="-") |
| 227 |
expected_result = b"this-is not-suitable-for-filesystem -" |
| 228 |
assert result == expected_result |
| 229 |
|
| 230 |
|
| 231 |
def test_make_filename_safe_8(): |
| 232 |
"""testing make_filename_safe with str input and substitute (bytes)""" |
| 233 |
test_value = b"this/is not:suitable*for?filesystem \\" |
| 234 |
result = make_filename_safe(test_value, substitute=b"-") |
| 235 |
expected_result = b"this-is not-suitable-for-filesystem -" |
| 236 |
assert result == expected_result |
| 237 |
|
| 238 |
|
| 239 |
def test_make_ascii_printable_1(): |
| 240 |
"""Test DisplayCAL.util_str.make_ascii_printable() function.""" |
| 241 |
test_value = b"TYPR371U504L\n" |
| 242 |
result = make_ascii_printable(test_value, substitute=b"\x00") |
| 243 |
expected_result = b"TYPR371U504L\n" |
| 244 |
assert result == expected_result |
| 245 |
|
| 246 |
|
| 247 |
def test_make_ascii_printable_2(): |
| 248 |
"""Test DisplayCAL.util_str.make_ascii_printable() function.""" |
| 249 |
test_value = b"\xcf\xff\xaf\xf0\x99\x80TYPR371U504L\n" |
| 250 |
result = make_ascii_printable(test_value, substitute=b"\x00") |
| 251 |
expected_result = b"\x00\x00\x00\x00\x00\x00TYPR371U504L\n" |
| 252 |
assert result == expected_result |
| 253 |
|
| 254 |
|
| 255 |
def test_make_ascii_printable_3(): |
| 256 |
"""Test DisplayCAL.util_str.make_ascii_printable() function.""" |
| 257 |
test_value = b"\xcf\xff\xaf\xf0\x99\x80TYPR371U504L\n" |
| 258 |
result = make_ascii_printable(test_value, substitute="\x00") |
| 259 |
expected_result = b"\x00\x00\x00\x00\x00\x00TYPR371U504L\n" |
| 260 |
assert result == expected_result |
| 261 |
|
| 262 |
|
| 263 |
def test_make_ascii_printable_4(): |
| 264 |
"""Test DisplayCAL.util_str.make_ascii_printable() function.""" |
| 265 |
test_value = "\xcf\xff\xaf\xf0\x99\x80TYPR371U504L\n" |
| 266 |
result = make_ascii_printable(test_value, substitute=b"\x00") |
| 267 |
expected_result = "\x00\x00\x00\x00\x00\x00TYPR371U504L\n" |
| 268 |
assert result == expected_result |
| 269 |
|
| 270 |
|
| 271 |
@pytest.mark.skip(reason="TODO: This test is moved from the module, properly implement it.") |
| 272 |
def test_from_modules(): |
| 273 |
from DisplayCAL.util_str import subst |
| 274 |
for k in subst: |
| 275 |
v = subst[k] |
| 276 |
print(k, v) |