33import unittest
44import plistlib
55import os
6+ import time
7+ import datetime
68from test import test_support
79
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 \xa4 ssig, 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 \x85 benraa</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+
891class TestPlistlib (unittest .TestCase ):
992
1093 def tearDown (self ):
@@ -14,28 +97,24 @@ def tearDown(self):
1497 pass
1598
1699 def _create (self ):
17- pl = plistlib . Dict (
100+ pl = dict (
18101 aString = "Doodah" ,
19- aList = ["A" , "B" , 12 , 32.1 , [1 , 2 , 3 ]],
20- aFloat = 0.1 ,
102+ aList = ["A" , "B" , 12 , 32.5 , [1 , 2 , 3 ]],
103+ aFloat = 0.5 ,
21104 anInt = 728 ,
22- aDict = plistlib . Dict (
23- anotherString = "<hello & hi there!>" ,
105+ aDict = dict (
106+ anotherString = "<hello & 'hi' there!>" ,
24107 aUnicodeValue = u'M\xe4 ssig, Ma\xdf ' ,
25108 aTrueValue = True ,
26109 aFalseValue = False ,
110+ deeperDict = dict (a = 17 , b = 32.5 , c = [1 , 2 , "text" ]),
27111 ),
28112 someData = plistlib .Data ("<binary gunk>" ),
29- someMoreData = plistlib .Data ("<lots of binary gunk>" * 10 ),
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 ),
30116 )
31- pl ['anotherInt' ] = 42
32- try :
33- from xml .utils .iso8601 import parse
34- import time
35- except ImportError :
36- pass
37- else :
38- pl ['aDate' ] = plistlib .Date (time .mktime (time .gmtime ()))
117+ pl [u'\xc5 benraa' ] = "That was a unicode key."
39118 return pl
40119
41120 def test_create (self ):
@@ -49,6 +128,26 @@ def test_io(self):
49128 pl2 = plistlib .readPlist (test_support .TESTFN )
50129 self .assertEqual (dict (pl ), dict (pl2 ))
51130
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+
52151 def test_stringio (self ):
53152 from StringIO import StringIO
54153 f = StringIO ()
0 commit comments