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

add automatic codepage-changing

This code is adapted from the works by Frédéric Van der Essen in
pyxmlescpos.
I had to adapt the code completely in order to make it compatible with
modern unicode-handling

Further changes:
* improve text unittests in CLI and MagicEncode with hypothesis
* add feature force_encoding in order to enable old behaviour
* disable cli_text_test (for now)
* fix charcode(): it does now cooperate with the new structure
* remove redundant variable codepage from class Escpos
This commit is contained in:
Patrick Kanzler
2016-07-23 22:16:11 +02:00
committed by Patrick Kanzler
parent b0af9e9652
commit 0cfedb5706
6 changed files with 357 additions and 141 deletions

View File

@@ -1,5 +1,6 @@
#!/usr/bin/python
"""tests for panel button function
# -*- coding: utf-8 -*-
"""tests for the magic encode module
:author: `Patrick Kanzler <patrick.kanzler@fablab.fau.de>`_
:organization: `python-escpos <https://github.com/python-escpos>`_
@@ -12,43 +13,90 @@ from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from nose.tools import with_setup
from nose.tools import raises, assert_raises
from hypothesis import given, example
import hypothesis.strategies as st
from escpos.magicencode import MagicEncode
from escpos.exceptions import CharCodeError, Error
from escpos.constants import CHARCODE
import escpos.printer as printer
import os
@raises(CharCodeError)
def test_magic_encode_unkown_char_constant_as_startenc():
"""tests whether MagicEncode raises the proper Exception when an unknown charcode-name is passed as startencoding"""
MagicEncode(startencoding="something")
devfile = 'testfile'
@raises(CharCodeError)
def test_magic_encode_unkown_char_constant_as_defaultenc():
"""tests whether MagicEncode raises the proper Exception when an unknown charcode-name is passed as defaultenc."""
MagicEncode(defaultencoding="something")
def test_magic_encode_wo_arguments():
"""tests whether MagicEncode works in the standard configuration"""
MagicEncode()
@raises(Error)
def test_magic_encode_w_non_binary_defaultsymbol():
"""tests whether MagicEncode catches non-binary defaultsymbols"""
MagicEncode(defaultsymbol="non-binary")
@given(symbol=st.binary())
def test_magic_encode_w_binary_defaultsymbol(symbol):
"""tests whether MagicEncode works with any binary symbol"""
MagicEncode(defaultsymbol=symbol)
@given(st.text())
@example("カタカナ")
@example("あいうえお")
@example("ハンカクカタカナ")
def test_magic_encode_encode_text_unicode_string(text):
"""tests whether MagicEncode can accept a unicode string"""
me = MagicEncode()
me.encode_text(text)
@given(char=st.characters())
def test_magic_encode_encode_char(char):
"""tests the encode_char-method of MagicEncode"""
me = MagicEncode()
me.encode_char(char)
@raises(Error)
@given(char=st.binary())
def test_magic_encode_encode_char_binary(char):
"""tests the encode_char-method of MagicEncode with binary input"""
me = MagicEncode()
me.encode_char(char)
def setup_testfile():
"""create a testfile as devfile"""
fhandle = open(devfile, 'a')
try:
os.utime(devfile, None)
finally:
fhandle.close()
def test_magic_encode_string_with_katakana_and_hiragana():
"""tests the encode_string-method with katakana and hiragana"""
me = MagicEncode()
me.encode_str("カタカナ")
me.encode_str("あいうえお")
@raises(CharCodeError)
def test_magic_encode_codepage_sequence_unknown_key():
"""tests whether MagicEncode.codepage_sequence raises the proper Exception with unknown charcode-names"""
MagicEncode.codepage_sequence("something")
def teardown_testfile():
"""destroy testfile again"""
os.remove(devfile)
@raises(CharCodeError)
def test_magic_encode_codepage_name_unknown_key():
"""tests whether MagicEncode.codepage_name raises the proper Exception with unknown charcode-names"""
MagicEncode.codepage_name("something")
def test_magic_encode_constants_getter():
"""tests whether the constants are properly fetched"""
for key in CHARCODE:
name = CHARCODE[key][1]
if name == '':
assert_raises(CharCodeError, MagicEncode.codepage_name, key)
else:
assert name == MagicEncode.codepage_name(key)
assert MagicEncode.codepage_sequence(key) == CHARCODE[key][0]
@with_setup(setup_testfile, teardown_testfile)
def test_function_panel_button_on():
"""test the panel button function (enabling) by comparing output"""
instance = printer.File(devfile=devfile)
instance.panel_buttons()
instance.flush()
with open(devfile, "rb") as f:
assert(f.read() == b'\x1B\x63\x35\x00')
@with_setup(setup_testfile, teardown_testfile)
def test_function_panel_button_off():
"""test the panel button function (disabling) by comparing output"""
instance = printer.File(devfile=devfile)
instance.panel_buttons(False)
instance.flush()
with open(devfile, "rb") as f:
assert(f.read() == b'\x1B\x63\x35\x01')
def test_magic_encode_force_encoding():
"""test whether force_encoding works as expected"""
me = MagicEncode()
assert me.force_encoding is False
me.set_encoding(encoding='KATAKANA', force_encoding=True)
assert me.encoding == 'KATAKANA'
assert me.force_encoding is True