Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add unit tests for the image functions
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA1:684ae7cbb9fddc32f18250283b7a63762caefb24
User & Date: brandon 2018-11-09 23:43:19
Context
2018-11-09
23:43
Add unit tests for the image functions Leaf check-in: 684ae7cbb9 user: brandon tags: trunk
2018-11-08
23:16
Integrate support for GIF where appropriate (*not* in Blorb or TADS) check-in: 0e64a95c3b user: brandon tags: trunk
Changes

Added tests/test_imgformats.py.



























































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
#
#       test_storyformat.py
#
#       Copyright © 2018 Brandon Invergo <brandon@invergo.net>
#
#       This file is part of pyIFBabel.
#
#       pyIFBabel is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
#       (at your option) any later version.
#
#       pyIFBabel is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with pyIFBabel.  If not, see <http://www.gnu.org/licenses/>.

import unittest
import glob
import os.path

from treatyofbabel.utils import _imgfuncs


class ImgTest(unittest.TestCase):
    def setUp(self):
        self.test_imgs = {"png": glob.glob("img/*.png"),
                          "jpeg": glob.glob("img/*.jpeg"),
                          "gif": glob.glob("img/*.gif")}

    def test_deduce_img_format(self):
        for img_fmt in self.test_imgs:
            for img in self.test_imgs[img_fmt]:
                with open(img, "rb") as h:
                    img_buf = h.read()
                self.assertEqual(_imgfuncs.deduce_img_format(img_buf),
                                 img_fmt)

    def test_get_jpeg_dim(self):
        jpgs = self.test_imgs["jpeg"]
        for img in jpgs:
            fname = os.path.basename(img).split(".")[0]
            dims = fname.split("-")[1]
            w, h = [int(x) for x in dims.split("x")]
            with open(img, "rb") as f:
                img_buf = f.read()
            test_w, test_h = _imgfuncs.get_jpeg_dim(img_buf)
            self.assertEqual(test_w, w)
            self.assertEqual(test_h, h)

    def test_get_png_dim(self):
        pngs = self.test_imgs["png"]
        for img in pngs:
            fname = os.path.basename(img).split(".")[0]
            dims = fname.split("-")[1]
            w, h = [int(x) for x in dims.split("x")]
            with open(img, "rb") as f:
                img_buf = f.read()
            test_w, test_h = _imgfuncs.get_png_dim(img_buf)
            self.assertEqual(test_w, w)
            self.assertEqual(test_h, h)

    def test_get_gif_dim(self):
        gifs = self.test_imgs["gif"]
        for img in gifs:
            fname = os.path.basename(img).split(".")[0]
            dims = fname.split("-")[1]
            w, h = [int(x) for x in dims.split("x")]
            with open(img, "rb") as f:
                img_buf = f.read()
            test_w, test_h = _imgfuncs.get_gif_dim(img_buf)
            self.assertEqual(test_w, w)
            self.assertEqual(test_h, h)