Added tests

This commit is contained in:
Andrew Murray 2017-09-01 20:36:51 +10:00
parent c10ad19a2b
commit 371933a597
12 changed files with 159 additions and 42 deletions

View File

@ -375,29 +375,31 @@ class TestFileGif(PillowTestCase):
def test_version(self):
out = self.tempfile('temp.gif')
def assertVersionAfterSave(im, version):
im.save(out)
reread = Image.open(out)
self.assertEqual(reread.info["version"], version)
# Test that GIF87a is used by default
im = Image.new('L', (100, 100), '#000')
im.save(out)
reread = Image.open(out)
self.assertEqual(reread.info["version"], b"GIF87a")
assertVersionAfterSave(im, b"GIF87a")
# Test setting the version to 89a
im = Image.new('L', (100, 100), '#000')
im.info["version"] = b"89a"
assertVersionAfterSave(im, b"GIF89a")
# Test that adding a GIF89a feature changes the version
im.info["transparency"] = 1
im.save(out)
reread = Image.open(out)
self.assertEqual(reread.info["version"], b"GIF89a")
assertVersionAfterSave(im, b"GIF89a")
# Test that a GIF87a image is also saved in that format
im = Image.open("Tests/images/test.colors.gif")
im.save(out)
reread = Image.open(out)
self.assertEqual(reread.info["version"], b"GIF87a")
assertVersionAfterSave(im, b"GIF87a")
# Test that a GIF89a image is also saved in that format
im.info["version"] = b"GIF89a"
im.save(out)
reread = Image.open(out)
self.assertEqual(reread.info["version"], b"GIF87a")
assertVersionAfterSave(im, b"GIF87a")
def test_append_images(self):
out = self.tempfile('temp.gif')

View File

@ -1,7 +1,8 @@
from helper import unittest, PillowTestCase
from PIL import Image
from PIL import Image, IcnsImagePlugin
import io
import sys
# sample icon file
@ -82,6 +83,23 @@ class TestFileIcns(PillowTestCase):
self.assertEqual(im2.mode, 'RGBA')
self.assertEqual(im2.size, (wr, hr))
def test_getimage(self):
with open(TEST_FILE, 'rb') as fp:
icns_file = IcnsImagePlugin.IcnsFile(fp)
im = icns_file.getimage()
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (1024, 1024))
im = icns_file.getimage((512, 512))
self.assertEqual(im.mode, "RGBA")
self.assertEqual(im.size, (512, 512))
def test_not_an_icns_file(self):
with io.BytesIO(b'invalid\n') as fp:
self.assertRaises(SyntaxError,
lambda: IcnsImagePlugin.IcnsFile(fp))
if __name__ == '__main__':
unittest.main()

View File

@ -43,12 +43,18 @@ class TestFileIm(PillowTestCase):
self.assertLess(im.tell(), n_frames)
def test_roundtrip(self):
out = self.tempfile('temp.im')
im = hopper()
im.save(out)
reread = Image.open(out)
for mode in ["RGB", "P"]:
out = self.tempfile('temp.im')
im = hopper(mode)
im.save(out)
reread = Image.open(out)
self.assert_image_equal(reread, im)
self.assert_image_equal(reread, im)
def test_save_unsupported_mode(self):
out = self.tempfile('temp.im')
im = hopper("HSV")
self.assertRaises(ValueError, lambda: im.save(out))
def test_invalid_file(self):
invalid_file = "Tests/images/flower.jpg"
@ -56,6 +62,9 @@ class TestFileIm(PillowTestCase):
self.assertRaises(SyntaxError,
lambda: ImImagePlugin.ImImageFile(invalid_file))
def test_number(self):
self.assertEqual(1.2, ImImagePlugin.number("1.2"))
if __name__ == '__main__':
unittest.main()

View File

@ -531,6 +531,12 @@ class TestFilePng(PillowTestCase):
# pHYs - before IDAT
self.assertLess(chunks.index(b"pHYs"), chunks.index(b"IDAT"))
def test_getchunks(self):
im = hopper()
chunks = PngImagePlugin.getchunks(im)
self.assertEqual(len(chunks), 3)
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS")
class TestTruncatedPngPLeaks(PillowTestCase):

View File

@ -85,6 +85,10 @@ class TestFileTiff(PillowTestCase):
im = Image.open("Tests/images/copyleft.tiff")
self.assertEqual(im.mode, 'RGB')
def test_set_legacy_api(self):
with self.assertRaises(Exception):
ImageFileDirectory_v2.legacy_api = None
def test_xyres_tiff(self):
filename = "Tests/images/pil168.tif"
im = Image.open(filename)

View File

@ -1,5 +1,7 @@
from helper import unittest, PillowTestCase, hopper
from PIL import Image
from PIL import WmfImagePlugin
class TestFileWmf(PillowTestCase):
@ -26,6 +28,23 @@ class TestFileWmf(PillowTestCase):
imref.load()
self.assert_image_similar(im, imref, 2.0)
def test_register_handler(self):
class TestHandler:
methodCalled = False
def save(self, im, fp, filename):
self.methodCalled = True
handler = TestHandler()
WmfImagePlugin.register_handler(handler)
im = hopper()
tmpfile = self.tempfile("temp.wmf")
im.save(tmpfile)
self.assertTrue(handler.methodCalled)
# Restore the state before this test
WmfImagePlugin.register_handler(None)
def test_save(self):
im = hopper()

View File

@ -70,6 +70,9 @@ class TestImage(PillowTestCase):
im = io.BytesIO(b'')
self.assertRaises(IOError, lambda: Image.open(im))
def test_bad_mode(self):
self.assertRaises(ValueError, lambda: Image.open("filename", "bad mode"))
@unittest.skipIf(sys.version_info < (3, 4),
"pathlib only available in Python 3.4 or later")
def test_pathlib(self):
@ -273,6 +276,20 @@ class TestImage(PillowTestCase):
target.crop((32, 32, 96, 96)))
self.assertEqual(source.size, (128, 128))
# errors
self.assertRaises(ValueError,
lambda: source.alpha_composite(over, "invalid source"))
self.assertRaises(ValueError,
lambda: source.alpha_composite(over, (0, 0), "invalid destination"))
self.assertRaises(ValueError,
lambda: source.alpha_composite(over, (0)))
self.assertRaises(ValueError,
lambda: source.alpha_composite(over, (0, 0), (0)))
self.assertRaises(ValueError,
lambda: source.alpha_composite(over, (0, -1)))
self.assertRaises(ValueError,
lambda: source.alpha_composite(over, (0, 0), (0, -1)))
def test_registered_extensions_uninitialized(self):
# Arrange
Image._initialized = 0
@ -445,6 +462,11 @@ class TestImage(PillowTestCase):
target = Image.open(target_file).convert(mode)
self.assert_image_equal(im, target)
def test_remap_palette(self):
# Test illegal image mode
im = hopper()
self.assertRaises(ValueError, lambda: im.remap_palette(None))
class MockEncoder(object):
pass

View File

@ -11,6 +11,9 @@ class TestImageFromBytes(PillowTestCase):
self.assert_image_equal(im1, im2)
def test_not_implemented(self):
self.assertRaises(NotImplementedError, Image.fromstring)
if __name__ == '__main__':
unittest.main()

View File

@ -112,6 +112,10 @@ class TestImageResize(PillowTestCase):
resize(mode, (112, 103))
resize(mode, (188, 214))
# Test unknown resampling filter
im = hopper()
self.assertRaises(ValueError, lambda: im.resize((10, 10), "unknown"))
if __name__ == '__main__':
unittest.main()

View File

@ -144,6 +144,12 @@ class TestImageCms(PillowTestCase):
'IEC 61966-2.1 Default RGB colour space - sRGB')
def test_exceptions(self):
# Test mode mismatch
psRGB = ImageCms.createProfile("sRGB")
pLab = ImageCms.createProfile("LAB")
t = ImageCms.buildTransform(pLab, psRGB, "LAB", "RGB")
self.assertRaises(ValueError, lambda: t.apply_in_place(hopper("RGBA")))
# the procedural pyCMS API uses PyCMSError for all sorts of errors
self.assertRaises(
ImageCms.PyCMSError,
@ -167,6 +173,14 @@ class TestImageCms(PillowTestCase):
ImageCms.createProfile("LAB", 5000)
ImageCms.createProfile("LAB", 6500)
def test_unsupported_color_space(self):
self.assertRaises(ImageCms.PyCMSError,
lambda: ImageCms.createProfile("unsupported"))
def test_invalid_color_temperature(self):
self.assertRaises(ImageCms.PyCMSError,
lambda: ImageCms.createProfile("LAB", "invalid"))
def test_simple_lab(self):
i = Image.new('RGB', (10, 10), (128, 128, 128))

View File

@ -2,20 +2,18 @@ from helper import unittest, PillowTestCase
from PIL import ImagePalette, Image
ImagePalette = ImagePalette.ImagePalette
class TestImagePalette(PillowTestCase):
def test_sanity(self):
ImagePalette("RGB", list(range(256))*3)
self.assertRaises(
ValueError, lambda: ImagePalette("RGB", list(range(256))*2))
ImagePalette.ImagePalette("RGB", list(range(256))*3)
self.assertRaises(ValueError,
lambda: ImagePalette.ImagePalette("RGB", list(range(256))*2))
def test_getcolor(self):
palette = ImagePalette()
palette = ImagePalette.ImagePalette()
test_map = {}
for i in range(256):
@ -24,34 +22,34 @@ class TestImagePalette(PillowTestCase):
self.assertEqual(len(test_map), 256)
self.assertRaises(ValueError, lambda: palette.getcolor((1, 2, 3)))
# Test unknown color specifier
self.assertRaises(ValueError, lambda: palette.getcolor("unknown"))
def test_file(self):
palette = ImagePalette("RGB", list(range(256))*3)
palette = ImagePalette.ImagePalette("RGB", list(range(256))*3)
f = self.tempfile("temp.lut")
palette.save(f)
from PIL.ImagePalette import load, raw
p = load(f)
p = ImagePalette.load(f)
# load returns raw palette information
self.assertEqual(len(p[0]), 768)
self.assertEqual(p[1], "RGB")
p = raw(p[1], p[0])
self.assertIsInstance(p, ImagePalette)
p = ImagePalette.raw(p[1], p[0])
self.assertIsInstance(p, ImagePalette.ImagePalette)
self.assertEqual(p.palette, palette.tobytes())
def test_make_linear_lut(self):
# Arrange
from PIL.ImagePalette import make_linear_lut
black = 0
white = 255
# Act
lut = make_linear_lut(black, white)
lut = ImagePalette.make_linear_lut(black, white)
# Assert
self.assertIsInstance(lut, list)
@ -63,22 +61,20 @@ class TestImagePalette(PillowTestCase):
def test_make_linear_lut_not_yet_implemented(self):
# Update after FIXME
# Arrange
from PIL.ImagePalette import make_linear_lut
black = 1
white = 255
# Act
self.assertRaises(
NotImplementedError,
lambda: make_linear_lut(black, white))
lambda: ImagePalette.make_linear_lut(black, white))
def test_make_gamma_lut(self):
# Arrange
from PIL.ImagePalette import make_gamma_lut
exp = 5
# Act
lut = make_gamma_lut(exp)
lut = ImagePalette.make_gamma_lut(exp)
# Assert
self.assertIsInstance(lut, list)
@ -92,8 +88,7 @@ class TestImagePalette(PillowTestCase):
def test_rawmode_valueerrors(self):
# Arrange
from PIL.ImagePalette import raw
palette = raw("RGB", list(range(256))*3)
palette = ImagePalette.raw("RGB", list(range(256))*3)
# Act / Assert
self.assertRaises(ValueError, palette.tobytes)
@ -104,7 +99,7 @@ class TestImagePalette(PillowTestCase):
def test_getdata(self):
# Arrange
data_in = list(range(256))*3
palette = ImagePalette("RGB", data_in)
palette = ImagePalette.ImagePalette("RGB", data_in)
# Act
mode, data_out = palette.getdata()
@ -114,9 +109,8 @@ class TestImagePalette(PillowTestCase):
def test_rawmode_getdata(self):
# Arrange
from PIL.ImagePalette import raw
data_in = list(range(256))*3
palette = raw("RGB", data_in)
palette = ImagePalette.raw("RGB", data_in)
# Act
rawmode, data_out = palette.getdata()
@ -138,6 +132,10 @@ class TestImagePalette(PillowTestCase):
self.assert_image_equal(img, reloaded)
def test_invalid_palette(self):
self.assertRaises(IOError,
lambda: ImagePalette.load("Tests/images/hopper.jpg"))
if __name__ == '__main__':
unittest.main()

View File

@ -1,4 +1,4 @@
from helper import unittest, PillowTestCase
from helper import unittest, PillowTestCase, hopper
from PIL import Image
from PIL import ImageShow
@ -10,6 +10,24 @@ class TestImageShow(PillowTestCase):
dir(Image)
dir(ImageShow)
def test_register(self):
# Test registering a viewer that is not a class
ImageShow.register("not a class")
def test_show(self):
class TestViewer:
methodCalled = False
def show(self, image, title=None, **options):
self.methodCalled = True
return True
viewer = TestViewer()
ImageShow.register(viewer, -1)
im = hopper()
self.assertTrue(ImageShow.show(im))
self.assertTrue(viewer.methodCalled)
def test_viewer(self):
viewer = ImageShow.Viewer()