1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import openstep_plist
from openstep_plist.writer import Writer, string_needs_quotes
from io import StringIO, BytesIO
from collections import OrderedDict
from textwrap import dedent
import string
import random
import pytest
class TestWriter(object):
def test_simple(self):
w = Writer()
assert w.write("abc") == 3
assert w.getvalue() == "abc"
f = StringIO()
w.dump(f)
assert f.getvalue() == "abc"
def test_None(self):
w = Writer()
w.write(None)
assert w.getvalue() == '"(nil)"'
def test_unquoted_string(self):
w = Writer()
assert w.write(".appVersion") == 11
assert w.getvalue() == ".appVersion"
@pytest.mark.parametrize(
"string, expected",
[
("", '""'),
("\t", '"\t"'),
("\n\a\b\v\f\r", '"\\n\\a\\b\\v\\f\\r"'),
("\\", '"\\\\"'),
('"', '"\\""'),
("\0\1\2\3\4\5\6", '"\\000\\001\\002\\003\\004\\005\\006"'),
("\x0E\x0F\x10\x11\x12\x13", '"\\016\\017\\020\\021\\022\\023"'),
("\x14\x15\x16\x17\x18\x19", '"\\024\\025\\026\\027\\030\\031"'),
("\x1a\x1b\x1c\x1d\x1e\x1f\x7f", '"\\032\\033\\034\\035\\036\\037\\177"'),
("\x80\x81\x9E\x9F\xA0", '"\\U0080\\U0081\\U009E\\U009F\\U00A0"'),
("\U0001F4A9", '"\\UD83D\\UDCA9"'), # '💩'
# if string may be confused with a number wrap it in quotes
("1", '"1"'),
("1.1", '"1.1"'),
("-23", '"-23"'),
("-23yyy", '"-23yyy"'),
("-", '"-"'),
("-a-", '"-a-"'),
],
)
def test_quoted_string(self, string, expected):
w = Writer()
w.write(string)
assert w.getvalue() == expected
def test_quoted_string_dont_escape_newlines(self):
w = Writer(escape_newlines=False)
w.write("a\n\n\nbc")
assert w.getvalue() == '"a\n\n\nbc"'
def test_quoted_string_no_unicode_escape(self):
w = Writer(unicode_escape=False)
w.write("\u0410") == 3
assert w.getvalue() == '"\u0410"'
w = Writer(unicode_escape=False)
assert w.write("\U0001F4A9") == 3
assert w.getvalue() == '"\U0001F4A9"'
@pytest.mark.parametrize(
"integer, expected",
[
(0, "0"),
(1, "1"),
(123, "123"),
(0x7fffffffffffffff, "9223372036854775807"),
(0x7fffffffffffffff + 1, "9223372036854775808"),
],
)
def test_int(self, integer, expected):
w = Writer()
w.write(integer)
assert w.getvalue() == expected
@pytest.mark.parametrize(
"flt, expected",
[
(0.0, "0"),
(1.0, "1"),
(123.456, "123.456"),
(0.01, "0.01"),
(0.001, "0.001"),
(0.0001, "0.0001"),
(0.00001, "0.00001"),
(0.000001, "0.000001"),
(0.0000001, "0"), # default precision is 6
],
)
def test_float(self, flt, expected):
w = Writer()
w.write(flt)
assert w.getvalue() == expected
def test_float_precision(self):
w = Writer(float_precision=3)
w.write(0.0001)
assert w.getvalue() == "0"
w = Writer(float_precision=0)
w.write(0.999)
assert w.getvalue() == "1"
@pytest.mark.parametrize(
"data, expected, expected_no_spaces",
[
(b"\x00", "<00>", "<00>"),
(b"\x00\x01", "<0001>", "<0001>"),
(b"\x00\x01\x02", "<000102>", "<000102>"),
(b"\x00\x01\x02\x03", "<00010203>", "<00010203>"),
(b"\x00\x01\x02\x03\x04", "<00010203 04>", "<0001020304>"),
(b"\x00\x01\x02\x03\x04\x05", "<00010203 0405>", "<000102030405>"),
(b"\x00\x01\x02\x03\x04\x05\x06", "<00010203 040506>", "<00010203040506>"),
(b"\x00\x01\x02\x03\x04\x05\x06\x07", "<00010203 04050607>", "<0001020304050607>"),
(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08", "<00010203 04050607 08>", "<000102030405060708>"),
(b"\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11", "<090A0B0C 0D0E0F10 11>", "<090A0B0C0D0E0F1011>"),
],
ids=lambda p: p.decode() if isinstance(p, bytes) else p,
)
def test_data(self, data, expected, expected_no_spaces):
w = Writer()
assert w.write(data) == len(expected)
assert w.getvalue() == expected
w = Writer(binary_spaces=True)
assert w.write(data) == len(expected)
assert w.getvalue() == expected
w = Writer(binary_spaces=False)
# assert w.write(data) == len(expected_no_spaces)
w.write(data)
assert w.getvalue() == expected_no_spaces
def test_bool(self):
w = Writer()
assert w.write(True) == 1
assert w.getvalue() == "1"
w = Writer()
assert w.write(False) == 1
assert w.getvalue() == "0"
@pytest.mark.parametrize(
"array, expected_no_indent, expected_indent",
[
([], "()", "()"),
((), "()", "()"),
([1], "(1)", "(\n 1\n)"),
([1, 2], "(1, 2)", "(\n 1,\n 2\n)"),
([1.2, 3.4, 5.6], "(1.2, 3.4, 5.6)", "(\n 1.2,\n 3.4,\n 5.6\n)"),
(
(1, "a", ("b", 2)),
"(1, a, (b, 2))",
"(\n 1,\n a,\n (\n b,\n 2\n )\n)",
),
([b"a", b"b"], "(<61>, <62>)", "(\n <61>,\n <62>\n)"),
(
[{"a": "b"}, {"c": "d"}],
"({a = b;}, {c = d;})",
"(\n {\n a = b;\n },\n {\n c = d;\n }\n)",
),
],
)
def test_array(self, array, expected_no_indent, expected_indent):
w = Writer()
assert w.write(array) == len(expected_no_indent)
assert w.getvalue() == expected_no_indent
w = Writer(indent=2)
assert w.write(array) == len(expected_indent)
assert w.getvalue() == expected_indent
@pytest.mark.parametrize(
"dictionary, expected_no_indent, expected_indent",
[
({}, "{}", "{}"),
(OrderedDict(), "{}", "{}"),
({"a": "b"}, "{a = b;}", "{\n a = b;\n}"),
({1: "c"}, '{"1" = c;}', '{\n "1" = c;\n}'),
(
{"hello world": 12, "abc": [34, 56.8]},
'{abc = (34, 56.8); "hello world" = 12;}',
'{\n abc = (\n 34,\n 56.8\n );\n "hello world" = 12;\n}',
),
(
OrderedDict([("z", 2), ("a", 1), (12, "c")]),
'{z = 2; a = 1; "12" = c;}',
'{\n z = 2;\n a = 1;\n "12" = c;\n}',
),
],
)
def test_dictionary(self, dictionary, expected_no_indent, expected_indent):
w = Writer()
assert w.write(dictionary) == len(expected_no_indent)
assert w.getvalue() == expected_no_indent
w = Writer(indent=" ")
assert w.write(dictionary) == len(expected_indent)
assert w.getvalue() == expected_indent
def test_type_error(self):
obj = object()
w = Writer()
with pytest.raises(TypeError, match="not PLIST serializable"):
w.write(obj)
def test_dumps():
assert openstep_plist.dumps(
{"a": 1, "b": 2.9999999, "c d": [33, 44], "e": (b"fghilmno", b"pqrstuvz")}
) == (
'{a = 1; b = 3; "c d" = (33, 44); '
"e = (<66676869 6C6D6E6F>, <70717273 7475767A>);}"
)
assert openstep_plist.dumps(
{
"features": dedent(
"""\
sub periodcentered by periodcentered.case;
sub bullet by bullet.case;
"""
),
},
escape_newlines=False,
) == (
'{features = "sub periodcentered by periodcentered.case;\n'
'sub bullet by bullet.case;\n'
'";}'
)
def test_dump():
plist = [1, b"2", {3: (4, "5\n6", "\U0001F4A9")}]
fp = StringIO()
openstep_plist.dump(plist, fp)
assert fp.getvalue() == '(1, <32>, {"3" = (4, "5\\n6", "\\UD83D\\UDCA9");})'
fp = BytesIO()
openstep_plist.dump(plist, fp, unicode_escape=False)
assert fp.getvalue() == b'(1, <32>, {"3" = (4, "5\\n6", "\xf0\x9f\x92\xa9");})'
fp = BytesIO()
openstep_plist.dump(plist, fp, escape_newlines=False, unicode_escape=False)
assert fp.getvalue() == b'(1, <32>, {"3" = (4, "5\n6", "\xf0\x9f\x92\xa9");})'
with pytest.raises(AttributeError):
openstep_plist.dump(plist, object())
valid_unquoted_chars = (
string.ascii_uppercase + string.ascii_lowercase + string.digits + "._$"
)
invalid_unquoted_chars = [
chr(c) for c in range(128) if chr(c) not in valid_unquoted_chars
]
@pytest.mark.parametrize(
"string, expected",
[
(string.ascii_uppercase, False),
(string.ascii_lowercase, False),
# digits are allowed unquoted if not in first position
("a" + string.digits, False),
(".appVersion", False),
("_private", False),
("$PWD", False),
("1zzz", False),
("192.168.1.1", False),
("0", True),
("1", True),
("2", True),
("3", True),
("4", True),
("5", True),
("6", True),
("7", True),
("8", True),
("9", True),
("", True),
("-", True),
("A-Z", True),
("hello world", True),
("\\backslash", True),
("http://github.com", True),
(random.choice(invalid_unquoted_chars), True),
],
)
def test_string_needs_quotes(string, expected):
assert string_needs_quotes(string) is expected
def test_single_line_tuples():
assert openstep_plist.dumps({"a": 1, "b": (2, 3), "c": "Hello"}, indent=0) == (
"""{
a = 1;
b = (
2,
3
);
c = Hello;
}"""
)
assert openstep_plist.dumps(
{"a": 1, "b": (2, 3), "c": "Hello"}, indent=0, single_line_tuples=True
) == (
"""{
a = 1;
b = (2,3);
c = Hello;
}"""
)
def test_sort_keys():
plist = {"c": 1, "b": {"z": 9, "y": 8, "x": 7}, "a": "Hello"}
sorted_result = "{a = Hello; b = {x = 7; y = 8; z = 9;}; c = 1;}"
unsorted_result = "{c = 1; b = {z = 9; y = 8; x = 7;}; a = Hello;}"
assert openstep_plist.dumps(plist) == sorted_result
assert openstep_plist.dumps(plist, sort_keys=True) == sorted_result
assert openstep_plist.dumps(plist, sort_keys=False) == unsorted_result
def test_single_line_empty_objects():
plist = {"a": [], "b": {}, "c": [{}], "d": [[]], "e": {"f": {}, "g": []}}
single_line_result = """{
a = ();
b = {};
c = (
{}
);
d = (
()
);
e = {
f = {};
g = ();
};
}"""
multi_line_result = """{
a = (
);
b = {
};
c = (
{
}
);
d = (
(
)
);
e = {
f = {
};
g = (
);
};
}"""
assert openstep_plist.dumps(plist, indent=0) == single_line_result
assert openstep_plist.dumps(plist, indent=0, single_line_empty_objects=True) == single_line_result
assert openstep_plist.dumps(plist, indent=0, single_line_empty_objects=False) == multi_line_result
|