ยปCore Development>Code coverage>Mac/Tools/plistlib_generate_testdata.py

Python code coverage for Mac/Tools/plistlib_generate_testdata.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a
3n/afrom Cocoa import NSMutableDictionary, NSMutableArray, NSString, NSDate, NSNumber
4n/afrom Cocoa import NSPropertyListSerialization, NSPropertyListOpenStepFormat
5n/afrom Cocoa import NSPropertyListXMLFormat_v1_0, NSPropertyListBinaryFormat_v1_0
6n/afrom Cocoa import CFUUIDCreateFromString, NSNull, NSUUID, CFPropertyListCreateData
7n/afrom Cocoa import NSURL
8n/a
9n/aimport datetime
10n/afrom collections import OrderedDict
11n/aimport binascii
12n/a
13n/aFORMATS=[
14n/a# ('openstep', NSPropertyListOpenStepFormat),
15n/a ('plistlib.FMT_XML', NSPropertyListXMLFormat_v1_0),
16n/a ('plistlib.FMT_BINARY', NSPropertyListBinaryFormat_v1_0),
17n/a]
18n/a
19n/adef nsstr(value):
20n/a return NSString.alloc().initWithString_(value)
21n/a
22n/a
23n/adef main():
24n/a pl = OrderedDict()
25n/a
26n/a # Note: pl is an OrderedDict to control the order
27n/a # of keys, and hence have some control on the structure
28n/a # of the output file.
29n/a # New keys should be added in alphabetical order.
30n/a
31n/a seconds = datetime.datetime(2004, 10, 26, 10, 33, 33, tzinfo=datetime.timezone(datetime.timedelta(0))).timestamp()
32n/a pl[nsstr('aBigInt')] = 2 ** 63 - 44
33n/a pl[nsstr('aBigInt2')] = NSNumber.numberWithUnsignedLongLong_(2 ** 63 + 44)
34n/a pl[nsstr('aDate')] = NSDate.dateWithTimeIntervalSince1970_(seconds)
35n/a
36n/a pl[nsstr('aDict')] = d = OrderedDict()
37n/a d[nsstr('aFalseValue')] = False
38n/a d[nsstr('aTrueValue')] = True
39n/a d[nsstr('aUnicodeValue')] = "M\xe4ssig, Ma\xdf"
40n/a d[nsstr('anotherString')] = "<hello & 'hi' there!>"
41n/a d[nsstr('deeperDict')] = dd = OrderedDict()
42n/a dd[nsstr('a')] = 17
43n/a dd[nsstr('b')] = 32.5
44n/a dd[nsstr('c')] = a = NSMutableArray.alloc().init()
45n/a a.append(1)
46n/a a.append(2)
47n/a a.append(nsstr('text'))
48n/a
49n/a pl[nsstr('aFloat')] = 0.5
50n/a
51n/a pl[nsstr('aList')] = a = NSMutableArray.alloc().init()
52n/a a.append(nsstr('A'))
53n/a a.append(nsstr('B'))
54n/a a.append(12)
55n/a a.append(32.5)
56n/a aa = NSMutableArray.alloc().init()
57n/a a.append(aa)
58n/a aa.append(1)
59n/a aa.append(2)
60n/a aa.append(3)
61n/a
62n/a pl[nsstr('aNegativeBigInt')] = -80000000000
63n/a pl[nsstr('aNegativeInt')] = -5
64n/a pl[nsstr('aString')] = nsstr('Doodah')
65n/a
66n/a pl[nsstr('anEmptyDict')] = NSMutableDictionary.alloc().init()
67n/a
68n/a pl[nsstr('anEmptyList')] = NSMutableArray.alloc().init()
69n/a
70n/a pl[nsstr('anInt')] = 728
71n/a
72n/a pl[nsstr('nestedData')] = a = NSMutableArray.alloc().init()
73n/a a.append(b'''<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03''')
74n/a
75n/a
76n/a pl[nsstr('someData')] = b'<binary gunk>'
77n/a
78n/a pl[nsstr('someMoreData')] = b'''<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03<lots of binary gunk>\x00\x01\x02\x03'''
79n/a
80n/a pl[nsstr('\xc5benraa')] = nsstr("That was a unicode key.")
81n/a
82n/a print("TESTDATA={")
83n/a for fmt_name, fmt_key in FORMATS:
84n/a data, error = NSPropertyListSerialization.dataWithPropertyList_format_options_error_(
85n/a pl, fmt_key, 0, None)
86n/a if data is None:
87n/a print("Cannot serialize", fmt_name, error)
88n/a
89n/a else:
90n/a print(" %s: binascii.a2b_base64(b'''\n %s'''),"%(fmt_name, _encode_base64(bytes(data)).decode('ascii')[:-1]))
91n/a
92n/a print("}")
93n/a print()
94n/a
95n/adef _encode_base64(s, maxlinelength=60):
96n/a maxbinsize = (maxlinelength//4)*3
97n/a pieces = []
98n/a for i in range(0, len(s), maxbinsize):
99n/a chunk = s[i : i + maxbinsize]
100n/a pieces.append(binascii.b2a_base64(chunk))
101n/a return b' '.join(pieces)
102n/a
103n/amain()