/Lib/test/test_plistlib.py

http://unladen-swallow.googlecode.com/ · Python · 192 lines · 169 code · 20 blank · 3 comment · 6 complexity · 72a6ae775e1de634b41a0433fbcb2d0b MD5 · raw file

  1. # Copyright (C) 2003 Python Software Foundation
  2. import unittest
  3. import plistlib
  4. import os
  5. import datetime
  6. from test import test_support
  7. # This test data was generated through Cocoa's NSDictionary class
  8. TESTDATA = """<?xml version="1.0" encoding="UTF-8"?>
  9. <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
  10. "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  11. <plist version="1.0">
  12. <dict>
  13. <key>aDate</key>
  14. <date>2004-10-26T10:33:33Z</date>
  15. <key>aDict</key>
  16. <dict>
  17. <key>aFalseValue</key>
  18. <false/>
  19. <key>aTrueValue</key>
  20. <true/>
  21. <key>aUnicodeValue</key>
  22. <string>M\xc3\xa4ssig, Ma\xc3\x9f</string>
  23. <key>anotherString</key>
  24. <string>&lt;hello &amp; 'hi' there!&gt;</string>
  25. <key>deeperDict</key>
  26. <dict>
  27. <key>a</key>
  28. <integer>17</integer>
  29. <key>b</key>
  30. <real>32.5</real>
  31. <key>c</key>
  32. <array>
  33. <integer>1</integer>
  34. <integer>2</integer>
  35. <string>text</string>
  36. </array>
  37. </dict>
  38. </dict>
  39. <key>aFloat</key>
  40. <real>0.5</real>
  41. <key>aList</key>
  42. <array>
  43. <string>A</string>
  44. <string>B</string>
  45. <integer>12</integer>
  46. <real>32.5</real>
  47. <array>
  48. <integer>1</integer>
  49. <integer>2</integer>
  50. <integer>3</integer>
  51. </array>
  52. </array>
  53. <key>aString</key>
  54. <string>Doodah</string>
  55. <key>anInt</key>
  56. <integer>728</integer>
  57. <key>nestedData</key>
  58. <array>
  59. <data>
  60. PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5r
  61. PgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5
  62. IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBi
  63. aW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3Rz
  64. IG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQID
  65. PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw==
  66. </data>
  67. </array>
  68. <key>someData</key>
  69. <data>
  70. PGJpbmFyeSBndW5rPg==
  71. </data>
  72. <key>someMoreData</key>
  73. <data>
  74. PGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8
  75. bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAzxs
  76. b3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxv
  77. dHMgb2YgYmluYXJ5IGd1bms+AAECAzxsb3RzIG9mIGJpbmFyeSBndW5rPgABAgM8bG90
  78. cyBvZiBiaW5hcnkgZ3Vuaz4AAQIDPGxvdHMgb2YgYmluYXJ5IGd1bms+AAECAw==
  79. </data>
  80. <key>\xc3\x85benraa</key>
  81. <string>That was a unicode key.</string>
  82. </dict>
  83. </plist>
  84. """.replace(" " * 8, "\t") # Apple as well as plistlib.py output hard tabs
  85. class TestPlistlib(unittest.TestCase):
  86. def tearDown(self):
  87. try:
  88. os.unlink(test_support.TESTFN)
  89. except:
  90. pass
  91. def _create(self):
  92. pl = dict(
  93. aString="Doodah",
  94. aList=["A", "B", 12, 32.5, [1, 2, 3]],
  95. aFloat = 0.5,
  96. anInt = 728,
  97. aDict=dict(
  98. anotherString="<hello & 'hi' there!>",
  99. aUnicodeValue=u'M\xe4ssig, Ma\xdf',
  100. aTrueValue=True,
  101. aFalseValue=False,
  102. deeperDict=dict(a=17, b=32.5, c=[1, 2, "text"]),
  103. ),
  104. someData = plistlib.Data("<binary gunk>"),
  105. someMoreData = plistlib.Data("<lots of binary gunk>\0\1\2\3" * 10),
  106. nestedData = [plistlib.Data("<lots of binary gunk>\0\1\2\3" * 10)],
  107. aDate = datetime.datetime(2004, 10, 26, 10, 33, 33),
  108. )
  109. pl[u'\xc5benraa'] = "That was a unicode key."
  110. return pl
  111. def test_create(self):
  112. pl = self._create()
  113. self.assertEqual(pl["aString"], "Doodah")
  114. self.assertEqual(pl["aDict"]["aFalseValue"], False)
  115. def test_io(self):
  116. pl = self._create()
  117. plistlib.writePlist(pl, test_support.TESTFN)
  118. pl2 = plistlib.readPlist(test_support.TESTFN)
  119. self.assertEqual(dict(pl), dict(pl2))
  120. def test_string(self):
  121. pl = self._create()
  122. data = plistlib.writePlistToString(pl)
  123. pl2 = plistlib.readPlistFromString(data)
  124. self.assertEqual(dict(pl), dict(pl2))
  125. data2 = plistlib.writePlistToString(pl2)
  126. self.assertEqual(data, data2)
  127. def test_appleformatting(self):
  128. pl = plistlib.readPlistFromString(TESTDATA)
  129. data = plistlib.writePlistToString(pl)
  130. self.assertEqual(data, TESTDATA,
  131. "generated data was not identical to Apple's output")
  132. def test_appleformattingfromliteral(self):
  133. pl = self._create()
  134. pl2 = plistlib.readPlistFromString(TESTDATA)
  135. self.assertEqual(dict(pl), dict(pl2),
  136. "generated data was not identical to Apple's output")
  137. def test_stringio(self):
  138. from StringIO import StringIO
  139. f = StringIO()
  140. pl = self._create()
  141. plistlib.writePlist(pl, f)
  142. pl2 = plistlib.readPlist(StringIO(f.getvalue()))
  143. self.assertEqual(dict(pl), dict(pl2))
  144. def test_cstringio(self):
  145. from cStringIO import StringIO
  146. f = StringIO()
  147. pl = self._create()
  148. plistlib.writePlist(pl, f)
  149. pl2 = plistlib.readPlist(StringIO(f.getvalue()))
  150. self.assertEqual(dict(pl), dict(pl2))
  151. def test_controlcharacters(self):
  152. for i in range(128):
  153. c = chr(i)
  154. testString = "string containing %s" % c
  155. if i >= 32 or c in "\r\n\t":
  156. # \r, \n and \t are the only legal control chars in XML
  157. plistlib.writePlistToString(testString)
  158. else:
  159. self.assertRaises(ValueError,
  160. plistlib.writePlistToString,
  161. testString)
  162. def test_nondictroot(self):
  163. test1 = "abc"
  164. test2 = [1, 2, 3, "abc"]
  165. result1 = plistlib.readPlistFromString(plistlib.writePlistToString(test1))
  166. result2 = plistlib.readPlistFromString(plistlib.writePlistToString(test2))
  167. self.assertEqual(test1, result1)
  168. self.assertEqual(test2, result2)
  169. def test_main():
  170. test_support.run_unittest(TestPlistlib)
  171. if __name__ == '__main__':
  172. test_main()