python-escpos/test/test_function_set.py

298 lines
8.7 KiB
Python
Raw Normal View History

2017-05-14 08:38:21 +00:00
import six
import escpos.printer as printer
from escpos.constants import TXT_NORMAL, TXT_STYLE, SET_FONT
from escpos.constants import TXT_SIZE
# Default test, please copy and paste this block to test set method calls
2021-10-30 16:15:22 +00:00
2017-05-14 08:38:21 +00:00
def test_default_values():
instance = printer.Dummy()
instance.set()
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
# Size tests
2021-10-30 16:15:22 +00:00
2017-05-14 08:38:21 +00:00
def test_set_size_2h():
instance = printer.Dummy()
2017-05-15 16:54:54 +00:00
instance.set(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_2w():
instance = printer.Dummy()
2017-05-15 16:54:54 +00:00
instance.set(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_2x():
instance = printer.Dummy()
2017-05-15 16:54:54 +00:00
instance.set(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_custom():
instance = printer.Dummy()
2017-05-15 16:54:54 +00:00
instance.set(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
# Flip
2021-10-30 16:15:22 +00:00
2017-05-14 08:38:21 +00:00
def test_set_flip():
instance = printer.Dummy()
instance.set(flip=True)
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
# Smooth
2021-10-30 16:15:22 +00:00
2017-05-14 08:38:21 +00:00
def test_smooth():
instance = printer.Dummy()
instance.set(smooth=True)
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():
instance = printer.Dummy()
instance.set(bold=True)
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():
instance = printer.Dummy()
instance.set(underline=1)
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():
instance = printer.Dummy()
instance.set(underline=2)
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
2017-05-14 08:38:21 +00:00
def test_align_center():
instance = printer.Dummy()
2021-10-30 16:15:22 +00:00
instance.set(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():
instance = printer.Dummy()
2021-10-30 16:15:22 +00:00
instance.set(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
2017-05-14 08:38:21 +00:00
def test_densities():
for density in range(8):
instance = printer.Dummy()
instance.set(density=density)
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
2017-05-14 08:38:21 +00:00
def test_invert():
instance = printer.Dummy()
instance.set(invert=True)
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)