|
1 | | -""" |
2 | | -
|
3 | | -Performs some tests with pyBarcode. All created barcodes where saved in the |
4 | | -tests subdirectory with a tests.html to watch them. |
5 | | -
|
6 | | -""" |
7 | | -__docformat__ = 'restructuredtext en' |
| 1 | +"""Generates barcodes for visually inspecting the results.""" |
8 | 2 |
|
9 | 3 | import codecs |
10 | 4 | import os |
11 | 5 | import sys |
12 | | -import unittest |
13 | 6 |
|
14 | 7 | from barcode import get_barcode, get_barcode_class, version |
15 | | -try: |
16 | | - from barcode.writer import ImageWriter |
17 | | -except ImportError: |
18 | | - ImageWriter = None # lint:ok |
| 8 | +from barcode.writer import ImageWriter |
19 | 9 |
|
20 | 10 | PATH = os.path.dirname(os.path.abspath(__file__)) |
21 | | -TESTPATH = os.path.join(PATH, 'tests') |
| 11 | +TESTPATH = os.path.join(PATH, 'test_outputs') |
22 | 12 | HTMLFILE = os.path.join(TESTPATH, 'index.html') |
23 | 13 |
|
24 | 14 | HTML = """<!DOCTYPE html> |
|
62 | 52 | ) |
63 | 53 |
|
64 | 54 |
|
65 | | -def test(): |
66 | | - if not os.path.isdir(TESTPATH): |
67 | | - try: |
68 | | - os.mkdir(TESTPATH) |
69 | | - except OSError as e: |
70 | | - print('Test not run.') |
71 | | - print('Error:', e) |
72 | | - sys.exit(1) |
| 55 | +def test_generating_barcodes(): |
| 56 | + os.makedirs(TESTPATH, exist_ok=True) |
| 57 | + |
73 | 58 | objects = [] |
74 | | - append = lambda x, y: objects.append(OBJECTS.format(filename=x, name=y)) |
75 | | - append_img = lambda x, y: objects.append(IMAGES.format(filename=x, name=y)) |
| 59 | + |
| 60 | + def append(x, y): |
| 61 | + objects.append(OBJECTS.format(filename=x, name=y)) |
| 62 | + |
| 63 | + def append_img(x, y): |
| 64 | + objects.append(IMAGES.format(filename=x, name=y)) |
| 65 | + |
76 | 66 | options = {'module_width': 0.495, 'module_height': 25.0} |
77 | 67 | for codename, code in TESTCODES: |
78 | 68 | bcode = get_barcode(codename, code) |
@@ -108,59 +98,4 @@ def test(): |
108 | 98 | obj = '\n'.join(objects) |
109 | 99 | f.write(HTML.format(version=version, body=obj)) |
110 | 100 |
|
111 | | - |
112 | | -class TestBarcodeBuilds(unittest.TestCase): |
113 | | - def test_ean8(self): |
114 | | - ref = ( |
115 | | - '1010100011000110100100110101111010101000100' |
116 | | - '100010011100101001000101' |
117 | | - ) |
118 | | - ean = get_barcode('ean8', '40267708') |
119 | | - bc = ean.build() |
120 | | - self.assertEqual(ref, bc[0]) |
121 | | - |
122 | | - |
123 | | -class TestChecksums(unittest.TestCase): |
124 | | - def test_code39(self): |
125 | | - code39 = get_barcode('code39', 'Code39') |
126 | | - self.assertEqual('CODE39W', code39.get_fullcode()) |
127 | | - |
128 | | - def test_pzn(self): |
129 | | - pzn = get_barcode('pzn', '103940') |
130 | | - self.assertEqual('PZN-1039406', pzn.get_fullcode()) |
131 | | - |
132 | | - def test_ean13(self): |
133 | | - ean = get_barcode('ean13', '400614457735') |
134 | | - self.assertEqual('4006144577350', ean.get_fullcode()) |
135 | | - |
136 | | - def test_ean8(self): |
137 | | - ean = get_barcode('ean8', '6032299') |
138 | | - self.assertEqual('60322999', ean.get_fullcode()) |
139 | | - |
140 | | - def test_jan(self): |
141 | | - jan = get_barcode('jan', '491400614457') |
142 | | - self.assertEqual('4914006144575', jan.get_fullcode()) |
143 | | - |
144 | | - def test_ean14(self): |
145 | | - ean = get_barcode('ean14', '1234567891258') |
146 | | - self.assertEqual('12345678912589', ean.get_fullcode()) |
147 | | - |
148 | | - def test_isbn10(self): |
149 | | - isbn = get_barcode('isbn10', '376926085') |
150 | | - self.assertEqual('3769260856', isbn.isbn10) |
151 | | - |
152 | | - def test_isbn13(self): |
153 | | - isbn = get_barcode('isbn13', '978376926085') |
154 | | - self.assertEqual('9783769260854', isbn.get_fullcode()) |
155 | | - |
156 | | - def test_gs1_128(self): |
157 | | - gs1_128 = get_barcode('gs1_128', '00376401856400470087') |
158 | | - self.assertEqual('00376401856400470087', gs1_128.get_fullcode()) |
159 | | - |
160 | | - |
161 | | -if __name__ == '__main__': |
162 | | - test() |
163 | 101 | print('\nNow open {htmlfile} in your browser.'.format(htmlfile=HTMLFILE)) |
164 | | - if '-v' not in sys.argv: |
165 | | - sys.argv.append('-v') |
166 | | - unittest.main() |
0 commit comments