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

Unified Diff: Lib/test/test_sunau.py

Issue 18878: Add support of the 'with' statement to sunau.open.
Patch Set: Created 6 years, 6 months ago
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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/sunau.py ('k') | Misc/NEWS » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/test/test_sunau.py Wed Sep 04 00:43:03 2013 +0300
+++ b/Lib/test/test_sunau.py Wed Sep 04 01:07:55 2013 +0300
@@ -1,4 +1,4 @@
-from test.support import run_unittest, TESTFN
+from test.support import TESTFN, unlink
import unittest
import pickle
import os
@@ -18,10 +18,7 @@
def tearDown(self):
if self.f is not None:
self.f.close()
- try:
- os.remove(TESTFN)
- except OSError:
- pass
+ unlink(TESTFN)
def test_lin(self):
self.f = sunau.open(TESTFN, 'w')
@@ -84,9 +81,49 @@
dump = pickle.dumps(params)
self.assertEqual(pickle.loads(dump), params)
+ def test_write_context_manager_calls_close(self):
+ # Close checks for a minimum header and will raise an error
+ # if it is not set, so this proves that close is called.
+ with self.assertRaises(sunau.Error):
+ with sunau.open(TESTFN, 'wb') as f:
+ pass
+ with self.assertRaises(sunau.Error):
+ with open(TESTFN, 'wb') as testfile:
+ with sunau.open(testfile):
+ pass
-def test_main():
- run_unittest(SunAUTest)
+ def test_context_manager_with_open_file(self):
+ with open(TESTFN, 'wb') as testfile:
+ with sunau.open(testfile) as f:
+ f.setnchannels(nchannels)
+ f.setsampwidth(sampwidth)
+ f.setframerate(framerate)
+ self.assertFalse(testfile.closed)
+ with open(TESTFN, 'rb') as testfile:
+ with sunau.open(testfile) as f:
+ self.assertFalse(f.getfp().closed)
+ params = f.getparams()
+ self.assertEqual(params[0], nchannels)
+ self.assertEqual(params[1], sampwidth)
+ self.assertEqual(params[2], framerate)
+ self.assertIsNone(f.getfp())
+ self.assertFalse(testfile.closed)
+
+ def test_context_manager_with_filename(self):
+ # If the file doesn't get closed, this test won't fail, but it will
+ # produce a resource leak warning.
+ with sunau.open(TESTFN, 'wb') as f:
+ f.setnchannels(nchannels)
+ f.setsampwidth(sampwidth)
+ f.setframerate(framerate)
+ with sunau.open(TESTFN) as f:
+ self.assertFalse(f.getfp().closed)
+ params = f.getparams()
+ self.assertEqual(params[0], nchannels)
+ self.assertEqual(params[1], sampwidth)
+ self.assertEqual(params[2], framerate)
+ self.assertIsNone(f.getfp())
+
if __name__ == "__main__":
unittest.main()
« 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+