PyXR

c:\python24\lib \ test \ test_plistlib.py



0001 # Copyright (C) 2003 Python Software Foundation
0002 
0003 import unittest
0004 import plistlib
0005 import os
0006 import time
0007 import datetime
0008 from test import test_support
0009 
0010 
0011 # This test data was generated through Cocoa's NSDictionary class
0012 TESTDATA = """<?xml version="1.0" encoding="UTF-8"?>
0013 <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
0014 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
0015 <plist version="1.0">
0016 <dict>
0017         <key>aDate</key>
0018         <date>2004-10-26T10:33:33Z</date>
0019         <key>aDict</key>
0020         <dict>
0021                 <key>aFalseValue</key>
0022                 <false/>
0023                 <key>aTrueValue</key>
0024                 <true/>
0025                 <key>aUnicodeValue</key>
0026                 <string>M\xc3\xa4ssig, Ma\xc3\x9f</string>
0027                 <key>anotherString</key>
0028                 <string>&lt;hello &amp; 'hi' there!&gt;</string>
0029                 <key>deeperDict</key>
0030                 <dict>
0031                         <key>a</key>
0032                         <integer>17</integer>
0033                         <key>b</key>
0034                         <real>32.5</real>
0035                         <key>c</key>
0036                         <array>
0037                                 <integer>1</integer>
0038                                 <integer>2</integer>
0039                                 <string>text</string>
0040                         </array>
0041                 </dict>
0042         </dict>
0043         <key>aFloat</key>
0044         <real>0.5</real>
0045         <key>aList</key>
0046         <array>
0047                 <string>A</string>
0048                 <string>B</string>
0049                 <integer>12</integer>
0050                 <real>32.5</real>
0051                 <array>
0052                         <integer>1</integer>
0053                         <integer>2</integer>
0054                         <integer>3</integer>
0055                 </array>
0056         </array>
0057         <key>aString</key>
0058         <string>Doodah</string>
0059         <key>anInt</key>
0060         <integer>728</integer>
0061         <key>nestedData</key>
0062         <array>
0063                 <data>
0064                 PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5r
0065                 PgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5
0066                 IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBi
0067                 aW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3Rz
0068                 IG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQID
0069                 PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw==
0070                 </data>
0071         </array>
0072         <key>someData</key>
0073         <data>
0074         PGJpbmFyeSBndW5rPg==
0075         </data>
0076         <key>someMoreData</key>
0077         <data>
0078         PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8
0079         bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxs
0080         b3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxv
0081         dHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90
0082         cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw==
0083         </data>
0084         <key>\xc3\x85benraa</key>
0085         <string>That was a unicode key.</string>
0086 </dict>
0087 </plist>
0088 """.replace(" " * 8, "\t")  # Apple as well as plistlib.py output hard tabs
0089 
0090 
0091 class TestPlistlib(unittest.TestCase):
0092 
0093     def tearDown(self):
0094         try:
0095             os.unlink(test_support.TESTFN)
0096         except:
0097             pass
0098 
0099     def _create(self):
0100         pl = dict(
0101             aString="Doodah",
0102             aList=["A", "B", 12, 32.5, [1, 2, 3]],
0103             aFloat = 0.5,
0104             anInt = 728,
0105             aDict=dict(
0106                 anotherString="<hello & 'hi' there!>",
0107                 aUnicodeValue=u'M\xe4ssig, Ma\xdf',
0108                 aTrueValue=True,
0109                 aFalseValue=False,
0110                 deeperDict=dict(a=17, b=32.5, c=[1, 2, "text"]),
0111             ),
0112             someData = plistlib.Data("<binary gunk>"),
0113             someMoreData = plistlib.Data("<lots of binary gunk>\0\1\2\3" * 10),
0114             nestedData = [plistlib.Data("<lots of binary gunk>\0\1\2\3" * 10)],
0115             aDate = datetime.datetime(2004, 10, 26, 10, 33, 33),
0116         )
0117         pl[u'\xc5benraa'] = "That was a unicode key."
0118         return pl
0119 
0120     def test_create(self):
0121         pl = self._create()
0122         self.assertEqual(pl["aString"], "Doodah")
0123         self.assertEqual(pl["aDict"]["aFalseValue"], False)
0124 
0125     def test_io(self):
0126         pl = self._create()
0127         plistlib.writePlist(pl, test_support.TESTFN)
0128         pl2 = plistlib.readPlist(test_support.TESTFN)
0129         self.assertEqual(dict(pl), dict(pl2))
0130 
0131     def test_string(self):
0132         pl = self._create()
0133         data = plistlib.writePlistToString(pl)
0134         pl2 = plistlib.readPlistFromString(data)
0135         self.assertEqual(dict(pl), dict(pl2))
0136         data2 = plistlib.writePlistToString(pl2)
0137         self.assertEqual(data, data2)
0138 
0139     def test_appleformatting(self):
0140         pl = plistlib.readPlistFromString(TESTDATA)
0141         data = plistlib.writePlistToString(pl)
0142         self.assertEqual(data, TESTDATA,
0143                          "generated data was not identical to Apple's output")
0144 
0145     def test_appleformattingfromliteral(self):
0146         pl = self._create()
0147         pl2 = plistlib.readPlistFromString(TESTDATA)
0148         self.assertEqual(dict(pl), dict(pl2),
0149                          "generated data was not identical to Apple's output")
0150 
0151     def test_stringio(self):
0152         from StringIO import StringIO
0153         f = StringIO()
0154         pl = self._create()
0155         plistlib.writePlist(pl, f)
0156         pl2 = plistlib.readPlist(StringIO(f.getvalue()))
0157         self.assertEqual(dict(pl), dict(pl2))
0158 
0159     def test_cstringio(self):
0160         from cStringIO import StringIO
0161         f = StringIO()
0162         pl = self._create()
0163         plistlib.writePlist(pl, f)
0164         pl2 = plistlib.readPlist(StringIO(f.getvalue()))
0165         self.assertEqual(dict(pl), dict(pl2))
0166 
0167 
0168 
0169 def test_main():
0170     test_support.run_unittest(TestPlistlib)
0171 
0172 
0173 if __name__ == '__main__':
0174     test_main()
0175 

Generated by PyXR 0.9.4
SourceForge.net Logo