Merge pull request #3158 from radarhere/exception

More specific Exception testing
This commit is contained in:
Hugo 2018-06-14 09:22:17 +03:00 committed by GitHub
commit a5bbd7655a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 17 deletions

View File

@ -439,17 +439,17 @@ class TestFileJpeg(PillowTestCase):
self._n_qtables_helper(4, "Tests/images/pil_sample_cmyk.jpg")
# not a sequence
self.assertRaises(Exception, self.roundtrip, im, qtables='a')
self.assertRaises(ValueError, self.roundtrip, im, qtables='a')
# sequence wrong length
self.assertRaises(Exception, self.roundtrip, im, qtables=[])
self.assertRaises(ValueError, self.roundtrip, im, qtables=[])
# sequence wrong length
self.assertRaises(Exception,
self.assertRaises(ValueError,
self.roundtrip, im, qtables=[1, 2, 3, 4, 5])
# qtable entry not a sequence
self.assertRaises(Exception, self.roundtrip, im, qtables=[1])
self.assertRaises(ValueError, self.roundtrip, im, qtables=[1])
# qtable entry has wrong number of items
self.assertRaises(Exception,
self.assertRaises(ValueError,
self.roundtrip, im, qtables=[[1, 2, 3, 4]])
@unittest.skipUnless(djpeg_available(), "djpeg not available")
@ -599,7 +599,7 @@ class TestFileCloseW32(PillowTestCase):
im = Image.open(tmpfile)
fp = im.fp
self.assertFalse(fp.closed)
self.assertRaises(Exception, os.remove, tmpfile)
self.assertRaises(WindowsError, os.remove, tmpfile)
im.load()
self.assertTrue(fp.closed)
# this should not fail, as load should have closed the file.

View File

@ -505,7 +505,7 @@ class TestFileTiffW32(PillowTestCase):
im = Image.open(tmpfile)
fp = im.fp
self.assertFalse(fp.closed)
self.assertRaises(Exception, os.remove, tmpfile)
self.assertRaises(WindowsError, os.remove, tmpfile)
im.load()
self.assertTrue(fp.closed)

View File

@ -39,7 +39,7 @@ class TestImageQuantize(PillowTestCase):
def test_rgba_quantize(self):
image = hopper('RGBA')
image.quantize()
self.assertRaises(Exception, image.quantize, method=0)
self.assertRaises(ValueError, image.quantize, method=0)
def test_quantize(self):
image = Image.open('Tests/images/caption_6_33_22.png').convert('RGB')

View File

@ -80,9 +80,15 @@ class MorphTests(PillowTestCase):
def test_no_operator_loaded(self):
mop = ImageMorph.MorphOp()
self.assertRaises(Exception, mop.apply, None)
self.assertRaises(Exception, mop.match, None)
self.assertRaises(Exception, mop.save_lut, None)
with self.assertRaises(Exception) as e:
mop.apply(None)
self.assertEqual(str(e.exception), 'No operator loaded')
with self.assertRaises(Exception) as e:
mop.match(None)
self.assertEqual(str(e.exception), 'No operator loaded')
with self.assertRaises(Exception) as e:
mop.save_lut(None)
self.assertEqual(str(e.exception), 'No operator loaded')
# Test the named patterns
def test_erosion8(self):
@ -213,9 +219,18 @@ class MorphTests(PillowTestCase):
im = hopper('RGB')
mop = ImageMorph.MorphOp(op_name="erosion8")
self.assertRaises(Exception, mop.apply, im)
self.assertRaises(Exception, mop.match, im)
self.assertRaises(Exception, mop.get_on_pixels, im)
with self.assertRaises(Exception) as e:
mop.apply(im)
self.assertEqual(str(e.exception),
'Image must be binary, meaning it must use mode L')
with self.assertRaises(Exception) as e:
mop.match(im)
self.assertEqual(str(e.exception),
'Image must be binary, meaning it must use mode L')
with self.assertRaises(Exception) as e:
mop.get_on_pixels(im)
self.assertEqual(str(e.exception),
'Image must be binary, meaning it must use mode L')
def test_add_patterns(self):
# Arrange
@ -248,7 +263,10 @@ class MorphTests(PillowTestCase):
lb.add_patterns(new_patterns)
# Act / Assert
self.assertRaises(Exception, lb.build_lut)
with self.assertRaises(Exception) as e:
lb.build_lut()
self.assertEqual(str(e.exception),
'Syntax error in pattern "a pattern with a syntax error"')
def test_load_invalid_mrl(self):
# Arrange
@ -256,7 +274,10 @@ class MorphTests(PillowTestCase):
mop = ImageMorph.MorphOp()
# Act / Assert
self.assertRaises(Exception, mop.load_lut, invalid_mrl)
with self.assertRaises(Exception) as e:
mop.load_lut(invalid_mrl)
self.assertEqual(str(e.exception),
'Wrong size operator file!')
def test_roundtrip_mrl(self):
# Arrange

View File

@ -683,7 +683,7 @@ def _save(im, fp, filename):
for idx, table in enumerate(qtables):
try:
if len(table) != 64:
raise
raise TypeError
table = array.array('B', table)
except TypeError:
raise ValueError("Invalid quantization table")