From fc93e17598f80a5362ef6096cbb4571e64d46381 Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Tue, 26 Oct 2004 11:02:08 +0000 Subject: [PATCH] - Added tests for the string load/dump function. - Added a chunk of plist data as generated by Cocoa's NSDictionary and verify we output the same (including formatting) - Changed the "literal" plist code to match the raw test data --- Lib/test/test_plistlib.py | 127 +++++++++++++++++++++++++++++++++----- 1 file changed, 113 insertions(+), 14 deletions(-) diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py index 4a2e48d06f5..b2a3b7de3fc 100644 --- a/Lib/test/test_plistlib.py +++ b/Lib/test/test_plistlib.py @@ -3,8 +3,91 @@ import unittest import plistlib import os +import time +import datetime from test import test_support + +# This test data was generated through Cocoa's NSDictionary class +TESTDATA = """ + + + + aDate + 2004-10-26T10:33:33Z + aDict + + aFalseValue + + aTrueValue + + aUnicodeValue + M\xc3\xa4ssig, Ma\xc3\x9f + anotherString + <hello & 'hi' there!> + deeperDict + + a + 17 + b + 32.5 + c + + 1 + 2 + text + + + + aFloat + 0.5 + aList + + A + B + 12 + 32.5 + + 1 + 2 + 3 + + + aString + Doodah + anInt + 728 + nestedData + + + PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5r + PgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5 + IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBi + aW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3Rz + IG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQID + PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw== + + + someData + + PGJpbmFyeSBndW5rPg== + + someMoreData + + PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8 + bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxs + b3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxv + dHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90 + cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw== + + \xc3\x85benraa + That was a unicode key. + + +""".replace(" " * 8, "\t") # Apple as well as plistlib.py output hard tabs + + class TestPlistlib(unittest.TestCase): def tearDown(self): @@ -14,28 +97,24 @@ class TestPlistlib(unittest.TestCase): pass def _create(self): - pl = plistlib.Dict( + pl = dict( aString="Doodah", - aList=["A", "B", 12, 32.1, [1, 2, 3]], - aFloat = 0.1, + aList=["A", "B", 12, 32.5, [1, 2, 3]], + aFloat = 0.5, anInt = 728, - aDict=plistlib.Dict( - anotherString="", + aDict=dict( + anotherString="", aUnicodeValue=u'M\xe4ssig, Ma\xdf', aTrueValue=True, aFalseValue=False, + deeperDict=dict(a=17, b=32.5, c=[1, 2, "text"]), ), someData = plistlib.Data(""), - someMoreData = plistlib.Data("" * 10), + someMoreData = plistlib.Data("\0\1\2\3" * 10), + nestedData = [plistlib.Data("\0\1\2\3" * 10)], + aDate = datetime.datetime(2004, 10, 26, 10, 33, 33), ) - pl['anotherInt'] = 42 - try: - from xml.utils.iso8601 import parse - import time - except ImportError: - pass - else: - pl['aDate'] = plistlib.Date(time.mktime(time.gmtime())) + pl[u'\xc5benraa'] = "That was a unicode key." return pl def test_create(self): @@ -49,6 +128,26 @@ class TestPlistlib(unittest.TestCase): pl2 = plistlib.readPlist(test_support.TESTFN) self.assertEqual(dict(pl), dict(pl2)) + def test_string(self): + pl = self._create() + data = plistlib.writePlistToString(pl) + pl2 = plistlib.readPlistFromString(data) + self.assertEqual(dict(pl), dict(pl2)) + data2 = plistlib.writePlistToString(pl2) + self.assertEqual(data, data2) + + def test_appleformatting(self): + pl = plistlib.readPlistFromString(TESTDATA) + data = plistlib.writePlistToString(pl) + self.assertEqual(data, TESTDATA, + "generated data was not identical to Apple's output") + + def test_appleformattingfromliteral(self): + pl = self._create() + pl2 = plistlib.readPlistFromString(TESTDATA) + self.assertEqual(dict(pl), dict(pl2), + "generated data was not identical to Apple's output") + def test_stringio(self): from StringIO import StringIO f = StringIO()