Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | # Copyright (C) 2003 Python Software Foundation |
2 | ||
3 | import unittest | |
4 | import plistlib | |
5 | import os | |
6 | import time | |
7 | import datetime | |
8 | from test import test_support | |
9 | ||
10 | ||
11 | # This test data was generated through Cocoa's NSDictionary class | |
12 | TESTDATA = """<?xml version="1.0" encoding="UTF-8"?> | |
13 | <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \ | |
14 | "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
15 | <plist version="1.0"> | |
16 | <dict> | |
17 | <key>aDate</key> | |
18 | <date>2004-10-26T10:33:33Z</date> | |
19 | <key>aDict</key> | |
20 | <dict> | |
21 | <key>aFalseValue</key> | |
22 | <false/> | |
23 | <key>aTrueValue</key> | |
24 | <true/> | |
25 | <key>aUnicodeValue</key> | |
26 | <string>M\xc3\xa4ssig, Ma\xc3\x9f</string> | |
27 | <key>anotherString</key> | |
28 | <string><hello & 'hi' there!></string> | |
29 | <key>deeperDict</key> | |
30 | <dict> | |
31 | <key>a</key> | |
32 | <integer>17</integer> | |
33 | <key>b</key> | |
34 | <real>32.5</real> | |
35 | <key>c</key> | |
36 | <array> | |
37 | <integer>1</integer> | |
38 | <integer>2</integer> | |
39 | <string>text</string> | |
40 | </array> | |
41 | </dict> | |
42 | </dict> | |
43 | <key>aFloat</key> | |
44 | <real>0.5</real> | |
45 | <key>aList</key> | |
46 | <array> | |
47 | <string>A</string> | |
48 | <string>B</string> | |
49 | <integer>12</integer> | |
50 | <real>32.5</real> | |
51 | <array> | |
52 | <integer>1</integer> | |
53 | <integer>2</integer> | |
54 | <integer>3</integer> | |
55 | </array> | |
56 | </array> | |
57 | <key>aString</key> | |
58 | <string>Doodah</string> | |
59 | <key>anInt</key> | |
60 | <integer>728</integer> | |
61 | <key>nestedData</key> | |
62 | <array> | |
63 | <data> | |
64 | PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5r | |
65 | PgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5 | |
66 | IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBi | |
67 | aW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3Rz | |
68 | IG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQID | |
69 | PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw== | |
70 | </data> | |
71 | </array> | |
72 | <key>someData</key> | |
73 | <data> | |
74 | PGJpbmFyeSBndW5rPg== | |
75 | </data> | |
76 | <key>someMoreData</key> | |
77 | <data> | |
78 | PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8 | |
79 | bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxs | |
80 | b3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxv | |
81 | dHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90 | |
82 | cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw== | |
83 | </data> | |
84 | <key>\xc3\x85benraa</key> | |
85 | <string>That was a unicode key.</string> | |
86 | </dict> | |
87 | </plist> | |
88 | """.replace(" " * 8, "\t") # Apple as well as plistlib.py output hard tabs | |
89 | ||
90 | ||
91 | class TestPlistlib(unittest.TestCase): | |
92 | ||
93 | def tearDown(self): | |
94 | try: | |
95 | os.unlink(test_support.TESTFN) | |
96 | except: | |
97 | pass | |
98 | ||
99 | def _create(self): | |
100 | pl = dict( | |
101 | aString="Doodah", | |
102 | aList=["A", "B", 12, 32.5, [1, 2, 3]], | |
103 | aFloat = 0.5, | |
104 | anInt = 728, | |
105 | aDict=dict( | |
106 | anotherString="<hello & 'hi' there!>", | |
107 | aUnicodeValue=u'M\xe4ssig, Ma\xdf', | |
108 | aTrueValue=True, | |
109 | aFalseValue=False, | |
110 | deeperDict=dict(a=17, b=32.5, c=[1, 2, "text"]), | |
111 | ), | |
112 | someData = plistlib.Data("<binary gunk>"), | |
113 | someMoreData = plistlib.Data("<lots of binary gunk>\0\1\2\3" * 10), | |
114 | nestedData = [plistlib.Data("<lots of binary gunk>\0\1\2\3" * 10)], | |
115 | aDate = datetime.datetime(2004, 10, 26, 10, 33, 33), | |
116 | ) | |
117 | pl[u'\xc5benraa'] = "That was a unicode key." | |
118 | return pl | |
119 | ||
120 | def test_create(self): | |
121 | pl = self._create() | |
122 | self.assertEqual(pl["aString"], "Doodah") | |
123 | self.assertEqual(pl["aDict"]["aFalseValue"], False) | |
124 | ||
125 | def test_io(self): | |
126 | pl = self._create() | |
127 | plistlib.writePlist(pl, test_support.TESTFN) | |
128 | pl2 = plistlib.readPlist(test_support.TESTFN) | |
129 | self.assertEqual(dict(pl), dict(pl2)) | |
130 | ||
131 | def test_string(self): | |
132 | pl = self._create() | |
133 | data = plistlib.writePlistToString(pl) | |
134 | pl2 = plistlib.readPlistFromString(data) | |
135 | self.assertEqual(dict(pl), dict(pl2)) | |
136 | data2 = plistlib.writePlistToString(pl2) | |
137 | self.assertEqual(data, data2) | |
138 | ||
139 | def test_appleformatting(self): | |
140 | pl = plistlib.readPlistFromString(TESTDATA) | |
141 | data = plistlib.writePlistToString(pl) | |
142 | self.assertEqual(data, TESTDATA, | |
143 | "generated data was not identical to Apple's output") | |
144 | ||
145 | def test_appleformattingfromliteral(self): | |
146 | pl = self._create() | |
147 | pl2 = plistlib.readPlistFromString(TESTDATA) | |
148 | self.assertEqual(dict(pl), dict(pl2), | |
149 | "generated data was not identical to Apple's output") | |
150 | ||
151 | def test_stringio(self): | |
152 | from StringIO import StringIO | |
153 | f = StringIO() | |
154 | pl = self._create() | |
155 | plistlib.writePlist(pl, f) | |
156 | pl2 = plistlib.readPlist(StringIO(f.getvalue())) | |
157 | self.assertEqual(dict(pl), dict(pl2)) | |
158 | ||
159 | def test_cstringio(self): | |
160 | from cStringIO import StringIO | |
161 | f = StringIO() | |
162 | pl = self._create() | |
163 | plistlib.writePlist(pl, f) | |
164 | pl2 = plistlib.readPlist(StringIO(f.getvalue())) | |
165 | self.assertEqual(dict(pl), dict(pl2)) | |
166 | ||
167 | def test_controlcharacters(self): | |
168 | for i in range(128): | |
169 | c = chr(i) | |
170 | testString = "string containing %s" % c | |
171 | if i >= 32 or c in "\r\n\t": | |
172 | # \r, \n and \t are the only legal control chars in XML | |
173 | plistlib.writePlistToString(testString) | |
174 | else: | |
175 | self.assertRaises(ValueError, | |
176 | plistlib.writePlistToString, | |
177 | testString) | |
178 | ||
179 | def test_nondictroot(self): | |
180 | test1 = "abc" | |
181 | test2 = [1, 2, 3, "abc"] | |
182 | result1 = plistlib.readPlistFromString(plistlib.writePlistToString(test1)) | |
183 | result2 = plistlib.readPlistFromString(plistlib.writePlistToString(test2)) | |
184 | self.assertEqual(test1, result1) | |
185 | self.assertEqual(test2, result2) | |
186 | ||
187 | ||
188 | def test_main(): | |
189 | test_support.run_unittest(TestPlistlib) | |
190 | ||
191 | ||
192 | if __name__ == '__main__': | |
193 | test_main() |