python-escpos/test/test_magicencode.py

134 lines
4.5 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""tests for the magic encode module
:author: `Patrick Kanzler <dev@pkanzler.de>`_
:organization: `python-escpos <https://github.com/python-escpos>`_
:copyright: Copyright (c) 2016 `python-escpos <https://github.com/python-escpos>`_
2017-01-29 23:39:43 +00:00
:license: MIT
"""
import types
import typing
import hypothesis.strategies as st
import pytest
from hypothesis import example, given
from escpos import printer
from escpos.exceptions import Error
from escpos.katakana import encode_katakana
from escpos.magicencode import Encoder, MagicEncode
class TestEncoder:
"""
Tests the single encoders.
"""
def test_can_encode(self) -> None:
2023-04-19 20:11:09 +00:00
assert not Encoder({"CP437": 1}).can_encode("CP437", "")
assert Encoder({"CP437": 1}).can_encode("CP437", "á")
2021-10-30 16:15:22 +00:00
assert not Encoder({"foobar": 1}).can_encode("foobar", "a")
def test_find_suitable_encoding(self) -> None:
2023-04-19 20:11:09 +00:00
assert not Encoder({"CP437": 1}).find_suitable_encoding("")
assert Encoder({"CP858": 1}).find_suitable_encoding("") == "CP858"
def test_find_suitable_encoding_unnecessary_codepage_swap(self) -> None:
enc = Encoder({"CP857": 1, "CP437": 2, "CP1252": 3, "CP852": 4, "CP858": 5})
# desired behavior would be that the encoder always stays in the lower
# available codepages if possible
for character in ("Á", "É", "Í", "Ó", "Ú"):
assert enc.find_suitable_encoding(character) == "CP857"
def test_get_encoding(self) -> None:
with pytest.raises(ValueError):
Encoder({}).get_encoding_name("latin1")
class TestMagicEncode:
"""
Tests the magic encode functionality.
"""
class TestInit:
"""
Test initialization.
"""
def test_disabled_requires_encoding(self, driver: printer.Dummy) -> None:
"""
Test that disabled without encoder raises an error.
:param driver:
"""
with pytest.raises(Error):
MagicEncode(driver, disabled=True)
class TestWriteWithEncoding:
def test_init_from_none(self, driver: printer.Dummy) -> None:
encode = MagicEncode(driver, encoding=None)
2021-10-30 16:15:22 +00:00
encode.write_with_encoding("CP858", "€ ist teuro.")
assert driver.output == b"\x1bt\x13\xd5 ist teuro."
def test_change_from_another(self, driver: printer.Dummy) -> None:
2021-10-30 16:15:22 +00:00
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: printer.Dummy) -> None:
2021-10-30 16:15:22 +00:00
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: printer.Dummy) -> None:
encode = MagicEncode(driver)
2021-10-30 16:15:22 +00:00
encode.write("€ ist teuro.")
assert driver.output == b"\x1bt\x0f\xa4 ist teuro."
def test_write_disabled(self, driver: printer.Dummy) -> None:
2021-10-30 16:15:22 +00:00
encode = MagicEncode(driver, encoding="CP437", disabled=True)
encode.write("€ ist teuro.")
assert driver.output == b"? ist teuro."
def test_write_no_codepage(self, driver: printer.Dummy) -> None:
encode = MagicEncode(
2021-10-30 16:15:22 +00:00
driver,
defaultsymbol="_",
encoder=Encoder({"CP437": 1}),
encoding="CP437",
)
2023-04-19 20:11:09 +00:00
encode.write("€ ist teuro.")
2021-10-30 16:15:22 +00:00
assert driver.output == b"_ ist teuro."
class TestForceEncoding:
def test(self, driver: printer.Dummy) -> None:
encode = MagicEncode(driver)
2021-10-30 16:15:22 +00:00
encode.force_encoding("CP437")
assert driver.output == b"\x1bt\x00"
2021-10-30 16:15:22 +00:00
encode.write("€ ist teuro.")
assert driver.output == b"\x1bt\x00? ist teuro."
jaconv: typing.Optional[types.ModuleType]
2016-08-30 16:07:56 +00:00
try:
import jaconv
2016-08-30 16:07:56 +00:00
except ImportError:
jaconv = None
2016-08-30 16:07:56 +00:00
@pytest.mark.skipif(not jaconv, reason="jaconv not installed")
class TestKatakana:
@given(st.text())
@example("カタカナ")
@example("あいうえお")
@example("ハンカクカタカナ")
def test_accept(self, text: str) -> None:
encode_katakana(text)
def test_result(self) -> None:
2021-10-30 16:15:22 +00:00
assert encode_katakana("カタカナ") == b"\xb6\xc0\xb6\xc5"
assert encode_katakana("あいうえお") == b"\xb1\xb2\xb3\xb4\xb5"