"Merge pull request #2 from patkan/miracle2k-text-encoding\n\nfix file-printer-tests by using pytest-mock"
This commit is contained in:
commit
398eb424a9
2
setup.py
2
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': [
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue