improve tests

This commit is contained in:
Patrick Kanzler 2023-09-04 00:18:12 +02:00
parent 8a4816d4d4
commit 8a461bf2aa
3 changed files with 59 additions and 3 deletions

View File

@ -48,7 +48,7 @@ install_requires =
setup_requires = setuptools_scm setup_requires = setuptools_scm
tests_require = tests_require =
jaconv jaconv
tox tox>=4.11
pytest>=7.4 pytest>=7.4
pytest-cov pytest-cov
pytest-mock pytest-mock

View File

@ -872,6 +872,7 @@ class Escpos(object):
invert: Optional[bool] = None, invert: Optional[bool] = None,
smooth: Optional[bool] = None, smooth: Optional[bool] = None,
flip: Optional[bool] = None, flip: Optional[bool] = None,
normal_textsize: Optional[bool] = None,
double_width: Optional[bool] = None, double_width: Optional[bool] = None,
double_height: Optional[bool] = None, double_height: Optional[bool] = None,
custom_size: Optional[bool] = None, custom_size: Optional[bool] = None,
@ -891,6 +892,7 @@ class Escpos(object):
special values 'a' or 'b', referring to fonts 0 and 1. special values 'a' or 'b', referring to fonts 0 and 1.
:param bold: text in bold :param bold: text in bold
:param underline: underline mode for text, decimal range 0-2 :param underline: underline mode for text, decimal range 0-2
:param normal_textsize: switch to normal textsize if True
:param double_height: doubles the height of the text :param double_height: doubles the height of the text
:param double_width: doubles the width of the text :param double_width: doubles the width of the text
:param custom_size: uses custom size specified by width and height :param custom_size: uses custom size specified by width and height
@ -902,7 +904,7 @@ class Escpos(object):
:param smooth: True enables text smoothing. Effective on 4x4 size text and larger :param smooth: True enables text smoothing. Effective on 4x4 size text and larger
:param flip: True enables upside-down printing :param flip: True enables upside-down printing
""" """
if custom_size is not None and custom_size: if custom_size:
if ( if (
isinstance(width, int) isinstance(width, int)
and isinstance(height, int) and isinstance(height, int)
@ -913,7 +915,7 @@ class Escpos(object):
self._raw(TXT_SIZE + six.int2byte(size_byte)) self._raw(TXT_SIZE + six.int2byte(size_byte))
else: else:
raise SetVariableError() raise SetVariableError()
elif custom_size is not None: elif normal_textsize or double_height or double_width:
self._raw(TXT_NORMAL) self._raw(TXT_NORMAL)
if double_width and double_height: if double_width and double_height:
self._raw(TXT_STYLE["size"]["2x"]) self._raw(TXT_STYLE["size"]["2x"])
@ -924,6 +926,7 @@ class Escpos(object):
else: else:
self._raw(TXT_STYLE["size"]["normal"]) self._raw(TXT_STYLE["size"]["normal"])
else: else:
# no text size handling requested
pass pass
if flip is not None: if flip is not None:
@ -993,6 +996,7 @@ class Escpos(object):
:param smooth: True enables text smoothing. Effective on 4x4 size text and larger, *default*: False :param smooth: True enables text smoothing. Effective on 4x4 size text and larger, *default*: False
:param flip: True enables upside-down printing, *default*: False :param flip: True enables upside-down printing, *default*: False
""" """
normal_textsize = not custom_size and not double_width and not double_height
self.set( self.set(
align=align, align=align,
font=font, font=font,
@ -1004,6 +1008,7 @@ class Escpos(object):
invert=invert, invert=invert,
smooth=smooth, smooth=smooth,
flip=flip, flip=flip,
normal_textsize=normal_textsize,
double_width=double_width, double_width=double_width,
double_height=double_height, double_height=double_height,
custom_size=custom_size, custom_size=custom_size,

View File

@ -1,4 +1,5 @@
import six import six
import pytest
import escpos.printer as printer import escpos.printer as printer
from escpos.constants import SET_FONT, TXT_NORMAL, TXT_SIZE, TXT_STYLE from escpos.constants import SET_FONT, TXT_NORMAL, TXT_SIZE, TXT_STYLE
@ -54,6 +55,18 @@ def test_set_size_2h():
assert instance.output == b"".join(expected_sequence) assert instance.output == b"".join(expected_sequence)
def test_set_size_2h_no_default():
instance = printer.Dummy()
instance.set(double_height=True)
expected_sequence = (
TXT_NORMAL,
TXT_STYLE["size"]["2h"], # Double height text size
)
assert instance.output == b"".join(expected_sequence)
def test_set_size_2w(): def test_set_size_2w():
instance = printer.Dummy() instance = printer.Dummy()
instance.set_with_default(double_width=True) instance.set_with_default(double_width=True)
@ -73,6 +86,18 @@ def test_set_size_2w():
assert instance.output == b"".join(expected_sequence) assert instance.output == b"".join(expected_sequence)
def test_set_size_2w_no_default():
instance = printer.Dummy()
instance.set(double_width=True)
expected_sequence = (
TXT_NORMAL,
TXT_STYLE["size"]["2w"], # Double width text size
)
assert instance.output == b"".join(expected_sequence)
def test_set_size_2x(): def test_set_size_2x():
instance = printer.Dummy() instance = printer.Dummy()
instance.set_with_default(double_height=True, double_width=True) instance.set_with_default(double_height=True, double_width=True)
@ -92,6 +117,18 @@ def test_set_size_2x():
assert instance.output == b"".join(expected_sequence) assert instance.output == b"".join(expected_sequence)
def test_set_size_2x_no_default():
instance = printer.Dummy()
instance.set(double_width=True, double_height=True)
expected_sequence = (
TXT_NORMAL,
TXT_STYLE["size"]["2x"], # Quad area text size
)
assert instance.output == b"".join(expected_sequence)
def test_set_size_custom(): def test_set_size_custom():
instance = printer.Dummy() instance = printer.Dummy()
instance.set_with_default(custom_size=True, width=8, height=7) instance.set_with_default(custom_size=True, width=8, height=7)
@ -111,6 +148,20 @@ def test_set_size_custom():
assert instance.output == b"".join(expected_sequence) assert instance.output == b"".join(expected_sequence)
@pytest.mark.parametrize("width", [1, 8])
@pytest.mark.parametrize("height", [1, 8])
def test_set_size_custom_no_default(width, height):
instance = printer.Dummy()
instance.set(custom_size=True, width=width, height=height)
expected_sequence = (
TXT_SIZE, # Custom text size, no normal reset
six.int2byte(TXT_STYLE["width"][width] + TXT_STYLE["height"][height]),
)
assert instance.output == b"".join(expected_sequence)
# Flip # Flip