From 87a66470530bd754f2681171ad8ff013b93aafe7 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Mon, 25 Jul 2016 17:25:13 +0200 Subject: [PATCH] fix force-encoding REBASE (contains todos) * fixed the code of forced-encoding in order to make it work * extended unittest for forced-encoding * fixed the constant for Katakana-encoding --- src/escpos/constants.py | 2 +- src/escpos/magicencode.py | 3 ++- test/test_magicencode.py | 18 +++++++++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/escpos/constants.py b/src/escpos/constants.py index 32c1a76..f91cbdf 100644 --- a/src/escpos/constants.py +++ b/src/escpos/constants.py @@ -106,7 +106,7 @@ CHARCODE = { 'PC437': [ESC + b'\x74\x00', 'cp437'], # PC437 USA 'KATAKANA': - [ESC + b'\x74\x01', 'katakana'], # KATAKANA (JAPAN) + [ESC + b'\x74\x01', ''], # KATAKANA (JAPAN) 'PC850': [ESC + b'\x74\x02', 'cp850'], # PC850 Multilingual 'PC860': diff --git a/src/escpos/magicencode.py b/src/escpos/magicencode.py index 9e7aeb6..1091b31 100644 --- a/src/escpos/magicencode.py +++ b/src/escpos/magicencode.py @@ -143,7 +143,7 @@ class MagicEncode(object): # make sure the right codepage is set in the printer buffer = self.codepage_sequence(self.encoding) if self.force_encoding: - buffer += txt.encode(self.codepage) + buffer += txt.encode(self.codepage_name(self.encoding)) else: for c in txt: buffer += self.encode_char(c) @@ -178,6 +178,7 @@ class MagicEncode(object): # todo emoticons mit charmap encoden # todo Escpos liste von unterdrückten charcodes mitgeben +# TODO Sichtbarkeit der Methode anpassen (Eigentlich braucht man nur die set_encode und die encode_text) TXT_ENC_KATAKANA_MAP = { # Maps UTF-8 Katakana symbols to KATAKANA Page Codes diff --git a/test/test_magicencode.py b/test/test_magicencode.py index 3c7f356..eb0f07b 100644 --- a/test/test_magicencode.py +++ b/test/test_magicencode.py @@ -93,12 +93,22 @@ def test_magic_encode_constants_getter(): assert name == MagicEncode.codepage_name(key) assert MagicEncode.codepage_sequence(key) == CHARCODE[key][0] -def test_magic_encode_force_encoding(): +@given(st.text()) +def test_magic_encode_force_encoding(text): """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' + me.set_encoding(encoding='PC850', force_encoding=True) + assert me.encoding == 'PC850' + assert me.force_encoding is True + try: + me.encode_text(text) + except UnicodeEncodeError: + # we discard these errors as they are to be expected + # what we want to check here is, whether encoding or codepage will switch through some of the magic code + # being called accidentally + pass + assert me.encoding == 'PC850' assert me.force_encoding is True @@ -109,3 +119,5 @@ def test_magic_encode_force_encoding(): # fertigen String mit hypothesis-string vergleichen (Achtung bei katana-conversion. Die am besten auch auf den hypothesis-string # anwenden) # TODO bei nicht kodierbarem Zeichen Fehler werfen! Als Option das verhalten von jetzt hinzufügen +# TODO tests sollten eigentlich nicht gehen, wenn encode_char gerufen wird (extra_char ist nicht definiert) +# TODO verhalten bei leerem String festlegen und testen