python-escpos/test/test_function_set.py

376 lines
11 KiB
Python
Raw Normal View History

from typing import Optional
import pytest
2017-05-14 08:38:21 +00:00
import six
import escpos.printer as printer
from escpos.constants import SET_FONT, TXT_NORMAL, TXT_SIZE, TXT_STYLE
from escpos.exceptions import SetVariableError
2017-05-14 08:38:21 +00:00
def test_default_values_with_default() -> None:
"""Default test, please copy and paste this block to test set method calls"""
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default()
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
def test_default_values() -> None:
"""Default test"""
instance = printer.Dummy()
instance.set()
assert instance.output == b""
2017-05-14 08:38:21 +00:00
# Size tests
2021-10-30 16:15:22 +00:00
def test_set_size_2h() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(double_height=True)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
def test_set_size_2h_no_default() -> None:
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() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(double_width=True)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
def test_set_size_2w_no_default() -> None:
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() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(double_height=True, double_width=True)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
def test_set_size_2x_no_default() -> None:
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() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(custom_size=True, width=8, height=7)
2017-05-14 08:38:21 +00:00
expected_sequence = (
TXT_SIZE, # Custom text size, no normal reset
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
@pytest.mark.parametrize("width", [1, 8])
@pytest.mark.parametrize("height", [1, 8])
def test_set_size_custom_no_default(width: int, height: int) -> None:
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)
@pytest.mark.parametrize("width", [None, 0, 9, 10, 4444])
@pytest.mark.parametrize("height", [None, 0, 9, 10, 4444])
def test_set_size_custom_invalid_input(
width: Optional[int], height: Optional[int]
) -> None:
instance = printer.Dummy()
with pytest.raises(SetVariableError):
instance.set(custom_size=True, width=width, height=height)
2017-05-14 08:38:21 +00:00
# Flip
2021-10-30 16:15:22 +00:00
def test_set_flip() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(flip=True)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
def test_set_flip_no_default() -> None:
instance = printer.Dummy()
instance.set(flip=True)
expected_sequence = (TXT_STYLE["flip"][True],) # Flip ON
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
# Smooth
2021-10-30 16:15:22 +00:00
def test_smooth() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(smooth=True)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
# Type
def test_set_bold() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(bold=True)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
def test_set_underline() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(underline=1)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
def test_set_underline2() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(underline=2)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
# Align
2021-10-30 16:15:22 +00:00
def test_align_center() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(align="center")
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
def test_align_right() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(align="right")
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
# Densities
2021-10-30 16:15:22 +00:00
def test_densities() -> None:
2017-05-14 08:38:21 +00:00
for density in range(8):
instance = printer.Dummy()
instance.set_with_default(density=density)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)
2017-05-14 08:38:21 +00:00
# Invert
2021-10-30 16:15:22 +00:00
def test_invert() -> None:
2017-05-14 08:38:21 +00:00
instance = printer.Dummy()
instance.set_with_default(invert=True)
2017-05-14 08:38:21 +00:00
expected_sequence = (
2021-10-30 16:15:22 +00:00
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
2017-05-14 08:38:21 +00:00
)
2021-10-30 16:15:22 +00:00
assert instance.output == b"".join(expected_sequence)