2016-07-23 20:09:35 +00:00
|
|
|
#!/usr/bin/python
|
2016-07-23 20:16:11 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""tests for the magic encode module
|
2016-07-23 20:09:35 +00:00
|
|
|
|
|
|
|
:author: `Patrick Kanzler <patrick.kanzler@fablab.fau.de>`_
|
|
|
|
:organization: `python-escpos <https://github.com/python-escpos>`_
|
|
|
|
:copyright: Copyright (c) 2016 `python-escpos <https://github.com/python-escpos>`_
|
|
|
|
:license: GNU GPL v3
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-08-27 09:09:08 +00:00
|
|
|
import pytest
|
2016-07-23 20:16:11 +00:00
|
|
|
from nose.tools import raises, assert_raises
|
|
|
|
from hypothesis import given, example
|
|
|
|
import hypothesis.strategies as st
|
2016-08-30 15:05:31 +00:00
|
|
|
from escpos.magicencode import MagicEncode, Encoder
|
|
|
|
from escpos.katakana import encode_katakana
|
2016-07-23 20:16:11 +00:00
|
|
|
from escpos.exceptions import CharCodeError, Error
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestEncoder:
|
2017-01-29 22:36:33 +00:00
|
|
|
"""
|
|
|
|
Tests the single encoders.
|
|
|
|
"""
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
def test_can_encode(self):
|
2016-09-13 10:28:54 +00:00
|
|
|
assert not Encoder({'CP437': 1}).can_encode('CP437', u'€')
|
|
|
|
assert Encoder({'CP437': 1}).can_encode('CP437', u'á')
|
2016-08-30 15:05:31 +00:00
|
|
|
assert not Encoder({'foobar': 1}).can_encode('foobar', 'a')
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
def test_find_suitable_encoding(self):
|
2016-09-13 10:28:54 +00:00
|
|
|
assert not Encoder({'CP437': 1}).find_suitable_encoding(u'€')
|
|
|
|
assert Encoder({'CP858': 1}).find_suitable_encoding(u'€') == 'CP858'
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
@raises(ValueError)
|
|
|
|
def test_get_encoding(self):
|
2016-09-11 11:03:55 +00:00
|
|
|
Encoder({}).get_encoding_name('latin1')
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestMagicEncode:
|
2017-01-29 22:36:33 +00:00
|
|
|
"""
|
|
|
|
Tests the magic encode functionality.
|
|
|
|
"""
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
class TestInit:
|
2017-01-29 22:36:33 +00:00
|
|
|
"""
|
|
|
|
Test initialization.
|
|
|
|
"""
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
def test_disabled_requires_encoding(self, driver):
|
2017-01-29 22:36:33 +00:00
|
|
|
"""
|
|
|
|
Test that disabled without encoder raises an error.
|
|
|
|
|
|
|
|
:param driver:
|
|
|
|
"""
|
2016-08-27 09:09:08 +00:00
|
|
|
with pytest.raises(Error):
|
|
|
|
MagicEncode(driver, disabled=True)
|
|
|
|
|
|
|
|
class TestWriteWithEncoding:
|
|
|
|
|
|
|
|
def test_init_from_none(self, driver):
|
|
|
|
encode = MagicEncode(driver, encoding=None)
|
2016-09-13 10:28:54 +00:00
|
|
|
encode.write_with_encoding('CP858', '€ ist teuro.')
|
2016-08-30 15:05:31 +00:00
|
|
|
assert driver.output == b'\x1bt\x13\xd5 ist teuro.'
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
def test_change_from_another(self, driver):
|
2016-09-13 10:28:54 +00:00
|
|
|
encode = MagicEncode(driver, encoding='CP437')
|
|
|
|
encode.write_with_encoding('CP858', '€ ist teuro.')
|
2016-08-30 15:05:31 +00:00
|
|
|
assert driver.output == b'\x1bt\x13\xd5 ist teuro.'
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
def test_no_change(self, driver):
|
2016-09-13 10:28:54 +00:00
|
|
|
encode = MagicEncode(driver, encoding='CP858')
|
|
|
|
encode.write_with_encoding('CP858', '€ ist teuro.')
|
2016-08-27 09:09:08 +00:00
|
|
|
assert driver.output == b'\xd5 ist teuro.'
|
|
|
|
|
|
|
|
class TestWrite:
|
|
|
|
|
|
|
|
def test_write(self, driver):
|
|
|
|
encode = MagicEncode(driver)
|
|
|
|
encode.write('€ ist teuro.')
|
2016-08-30 15:05:31 +00:00
|
|
|
assert driver.output == b'\x1bt\x0f\xa4 ist teuro.'
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
def test_write_disabled(self, driver):
|
2016-09-13 10:28:54 +00:00
|
|
|
encode = MagicEncode(driver, encoding='CP437', disabled=True)
|
2016-08-27 09:09:08 +00:00
|
|
|
encode.write('€ ist teuro.')
|
|
|
|
assert driver.output == b'? ist teuro.'
|
|
|
|
|
|
|
|
def test_write_no_codepage(self, driver):
|
|
|
|
encode = MagicEncode(
|
2016-09-13 10:28:54 +00:00
|
|
|
driver, defaultsymbol="_", encoder=Encoder({'CP437': 1}),
|
|
|
|
encoding='CP437')
|
2016-08-27 09:09:08 +00:00
|
|
|
encode.write(u'€ ist teuro.')
|
|
|
|
assert driver.output == b'_ ist teuro.'
|
|
|
|
|
|
|
|
class TestForceEncoding:
|
|
|
|
|
|
|
|
def test(self, driver):
|
|
|
|
encode = MagicEncode(driver)
|
2016-09-13 10:28:54 +00:00
|
|
|
encode.force_encoding('CP437')
|
2016-08-30 15:05:31 +00:00
|
|
|
assert driver.output == b'\x1bt\x00'
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
encode.write('€ ist teuro.')
|
2016-08-30 15:05:31 +00:00
|
|
|
assert driver.output == b'\x1bt\x00? ist teuro.'
|
2016-08-27 09:09:08 +00:00
|
|
|
|
|
|
|
|
2016-08-30 16:07:56 +00:00
|
|
|
try:
|
2016-09-27 18:26:22 +00:00
|
|
|
import jaconv
|
2016-08-30 16:07:56 +00:00
|
|
|
except ImportError:
|
2016-09-27 18:26:22 +00:00
|
|
|
jaconv = None
|
2016-08-30 16:07:56 +00:00
|
|
|
|
|
|
|
|
2016-09-27 18:26:22 +00:00
|
|
|
@pytest.mark.skipif(not jaconv, reason="jaconv not installed")
|
2016-08-27 09:09:08 +00:00
|
|
|
class TestKatakana:
|
|
|
|
@given(st.text())
|
|
|
|
@example("カタカナ")
|
|
|
|
@example("あいうえお")
|
|
|
|
@example("ハンカクカタカナ")
|
|
|
|
def test_accept(self, text):
|
|
|
|
encode_katakana(text)
|
|
|
|
|
|
|
|
def test_result(self):
|
|
|
|
assert encode_katakana('カタカナ') == b'\xb6\xc0\xb6\xc5'
|
|
|
|
assert encode_katakana("あいうえお") == b'\xb1\xb2\xb3\xb4\xb5'
|