/Lib/test/test_aifc.py

http://unladen-swallow.googlecode.com/ · Python · 69 lines · 53 code · 13 blank · 3 comment · 1 complexity · fdae2cd4a869eb37e43a3fd4f771a4bc MD5 · raw file

  1. from test.test_support import findfile, run_unittest
  2. import unittest
  3. import aifc
  4. class AIFCTest(unittest.TestCase):
  5. def setUp(self):
  6. self.sndfilepath = findfile('Sine-1000Hz-300ms.aif')
  7. def test_skipunknown(self):
  8. #Issue 2245
  9. #This file contains chunk types aifc doesn't recognize.
  10. f = aifc.open(self.sndfilepath)
  11. f.close()
  12. def test_params(self):
  13. f = aifc.open(self.sndfilepath)
  14. self.assertEqual(f.getnchannels(), 2)
  15. self.assertEqual(f.getsampwidth(), 2)
  16. self.assertEqual(f.getframerate(), 48000)
  17. self.assertEqual(f.getnframes(), 14400)
  18. self.assertEqual(f.getcomptype(), 'NONE')
  19. self.assertEqual(f.getcompname(), 'not compressed')
  20. self.assertEqual(f.getparams(), (2, 2, 48000, 14400, 'NONE', 'not compressed'))
  21. f.close()
  22. def test_read(self):
  23. f = aifc.open(self.sndfilepath)
  24. self.assertEqual(f.tell(), 0)
  25. self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
  26. f.rewind()
  27. pos0 = f.tell()
  28. self.assertEqual(pos0, 0)
  29. self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
  30. pos2 = f.tell()
  31. self.assertEqual(pos2, 2)
  32. self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad')
  33. f.setpos(pos2)
  34. self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad')
  35. f.setpos(pos0)
  36. self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
  37. f.close()
  38. #XXX Need more tests!
  39. def test_close(self):
  40. class Wrapfile(object):
  41. def __init__(self, file):
  42. self.file = open(file, 'rb')
  43. self.closed = False
  44. def close(self):
  45. self.file.close()
  46. self.closed = True
  47. def __getattr__(self, attr): return getattr(self.file, attr)
  48. testfile = Wrapfile(self.sndfilepath)
  49. f = self.f = aifc.open(testfile)
  50. self.assertEqual(testfile.closed, False)
  51. f.close()
  52. self.assertEqual(testfile.closed, True)
  53. def test_main():
  54. run_unittest(AIFCTest)
  55. if __name__ == "__main__":
  56. unittest.main()