Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(9)

Side by Side Diff: Lib/test/test_sunau.py

Issue 18878: Add support of the 'with' statement to sunau.open.
Patch Set: Created 6 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/sunau.py ('k') | Misc/NEWS » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 from test.support import run_unittest, TESTFN 1 from test.support import TESTFN, unlink
2 import unittest 2 import unittest
3 import pickle 3 import pickle
4 import os 4 import os
5 5
6 import sunau 6 import sunau
7 7
8 nchannels = 2 8 nchannels = 2
9 sampwidth = 2 9 sampwidth = 2
10 framerate = 8000 10 framerate = 8000
11 nframes = 100 11 nframes = 100
12 12
13 class SunAUTest(unittest.TestCase): 13 class SunAUTest(unittest.TestCase):
14 14
15 def setUp(self): 15 def setUp(self):
16 self.f = None 16 self.f = None
17 17
18 def tearDown(self): 18 def tearDown(self):
19 if self.f is not None: 19 if self.f is not None:
20 self.f.close() 20 self.f.close()
21 try: 21 unlink(TESTFN)
22 os.remove(TESTFN)
23 except OSError:
24 pass
25 22
26 def test_lin(self): 23 def test_lin(self):
27 self.f = sunau.open(TESTFN, 'w') 24 self.f = sunau.open(TESTFN, 'w')
28 self.f.setnchannels(nchannels) 25 self.f.setnchannels(nchannels)
29 self.f.setsampwidth(sampwidth) 26 self.f.setsampwidth(sampwidth)
30 self.f.setframerate(framerate) 27 self.f.setframerate(framerate)
31 self.f.setcomptype('NONE', 'not compressed') 28 self.f.setcomptype('NONE', 'not compressed')
32 output = b'\xff\x00\x12\xcc' * (nframes * nchannels * sampwidth // 4) 29 output = b'\xff\x00\x12\xcc' * (nframes * nchannels * sampwidth // 4)
33 self.f.writeframes(output) 30 self.f.writeframes(output)
34 self.f.close() 31 self.f.close()
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 params = self.f.getparams() 74 params = self.f.getparams()
78 self.assertEqual(params.nchannels, nchannels) 75 self.assertEqual(params.nchannels, nchannels)
79 self.assertEqual(params.sampwidth, sampwidth) 76 self.assertEqual(params.sampwidth, sampwidth)
80 self.assertEqual(params.framerate, framerate) 77 self.assertEqual(params.framerate, framerate)
81 self.assertEqual(params.nframes, nframes) 78 self.assertEqual(params.nframes, nframes)
82 self.assertEqual(params.comptype, 'ULAW') 79 self.assertEqual(params.comptype, 'ULAW')
83 80
84 dump = pickle.dumps(params) 81 dump = pickle.dumps(params)
85 self.assertEqual(pickle.loads(dump), params) 82 self.assertEqual(pickle.loads(dump), params)
86 83
84 def test_write_context_manager_calls_close(self):
85 # Close checks for a minimum header and will raise an error
86 # if it is not set, so this proves that close is called.
87 with self.assertRaises(sunau.Error):
88 with sunau.open(TESTFN, 'wb') as f:
89 pass
90 with self.assertRaises(sunau.Error):
91 with open(TESTFN, 'wb') as testfile:
92 with sunau.open(testfile):
93 pass
87 94
88 def test_main(): 95 def test_context_manager_with_open_file(self):
89 run_unittest(SunAUTest) 96 with open(TESTFN, 'wb') as testfile:
97 with sunau.open(testfile) as f:
98 f.setnchannels(nchannels)
99 f.setsampwidth(sampwidth)
100 f.setframerate(framerate)
101 self.assertFalse(testfile.closed)
102 with open(TESTFN, 'rb') as testfile:
103 with sunau.open(testfile) as f:
104 self.assertFalse(f.getfp().closed)
105 params = f.getparams()
106 self.assertEqual(params[0], nchannels)
107 self.assertEqual(params[1], sampwidth)
108 self.assertEqual(params[2], framerate)
109 self.assertIsNone(f.getfp())
110 self.assertFalse(testfile.closed)
111
112 def test_context_manager_with_filename(self):
113 # If the file doesn't get closed, this test won't fail, but it will
114 # produce a resource leak warning.
115 with sunau.open(TESTFN, 'wb') as f:
116 f.setnchannels(nchannels)
117 f.setsampwidth(sampwidth)
118 f.setframerate(framerate)
119 with sunau.open(TESTFN) as f:
120 self.assertFalse(f.getfp().closed)
121 params = f.getparams()
122 self.assertEqual(params[0], nchannels)
123 self.assertEqual(params[1], sampwidth)
124 self.assertEqual(params[2], framerate)
125 self.assertIsNone(f.getfp())
126
90 127
91 if __name__ == "__main__": 128 if __name__ == "__main__":
92 unittest.main() 129 unittest.main()
OLDNEW
« no previous file with comments | « Lib/sunau.py ('k') | Misc/NEWS » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+