1 # Copyright (C) 2003 Python Software Foundation
7 from test
import test_support
10 # This test data was generated through Cocoa's NSDictionary class
11 TESTDATA
= """<?xml version="1.0" encoding="UTF-8"?>
12 <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
13 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
17 <date>2004-10-26T10:33:33Z</date>
20 <key>aFalseValue</key>
24 <key>aUnicodeValue</key>
25 <string>M\xc3\xa4ssig, Ma\xc3\x9f</string>
26 <key>anotherString</key>
27 <string><hello & 'hi' there!></string>
57 <string>Doodah</string>
59 <integer>728</integer>
63 PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5r
64 PgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5
65 IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBi
66 aW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3Rz
67 IG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQID
68 PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw==
75 <key>someMoreData</key>
77 PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8
78 bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxs
79 b3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxv
80 dHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90
81 cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw==
83 <key>\xc3\x85benraa</key>
84 <string>That was a unicode key.</string>
87 """.replace(" " * 8, "\t") # Apple as well as plistlib.py output hard tabs
90 class TestPlistlib(unittest
.TestCase
):
94 os
.unlink(test_support
.TESTFN
)
101 aList
=["A", "B", 12, 32.5, [1, 2, 3]],
105 anotherString
="<hello & 'hi' there!>",
106 aUnicodeValue
=u
'M\xe4ssig, Ma\xdf',
109 deeperDict
=dict(a
=17, b
=32.5, c
=[1, 2, "text"]),
111 someData
= plistlib
.Data("<binary gunk>"),
112 someMoreData
= plistlib
.Data("<lots of binary gunk>\0\1\2\3" * 10),
113 nestedData
= [plistlib
.Data("<lots of binary gunk>\0\1\2\3" * 10)],
114 aDate
= datetime
.datetime(2004, 10, 26, 10, 33, 33),
116 pl
[u
'\xc5benraa'] = "That was a unicode key."
119 def test_create(self
):
121 self
.assertEqual(pl
["aString"], "Doodah")
122 self
.assertEqual(pl
["aDict"]["aFalseValue"], False)
126 plistlib
.writePlist(pl
, test_support
.TESTFN
)
127 pl2
= plistlib
.readPlist(test_support
.TESTFN
)
128 self
.assertEqual(dict(pl
), dict(pl2
))
130 def test_string(self
):
132 data
= plistlib
.writePlistToString(pl
)
133 pl2
= plistlib
.readPlistFromString(data
)
134 self
.assertEqual(dict(pl
), dict(pl2
))
135 data2
= plistlib
.writePlistToString(pl2
)
136 self
.assertEqual(data
, data2
)
138 def test_appleformatting(self
):
139 pl
= plistlib
.readPlistFromString(TESTDATA
)
140 data
= plistlib
.writePlistToString(pl
)
141 self
.assertEqual(data
, TESTDATA
,
142 "generated data was not identical to Apple's output")
144 def test_appleformattingfromliteral(self
):
146 pl2
= plistlib
.readPlistFromString(TESTDATA
)
147 self
.assertEqual(dict(pl
), dict(pl2
),
148 "generated data was not identical to Apple's output")
150 def test_stringio(self
):
151 from StringIO
import StringIO
154 plistlib
.writePlist(pl
, f
)
155 pl2
= plistlib
.readPlist(StringIO(f
.getvalue()))
156 self
.assertEqual(dict(pl
), dict(pl2
))
158 def test_cstringio(self
):
159 from cStringIO
import StringIO
162 plistlib
.writePlist(pl
, f
)
163 pl2
= plistlib
.readPlist(StringIO(f
.getvalue()))
164 self
.assertEqual(dict(pl
), dict(pl2
))
166 def test_controlcharacters(self
):
169 testString
= "string containing %s" % c
170 if i
>= 32 or c
in "\r\n\t":
171 # \r, \n and \t are the only legal control chars in XML
172 plistlib
.writePlistToString(testString
)
174 self
.assertRaises(ValueError,
175 plistlib
.writePlistToString
,
178 def test_nondictroot(self
):
180 test2
= [1, 2, 3, "abc"]
181 result1
= plistlib
.readPlistFromString(plistlib
.writePlistToString(test1
))
182 result2
= plistlib
.readPlistFromString(plistlib
.writePlistToString(test2
))
183 self
.assertEqual(test1
, result1
)
184 self
.assertEqual(test2
, result2
)
188 test_support
.run_unittest(TestPlistlib
)
191 if __name__
== '__main__':