Files
rosetta/tools/fixtures/plist.py
T

84 lines
3.7 KiB
Python

# data/plist fixtures: random values written by Python's plistlib as XML (insertion order) and as binary, with the
# canonical form of plistlib's own reading (aware UTC datetimes; UIDs as ["uid", n]).
# tests/gen/plist.luau { { xml, binary, canon } } (our XML writer must reproduce xml byte for byte)
# + wide = { { binary, canon } } (UIDs, 64-bit integers: binary only)
import datetime, importlib.util, os, plistlib
_spec = importlib.util.spec_from_file_location("_data_common", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_data_common.py"))
C = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(C)
def rval(rng, d, wide):
r = rng.random()
if d > 3 or r < 0.5:
k = rng.randrange(8)
if k == 0:
return rng.random() < 0.5
if k == 1:
return rng.randrange(-2 ** 31, 2 ** 31)
if k == 2:
return rng.choice([rng.randrange(-2 ** 63, 2 ** 63), rng.randrange(0, 2 ** 64)]) if wide else rng.randrange(-2 ** 53, 2 ** 53)
if k == 3:
f = rng.uniform(-1e6, 1e6) * 10 ** rng.randrange(-5, 5)
return f if not f.is_integer() else f + 0.5
if k == 4:
return "".join(rng.choice("ab <>&\"'é中\U0001f600\n\t") for _ in range(rng.randrange(0, 12)))
if k == 5:
return rng.randbytes(rng.choice([0, 1, 2, 3, 40, 57, 58, 200]))
if k == 6:
return datetime.datetime.fromtimestamp(rng.randrange(-2 ** 31, 2 ** 33), datetime.timezone.utc)
return plistlib.UID(rng.randrange(0, 2 ** 32)) if wide else "x"
if r < 0.75:
return [rval(rng, d + 1, wide) for _ in range(rng.randrange(0, 6))]
return {"".join(rng.choice("abcé <&") for _ in range(rng.randrange(0, 6))): rval(rng, d + 1, wide) for _ in range(rng.randrange(0, 6))}
def conv(v):
if isinstance(v, plistlib.UID):
return ["uid", v.data]
if isinstance(v, list):
return [conv(x) for x in v]
if isinstance(v, dict):
return {k: conv(x) for k, x in v.items()}
return v
def generate(ctx):
rng = ctx.rng(21)
cases = []
for i in range(200):
v = rval(rng, 0, False)
if not isinstance(v, (list, dict)) and rng.random() < 0.5:
v = {"root": v}
xml = plistlib.dumps(v, fmt=plistlib.FMT_XML, sort_keys=False, aware_datetime=True)
binary = plistlib.dumps(v, fmt=plistlib.FMT_BINARY, sort_keys=False, aware_datetime=True)
back = plistlib.loads(xml, aware_datetime=True)
assert C.canon(conv(plistlib.loads(binary, aware_datetime=True))) == C.canon(conv(back))
cases.append({"xml": xml, "binary": binary, "canon": C.canon(conv(back))})
wide = []
for i in range(80):
v = [rval(rng, 1, True) for _ in range(4)]
binary = plistlib.dumps(v, fmt=plistlib.FMT_BINARY, aware_datetime=True)
wide.append({"binary": binary, "canon": C.canon(conv(plistlib.loads(binary, aware_datetime=True)))})
# a hand-written XML plist with whitespace, comments, hex integers, odd reals, a bare value root
extra = b"""<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<!-- comment -->
<dict>
<key>hex</key> <integer>0x1F</integer>
<key>neg</key> <integer>-5</integer>
<key>real</key> <real> 2.5e3 </real>
<key>data</key> <data>
AAEC
AwQ=
</data>
<key>empty</key> <string/>
<key>date</key> <date>2001-01-01T00:00:00Z</date>
</dict>
</plist>
"""
ctx.emit("plist", {"cases": cases, "wide": wide, "extra": extra,
"extraCanon": C.canon(plistlib.loads(extra, aware_datetime=True))})