1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-08-24 09:03:34 +00:00

reformat codebase

This commit is contained in:
Patrick Kanzler
2021-10-30 18:15:22 +02:00
parent 109a5d8a92
commit 435f2bba24
41 changed files with 1706 additions and 1398 deletions

View File

@@ -21,6 +21,6 @@ def test_abstract_base_class_raises():
def test_abstract_base_class():
""" test whether Escpos has the metaclass ABCMeta """
"""test whether Escpos has the metaclass ABCMeta"""
assert issubclass(escpos.Escpos, object)
assert type(escpos.Escpos) is ABCMeta

View File

@@ -9,40 +9,39 @@ from scripttest import TestFileEnvironment
from nose.tools import assert_equal, nottest
import escpos
TEST_DIR = os.path.abspath('test/test-cli-output')
TEST_DIR = os.path.abspath("test/test-cli-output")
DEVFILE_NAME = 'testfile'
DEVFILE_NAME = "testfile"
DEVFILE = os.path.join(TEST_DIR, DEVFILE_NAME)
CONFIGFILE = 'testconfig.yaml'
CONFIG_YAML = '''
CONFIGFILE = "testconfig.yaml"
CONFIG_YAML = """
---
printer:
type: file
devfile: {testfile}
'''.format(
""".format(
testfile=DEVFILE,
)
class TestCLI:
""" Contains setups, teardowns, and tests for CLI
"""
"""Contains setups, teardowns, and tests for CLI"""
@classmethod
def setup_class(cls):
""" Create a config file to read from """
with open(CONFIGFILE, 'w') as config:
"""Create a config file to read from"""
with open(CONFIGFILE, "w") as config:
config.write(CONFIG_YAML)
@classmethod
def teardown_class(cls):
""" Remove config file """
"""Remove config file"""
os.remove(CONFIGFILE)
def setup(self):
""" Create a file to print to and set up env"""
"""Create a file to print to and set up env"""
self.env = None
self.default_args = None
@@ -52,63 +51,59 @@ class TestCLI:
)
self.default_args = (
'python-escpos',
'-c',
"python-escpos",
"-c",
CONFIGFILE,
)
fhandle = open(DEVFILE, 'a')
fhandle = open(DEVFILE, "a")
try:
os.utime(DEVFILE, None)
finally:
fhandle.close()
def teardown(self):
""" Destroy printer file and env """
"""Destroy printer file and env"""
os.remove(DEVFILE)
self.env.clear()
def test_cli_help(self):
""" Test getting help from cli """
result = self.env.run('python-escpos', '-h')
"""Test getting help from cli"""
result = self.env.run("python-escpos", "-h")
assert not result.stderr
assert 'usage' in result.stdout
assert "usage" in result.stdout
def test_cli_version(self):
""" Test the version string """
result = self.env.run('python-escpos', 'version')
"""Test the version string"""
result = self.env.run("python-escpos", "version")
assert not result.stderr
assert_equal(escpos.__version__, result.stdout.strip())
@nottest # disable this test as it is not that easy anymore to predict the outcome of this call
def test_cli_text(self):
""" Make sure text returns what we sent it """
test_text = 'this is some text'
"""Make sure text returns what we sent it"""
test_text = "this is some text"
result = self.env.run(
*(self.default_args + (
'text',
'--txt',
test_text,
))
*(
self.default_args
+ (
"text",
"--txt",
test_text,
)
)
)
assert not result.stderr
assert DEVFILE_NAME in result.files_updated.keys()
assert_equals(
result.files_updated[DEVFILE_NAME].bytes,
test_text + '\n'
)
assert_equals(result.files_updated[DEVFILE_NAME].bytes, test_text + "\n")
def test_cli_text_inavlid_args(self):
""" Test a failure to send valid arguments """
"""Test a failure to send valid arguments"""
result = self.env.run(
*(self.default_args + (
'text',
'--invalid-param',
'some data'
)),
*(self.default_args + ("text", "--invalid-param", "some data")),
expect_error=True,
expect_stderr=True
)
assert_equal(result.returncode, 2)
assert 'error:' in result.stderr
assert "error:" in result.stderr
assert not result.files_updated

View File

@@ -6,42 +6,51 @@ from escpos.exceptions import BarcodeTypeError, BarcodeCodeError
import pytest
@pytest.mark.parametrize("bctype,data,expected", [
('EAN13', '4006381333931',
b'\x1ba\x01\x1dh@\x1dw\x03\x1df\x00\x1dH\x02\x1dk\x024006381333931\x00')
])
@pytest.mark.parametrize(
"bctype,data,expected",
[
(
"EAN13",
"4006381333931",
b"\x1ba\x01\x1dh@\x1dw\x03\x1df\x00\x1dH\x02\x1dk\x024006381333931\x00",
)
],
)
def test_barcode(bctype, data, expected):
"""should generate different barcode types correctly.
"""
"""should generate different barcode types correctly."""
instance = printer.Dummy()
instance.barcode(data, bctype)
assert instance.output == expected
@pytest.mark.parametrize("bctype,supports_b", [
('invalid', True),
('CODE128', False),
])
@pytest.mark.parametrize(
"bctype,supports_b",
[
("invalid", True),
("CODE128", False),
],
)
def test_lacks_support(bctype, supports_b):
"""should raise an error if the barcode type is not supported.
"""
"""should raise an error if the barcode type is not supported."""
profile = Profile(features={BARCODE_B: supports_b})
instance = printer.Dummy(profile=profile)
with pytest.raises(BarcodeTypeError):
instance.barcode('test', bctype)
instance.barcode("test", bctype)
assert instance.output == b''
assert instance.output == b""
@pytest.mark.parametrize("bctype,data", [
('EAN13', 'AA'),
('CODE128', '{D2354AA'),
])
@pytest.mark.parametrize(
"bctype,data",
[
("EAN13", "AA"),
("CODE128", "{D2354AA"),
],
)
def test_code_check(bctype, data):
"""should raise an error if the barcode code is invalid.
"""
"""should raise an error if the barcode code is invalid."""
instance = printer.Dummy()
with pytest.raises(BarcodeCodeError):
instance.barcode(data, bctype)
assert instance.output == b''
assert instance.output == b""

View File

@@ -6,10 +6,8 @@ import pytest
def test_raise_CashDrawerError():
"""should raise an error if the sequence is invalid.
"""
"""should raise an error if the sequence is invalid."""
instance = printer.Dummy()
with pytest.raises(CashDrawerError):
# call with sequence that is too long
instance.cashdraw([1,1,1,1,1,1])
instance.cashdraw([1, 1, 1, 1, 1, 1])

View File

@@ -6,95 +6,101 @@ import escpos.printer as printer
import pytest
@pytest.mark.parametrize("bctype,data", [
('UPC-A', '01234567890'),
('UPC-A', '012345678905'),
('UPC-E', '01234567'),
('UPC-E', '0123456'),
('UPC-E', '012345678905'),
('EAN13', '0123456789012'),
('EAN13', '012345678901'),
('EAN8', '01234567'),
('EAN8', '0123456'),
('CODE39', 'ABC-1234'),
('CODE39', 'ABC-1234-$$-+A'),
('CODE39', '*WIKIPEDIA*'),
('ITF', '010203040506070809'),
('ITF', '11221133113344556677889900'),
('CODABAR', 'A2030405060B'),
('CODABAR', 'C11221133113344556677889900D'),
('CODABAR', 'D0D'),
('NW7', 'A2030405060B'),
('NW7', 'C11221133113344556677889900D'),
('NW7', 'D0D'),
('CODE93', 'A2030405060B'),
('CODE93', '+:$&23-7@$'),
('CODE93', 'D0D'),
('CODE128', '{A2030405060B'),
('CODE128', '{C+:$&23-7@$'),
('CODE128', '{B0D'),
('GS1-128', '{A2030405060B'),
('GS1-128', '{C+:$&23-7@$'),
('GS1-128', '{B0D'),
('GS1 DATABAR OMNIDIRECTIONAL', '0123456789123'),
('GS1 DATABAR TRUNCATED', '0123456789123'),
('GS1 DATABAR LIMITED', '0123456789123'),
('GS1 DATABAR EXPANDED', '(9A{A20304+-%&06a0B'),
('GS1 DATABAR EXPANDED', '(1 {C+:&23-7%'),
('GS1 DATABAR EXPANDED', '(00000001234567678'),
])
@pytest.mark.parametrize(
"bctype,data",
[
("UPC-A", "01234567890"),
("UPC-A", "012345678905"),
("UPC-E", "01234567"),
("UPC-E", "0123456"),
("UPC-E", "012345678905"),
("EAN13", "0123456789012"),
("EAN13", "012345678901"),
("EAN8", "01234567"),
("EAN8", "0123456"),
("CODE39", "ABC-1234"),
("CODE39", "ABC-1234-$$-+A"),
("CODE39", "*WIKIPEDIA*"),
("ITF", "010203040506070809"),
("ITF", "11221133113344556677889900"),
("CODABAR", "A2030405060B"),
("CODABAR", "C11221133113344556677889900D"),
("CODABAR", "D0D"),
("NW7", "A2030405060B"),
("NW7", "C11221133113344556677889900D"),
("NW7", "D0D"),
("CODE93", "A2030405060B"),
("CODE93", "+:$&23-7@$"),
("CODE93", "D0D"),
("CODE128", "{A2030405060B"),
("CODE128", "{C+:$&23-7@$"),
("CODE128", "{B0D"),
("GS1-128", "{A2030405060B"),
("GS1-128", "{C+:$&23-7@$"),
("GS1-128", "{B0D"),
("GS1 DATABAR OMNIDIRECTIONAL", "0123456789123"),
("GS1 DATABAR TRUNCATED", "0123456789123"),
("GS1 DATABAR LIMITED", "0123456789123"),
("GS1 DATABAR EXPANDED", "(9A{A20304+-%&06a0B"),
("GS1 DATABAR EXPANDED", "(1 {C+:&23-7%"),
("GS1 DATABAR EXPANDED", "(00000001234567678"),
],
)
def test_check_valid_barcode(bctype, data):
assert (printer.Escpos.check_barcode(bctype, data))
assert printer.Escpos.check_barcode(bctype, data)
@pytest.mark.parametrize("bctype,data", [
('UPC-A', '01234567890123'), # too long
('UPC-A', '0123456789'), # too short
('UPC-A', '72527273-711'), # invalid '-'
('UPC-A', 'A12345678901'), # invalid 'A'
('UPC-E', '01234567890123'), # too long
('UPC-E', '012345'), # too short
('UPC-E', '72527-2'), # invalid '-'
('UPC-E', 'A123456'), # invalid 'A'
('EAN13', '0123456789'), # too short
('EAN13', 'A123456789012'), # invalid 'A'
('EAN13', '012345678901234'), # too long
('EAN8', '012345'), # too short
('EAN8', 'A123456789012'), # invalid 'A'
('EAN8', '012345678901234'), # too long
('CODE39', 'ALKJ_34'), # invalid '_'
('CODE39', 'A' * 256), # too long
('ITF', '010203040'), # odd length
('ITF', '0' * 256), # too long
('ITF', 'AB01'), # invalid 'A'
('CODABAR', '010203040'), # no start/stop
('CODABAR', '0' * 256), # too long
('CODABAR', 'AB-01F'), # invalid 'B'
('NW7', '010203040'), # no start/stop
('NW7', '0' * 256), # too long
('NW7', 'AB-01F'), # invalid 'B'
('CODE93', 'é010203040'), # invalid 'é'
('CODE93', '0' * 256), # too long
('CODE128', '010203040'), # missing leading {
('CODE128', '{D2354AA'), # second char not between A-C
('CODE128', '0' * 256), # too long
('GS1-128', '010203040'), # missing leading {
('GS1-128', '{D2354AA'), # second char not between A-C
('GS1-128', '0' * 256), # too long
('GS1 DATABAR OMNIDIRECTIONAL', '01234567891234'), # too long
('GS1 DATABAR OMNIDIRECTIONAL', '012345678912'), # too short
('GS1 DATABAR OMNIDIRECTIONAL', '012345678A1234'), # invalid 'A'
('GS1 DATABAR TRUNCATED', '01234567891234'), # too long
('GS1 DATABAR TRUNCATED', '012345678912'), # too short
('GS1 DATABAR TRUNCATED', '012345678A1234'), # invalid 'A'
('GS1 DATABAR LIMITED', '01234567891234'), # too long
('GS1 DATABAR LIMITED', '012345678912'), # too short
('GS1 DATABAR LIMITED', '012345678A1234'), # invalid 'A'
('GS1 DATABAR LIMITED', '02345678912341'), # invalid start (should be 01)
('GS1 DATABAR EXPANDED', '010203040'), # missing leading (
('GS1-128', '(' + ('0' * 256)), # too long
('GS1 DATABAR EXPANDED', '(a{D2354AA'), # second char not between 0-9
('GS1 DATABAR EXPANDED', 'IT will fail'), # first char not '('
])
@pytest.mark.parametrize(
"bctype,data",
[
("UPC-A", "01234567890123"), # too long
("UPC-A", "0123456789"), # too short
("UPC-A", "72527273-711"), # invalid '-'
("UPC-A", "A12345678901"), # invalid 'A'
("UPC-E", "01234567890123"), # too long
("UPC-E", "012345"), # too short
("UPC-E", "72527-2"), # invalid '-'
("UPC-E", "A123456"), # invalid 'A'
("EAN13", "0123456789"), # too short
("EAN13", "A123456789012"), # invalid 'A'
("EAN13", "012345678901234"), # too long
("EAN8", "012345"), # too short
("EAN8", "A123456789012"), # invalid 'A'
("EAN8", "012345678901234"), # too long
("CODE39", "ALKJ_34"), # invalid '_'
("CODE39", "A" * 256), # too long
("ITF", "010203040"), # odd length
("ITF", "0" * 256), # too long
("ITF", "AB01"), # invalid 'A'
("CODABAR", "010203040"), # no start/stop
("CODABAR", "0" * 256), # too long
("CODABAR", "AB-01F"), # invalid 'B'
("NW7", "010203040"), # no start/stop
("NW7", "0" * 256), # too long
("NW7", "AB-01F"), # invalid 'B'
("CODE93", "é010203040"), # invalid 'é'
("CODE93", "0" * 256), # too long
("CODE128", "010203040"), # missing leading {
("CODE128", "{D2354AA"), # second char not between A-C
("CODE128", "0" * 256), # too long
("GS1-128", "010203040"), # missing leading {
("GS1-128", "{D2354AA"), # second char not between A-C
("GS1-128", "0" * 256), # too long
("GS1 DATABAR OMNIDIRECTIONAL", "01234567891234"), # too long
("GS1 DATABAR OMNIDIRECTIONAL", "012345678912"), # too short
("GS1 DATABAR OMNIDIRECTIONAL", "012345678A1234"), # invalid 'A'
("GS1 DATABAR TRUNCATED", "01234567891234"), # too long
("GS1 DATABAR TRUNCATED", "012345678912"), # too short
("GS1 DATABAR TRUNCATED", "012345678A1234"), # invalid 'A'
("GS1 DATABAR LIMITED", "01234567891234"), # too long
("GS1 DATABAR LIMITED", "012345678912"), # too short
("GS1 DATABAR LIMITED", "012345678A1234"), # invalid 'A'
("GS1 DATABAR LIMITED", "02345678912341"), # invalid start (should be 01)
("GS1 DATABAR EXPANDED", "010203040"), # missing leading (
("GS1-128", "(" + ("0" * 256)), # too long
("GS1 DATABAR EXPANDED", "(a{D2354AA"), # second char not between 0-9
("GS1 DATABAR EXPANDED", "IT will fail"), # first char not '('
],
)
def test_check_invalid_barcode(bctype, data):
assert (not printer.Escpos.check_barcode(bctype, data))
assert not printer.Escpos.check_barcode(bctype, data)

View File

@@ -1,4 +1,3 @@
import six
import escpos.printer as printer
@@ -9,5 +8,5 @@ def test_cut_without_feed():
"""Test cut without feeding paper"""
instance = printer.Dummy()
instance.cut(feed=False)
expected = GS + b'V' + six.int2byte(66) + b'\x00'
assert(instance.output == expected)
expected = GS + b"V" + six.int2byte(66) + b"\x00"
assert instance.output == expected

View File

@@ -1,8 +1,9 @@
from nose.tools import assert_raises
from escpos.printer import Dummy
def test_printer_dummy_clear():
printer = Dummy()
printer.text("Hello")
printer.clear()
assert(printer.output == b'')
assert printer.output == b""

View File

@@ -22,13 +22,13 @@ def test_bit_image_black():
Test printing solid black bit image (raster)
"""
instance = printer.Dummy()
instance.image('test/resources/canvas_black.png', impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\x80')
instance.image("test/resources/canvas_black.png", impl="bitImageRaster")
assert instance.output == b"\x1dv0\x00\x01\x00\x01\x00\x80"
# Same thing w/ object created on the fly, rather than a filename
instance = printer.Dummy()
im = Image.new("RGB", (1, 1), (0, 0, 0))
instance.image(im, impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\x80')
assert instance.output == b"\x1dv0\x00\x01\x00\x01\x00\x80"
def test_bit_image_white():
@@ -36,8 +36,8 @@ def test_bit_image_white():
Test printing solid white bit image (raster)
"""
instance = printer.Dummy()
instance.image('test/resources/canvas_white.png', impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\x00')
instance.image("test/resources/canvas_white.png", impl="bitImageRaster")
assert instance.output == b"\x1dv0\x00\x01\x00\x01\x00\x00"
def test_bit_image_both():
@@ -45,8 +45,8 @@ def test_bit_image_both():
Test printing black/white bit image (raster)
"""
instance = printer.Dummy()
instance.image('test/resources/black_white.png', impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x02\x00\xc0\x00')
instance.image("test/resources/black_white.png", impl="bitImageRaster")
assert instance.output == b"\x1dv0\x00\x01\x00\x02\x00\xc0\x00"
def test_bit_image_transparent():
@@ -54,8 +54,8 @@ def test_bit_image_transparent():
Test printing black/transparent bit image (raster)
"""
instance = printer.Dummy()
instance.image('test/resources/black_transparent.png', impl="bitImageRaster")
assert(instance.output == b'\x1dv0\x00\x01\x00\x02\x00\xc0\x00')
instance.image("test/resources/black_transparent.png", impl="bitImageRaster")
assert instance.output == b"\x1dv0\x00\x01\x00\x02\x00\xc0\x00"
# Column format print
@@ -64,8 +64,8 @@ def test_bit_image_colfmt_black():
Test printing solid black bit image (column format)
"""
instance = printer.Dummy()
instance.image('test/resources/canvas_black.png', impl="bitImageColumn")
assert(instance.output == b'\x1b3\x10\x1b*!\x01\x00\x80\x00\x00\x0a\x1b2')
instance.image("test/resources/canvas_black.png", impl="bitImageColumn")
assert instance.output == b"\x1b3\x10\x1b*!\x01\x00\x80\x00\x00\x0a\x1b2"
def test_bit_image_colfmt_white():
@@ -73,8 +73,8 @@ def test_bit_image_colfmt_white():
Test printing solid white bit image (column format)
"""
instance = printer.Dummy()
instance.image('test/resources/canvas_white.png', impl="bitImageColumn")
assert(instance.output == b'\x1b3\x10\x1b*!\x01\x00\x00\x00\x00\x0a\x1b2')
instance.image("test/resources/canvas_white.png", impl="bitImageColumn")
assert instance.output == b"\x1b3\x10\x1b*!\x01\x00\x00\x00\x00\x0a\x1b2"
def test_bit_image_colfmt_both():
@@ -82,8 +82,10 @@ def test_bit_image_colfmt_both():
Test printing black/white bit image (column format)
"""
instance = printer.Dummy()
instance.image('test/resources/black_white.png', impl="bitImageColumn")
assert(instance.output == b'\x1b3\x10\x1b*!\x02\x00\x80\x00\x00\x80\x00\x00\x0a\x1b2')
instance.image("test/resources/black_white.png", impl="bitImageColumn")
assert (
instance.output == b"\x1b3\x10\x1b*!\x02\x00\x80\x00\x00\x80\x00\x00\x0a\x1b2"
)
def test_bit_image_colfmt_transparent():
@@ -91,8 +93,10 @@ def test_bit_image_colfmt_transparent():
Test printing black/transparent bit image (column format)
"""
instance = printer.Dummy()
instance.image('test/resources/black_transparent.png', impl="bitImageColumn")
assert(instance.output == b'\x1b3\x10\x1b*!\x02\x00\x80\x00\x00\x80\x00\x00\x0a\x1b2')
instance.image("test/resources/black_transparent.png", impl="bitImageColumn")
assert (
instance.output == b"\x1b3\x10\x1b*!\x02\x00\x80\x00\x00\x80\x00\x00\x0a\x1b2"
)
# Graphics print
@@ -101,8 +105,11 @@ def test_graphics_black():
Test printing solid black graphics
"""
instance = printer.Dummy()
instance.image('test/resources/canvas_black.png', impl="graphics")
assert(instance.output == b'\x1d(L\x0b\x000p0\x01\x011\x01\x00\x01\x00\x80\x1d(L\x02\x0002')
instance.image("test/resources/canvas_black.png", impl="graphics")
assert (
instance.output
== b"\x1d(L\x0b\x000p0\x01\x011\x01\x00\x01\x00\x80\x1d(L\x02\x0002"
)
def test_graphics_white():
@@ -110,8 +117,11 @@ def test_graphics_white():
Test printing solid white graphics
"""
instance = printer.Dummy()
instance.image('test/resources/canvas_white.png', impl="graphics")
assert(instance.output == b'\x1d(L\x0b\x000p0\x01\x011\x01\x00\x01\x00\x00\x1d(L\x02\x0002')
instance.image("test/resources/canvas_white.png", impl="graphics")
assert (
instance.output
== b"\x1d(L\x0b\x000p0\x01\x011\x01\x00\x01\x00\x00\x1d(L\x02\x0002"
)
def test_graphics_both():
@@ -119,8 +129,11 @@ def test_graphics_both():
Test printing black/white graphics
"""
instance = printer.Dummy()
instance.image('test/resources/black_white.png', impl="graphics")
assert(instance.output == b'\x1d(L\x0c\x000p0\x01\x011\x02\x00\x02\x00\xc0\x00\x1d(L\x02\x0002')
instance.image("test/resources/black_white.png", impl="graphics")
assert (
instance.output
== b"\x1d(L\x0c\x000p0\x01\x011\x02\x00\x02\x00\xc0\x00\x1d(L\x02\x0002"
)
def test_graphics_transparent():
@@ -128,8 +141,11 @@ def test_graphics_transparent():
Test printing black/transparent graphics
"""
instance = printer.Dummy()
instance.image('test/resources/black_transparent.png', impl="graphics")
assert(instance.output == b'\x1d(L\x0c\x000p0\x01\x011\x02\x00\x02\x00\xc0\x00\x1d(L\x02\x0002')
instance.image("test/resources/black_transparent.png", impl="graphics")
assert (
instance.output
== b"\x1d(L\x0c\x000p0\x01\x011\x02\x00\x02\x00\xc0\x00\x1d(L\x02\x0002"
)
def test_large_graphics():
@@ -137,20 +153,19 @@ def test_large_graphics():
Test whether 'large' graphics that induce a fragmentation are handled correctly.
"""
instance = printer.Dummy()
instance.image('test/resources/black_white.png', impl="bitImageRaster", fragment_height=1)
assert(instance.output == b'\x1dv0\x00\x01\x00\x01\x00\xc0\x1dv0\x00\x01\x00\x01\x00\x00')
instance.image(
"test/resources/black_white.png", impl="bitImageRaster", fragment_height=1
)
assert (
instance.output
== b"\x1dv0\x00\x01\x00\x01\x00\xc0\x1dv0\x00\x01\x00\x01\x00\x00"
)
@pytest.fixture
def dummy_with_width():
instance = printer.Dummy()
instance.profile.profile_data = {
'media': {
'width': {
'pixels': 384
}
}
}
instance.profile.profile_data = {"media": {"width": {"pixels": 384}}}
return instance

View File

@@ -15,17 +15,18 @@ def test_function_linedisplay_select_on():
"""test the linedisplay_select function (activate)"""
instance = printer.Dummy()
instance.linedisplay_select(select_display=True)
assert(instance.output == b'\x1B\x3D\x02')
assert instance.output == b"\x1B\x3D\x02"
def test_function_linedisplay_select_off():
"""test the linedisplay_select function (deactivate)"""
instance = printer.Dummy()
instance.linedisplay_select(select_display=False)
assert(instance.output == b'\x1B\x3D\x01')
assert instance.output == b"\x1B\x3D\x01"
def test_function_linedisplay_clear():
"""test the linedisplay_clear function"""
instance = printer.Dummy()
instance.linedisplay_clear()
assert(instance.output == b'\x1B\x40')
assert instance.output == b"\x1B\x40"

View File

@@ -15,11 +15,11 @@ def test_function_panel_button_on():
"""test the panel button function (enabling) by comparing output"""
instance = printer.Dummy()
instance.panel_buttons()
assert(instance.output == b'\x1B\x63\x35\x00')
assert instance.output == b"\x1B\x63\x35\x00"
def test_function_panel_button_off():
"""test the panel button function (disabling) by comparing output"""
instance = printer.Dummy()
instance.panel_buttons(False)
assert(instance.output == b'\x1B\x63\x35\x01')
assert instance.output == b"\x1B\x63\x35\x01"

View File

@@ -19,42 +19,51 @@ def test_defaults():
"""Test QR code with defaults"""
instance = printer.Dummy()
instance.qr("1234", native=True)
expected = b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d' \
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
expected = (
b"\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d"
b"(k\x07\x001P01234\x1d(k\x03\x001Q0"
)
assert instance.output == expected
def test_empty():
"""Test QR printing blank code"""
instance = printer.Dummy()
instance.qr("", native=True)
assert(instance.output == b'')
assert instance.output == b""
def test_ec():
"""Test QR error correction setting"""
instance = printer.Dummy()
instance.qr("1234", native=True, ec=QR_ECLEVEL_H)
expected = b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E3\x1d' \
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
expected = (
b"\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E3\x1d"
b"(k\x07\x001P01234\x1d(k\x03\x001Q0"
)
assert instance.output == expected
def test_size():
"""Test QR box size"""
instance = printer.Dummy()
instance.qr("1234", native=True, size=7)
expected = b'\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x07\x1d(k\x03\x001E0\x1d' \
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
expected = (
b"\x1d(k\x04\x001A2\x00\x1d(k\x03\x001C\x07\x1d(k\x03\x001E0\x1d"
b"(k\x07\x001P01234\x1d(k\x03\x001Q0"
)
assert instance.output == expected
def test_model():
"""Test QR model"""
instance = printer.Dummy()
instance.qr("1234", native=True, model=QR_MODEL_1)
expected = b'\x1d(k\x04\x001A1\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d' \
b'(k\x07\x001P01234\x1d(k\x03\x001Q0'
assert(instance.output == expected)
expected = (
b"\x1d(k\x04\x001A1\x00\x1d(k\x03\x001C\x03\x1d(k\x03\x001E0\x1d"
b"(k\x07\x001P01234\x1d(k\x03\x001Q0"
)
assert instance.output == expected
@raises(ValueError)
@@ -84,12 +93,14 @@ def test_image():
instance = printer.Dummy()
instance.qr("1", native=False, size=1)
print(instance.output)
expected = b'\x1bt\x00\n' \
b'\x1dv0\x00\x03\x00\x17\x00\x00\x00\x00\x7f]\xfcA\x19\x04]it]et' \
b']ItA=\x04\x7fU\xfc\x00\x0c\x00y~t4\x7f =\xa84j\xd9\xf0\x05\xd4\x90\x00' \
b'i(\x7f<\xa8A \xd8]\'\xc4]y\xf8]E\x80Ar\x94\x7fR@\x00\x00\x00' \
b'\n\n'
assert(instance.output == expected)
expected = (
b"\x1bt\x00\n"
b"\x1dv0\x00\x03\x00\x17\x00\x00\x00\x00\x7f]\xfcA\x19\x04]it]et"
b"]ItA=\x04\x7fU\xfc\x00\x0c\x00y~t4\x7f =\xa84j\xd9\xf0\x05\xd4\x90\x00"
b"i(\x7f<\xa8A \xd8]'\xc4]y\xf8]E\x80Ar\x94\x7fR@\x00\x00\x00"
b"\n\n"
)
assert instance.output == expected
@raises(ValueError)

View File

@@ -16,7 +16,7 @@ from escpos.printer import Dummy
from PIL import Image
@mock.patch('escpos.printer.Dummy.image', spec=Dummy)
@mock.patch("escpos.printer.Dummy.image", spec=Dummy)
def test_type_of_object_passed_to_image_function(img_function):
"""
Test the type of object that is passed to the image function during non-native qr-printing.

View File

@@ -1,4 +1,3 @@
import six
import escpos.printer as printer
@@ -8,41 +7,46 @@ from escpos.constants import TXT_SIZE
# Default test, please copy and paste this block to test set method calls
def test_default_values():
instance = printer.Dummy()
instance.set()
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert(instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
# Size tests
def test_set_size_2h():
instance = printer.Dummy()
instance.set(double_height=True)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['2h'], # Double height text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["2h"], # Double height text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert (instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
def test_set_size_2w():
@@ -50,17 +54,18 @@ def test_set_size_2w():
instance.set(double_width=True)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['2w'], # Double width text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["2w"], # Double width text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert (instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
def test_set_size_2x():
@@ -68,17 +73,18 @@ def test_set_size_2x():
instance.set(double_height=True, double_width=True)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['2x'], # Double text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["2x"], # Double text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert (instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
def test_set_size_custom():
@@ -87,55 +93,61 @@ def test_set_size_custom():
expected_sequence = (
TXT_SIZE, # Custom text size, no normal reset
six.int2byte(TXT_STYLE['width'][8] + TXT_STYLE['height'][7]),
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
six.int2byte(TXT_STYLE["width"][8] + TXT_STYLE["height"][7]),
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert (instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
# Flip
def test_set_flip():
instance = printer.Dummy()
instance.set(flip=True)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][True], # Flip ON
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][True], # Flip ON
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert (instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
# Smooth
def test_smooth():
instance = printer.Dummy()
instance.set(smooth=True)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][True], # Smooth ON
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][True], # Smooth ON
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert(instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
# Type
@@ -146,17 +158,18 @@ def test_set_bold():
instance.set(bold=True)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][True], # Bold ON
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][True], # Bold ON
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert (instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
def test_set_underline():
@@ -164,17 +177,18 @@ def test_set_underline():
instance.set(underline=1)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][1], # Underline ON, type 1
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][1], # Underline ON, type 1
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert (instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
def test_set_underline2():
@@ -182,59 +196,64 @@ def test_set_underline2():
instance.set(underline=2)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][2], # Underline ON, type 2
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][2], # Underline ON, type 2
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][False], # Inverted OFF
)
assert (instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
# Align
def test_align_center():
instance = printer.Dummy()
instance.set(align='center')
instance.set(align="center")
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['center'], # Align center
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["center"], # Align center
TXT_STYLE["invert"][False], # Inverted OFF
)
assert(instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
def test_align_right():
instance = printer.Dummy()
instance.set(align='right')
instance.set(align="right")
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['right'], # Align right
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["right"], # Align right
TXT_STYLE["invert"][False], # Inverted OFF
)
assert(instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
# Densities
def test_densities():
for density in range(8):
@@ -242,35 +261,38 @@ def test_densities():
instance.set(density=density)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['density'][density], # Custom density from 0 to 8
TXT_STYLE['invert'][False] # Inverted OFF
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["density"][density], # Custom density from 0 to 8
TXT_STYLE["invert"][False], # Inverted OFF
)
assert(instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)
# Invert
def test_invert():
instance = printer.Dummy()
instance.set(invert=True)
expected_sequence = (
TXT_NORMAL, TXT_STYLE['size']['normal'], # Normal text size
TXT_STYLE['flip'][False], # Flip OFF
TXT_STYLE['smooth'][False], # Smooth OFF
TXT_STYLE['bold'][False], # Bold OFF
TXT_STYLE['underline'][0], # Underline OFF
SET_FONT(b'\x00'), # Default font
TXT_STYLE['align']['left'], # Align left
TXT_STYLE['invert'][True] # Inverted ON
TXT_NORMAL,
TXT_STYLE["size"]["normal"], # Normal text size
TXT_STYLE["flip"][False], # Flip OFF
TXT_STYLE["smooth"][False], # Smooth OFF
TXT_STYLE["bold"][False], # Bold OFF
TXT_STYLE["underline"][0], # Underline OFF
SET_FONT(b"\x00"), # Default font
TXT_STYLE["align"]["left"], # Align left
TXT_STYLE["invert"][True], # Inverted ON
)
assert(instance.output == b''.join(expected_sequence))
assert instance.output == b"".join(expected_sequence)

View File

@@ -9,11 +9,13 @@ import pytest
def instance():
return printer.Dummy()
def test_soft_barcode_ean8_invalid(instance):
"""test with an invalid barcode"""
with pytest.raises(barcode.errors.BarcodeError):
instance.soft_barcode("ean8", "1234")
def test_soft_barcode_ean8(instance):
"""test with a valid ean8 barcode"""
instance.soft_barcode("ean8", "1234567")

View File

@@ -16,13 +16,12 @@ from escpos.printer import Dummy
def get_printer():
return Dummy(magic_encode_args={'disabled': True, 'encoding': 'CP437'})
return Dummy(magic_encode_args={"disabled": True, "encoding": "CP437"})
@given(text=st.text())
def test_text(text):
"""Test that text() calls the MagicEncode object.
"""
"""Test that text() calls the MagicEncode object."""
instance = get_printer()
instance.magic.write = mock.Mock()
instance.text(text)
@@ -32,30 +31,32 @@ def test_text(text):
def test_block_text():
printer = get_printer()
printer.block_text(
"All the presidents men were eating falafel for breakfast.", font='a')
assert printer.output == \
b'All the presidents men were eating falafel\nfor breakfast.'
"All the presidents men were eating falafel for breakfast.", font="a"
)
assert (
printer.output == b"All the presidents men were eating falafel\nfor breakfast."
)
def test_textln():
printer = get_printer()
printer.textln('hello, world')
assert printer.output == b'hello, world\n'
printer.textln("hello, world")
assert printer.output == b"hello, world\n"
def test_textln_empty():
printer = get_printer()
printer.textln()
assert printer.output == b'\n'
assert printer.output == b"\n"
def test_ln():
printer = get_printer()
printer.ln()
assert printer.output == b'\n'
assert printer.output == b"\n"
def test_multiple_ln():
printer = get_printer()
printer.ln(3)
assert printer.output == b'\n\n\n'
assert printer.output == b"\n\n\n"

View File

@@ -5,13 +5,13 @@ from escpos.printer import Dummy
def test_line_spacing_code_gen():
printer = Dummy()
printer.line_spacing(10)
assert printer.output == b'\x1b3\n'
assert printer.output == b"\x1b3\n"
def test_line_spacing_rest():
printer = Dummy()
printer.line_spacing()
assert printer.output == b'\x1b2'
assert printer.output == b"\x1b2"
def test_line_spacing_error_handling():

View File

@@ -15,57 +15,67 @@ def test_image_black():
"""
Test rendering solid black image
"""
for img_format in ['png', 'jpg', 'gif']:
_load_and_check_img('canvas_black.' + img_format, 1, 1, b'\x80', [b'\x80'])
for img_format in ["png", "jpg", "gif"]:
_load_and_check_img("canvas_black." + img_format, 1, 1, b"\x80", [b"\x80"])
def test_image_black_transparent():
"""
Test rendering black/transparent image
"""
for img_format in ['png', 'gif']:
_load_and_check_img('black_transparent.' + img_format, 2, 2, b'\xc0\x00', [b'\x80\x80'])
for img_format in ["png", "gif"]:
_load_and_check_img(
"black_transparent." + img_format, 2, 2, b"\xc0\x00", [b"\x80\x80"]
)
def test_image_black_white():
"""
Test rendering black/white image
"""
for img_format in ['png', 'jpg', 'gif']:
_load_and_check_img('black_white.' + img_format, 2, 2, b'\xc0\x00', [b'\x80\x80'])
for img_format in ["png", "jpg", "gif"]:
_load_and_check_img(
"black_white." + img_format, 2, 2, b"\xc0\x00", [b"\x80\x80"]
)
def test_image_white():
"""
Test rendering solid white image
"""
for img_format in ['png', 'jpg', 'gif']:
_load_and_check_img('canvas_white.' + img_format, 1, 1, b'\x00', [b'\x00'])
for img_format in ["png", "jpg", "gif"]:
_load_and_check_img("canvas_white." + img_format, 1, 1, b"\x00", [b"\x00"])
def test_split():
"""
test whether the split-function works as expected
"""
im = EscposImage('test/resources/black_white.png')
im = EscposImage("test/resources/black_white.png")
(upper_part, lower_part) = im.split(1)
upper_part = EscposImage(upper_part)
lower_part = EscposImage(lower_part)
assert(upper_part.width == lower_part.width == 2)
assert(upper_part.height == lower_part.height == 1)
assert(upper_part.to_raster_format() == b'\xc0')
assert(lower_part.to_raster_format() == b'\x00')
assert upper_part.width == lower_part.width == 2
assert upper_part.height == lower_part.height == 1
assert upper_part.to_raster_format() == b"\xc0"
assert lower_part.to_raster_format() == b"\x00"
def _load_and_check_img(filename, width_expected, height_expected, raster_format_expected, column_format_expected):
def _load_and_check_img(
filename,
width_expected,
height_expected,
raster_format_expected,
column_format_expected,
):
"""
Load an image, and test whether raster & column formatted output, sizes, etc match expectations.
"""
im = EscposImage('test/resources/' + filename)
assert(im.width == width_expected)
assert(im.height == height_expected)
assert(im.to_raster_format() == raster_format_expected)
i = 0
im = EscposImage("test/resources/" + filename)
assert im.width == width_expected
assert im.height == height_expected
assert im.to_raster_format() == raster_format_expected
i = 0
for row in im.to_column_format(False):
assert(row == column_format_expected[i])
assert row == column_format_expected[i]
i += 1

View File

@@ -14,4 +14,4 @@ import escpos.printer as printer
def test_instantiation():
"""test the instantiation of a escpos-printer class and basic printing"""
instance = printer.Dummy()
instance.text('This is a test\n')
instance.text("This is a test\n")

View File

@@ -24,17 +24,17 @@ class TestEncoder:
"""
def test_can_encode(self):
assert not Encoder({'CP437': 1}).can_encode('CP437', u'')
assert Encoder({'CP437': 1}).can_encode('CP437', u'á')
assert not Encoder({'foobar': 1}).can_encode('foobar', 'a')
assert not Encoder({"CP437": 1}).can_encode("CP437", u"")
assert Encoder({"CP437": 1}).can_encode("CP437", u"á")
assert not Encoder({"foobar": 1}).can_encode("foobar", "a")
def test_find_suitable_encoding(self):
assert not Encoder({'CP437': 1}).find_suitable_encoding(u'')
assert Encoder({'CP858': 1}).find_suitable_encoding(u'') == 'CP858'
assert not Encoder({"CP437": 1}).find_suitable_encoding(u"")
assert Encoder({"CP858": 1}).find_suitable_encoding(u"") == "CP858"
@raises(ValueError)
def test_get_encoding(self):
Encoder({}).get_encoding_name('latin1')
Encoder({}).get_encoding_name("latin1")
class TestMagicEncode:
@@ -57,50 +57,50 @@ class TestMagicEncode:
MagicEncode(driver, disabled=True)
class TestWriteWithEncoding:
def test_init_from_none(self, driver):
encode = MagicEncode(driver, encoding=None)
encode.write_with_encoding('CP858', '€ ist teuro.')
assert driver.output == b'\x1bt\x13\xd5 ist teuro.'
encode.write_with_encoding("CP858", "€ ist teuro.")
assert driver.output == b"\x1bt\x13\xd5 ist teuro."
def test_change_from_another(self, driver):
encode = MagicEncode(driver, encoding='CP437')
encode.write_with_encoding('CP858', '€ ist teuro.')
assert driver.output == b'\x1bt\x13\xd5 ist teuro.'
encode = MagicEncode(driver, encoding="CP437")
encode.write_with_encoding("CP858", "€ ist teuro.")
assert driver.output == b"\x1bt\x13\xd5 ist teuro."
def test_no_change(self, driver):
encode = MagicEncode(driver, encoding='CP858')
encode.write_with_encoding('CP858', '€ ist teuro.')
assert driver.output == b'\xd5 ist teuro.'
encode = MagicEncode(driver, encoding="CP858")
encode.write_with_encoding("CP858", "€ ist teuro.")
assert driver.output == b"\xd5 ist teuro."
class TestWrite:
def test_write(self, driver):
encode = MagicEncode(driver)
encode.write('€ ist teuro.')
assert driver.output == b'\x1bt\x0f\xa4 ist teuro.'
encode.write("€ ist teuro.")
assert driver.output == b"\x1bt\x0f\xa4 ist teuro."
def test_write_disabled(self, driver):
encode = MagicEncode(driver, encoding='CP437', disabled=True)
encode.write('€ ist teuro.')
assert driver.output == b'? ist teuro.'
encode = MagicEncode(driver, encoding="CP437", disabled=True)
encode.write("€ ist teuro.")
assert driver.output == b"? ist teuro."
def test_write_no_codepage(self, driver):
encode = MagicEncode(
driver, defaultsymbol="_", encoder=Encoder({'CP437': 1}),
encoding='CP437')
encode.write(u'€ ist teuro.')
assert driver.output == b'_ ist teuro.'
driver,
defaultsymbol="_",
encoder=Encoder({"CP437": 1}),
encoding="CP437",
)
encode.write(u"€ ist teuro.")
assert driver.output == b"_ ist teuro."
class TestForceEncoding:
def test(self, driver):
encode = MagicEncode(driver)
encode.force_encoding('CP437')
assert driver.output == b'\x1bt\x00'
encode.force_encoding("CP437")
assert driver.output == b"\x1bt\x00"
encode.write('€ ist teuro.')
assert driver.output == b'\x1bt\x00? ist teuro.'
encode.write("€ ist teuro.")
assert driver.output == b"\x1bt\x00? ist teuro."
try:
@@ -119,5 +119,5 @@ class TestKatakana:
encode_katakana(text)
def test_result(self):
assert encode_katakana('カタカナ') == b'\xb6\xc0\xb6\xc5'
assert encode_katakana("あいうえお") == b'\xb1\xb2\xb3\xb4\xb5'
assert encode_katakana("カタカナ") == b"\xb6\xc0\xb6\xc5"
assert encode_katakana("あいうえお") == b"\xb1\xb2\xb3\xb4\xb5"

View File

@@ -18,16 +18,16 @@ from hypothesis.strategies import text
import escpos.printer as printer
if six.PY3:
mock_open_call = 'builtins.open'
mock_open_call = "builtins.open"
else:
mock_open_call = '__builtin__.open'
mock_open_call = "__builtin__.open"
@pytest.mark.skip("this test is broken and has to be fixed or discarded")
@given(path=text())
def test_load_file_printer(mocker, path):
"""test the loading of the file-printer"""
mock_escpos = mocker.patch('escpos.escpos.Escpos.__init__')
mock_escpos = mocker.patch("escpos.escpos.Escpos.__init__")
mock_open = mocker.patch(mock_open_call)
printer.File(devfile=path)
assert mock_escpos.called
@@ -38,9 +38,9 @@ def test_load_file_printer(mocker, path):
@given(txt=text())
def test_auto_flush(mocker, txt):
"""test auto_flush in file-printer"""
mock_escpos = mocker.patch('escpos.escpos.Escpos.__init__')
mock_escpos = mocker.patch("escpos.escpos.Escpos.__init__")
mock_open = mocker.patch(mock_open_call)
mock_device = mocker.patch.object(printer.File, 'device')
mock_device = mocker.patch.object(printer.File, "device")
p = printer.File(auto_flush=False)
# inject the mocked device-object
@@ -60,7 +60,7 @@ def test_auto_flush(mocker, txt):
def test_flush_on_close(mocker, txt):
"""test flush on close in file-printer"""
mock_open = mocker.patch(mock_open_call)
mock_device = mocker.patch.object(printer.File, 'device')
mock_device = mocker.patch.object(printer.File, "device")
p = printer.File(auto_flush=False)
# inject the mocked device-object

View File

@@ -11,6 +11,7 @@ def instance():
socket.socket.connect = mock.Mock()
return printer.Network("localhost")
def test_close_without_open(instance):
"""try to close without opening (should fail gracefully)
@@ -20,4 +21,4 @@ def test_close_without_open(instance):
(if possible, this should not raise)
"""
instance.close()
instance.close()
instance.close()

View File

@@ -4,35 +4,33 @@ from escpos.capabilities import get_profile, NotSupported, BARCODE_B, Profile
@pytest.fixture
def profile():
return get_profile('default')
return get_profile("default")
class TestBaseProfile:
"""Test the `BaseProfile` class.
"""
"""Test the `BaseProfile` class."""
def test_get_font(self, profile):
with pytest.raises(NotSupported):
assert profile.get_font('3')
assert profile.get_font("3")
assert profile.get_font(1) == 1
assert profile.get_font('a') == 0
assert profile.get_font("a") == 0
def test_supports(self, profile):
assert not profile.supports('asdf asdf')
assert not profile.supports("asdf asdf")
assert profile.supports(BARCODE_B)
def test_get_columns(self, profile):
assert profile.get_columns('a') > 5
assert profile.get_columns("a") > 5
with pytest.raises(NotSupported):
assert profile.get_columns('asdfasdf')
assert profile.get_columns("asdfasdf")
class TestCustomProfile:
"""Test custom profile options with the `Profile` class.
"""
"""Test custom profile options with the `Profile` class."""
def test_columns(self):
assert Profile(columns=10).get_columns('sdfasdf') == 10
assert Profile(columns=10).get_columns("sdfasdf") == 10
def test_features(self):
assert Profile(features={'foo': True}).supports('foo')
assert Profile(features={"foo": True}).supports("foo")

View File

@@ -16,5 +16,5 @@ def test_with_statement():
"""Use with statement"""
dummy_printer = printer.Dummy()
with escpos.EscposIO(dummy_printer) as p:
p.writelines('Some text.\n')
p.writelines("Some text.\n")
# TODO extend these tests as they don't really do anything at the moment