test_applesingle.py 1.8 KB

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