|
1 | 1 | import io |
2 | | -import unittest |
3 | 2 |
|
| 3 | +import pytest |
4 | 4 | from PIL import features |
5 | 5 |
|
6 | | -from .helper import PillowTestCase |
7 | | - |
8 | 6 | try: |
9 | 7 | from PIL import _webp |
10 | 8 |
|
|
13 | 11 | HAVE_WEBP = False |
14 | 12 |
|
15 | 13 |
|
16 | | -class TestFeatures(PillowTestCase): |
17 | | - def test_check(self): |
18 | | - # Check the correctness of the convenience function |
19 | | - for module in features.modules: |
20 | | - self.assertEqual(features.check_module(module), features.check(module)) |
21 | | - for codec in features.codecs: |
22 | | - self.assertEqual(features.check_codec(codec), features.check(codec)) |
23 | | - for feature in features.features: |
24 | | - self.assertEqual(features.check_feature(feature), features.check(feature)) |
25 | | - |
26 | | - @unittest.skipUnless(HAVE_WEBP, "WebP not available") |
27 | | - def test_webp_transparency(self): |
28 | | - self.assertEqual( |
29 | | - features.check("transp_webp"), not _webp.WebPDecoderBuggyAlpha() |
30 | | - ) |
31 | | - self.assertEqual(features.check("transp_webp"), _webp.HAVE_TRANSPARENCY) |
32 | | - |
33 | | - @unittest.skipUnless(HAVE_WEBP, "WebP not available") |
34 | | - def test_webp_mux(self): |
35 | | - self.assertEqual(features.check("webp_mux"), _webp.HAVE_WEBPMUX) |
36 | | - |
37 | | - @unittest.skipUnless(HAVE_WEBP, "WebP not available") |
38 | | - def test_webp_anim(self): |
39 | | - self.assertEqual(features.check("webp_anim"), _webp.HAVE_WEBPANIM) |
40 | | - |
41 | | - def test_check_modules(self): |
42 | | - for feature in features.modules: |
43 | | - self.assertIn(features.check_module(feature), [True, False]) |
44 | | - for feature in features.codecs: |
45 | | - self.assertIn(features.check_codec(feature), [True, False]) |
46 | | - |
47 | | - def test_supported_modules(self): |
48 | | - self.assertIsInstance(features.get_supported_modules(), list) |
49 | | - self.assertIsInstance(features.get_supported_codecs(), list) |
50 | | - self.assertIsInstance(features.get_supported_features(), list) |
51 | | - self.assertIsInstance(features.get_supported(), list) |
52 | | - |
53 | | - def test_unsupported_codec(self): |
54 | | - # Arrange |
55 | | - codec = "unsupported_codec" |
56 | | - # Act / Assert |
57 | | - self.assertRaises(ValueError, features.check_codec, codec) |
58 | | - |
59 | | - def test_unsupported_module(self): |
60 | | - # Arrange |
61 | | - module = "unsupported_module" |
62 | | - # Act / Assert |
63 | | - self.assertRaises(ValueError, features.check_module, module) |
64 | | - |
65 | | - def test_pilinfo(self): |
66 | | - buf = io.StringIO() |
67 | | - features.pilinfo(buf) |
68 | | - out = buf.getvalue() |
69 | | - lines = out.splitlines() |
70 | | - self.assertEqual(lines[0], "-" * 68) |
71 | | - self.assertTrue(lines[1].startswith("Pillow ")) |
72 | | - self.assertTrue(lines[2].startswith("Python ")) |
73 | | - lines = lines[3:] |
74 | | - while lines[0].startswith(" "): |
75 | | - lines = lines[1:] |
76 | | - self.assertEqual(lines[0], "-" * 68) |
77 | | - self.assertTrue(lines[1].startswith("Python modules loaded from ")) |
78 | | - self.assertTrue(lines[2].startswith("Binary modules loaded from ")) |
79 | | - self.assertEqual(lines[3], "-" * 68) |
80 | | - jpeg = ( |
81 | | - "\n" |
82 | | - + "-" * 68 |
83 | | - + "\n" |
84 | | - + "JPEG image/jpeg\n" |
85 | | - + "Extensions: .jfif, .jpe, .jpeg, .jpg\n" |
86 | | - + "Features: open, save\n" |
87 | | - + "-" * 68 |
88 | | - + "\n" |
89 | | - ) |
90 | | - self.assertIn(jpeg, out) |
| 14 | +def test_check(): |
| 15 | + # Check the correctness of the convenience function |
| 16 | + for module in features.modules: |
| 17 | + assert features.check_module(module) == features.check(module) |
| 18 | + for codec in features.codecs: |
| 19 | + assert features.check_codec(codec) == features.check(codec) |
| 20 | + for feature in features.features: |
| 21 | + assert features.check_feature(feature) == features.check(feature) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available") |
| 25 | +def test_webp_transparency(): |
| 26 | + assert features.check("transp_webp") != _webp.WebPDecoderBuggyAlpha() |
| 27 | + assert features.check("transp_webp") == _webp.HAVE_TRANSPARENCY |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available") |
| 31 | +def test_webp_mux(): |
| 32 | + assert features.check("webp_mux") == _webp.HAVE_WEBPMUX |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available") |
| 36 | +def test_webp_anim(): |
| 37 | + assert features.check("webp_anim") == _webp.HAVE_WEBPANIM |
| 38 | + |
| 39 | + |
| 40 | +def test_check_modules(): |
| 41 | + for feature in features.modules: |
| 42 | + assert features.check_module(feature) in [True, False] |
| 43 | + for feature in features.codecs: |
| 44 | + assert features.check_codec(feature) in [True, False] |
| 45 | + |
| 46 | + |
| 47 | +def test_supported_modules(): |
| 48 | + assert isinstance(features.get_supported_modules(), list) |
| 49 | + assert isinstance(features.get_supported_codecs(), list) |
| 50 | + assert isinstance(features.get_supported_features(), list) |
| 51 | + assert isinstance(features.get_supported(), list) |
| 52 | + |
| 53 | + |
| 54 | +def test_unsupported_codec(): |
| 55 | + # Arrange |
| 56 | + codec = "unsupported_codec" |
| 57 | + # Act / Assert |
| 58 | + with pytest.raises(ValueError): |
| 59 | + features.check_codec(codec) |
| 60 | + |
| 61 | + |
| 62 | +def test_unsupported_module(): |
| 63 | + # Arrange |
| 64 | + module = "unsupported_module" |
| 65 | + # Act / Assert |
| 66 | + with pytest.raises(ValueError): |
| 67 | + features.check_module(module) |
| 68 | + |
| 69 | + |
| 70 | +def test_pilinfo(): |
| 71 | + buf = io.StringIO() |
| 72 | + features.pilinfo(buf) |
| 73 | + out = buf.getvalue() |
| 74 | + lines = out.splitlines() |
| 75 | + assert lines[0] == "-" * 68 |
| 76 | + assert lines[1].startswith("Pillow ") |
| 77 | + assert lines[2].startswith("Python ") |
| 78 | + lines = lines[3:] |
| 79 | + while lines[0].startswith(" "): |
| 80 | + lines = lines[1:] |
| 81 | + assert lines[0] == "-" * 68 |
| 82 | + assert lines[1].startswith("Python modules loaded from ") |
| 83 | + assert lines[2].startswith("Binary modules loaded from ") |
| 84 | + assert lines[3] == "-" * 68 |
| 85 | + jpeg = ( |
| 86 | + "\n" |
| 87 | + + "-" * 68 |
| 88 | + + "\n" |
| 89 | + + "JPEG image/jpeg\n" |
| 90 | + + "Extensions: .jfif, .jpe, .jpeg, .jpg\n" |
| 91 | + + "Features: open, save\n" |
| 92 | + + "-" * 68 |
| 93 | + + "\n" |
| 94 | + ) |
| 95 | + assert jpeg in out |
0 commit comments