Skip to content

Commit

Permalink
Added various tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jul 3, 2015
1 parent 1a5f9cf commit a06b59b
Show file tree
Hide file tree
Showing 38 changed files with 264 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Tests/check_imaging_leaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import division
from helper import unittest, PillowTestCase
import sys
from PIL import Image, ImageFilter
from PIL import Image

min_iterations = 100
max_iterations = 10000
Expand Down
12 changes: 12 additions & 0 deletions Tests/test_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def _test_get_access(self, im):
for y in range(0, h, 10):
self.assertEqual(access[(x, y)], caccess[(x, y)])

# Access an out-of-range pixel
self.assertRaises(ValueError,
lambda: access[(access.xsize+1, access.ysize+1)])

def test_get_vs_c(self):
rgb = hopper('RGB')
rgb.load()
Expand Down Expand Up @@ -103,6 +107,14 @@ def _test_set_access(self, im, color):
access[(x, y)] = color
self.assertEqual(color, caccess[(x, y)])

# Attempt to set the value on a read-only image
access = PyAccess.new(im, True)
try:
access[(0, 0)] = color
except ValueError:
return
self.fail("Putpixel did not fail on a read-only image")

def test_set_vs_c(self):
rgb = hopper('RGB')
rgb.load()
Expand Down
6 changes: 5 additions & 1 deletion Tests/test_file_bmp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper

from PIL import Image
from PIL import Image, BmpImagePlugin
import io


Expand All @@ -25,6 +25,10 @@ def test_sanity(self):
self.roundtrip(hopper("P"))
self.roundtrip(hopper("RGB"))

def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, lambda: BmpImagePlugin.BmpImageFile(fp))

def test_save_to_bytes(self):
output = io.BytesIO()
im = hopper()
Expand Down
15 changes: 15 additions & 0 deletions Tests/test_file_bufrstub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase

from PIL import BufrStubImagePlugin


class TestFileBufrStub(PillowTestCase):

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: BufrStubImagePlugin.BufrStubImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()

# End of file
3 changes: 3 additions & 0 deletions Tests/test_file_cur.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def test_sanity(self):
self.assertEqual(im.getpixel((11, 1)), (253, 254, 254, 1))
self.assertEqual(im.getpixel((16, 16)), (84, 87, 86, 255))

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: CurImagePlugin.CurImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()
Expand Down
4 changes: 4 additions & 0 deletions Tests/test_file_dcx.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def test_sanity(self):
orig = hopper()
self.assert_image_equal(im, orig)

def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, lambda: DcxImagePlugin.DcxImageFile(fp))

def test_tell(self):
# Arrange
im = Image.open(TEST_FILE)
Expand Down
3 changes: 3 additions & 0 deletions Tests/test_file_eps.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def test_sanity(self):
self.assertEqual(image2_scale2.size, (720, 504))
self.assertEqual(image2_scale2.format, "EPS")

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: EpsImagePlugin.EpsImageFile("Tests/images/flower.jpg"))

def test_file_object(self):
# issue 479
image1 = Image.open(file1)
Expand Down
15 changes: 15 additions & 0 deletions Tests/test_file_fitsstub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase

from PIL import FitsStubImagePlugin


class TestFileFitsStub(PillowTestCase):

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: FitsStubImagePlugin.FITSStubImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()

# End of file
5 changes: 4 additions & 1 deletion Tests/test_file_fli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase

from PIL import Image
from PIL import Image, FliImagePlugin

# sample ppm stream
# created as an export of a palette image from Gimp2.6
Expand All @@ -18,6 +18,9 @@ def test_sanity(self):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "FLI")

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: FliImagePlugin.FliImageFile("Tests/images/flower.jpg"))

def test_n_frames(self):
im = Image.open(test_file)
self.assertEqual(im.n_frames, 1)
Expand Down
15 changes: 15 additions & 0 deletions Tests/test_file_fpx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase

from PIL import FpxImagePlugin


class TestFileFpx(PillowTestCase):

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: FpxImagePlugin.FpxImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()

# End of file
15 changes: 15 additions & 0 deletions Tests/test_file_gbr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase

from PIL import GbrImagePlugin


class TestFileGbr(PillowTestCase):

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: GbrImagePlugin.GbrImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()

# End of file
3 changes: 3 additions & 0 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def test_sanity(self):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "GIF")

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: GifImagePlugin.GifImageFile("Tests/images/flower.jpg"))

def test_optimize(self):
from io import BytesIO

Expand Down
15 changes: 15 additions & 0 deletions Tests/test_file_gribstub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase

from PIL import GribStubImagePlugin


class TestFileGribStub(PillowTestCase):

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: GribStubImagePlugin.GribStubImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()

# End of file
15 changes: 15 additions & 0 deletions Tests/test_file_hdf5stub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase

from PIL import Hdf5StubImagePlugin


class TestFileHdf5Stub(PillowTestCase):

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: Hdf5StubImagePlugin.HDF5StubImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()

# End of file
6 changes: 5 additions & 1 deletion Tests/test_file_ico.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from helper import unittest, PillowTestCase, hopper

import io
from PIL import Image
from PIL import Image, IcoImagePlugin

# sample ppm stream
TEST_ICO_FILE = "Tests/images/hopper.ico"
Expand All @@ -17,6 +17,10 @@ def test_sanity(self):
self.assertEqual(im.size, (16, 16))
self.assertEqual(im.format, "ICO")

def test_invalid_file(self):
with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, lambda: IcoImagePlugin.IcoImageFile(fp))

def test_save_to_bytes(self):
output = io.BytesIO()
im = hopper()
Expand Down
6 changes: 5 additions & 1 deletion Tests/test_file_im.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper

from PIL import Image
from PIL import Image, ImImagePlugin

# sample im
TEST_IM = "Tests/images/hopper.im"
Expand Down Expand Up @@ -40,6 +40,10 @@ def test_roundtrip(self):

self.assert_image_equal(reread, im)

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: ImImagePlugin.ImImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()

Expand Down
5 changes: 4 additions & 1 deletion Tests/test_file_jpeg2k.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase

from PIL import Image
from PIL import Image, Jpeg2KImagePlugin
from io import BytesIO

codecs = dir(Image.core)
Expand Down Expand Up @@ -39,6 +39,9 @@ def test_sanity(self):
self.assertEqual(im.size, (640, 480))
self.assertEqual(im.format, 'JPEG2000')

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: Jpeg2KImagePlugin.Jpeg2KImageFile("Tests/images/flower.jpg"))

def test_bytesio(self):
with open('Tests/images/test-card-lossless.jp2', 'rb') as f:
data = BytesIO(f.read())
Expand Down
15 changes: 15 additions & 0 deletions Tests/test_file_mcidas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase

from PIL import McIdasImagePlugin


class TestFileMcIdas(PillowTestCase):

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: McIdasImagePlugin.McIdasImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()

# End of file
15 changes: 15 additions & 0 deletions Tests/test_file_mic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from helper import unittest, PillowTestCase

from PIL import MicImagePlugin


class TestFileMic(PillowTestCase):

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: MicImagePlugin.MicImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()

# End of file
5 changes: 4 additions & 1 deletion Tests/test_file_msp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper

from PIL import Image
from PIL import Image, MspImagePlugin

TEST_FILE = "Tests/images/hopper.msp"

Expand All @@ -18,6 +18,9 @@ def test_sanity(self):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "MSP")

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: MspImagePlugin.MspImageFile("Tests/images/flower.jpg"))

def test_open(self):
# Arrange
# Act
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_file_pcx.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase, hopper

from PIL import Image
from PIL import Image, PcxImagePlugin


class TestFilePcx(PillowTestCase):
Expand All @@ -19,6 +19,9 @@ def test_sanity(self):
for mode in ('1', 'L', 'P', 'RGB'):
self._roundtrip(hopper(mode))

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: PcxImagePlugin.PcxImageFile("Tests/images/flower.jpg"))

def test_odd(self):
# see issue #523, odd sized images should have a stride that's even.
# not that imagemagick or gimp write pcx that way.
Expand Down
6 changes: 6 additions & 0 deletions Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def test_cmyk_mode(self):
# Act / Assert
self.helper_save_as_pdf(mode)

def test_unsupported_mode(self):
im = hopper("LA")
outfile = self.tempfile("temp_LA.pdf")

self.assertRaises(ValueError, lambda: im.save(outfile))


if __name__ == '__main__':
unittest.main()
Expand Down
3 changes: 3 additions & 0 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def test_sanity(self):
hopper("I").save(test_file)
im = Image.open(test_file)

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: PngImagePlugin.PngImageFile("Tests/images/flower.jpg"))

def test_broken(self):
# Check reading of totally broken files. In this case, the test
# file was checked into Subversion as a text file.
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_file_psd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase

from PIL import Image
from PIL import Image, PsdImagePlugin

# sample ppm stream
test_file = "Tests/images/hopper.psd"
Expand All @@ -16,6 +16,9 @@ def test_sanity(self):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "PSD")

def test_invalid_file(self):
self.assertRaises(SyntaxError, lambda: PsdImagePlugin.PsdImageFile("Tests/images/flower.jpg"))

def test_n_frames(self):
im = Image.open("Tests/images/hopper_merged.psd")
self.assertEqual(im.n_frames, 1)
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_file_sgi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from helper import unittest, PillowTestCase

from PIL import Image
from PIL import Image, SgiImagePlugin


class TestFileSgi(PillowTestCase):
Expand Down Expand Up @@ -32,6 +32,9 @@ def test_rgba(self):
# Act / Assert
self.assertRaises(ValueError, lambda: Image.open(test_file))

def test_invalid_file(self):
self.assertRaises(ValueError, lambda: SgiImagePlugin.SgiImageFile("Tests/images/flower.jpg"))


if __name__ == '__main__':
unittest.main()
Expand Down
Loading

0 comments on commit a06b59b

Please sign in to comment.