Pillow/Tests/test_file_msp.py

81 lines
2.5 KiB
Python
Raw Normal View History

from .helper import unittest, PillowTestCase, hopper
2017-02-23 13:20:25 +03:00
from PIL import Image, MspImagePlugin
2017-01-30 09:21:48 +03:00
import os
2014-09-18 15:48:07 +04:00
TEST_FILE = "Tests/images/hopper.msp"
2017-01-30 09:21:48 +03:00
EXTRA_DIR = "Tests/images/picins"
2017-02-21 16:37:27 +03:00
YA_EXTRA_DIR = "Tests/images/msp"
2014-09-18 15:48:07 +04:00
2014-06-10 13:10:47 +04:00
class TestFileMsp(PillowTestCase):
2014-06-10 13:10:47 +04:00
def test_sanity(self):
2019-03-03 05:02:00 +03:00
test_file = self.tempfile("temp.msp")
2014-06-10 13:10:47 +04:00
2019-03-03 05:02:00 +03:00
hopper("1").save(test_file)
2014-06-10 13:10:47 +04:00
2019-03-03 05:02:00 +03:00
im = Image.open(test_file)
2014-06-10 13:10:47 +04:00
im.load()
self.assertEqual(im.mode, "1")
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "MSP")
2015-07-03 08:03:25 +03:00
def test_invalid_file(self):
2015-07-03 09:22:56 +03:00
invalid_file = "Tests/images/flower.jpg"
self.assertRaises(SyntaxError,
2017-09-01 14:05:40 +03:00
MspImagePlugin.MspImageFile, invalid_file)
2015-07-03 08:03:25 +03:00
2017-01-29 23:28:41 +03:00
def test_bad_checksum(self):
# Arrange
# This was created by forcing Pillow to save with checksum=0
bad_checksum = "Tests/images/hopper_bad_checksum.msp"
# Act / Assert
self.assertRaises(SyntaxError,
2017-09-01 14:05:40 +03:00
MspImagePlugin.MspImageFile, bad_checksum)
2017-01-29 23:28:41 +03:00
2017-01-30 09:21:48 +03:00
def test_open_windows_v1(self):
2014-09-18 15:48:07 +04:00
# Arrange
# Act
im = Image.open(TEST_FILE)
# Assert
2017-01-30 16:32:31 +03:00
self.assert_image_equal(im, hopper("1"))
2017-01-30 09:21:48 +03:00
self.assertIsInstance(im, MspImagePlugin.MspImageFile)
2017-02-21 16:37:27 +03:00
def _assert_file_image_equal(self, source_path, target_path):
with Image.open(source_path) as im:
target = Image.open(target_path)
self.assert_image_equal(im, target)
2017-01-30 09:21:48 +03:00
@unittest.skipIf(not os.path.exists(EXTRA_DIR),
"Extra image files not installed")
2017-01-30 10:20:15 +03:00
def test_open_windows_v2(self):
2017-02-21 16:37:27 +03:00
2017-01-30 09:21:48 +03:00
files = (os.path.join(EXTRA_DIR, f) for f in os.listdir(EXTRA_DIR)
if os.path.splitext(f)[1] == '.msp')
for path in files:
2017-02-21 16:37:27 +03:00
self._assert_file_image_equal(path,
2017-04-20 14:14:23 +03:00
path.replace('.msp', '.png'))
2017-02-21 16:37:27 +03:00
@unittest.skipIf(not os.path.exists(YA_EXTRA_DIR),
"Even More Extra image files not installed")
def test_msp_v2(self):
for f in os.listdir(YA_EXTRA_DIR):
2017-04-20 14:14:23 +03:00
if '.MSP' not in f:
continue
2017-02-21 16:37:27 +03:00
path = os.path.join(YA_EXTRA_DIR, f)
self._assert_file_image_equal(path,
2017-04-20 14:14:23 +03:00
path.replace('.MSP', '.png'))
2014-09-18 15:48:07 +04:00
2014-11-01 10:55:15 +03:00
def test_cannot_save_wrong_mode(self):
2014-09-18 15:48:07 +04:00
# Arrange
im = hopper()
2014-11-01 10:55:15 +03:00
filename = self.tempfile("temp.msp")
2014-09-18 15:48:07 +04:00
# Act/Assert
2017-09-01 14:05:40 +03:00
self.assertRaises(IOError, im.save, filename)