Pillow/Tests/test_file_msp.py

79 lines
2.3 KiB
Python
Raw Normal View History

import os
2017-02-23 13:20:25 +03:00
from PIL import Image, MspImagePlugin
from .helper import PillowTestCase, hopper, unittest
2017-01-30 09:21:48 +03:00
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):
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"
2019-06-13 18:54:11 +03:00
self.assertRaises(SyntaxError, 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
2019-06-13 18:54:11 +03:00
self.assertRaises(SyntaxError, 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)
2019-06-13 18:54:11 +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
2019-06-13 18:54:11 +03:00
files = (
os.path.join(EXTRA_DIR, f)
for f in os.listdir(EXTRA_DIR)
if os.path.splitext(f)[1] == ".msp"
)
2017-01-30 09:21:48 +03:00
for path in files:
2019-06-13 18:54:11 +03:00
self._assert_file_image_equal(path, path.replace(".msp", ".png"))
2017-02-21 16:37:27 +03:00
2019-06-13 18:54:11 +03:00
@unittest.skipIf(
not os.path.exists(YA_EXTRA_DIR), "Even More Extra image files not installed"
)
2017-02-21 16:37:27 +03:00
def test_msp_v2(self):
for f in os.listdir(YA_EXTRA_DIR):
2019-06-13 18:54:11 +03:00
if ".MSP" not in f:
2017-04-20 14:14:23 +03:00
continue
2017-02-21 16:37:27 +03:00
path = os.path.join(YA_EXTRA_DIR, f)
2019-06-13 18:54:11 +03:00
self._assert_file_image_equal(path, 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)