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