summaryrefslogtreecommitdiffstats
path: root/share/extensions/tests/test_render_barcode.py
blob: e148691d1fda86e5bf8e0a2451e446d337c9c8b0 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# coding=utf-8
#
# Copyright (C) 2018 Martin Owens
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA.
#
"""
Written to test the coding of generating barcodes.
"""
from collections import defaultdict

from barcode import get_barcode
from render_barcode import Barcode

from inkex.tester import ComparisonMixin, TestCase


class BarcodeBasicTest(ComparisonMixin, TestCase):
    effect_class = Barcode
    comparisons = [
        ("--type", "Ean2", "--text", "55"),
        ("--type", "Code93", "--text", "3332222"),
        ("--type", "Upce", "--text", "123456"),
    ]


class GetBarcodeTest(TestCase):
    """Test each available barcode type"""

    data = defaultdict(list)

    @classmethod
    def setUpClass(cls):
        with open(cls.data_file("batches/barcodes.dat"), "r") as fhl:
            for line in fhl:
                (btype, text, code) = line.strip().split(":", 2)
                cls.data[btype].append((text, code))

    def test_render_barcode_ian5(self):
        """Barcode IAN5"""
        self.barcode_test("Ean5")

    def test_render_barcode_ian8(self):
        """Barcode IAN5"""
        self.barcode_test("Ean8")

    def test_render_barcode_ian13(self):
        """Barcode IAN5"""
        self.barcode_test("Ean13")

    def test_render_barcode_upca(self):
        """Barcode IAN5"""
        self.barcode_test("Upca")

    def test_render_barcode_upce(self):
        """Barcode UPCE"""
        self.barcode_test("Upce")

    def test_render_barcode_code128(self):
        """Barcode Code128"""
        self.barcode_test("Code128")

    def test_render_barcode_code25i(self):
        """Barcode Code25i"""
        self.barcode_test("Code25i")

    def test_render_barcode_code39(self):
        """Barcode Code39"""
        self.barcode_test("Code39")

    def test_render_barcode_code39ext(self):
        """Barcode Code39Ext"""
        self.barcode_test("Code39Ext")

    def test_render_barcode_ean2(self):
        """Barcode Ean2"""
        self.barcode_test("Ean2")

    def test_render_barcode_royal_mail(self):
        """Barcode RM4CC/RM4SCC"""
        self.barcode_test("Rm4scc")

    def barcode_test(self, name):
        """Base module for all barcode testing"""

        assert self.data[name.lower()], "No test data available for {}".format(name)
        for datum in self.data[name.lower()]:
            (text, code) = datum
            coder = get_barcode(name, text=text)
            code2 = coder.encode(text)
            assert code == code2