/Lib/test/test_applesingle.py

http://unladen-swallow.googlecode.com/ · Python · 71 lines · 58 code · 12 blank · 1 comment · 7 complexity · 9d183a2b9243c3a84b55bedb16902c66 MD5 · raw file

  1. # Copyright (C) 2003 Python Software Foundation
  2. import unittest
  3. import macostools
  4. import Carbon.File
  5. import MacOS
  6. import os
  7. from test import test_support
  8. import struct
  9. import applesingle
  10. AS_MAGIC=0x00051600
  11. AS_VERSION=0x00020000
  12. dataforkdata = 'hello\r\0world\n'
  13. resourceforkdata = 'goodbye\ncruel\0world\r'
  14. applesingledata = struct.pack(">ll16sh", AS_MAGIC, AS_VERSION, "foo", 2) + \
  15. struct.pack(">llllll", 1, 50, len(dataforkdata),
  16. 2, 50+len(dataforkdata), len(resourceforkdata)) + \
  17. dataforkdata + \
  18. resourceforkdata
  19. TESTFN2 = test_support.TESTFN + '2'
  20. class TestApplesingle(unittest.TestCase):
  21. def setUp(self):
  22. fp = open(test_support.TESTFN, 'w')
  23. fp.write(applesingledata)
  24. fp.close()
  25. def tearDown(self):
  26. try:
  27. os.unlink(test_support.TESTFN)
  28. except:
  29. pass
  30. try:
  31. os.unlink(TESTFN2)
  32. except:
  33. pass
  34. def compareData(self, isrf, data):
  35. if isrf:
  36. fp = MacOS.openrf(TESTFN2, '*rb')
  37. else:
  38. fp = open(TESTFN2, 'rb')
  39. filedata = fp.read(1000)
  40. self.assertEqual(data, filedata)
  41. def test_applesingle(self):
  42. try:
  43. os.unlink(TESTFN2)
  44. except:
  45. pass
  46. applesingle.decode(test_support.TESTFN, TESTFN2)
  47. self.compareData(False, dataforkdata)
  48. self.compareData(True, resourceforkdata)
  49. def test_applesingle_resonly(self):
  50. try:
  51. os.unlink(TESTFN2)
  52. except:
  53. pass
  54. applesingle.decode(test_support.TESTFN, TESTFN2, resonly=True)
  55. self.compareData(False, resourceforkdata)
  56. def test_main():
  57. test_support.run_unittest(TestApplesingle)
  58. if __name__ == '__main__':
  59. test_main()