diff --git a/setup.py b/setup.py index ef7b5b1..9d00261 100755 --- a/setup.py +++ b/setup.py @@ -118,7 +118,7 @@ setup( setup_requires=[ 'setuptools_scm', ], - tests_require=['tox', 'pytest', 'pytest-cov', 'nose', 'scripttest', 'mock', 'hypothesis'], + tests_require=['jaconv', 'tox', 'pytest', 'pytest-cov', 'pytest-mock', 'nose', 'scripttest', 'mock', 'hypothesis'], cmdclass={'test': Tox}, entry_points={ 'console_scripts': [ diff --git a/src/escpos/katakana.py b/src/escpos/katakana.py index 7c2e2c7..14b2e61 100644 --- a/src/escpos/katakana.py +++ b/src/escpos/katakana.py @@ -11,24 +11,27 @@ from __future__ import unicode_literals try: - import jcconv + import jaconv except ImportError: - jcconv = None + jaconv = None def encode_katakana(text): """I don't think this quite works yet.""" encoded = [] for char in text: - if jcconv: + if jaconv: # try to convert japanese text to half-katakanas - char = jcconv.kata2half(jcconv.hira2kata(char)) + char = jaconv.z2h(jaconv.hira2kata(char)) # TODO: "the conversion may result in multiple characters" - # When? What should we do about it? + # If that really can happen (I am not really shure), than the string would have to be split and every single + # character has to passed through the following lines. if char in TXT_ENC_KATAKANA_MAP: encoded.append(TXT_ENC_KATAKANA_MAP[char]) else: + #TODO doesn't this discard all that is not in the map? Can we be shure that the input does contain only + # encodable characters? We could at least throw an exception if encoding is not possible. pass return b"".join(encoded) @@ -36,6 +39,7 @@ def encode_katakana(text): TXT_ENC_KATAKANA_MAP = { # Maps UTF-8 Katakana symbols to KATAKANA Page Codes + # TODO: has this really to be hardcoded? # Half-Width Katakanas '。': b'\xa1', diff --git a/test/test_magicencode.py b/test/test_magicencode.py index 45b0d55..fc7a31e 100644 --- a/test/test_magicencode.py +++ b/test/test_magicencode.py @@ -95,12 +95,12 @@ class TestMagicEncode: try: - import jcconv + import jaconv except ImportError: - jcconv = None + jaconv = None -@pytest.mark.skipif(not jcconv, reason="jcconv not installed") +@pytest.mark.skipif(not jaconv, reason="jaconv not installed") class TestKatakana: @given(st.text()) @example("カタカナ") @@ -112,14 +112,3 @@ class TestKatakana: def test_result(self): assert encode_katakana('カタカナ') == b'\xb6\xc0\xb6\xc5' assert encode_katakana("あいうえお") == b'\xb1\xb2\xb3\xb4\xb5' - - -# TODO Idee für unittest: hypothesis-strings erzeugen, in encode_text werfen -# Ergebnis durchgehen: Vorkommnisse von Stuersequenzen suchen und daran den Text splitten in ein sortiertes dict mit Struktur: -# encoding: textfolge -# das alles wieder in unicode dekodieren mit den codepages und dann zusammenbauen -# 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 diff --git a/test/test_printer_file.py b/test/test_printer_file.py index bce9a0e..4d1b188 100644 --- a/test/test_printer_file.py +++ b/test/test_printer_file.py @@ -15,7 +15,7 @@ from __future__ import unicode_literals import six -import mock +import pytest from hypothesis import given from hypothesis.strategies import text @@ -27,21 +27,22 @@ else: mock_open_call = '__builtin__.open' @given(path=text()) -@mock.patch(mock_open_call) -@mock.patch('escpos.escpos.Escpos.__init__') -def test_load_file_printer(mock_escpos, mock_open, path): +def test_load_file_printer(mocker, path): """test the loading of the file-printer""" + mock_escpos = mocker.patch('escpos.escpos.Escpos.__init__') + mock_open = mocker.patch(mock_open_call) printer.File(devfile=path) assert mock_escpos.called mock_open.assert_called_with(path, "wb") @given(txt=text()) -@mock.patch.object(printer.File, 'device') -@mock.patch(mock_open_call) -@mock.patch('escpos.escpos.Escpos.__init__') -def test_auto_flush(mock_escpos, mock_open, mock_device, txt): +def test_auto_flush(mocker, txt): """test auto_flush in file-printer""" + mock_escpos = mocker.patch('escpos.escpos.Escpos.__init__') + mock_open = mocker.patch(mock_open_call) + mock_device = mocker.patch.object(printer.File, 'device') + p = printer.File(auto_flush=False) # inject the mocked device-object p.device = mock_device @@ -56,10 +57,11 @@ def test_auto_flush(mock_escpos, mock_open, mock_device, txt): @given(txt=text()) -@mock.patch.object(printer.File, 'device') -@mock.patch(mock_open_call) -def test_flush_on_close(mock_open, mock_device, txt): +def test_flush_on_close(mocker, txt): """test flush on close in file-printer""" + mock_open = mocker.patch(mock_open_call) + mock_device = mocker.patch.object(printer.File, 'device') + p = printer.File(auto_flush=False) # inject the mocked device-object p.device = mock_device diff --git a/tox.ini b/tox.ini index 362f2a6..7fbd3b8 100644 --- a/tox.ini +++ b/tox.ini @@ -3,11 +3,13 @@ envlist = py27, py34, py35, docs [testenv] deps = nose + jaconv coverage scripttest mock pytest pytest-cov + pytest-mock hypothesis commands = py.test --cov escpos