pyBarcode / test.py

# -*- coding: utf-8 -*-

from __future__ import unicode_literals, print_function

"""

Performs some tests with pyBarcode. All created barcodes where saved in the
tests subdirectory with a tests.html to watch them.

"""
__docformat__ = 'restructuredtext en'

import codecs
import os
import sys
import webbrowser

from barcode import BARCODE_MAP, get_barcode, __version__


PATH = os.path.dirname(os.path.abspath(__file__))
TESTPATH = os.path.join(PATH, 'tests')
HTMLFILE = os.path.join(TESTPATH, 'tests.html')

HTML = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <title>pyBarcode {version} Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <h1>pyBarcode {version} Tests</h1>
        {body}
    </body>
</html>
"""

OBJECTS = ('<p><h2>{name}</h2><br />\n'
           '<object data="{filename}" type="image/svg+xml">\n'
           '<param name="src" value="{filename}" /></object></p>')


def test():
    if not os.path.isdir(TESTPATH):
        try:
            os.mkdir(TESTPATH)
        except OSError, e:
            print('Test not run.')
            print('Error:', e)
            sys.exit(1)
    objects = []
    append = lambda x, y: objects.append(OBJECTS.format(filename=x, name=y))
    # Test EAN-8
    ean8 = get_barcode('ean8', '40267708')
    name = ean8.save(os.path.join(TESTPATH, 'ean8'))
    append(name, ean8.name)
    # Test EAN-13
    ean13 = get_barcode('ean13', '5901234123457')
    name = ean13.save(os.path.join(TESTPATH, 'ean13'))
    append(name, ean13.name)
    # Test UPC-A
    upca = get_barcode('upca', '36000291452')
    name = upca.save(os.path.join(TESTPATH, 'upca'))
    append(name, upca.name)
    # Test JAN
    jan = get_barcode('jan', '4901234567894')
    name = jan.save(os.path.join(TESTPATH, 'jan.svg'))
    append(name, jan.name)
    # Test ISBN-10
    isbn10 = get_barcode('isbn10', '3-12-517154-7')
    name = isbn10.save(os.path.join(TESTPATH, 'isbn10'))
    append(name, isbn10.name)
    # Test ISBN-13
    isbn13 = get_barcode('isbn13', '978-3-16-148410-0')
    name = isbn13.save(os.path.join(TESTPATH, 'isbn13'))
    append(name, isbn13.name)
    # Test ISSN
    issn = get_barcode('issn', '1144875X')
    name = issn.save(os.path.join(TESTPATH, 'issn'))
    append(name, issn.name)
    # Test Code 39
    code39 = get_barcode('code39', 'Example Code 39')
    name = code39.save(os.path.join(TESTPATH, 'code39'))
    append(name, code39.name)
    # Test PZN
    pzn = get_barcode('pzn', '487780')
    name = pzn.save(os.path.join(TESTPATH, 'pzn'))
    append(name, pzn.name)
    # Save htmlfile with all objects
    with codecs.open(HTMLFILE, 'w', encoding='utf-8') as f:
        obj = '\n'.join(objects)
        f.write(HTML.format(version=__version__, body=obj))


if __name__ == '__main__':
    test()
    webbrowser.open(HTMLFILE)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.