1 # Copyright (C) 2003 Python Software Foundation
8 from test
import test_support
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">
18 <date>2004-10-26T10:33:33Z</date>
21 <key>aFalseValue</key>
25 <key>aUnicodeValue</key>
26 <string>M\xc3\xa4ssig, Ma\xc3\x9f</string>
27 <key>anotherString</key>
28 <string><hello & 'hi' there!></string>
58 <string>Doodah</string>
60 <integer>728</integer>
64 PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5r
65 PgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5
66 IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBi
67 aW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3Rz
68 IG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQID
69 PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw==
76 <key>someMoreData</key>
78 PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8
79 bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxs
80 b3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxv
81 dHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90
82 cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw==
84 <key>\xc3\x85benraa</key>
85 <string>That was a unicode key.</string>
88 """.replace(" " * 8, "\t") # Apple as well as plistlib.py output hard tabs
91 class TestPlistlib(unittest
.TestCase
):
95 os
.unlink(test_support
.TESTFN
)
102 aList
=["A", "B", 12, 32.5, [1, 2, 3]],
106 anotherString
="<hello & 'hi' there!>",
107 aUnicodeValue
=u
'M\xe4ssig, Ma\xdf',
110 deeperDict
=dict(a
=17, b
=32.5, c
=[1, 2, "text"]),
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),
117 pl
[u
'\xc5benraa'] = "That was a unicode key."
120 def test_create(self
):
122 self
.assertEqual(pl
["aString"], "Doodah")
123 self
.assertEqual(pl
["aDict"]["aFalseValue"], False)
127 plistlib
.writePlist(pl
, test_support
.TESTFN
)
128 pl2
= plistlib
.readPlist(test_support
.TESTFN
)
129 self
.assertEqual(dict(pl
), dict(pl2
))
131 def test_string(self
):
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
)
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")
145 def test_appleformattingfromliteral(self
):
147 pl2
= plistlib
.readPlistFromString(TESTDATA
)
148 self
.assertEqual(dict(pl
), dict(pl2
),
149 "generated data was not identical to Apple's output")
151 def test_stringio(self
):
152 from StringIO
import StringIO
155 plistlib
.writePlist(pl
, f
)
156 pl2
= plistlib
.readPlist(StringIO(f
.getvalue()))
157 self
.assertEqual(dict(pl
), dict(pl2
))
159 def test_cstringio(self
):
160 from cStringIO
import StringIO
163 plistlib
.writePlist(pl
, f
)
164 pl2
= plistlib
.readPlist(StringIO(f
.getvalue()))
165 self
.assertEqual(dict(pl
), dict(pl2
))
167 def test_controlcharacters(self
):
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
)
175 self
.assertRaises(ValueError,
176 plistlib
.writePlistToString
,
179 def test_nondictroot(self
):
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
)
189 test_support
.run_unittest(TestPlistlib
)
192 if __name__
== '__main__':