1# vim: set fileencoding=utf-8 :
2import pytest
3
4import pyvips
5from helpers import assert_equal_objects
6
7
8class TestIofuncs:
9    # test the vips7 filename splitter ... this is very fragile and annoying
10    # code with lots of cases
11
12    @pytest.mark.xfail(raises=AttributeError, reason="uses deprecated symbols")
13    def test_split7(self):
14        def split(path):
15            filename7 = pyvips.path_filename7(path)
16            mode7 = pyvips.path_mode7(path)
17
18            return [filename7, mode7]
19
20        cases = [
21            ["c:\\silly:dir:name\\fr:ed.tif:jpeg:95,,,,c:\\icc\\srgb.icc",
22             ["c:\\silly:dir:name\\fr:ed.tif",
23              "jpeg:95,,,,c:\\icc\\srgb.icc"]],
24            ["I180:",
25             ["I180",
26              ""]],
27            ["c:\\silly:",
28             ["c:\\silly",
29              ""]],
30            ["c:\\program files\\x:hello",
31             ["c:\\program files\\x",
32              "hello"]],
33            ["C:\\fixtures\\2569067123_aca715a2ee_o.jpg",
34             ["C:\\fixtures\\2569067123_aca715a2ee_o.jpg",
35              ""]]
36        ]
37
38        for case in cases:
39            assert_equal_objects(split(case[0]), case[1])
40
41    def test_new_from_image(self):
42        im = pyvips.Image.mask_ideal(100, 100, 0.5,
43                                     reject=True, optical=True)
44
45        im2 = im.new_from_image(12)
46
47        assert im2.width == im.width
48        assert im2.height == im.height
49        assert im2.interpretation == im.interpretation
50        assert im2.format == im.format
51        assert im2.xres == im.xres
52        assert im2.yres == im.yres
53        assert im2.xoffset == im.xoffset
54        assert im2.yoffset == im.yoffset
55        assert im2.bands == 1
56        assert im2.avg() == 12
57
58        im2 = im.new_from_image([1, 2, 3])
59
60        assert im2.bands == 3
61        assert im2.avg() == 2
62
63    def test_new_from_memory(self):
64        s = bytearray(200)
65        im = pyvips.Image.new_from_memory(s, 20, 10, 1, 'uchar')
66
67        assert im.width == 20
68        assert im.height == 10
69        assert im.format == 'uchar'
70        assert im.bands == 1
71        assert im.avg() == 0
72
73        im += 10
74
75        assert im.avg() == 10
76
77    @pytest.mark.skipif(not pyvips.at_least_libvips(8, 5),
78                        reason="requires libvips >= 8.5")
79    def test_get_fields(self):
80        im = pyvips.Image.black(10, 10)
81        fields = im.get_fields()
82        # we might add more fields later
83        assert len(fields) > 10
84        assert fields[0] == 'width'
85
86    def test_write_to_memory(self):
87        s = bytearray(200)
88        im = pyvips.Image.new_from_memory(s, 20, 10, 1, 'uchar')
89        t = im.write_to_memory()
90
91        assert s == t
92
93
94if __name__ == '__main__':
95    pytest.main()
96