From bb6a3ce131bfe7cc34da4a26285f77b7d5793fd6 Mon Sep 17 00:00:00 2001 From: Adrian Schlatter <10478149+adrianschlatter@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:40:07 +0200 Subject: [PATCH] DRY up codec tests The test cases for codecs are almost identically repeated 5 times => created a base class for these test cases which is inherited for each codec. Had to add full-alphabet test against datamatrix-svg for ascii (as this was currently missing). Also changed full-alphabet tests of TEXT and X12: So far, we tested a reduced alphabet against datamatrix-svg because datamatrix-svg has issues with some characters. Switched this to testing full alphabet but with ground truth added "manually". Modified flake8 exceptions in setup.cfg (import of ppf.datamatrix appears unused but actually *is* used via codecs). work on issue #14 --- setup.cfg | 2 + src/ppf/datamatrix/codec_C40.py | 12 +-- src/ppf/datamatrix/codec_X12.py | 12 +-- src/ppf/datamatrix/codec_ascii.py | 12 +-- src/ppf/datamatrix/codec_edifact.py | 10 +- src/ppf/datamatrix/codec_text.py | 12 +-- tests/common.py | 94 +++++++++++++++++++ tests/test_C40.py | 68 ++++---------- tests/test_X12.py | 50 +++------- tests/test_ascii.py | 57 +++-------- .../{test_common.py => test_codec_common.py} | 2 +- tests/test_edifact.py | 85 +++-------------- tests/test_text.py | 78 +++++---------- 13 files changed, 200 insertions(+), 294 deletions(-) rename tests/{test_common.py => test_codec_common.py} (93%) diff --git a/setup.cfg b/setup.cfg index a2edefe..2e58115 100644 --- a/setup.cfg +++ b/setup.cfg @@ -53,6 +53,8 @@ per-file-ignores = # imported but unused, import *, undefined name: __init__.py: F401, F403, F821 # imported but unused: Needed due to side-effects of import: + test_ascii.py: F401 + test_edifact.py: F401 test_X12.py: F401 test_C40.py: F401 test_text.py: F401 diff --git a/src/ppf/datamatrix/codec_C40.py b/src/ppf/datamatrix/codec_C40.py index fa1bd00..d6b3797 100644 --- a/src/ppf/datamatrix/codec_C40.py +++ b/src/ppf/datamatrix/codec_C40.py @@ -32,12 +32,12 @@ add_inverse_lookup(codepage) -def encode_to_C40(msg): +def encode(msg): """Encode to datamatrix.C40.""" return encode_text_mode(msg, codepage, b'\xE6', True) -def decode_from_C40(enc): +def decode(enc): """Decode datamatrix.C40-encoded message.""" try: msg, length = decode_text_mode(enc, codepage, b'\xE6', True) @@ -47,14 +47,14 @@ def decode_from_C40(enc): return msg, length -def search_codec_C40(encoding_name): +def search_codec(encoding_name): """Search function needed for registration in python codecs.""" if encoding_name != 'datamatrix.c40': return None - return codecs.CodecInfo(encode_to_C40, - decode_from_C40, + return codecs.CodecInfo(encode, + decode, name='datamatrix.C40') -codecs.register(search_codec_C40) +codecs.register(search_codec) diff --git a/src/ppf/datamatrix/codec_X12.py b/src/ppf/datamatrix/codec_X12.py index 49cbf98..7911a57 100644 --- a/src/ppf/datamatrix/codec_X12.py +++ b/src/ppf/datamatrix/codec_X12.py @@ -24,7 +24,7 @@ add_inverse_lookup(codepage) -def encode_to_X12(msg): +def encode(msg): """Encode to datamatrix.X12.""" try: enc, length = encode_text_mode(msg, codepage, b'\xEE', False) @@ -34,7 +34,7 @@ def encode_to_X12(msg): return enc, length -def decode_from_X12(enc): +def decode(enc): """Decode datamatrix.X12-encoded message.""" try: msg, length = decode_text_mode(enc, codepage, b'\xEE', False) @@ -44,14 +44,14 @@ def decode_from_X12(enc): return msg, length -def search_codec_X12(encoding_name): +def search_codec(encoding_name): """Search function needed for registration in python codecs.""" if encoding_name != 'datamatrix.x12': return None - return codecs.CodecInfo(encode_to_X12, - decode_from_X12, + return codecs.CodecInfo(encode, + decode, name='datamatrix.X12') -codecs.register(search_codec_X12) +codecs.register(search_codec) diff --git a/src/ppf/datamatrix/codec_ascii.py b/src/ppf/datamatrix/codec_ascii.py index e9bcbf7..daf2723 100644 --- a/src/ppf/datamatrix/codec_ascii.py +++ b/src/ppf/datamatrix/codec_ascii.py @@ -26,7 +26,7 @@ DIGITS = '0123456789' -def encode_to_ascii(msg): +def encode(msg): """Encode to datamatrix.ascii.""" enc = [] i = 0 @@ -41,7 +41,7 @@ def encode_to_ascii(msg): return bytes(enc), len(enc) -def decode_from_ascii(code): +def decode(code): """Decode datamatrix.ascii-encoded message.""" msg = '' for c in code: @@ -53,14 +53,14 @@ def decode_from_ascii(code): return msg, len(msg) -def search_codec_ascii(encoding_name): +def search_codec(encoding_name): """Search function needed for registration in python codecs.""" if encoding_name != 'datamatrix.ascii': return None - return codecs.CodecInfo(encode_to_ascii, - decode_from_ascii, + return codecs.CodecInfo(encode, + decode, name='datamatrix.ascii') -codecs.register(search_codec_ascii) +codecs.register(search_codec) diff --git a/src/ppf/datamatrix/codec_edifact.py b/src/ppf/datamatrix/codec_edifact.py index 62ea0ff..3ed4f81 100644 --- a/src/ppf/datamatrix/codec_edifact.py +++ b/src/ppf/datamatrix/codec_edifact.py @@ -35,7 +35,7 @@ def pack(ascii): return bytes(packed) -def encode_to_edifact(msg): +def encode(msg): """Encode message as datamatrix.edifact.""" # We want to encode the characters in msg + the "return to ASCII" code (1F) # Edifact packs 4 input bytes into 3 output bytes. @@ -57,7 +57,7 @@ def encode_to_edifact(msg): return enc, len(enc) -def decode_from_edifact(enc): +def decode(enc): """Decode edifact-encoded message.""" edifact = list(enc) if edifact[0] != 0xF0: @@ -92,13 +92,13 @@ def decode_from_edifact(enc): return msg, len(msg) -def search_codec_edifact(encoding_name): +def search_codec(encoding_name): """Search function needed for registration in python codecs.""" if encoding_name != 'datamatrix.edifact': return None - return codecs.CodecInfo(encode_to_edifact, decode_from_edifact, + return codecs.CodecInfo(encode, decode, name='datamatrix.edifact') -codecs.register(search_codec_edifact) +codecs.register(search_codec) diff --git a/src/ppf/datamatrix/codec_text.py b/src/ppf/datamatrix/codec_text.py index e98c780..9a38684 100644 --- a/src/ppf/datamatrix/codec_text.py +++ b/src/ppf/datamatrix/codec_text.py @@ -32,12 +32,12 @@ add_inverse_lookup(codepage) -def encode_to_text(msg): +def encode(msg): """Encode to datamatrix.text.""" return encode_text_mode(msg, codepage, b'\xEF', True) -def decode_from_text(enc): +def decode(enc): """Decode datamatrix.text-encoded message.""" try: msg, length = decode_text_mode(enc, codepage, b'\xEF', True) @@ -47,14 +47,14 @@ def decode_from_text(enc): return msg, length -def search_codec_text(encoding_name): +def search_codec(encoding_name): """Search function needed for registration in python codecs.""" if encoding_name != 'datamatrix.text': return None - return codecs.CodecInfo(encode_to_text, - decode_from_text, + return codecs.CodecInfo(encode, + decode, name='datamatrix.text') -codecs.register(search_codec_text) +codecs.register(search_codec) diff --git a/tests/common.py b/tests/common.py index f1cb69f..7225916 100644 --- a/tests/common.py +++ b/tests/common.py @@ -7,6 +7,9 @@ .. author: Adrian Schlatter """ +import ppf.datamatrix as put +import sys + # Datamatrix has the following encodings: # TEXT, C40, X12, EDIFACT, BASE256 # TEXT and C40 both encode the entire ASCII character table (in a @@ -18,3 +21,94 @@ EDIFACT = bytes(range(32, 95)).decode('ascii') X12 = '\r*> 01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ' BASE256 = bytes(range(256)) + + +class Codec_Test(object): + """ + Template class to test codec + + Create an actual test class by inheriting from this class and from + unittest.TestCase. Make sure to customize class variables. + """ + + CODEC = 'name codec here' + ALPHABET = 'list valid characters here' + ALPHABET_ENC = b'what datamatrix-svg encodes ALPHABET to' + KNOWN_PAIR = {'msg': 'A', 'enc': b'\x63'} + CODEC_MODULE_NAME = 'codec_name' + + def test_consistency(self): + """Verify that coding + decoding return the original message.""" + for i in range(len(self.ALPHABET)): + if 2 * i > len(self.ALPHABET): + msg = (self.ALPHABET[i:] + + self.ALPHABET[:3 * i - len(self.ALPHABET)]) + else: + msg = self.ALPHABET[i:2 * i] + + code = msg.encode(self.CODEC) + decoded = code.decode(self.CODEC) + self.assertEqual(decoded, msg) + + def test_raises(self): + """Verify that error is raised for invalid code.""" + code = bytes([0]) + with self.assertRaises(ValueError): + code.decode(self.CODEC) + + def test_encode_to_square_datamatrix(self): + """Verify that encoding to square datamatrix works.""" + for i in range(len(self.ALPHABET)): + if 2 * i > len(self.ALPHABET): + msg = (self.ALPHABET[i:] + + self.ALPHABET[:3 * i - len(self.ALPHABET)]) + else: + msg = self.ALPHABET[i:2 * i] + + # assert that this does not raise: + datamatrix = put.DataMatrix(msg) + + self.assertTrue(len(datamatrix.matrix) > 0) + + def test_encode_to_rect_datamatrix(self): + """Verify that encoding to rectangular datamatrix works.""" + for i in range(len(self.ALPHABET)): + if 2 * i > len(self.ALPHABET): + msg = (self.ALPHABET[i:] + + self.ALPHABET[:3 * i - len(self.ALPHABET)]) + else: + msg = self.ALPHABET[i:2 * i] + + # assert that this does not raise: + datamatrix = put.DataMatrix(msg, rect=True) + + m = datamatrix.matrix + self.assertTrue(len(m) > 0) + + def test_encode_known(self): + """Test single-char edifact encoding""" + enc = self.KNOWN_PAIR['msg'].encode(self.CODEC) + self.assertEqual(enc, self.KNOWN_PAIR['enc']) + + def test_encode_alphabet(self): + """Encode entire alphabet and compare to datamatrix-svg.""" + enc = self.ALPHABET.encode(self.CODEC) + self.assertEqual(enc, self.ALPHABET_ENC) + + def test_decode_invalid(self): + """Try to decode invalid code.""" + + code = 9 * b'\x00' + with self.assertRaises(ValueError): + code.decode(self.CODEC) + + def test_search_wrong_codec(self): + """ + Test that search_codec callback returns None for non-matching + codec. + """ + + full_mod_name = 'ppf.datamatrix.' + self.CODEC_MODULE_NAME + __import__(full_mod_name) + codec_mod = sys.modules[full_mod_name] + self.assertTrue(codec_mod.search_codec('invalid') is None) diff --git a/tests/test_C40.py b/tests/test_C40.py index 1867652..50898ee 100644 --- a/tests/test_C40.py +++ b/tests/test_C40.py @@ -7,34 +7,27 @@ .. author: Adrian Schlatter """ +import ppf.datamatrix import unittest -from .common import ASCII -import ppf.datamatrix as pu +from .common import ASCII, Codec_Test -class Test_datamatrix_C40(unittest.TestCase): +class Test_datamatrix_C40(Codec_Test, unittest.TestCase): """Test codecs datamatrix.C40.""" - def test_consistency(self): - """Verify that coding + decoding return the original message.""" - for i in range(128): - if 2 * i > len(ASCII): - msg = ASCII[i:] + ASCII[:3 * i - len(ASCII)] - else: - msg = ASCII[i:2 * i] - - code = msg.encode('datamatrix.C40') - decoded = code.decode('datamatrix.C40') - self.assertEqual(decoded, msg) - - def test_encode_known_short(self): - """ - Encode short string and verify correctness. - - 'short' means: Too short to pack a single word. - """ - code = 'A'.encode('datamatrix.C40') - self.assertEqual(code, b'B') + CODEC = 'datamatrix.C40' + ALPHABET = ASCII + ALPHABET_ENC = ( + b'\xe6\x00\x01\x06C\x00y\x19\x06\x00\xf1+\xc9\x01i>\x8c\x01' + b'\xe1QO\x02Yd\x12\x02\xd1v\xd5\x03I\x89\x98\x03\xc1\x9c[\x04' + b'9\xaf\x1e\x04\xb1\xc2:\x00*\x06\x92\x12\xed\x07\n%\xb0\x07' + b'\x828s\x07\xfaK6\x08u 83sF\xae\x08\x9ad:\t\x12v\xfd\t\x97`' + b'Rs\x8d\x86\xc8\x9a\x03\xad>\xc0y\xd3\xb4\xe6\xef\xf3\xff\t' + b'\xda\x96B\nS\x00R\x0c\xd3\x13\x15\rK%\xd8\r\xc38\x9b\x0e;K^' + b'\x0e\xb3^!\x0f+p\xe4\x0f\xa3\x83\xa7\x10\x1b\x96j\x10\x93' + b'\xa9-\x11\x0b\xbb\xf0\xfe') + KNOWN_PAIR = {'msg': 'A', 'enc': b'B'} + CODEC_MODULE_NAME = 'codec_C40' def test_encode_known_long(self): """ @@ -45,35 +38,6 @@ def test_encode_known_long(self): code = (9 * 'A' + '!').encode('datamatrix.C40') self.assertEqual(code, b'\xe6Y\xbfY\xbfY\xbf\xfe"') - def test_encode_ASCII(self): - """Encode ASCII and compare to datamatrix-svg.""" - code = ASCII.encode('datamatrix.C40') - truth = (b'\xe6\x00\x01\x06C\x00y\x19\x06\x00\xf1+\xc9\x01i>\x8c\x01' - b'\xe1QO\x02Yd\x12\x02\xd1v\xd5\x03I\x89\x98\x03\xc1\x9c[\x04' - b'9\xaf\x1e\x04\xb1\xc2:\x00*\x06\x92\x12\xed\x07\n%\xb0\x07' - b'\x828s\x07\xfaK6\x08u 83sF\xae\x08\x9ad:\t\x12v\xfd\t\x97`' - b'Rs\x8d\x86\xc8\x9a\x03\xad>\xc0y\xd3\xb4\xe6\xef\xf3\xff\t' - b'\xda\x96B\nS\x00R\x0c\xd3\x13\x15\rK%\xd8\r\xc38\x9b\x0e;K^' - b'\x0e\xb3^!\x0f+p\xe4\x0f\xa3\x83\xa7\x10\x1b\x96j\x10\x93' - b'\xa9-\x11\x0b\xbb\xf0\xfe') - # Note: truth's last code is \xfe which means 'return to ascii'. - # We do not consider it as an error if code is equal to truth - # except for missing an 0xFE at the end: - self.assertTrue(code == truth or code == truth[:-1]) - - def test_decode_invalid_C40(self): - """Try to decode invalid code.""" - - code = 9 * b'\x00' - with self.assertRaises(ValueError): - code.decode('datamatrix.C40') - - def test_search_nonTEXT(self): - """Test that search_codec callback returns None for non-C40.""" - - from ppf.datamatrix import codec_C40 - self.assertTrue(codec_C40.search_codec_C40('invalid') is None) - if __name__ == '__main__': # This enables running the unit tests by running this script which is diff --git a/tests/test_X12.py b/tests/test_X12.py index c550030..4deddbf 100644 --- a/tests/test_X12.py +++ b/tests/test_X12.py @@ -7,34 +7,25 @@ .. author: Adrian Schlatter """ +import ppf.datamatrix import unittest -from .common import X12 -import ppf.datamatrix as pu +from .common import X12, Codec_Test -class Test_datamatrix_X12(unittest.TestCase): +class Test_datamatrix_X12(Codec_Test, unittest.TestCase): """Test codecs datamatrix.X12.""" - def test_consistency(self): - """Verify that coding + decoding return the original message.""" - for i in range(len(X12)): - if 2 * i > len(X12): - msg = X12[i:] + X12[:3 * i - len(X12)] - else: - msg = X12[i:2 * i] - - code = msg.encode('datamatrix.X12') - decoded = code.decode('datamatrix.X12') - self.assertEqual(decoded, msg) - - def test_encode_known_short(self): - """ - Encode short string and verify correctness. - - 'short' means: Too short to pack a single word. - """ - code = 'A'.encode('datamatrix.X12') - self.assertEqual(code, b'B') + CODEC = 'datamatrix.X12' + ALPHABET = X12 + # NOTED: datamatrix-svg *fails* at '>'! => Let datamatrix-svg encode: + # X12_ = '\r* 01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ' + # => ppf.datamatrix passed test. Then, encoded full X12 with this + # ppf.datamatrix => ALPHABET_ENC: + ALPHABET_ENC = ( + b'\xee\x00+\x13f&\xa19\xdcM\rY\xe9m$\x80_\x93\x9a\xa6\xd5\xba' + b'\x10\xcdK\xe0\x86\xfeZ[') + KNOWN_PAIR = {'msg': 'A', 'enc': b'B'} + CODEC_MODULE_NAME = 'codec_X12' def test_encode_known_long(self): """ @@ -53,19 +44,6 @@ def test_encode_X12(self): self.assertEqual(code, b'\xee\x00,\x19\xcf-\n@EQ\xef`Rs\x8d\x86\xc8' b'\x9a\x03\xad>\xc0y\xd3\xb4\xe6\xef\xfe[') - def test_decode_invalid_X12(self): - """Try to decode invalid code.""" - - code = 9 * b'\x00' - with self.assertRaises(ValueError): - code.decode('datamatrix.X12') - - def test_search_nonX12(self): - """Test that search_codec callback returns None for non-X12.""" - - from ppf.datamatrix import codec_X12 - self.assertTrue(codec_X12.search_codec_X12('invalid') is None) - if __name__ == '__main__': # This enables running the unit tests by running this script which is diff --git a/tests/test_ascii.py b/tests/test_ascii.py index 13f433c..d6eaeda 100644 --- a/tests/test_ascii.py +++ b/tests/test_ascii.py @@ -7,30 +7,23 @@ .. author: Adrian Schlatter """ +import ppf.datamatrix import unittest -import ppf.datamatrix as put -from .common import ASCII +from .common import ASCII, Codec_Test -class Test_datamatrix_ascii(unittest.TestCase): +class Test_datamatrix_ascii(Codec_Test, unittest.TestCase): """Test codecs datamatrix.ascii.""" - def test_consistency(self): - """Verify that coding + decoding return the original message.""" - for i in range(128): - if 2 * i > len(ASCII): - msg = ASCII[i:] + ASCII[:3 * i - len(ASCII)] - else: - msg = ASCII[i:2 * i] - - code = msg.encode('datamatrix.ascii') - decoded = code.decode('datamatrix.ascii') - self.assertEqual(decoded, msg) - - def test_encode_known(self): - """Encode and verify correctness.""" - code = 'A'.encode('datamatrix.ascii') - self.assertEqual(code, b'B') + CODEC = 'datamatrix.ascii' + ALPHABET = ASCII + ALPHABET_ENC = ( + b'\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11' + b'\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&' + b'\'()*+,-./0\x83\x99\xaf\xc5\xdb;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ' + b'[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80') + KNOWN_PAIR = {'msg': 'A', 'enc': b'B'} + CODEC_MODULE_NAME = 'codec_ascii' def test_encode_digitpair(self): """Encode a pair of digits and verify correctness.""" @@ -42,32 +35,6 @@ def test_encode_digittriplet(self): code = '325'.encode('datamatrix.ascii') self.assertEqual(code, bytes([130 + 32, ord('5') + 1])) - def test_encode_to_datamatrix(self): - """Verify that encoding to datamatrix works.""" - for i in range(len(ASCII)): - if 2 * i > len(ASCII): - msg = ASCII[i:] + ASCII[:3 * i - len(ASCII)] - else: - msg = ASCII[i:2 * i] - - # assert that this does not raise: - datamatrix = put.DataMatrix(msg) - - self.assertTrue(len(datamatrix.matrix) > 0) - - def test_encode_to_rect_datamatrix(self): - """Verify that encoding to datamatrix works.""" - for i in range(len(ASCII)): - if 2 * i > len(ASCII): - msg = ASCII[i:] + ASCII[:3 * i - len(ASCII)] - else: - msg = ASCII[i:2 * i] - - # assert that this does not raise: - datamatrix = put.DataMatrix(msg, rect=True) - - self.assertTrue(len(datamatrix.matrix) > 0) - if __name__ == '__main__': # This enables running the unit tests by running this script which is diff --git a/tests/test_common.py b/tests/test_codec_common.py similarity index 93% rename from tests/test_common.py rename to tests/test_codec_common.py index 8c7c903..8fa80cc 100644 --- a/tests/test_common.py +++ b/tests/test_codec_common.py @@ -11,7 +11,7 @@ from ppf.datamatrix import codec_common -class Test_Common(unittest.TestCase): +class Test_Codec_Common(unittest.TestCase): """Test codec_common""" def test_pack_words_of_invalid_raw(self): diff --git a/tests/test_edifact.py b/tests/test_edifact.py index f85c255..2fec0b2 100644 --- a/tests/test_edifact.py +++ b/tests/test_edifact.py @@ -7,63 +7,22 @@ .. author: Adrian Schlatter """ +import ppf.datamatrix import unittest -from .common import EDIFACT -import ppf.datamatrix as put +from .common import EDIFACT, Codec_Test -class Test_datamatrix_edifact(unittest.TestCase): +class Test_datamatrix_edifact(Codec_Test, unittest.TestCase): """Test codecs datamatrix.edifact.""" - def test_encode_decode(self): - """Verify that coding + decoding return the original message.""" - for i in range(len(EDIFACT)): - if 2 * i > len(EDIFACT): - msg = EDIFACT[i:] + EDIFACT[:3 * i - len(EDIFACT)] - else: - msg = EDIFACT[i:2 * i] - - code = msg.encode('datamatrix.edifact') - decoded = code.decode('datamatrix.edifact') - self.assertEqual(decoded, msg) - - def test_raises(self): - """Verify that error is raised for invalid EDIFACT.""" - code = bytes([0]) - with self.assertRaises(ValueError): - code.decode('datamatrix.edifact') - - def test_encode_to_square_datamatrix(self): - """Verify that encoding to square datamatrix works.""" - for i in range(len(EDIFACT)): - if 2 * i > len(EDIFACT): - msg = EDIFACT[i:] + EDIFACT[:3 * i - len(EDIFACT)] - else: - msg = EDIFACT[i:2 * i] - - # assert that this does not raise: - datamatrix = put.DataMatrix(msg) - - self.assertTrue(len(datamatrix.matrix) > 0) - - def test_encode_to_rect_datamatrix(self): - """Verify that encoding to rectangular datamatrix works.""" - for i in range(len(EDIFACT)): - if 2 * i > len(EDIFACT): - msg = EDIFACT[i:] + EDIFACT[:3 * i - len(EDIFACT)] - else: - msg = EDIFACT[i:2 * i] - - # assert that this does not raise: - datamatrix = put.DataMatrix(msg, rect=True) - - m = datamatrix.matrix - self.assertTrue(len(m) > 0) - - def test_encode_known(self): - """Test single-char edifact encoding""" - enc = 'A'.encode('datamatrix.edifact') - self.assertEqual(enc, b'\x42') + CODEC = 'datamatrix.edifact' + ALPHABET = EDIFACT + ALPHABET_ENC = ( + b'\xf0\x82\x18\xa3\x92Y\xa7\xa2\x9a\xab\xb2\xdb\xaf\xc3' + b'\x1c\xb3\xd3]\xb7\xe3\x9e\xbb\xf3\xdf\xbf\x00\x10\x83\x10Q\x87' + b' \x92\x8b0\xd3\x8fA\x14\x93QU\x97a\x96\x9bq\xd7\x9f') + KNOWN_PAIR = {'msg': 'A', 'enc': b'\x42'} + CODEC_MODULE_NAME = 'codec_edifact' def test_31(self): """ @@ -75,28 +34,6 @@ def test_31(self): enc = 'G1<'.encode('datamatrix.edifact') self.assertEqual(msg, enc.decode('datamatrix.edifact')) - def test_encode_EDIFACT(self): - """Encode entire alphabet and compare to datamatrix-svg.""" - truth = [240, 130, 24, 163, 146, 89, 167, 162, 154, 171, 178, 219, 175, - 195, 28, 179, 211, 93, 183, 227, 158, 187, 243, 223, 191, 0, - 16, 131, 16, 81, 135, 32, 146, 139, 48, 211, 143, 65, 20, 147, - 81, 85, 151, 97, 150, 155, 113, 215, 159] - enc = EDIFACT.encode('datamatrix.edifact') - self.assertEqual(enc, bytes(truth)) - - def test_decode_invalid_EDIFACT(self): - """Try to decode invalid code.""" - - code = 9 * b'\x00' - with self.assertRaises(ValueError): - code.decode('datamatrix.edifact') - - def test_search_nonEDIFACT(self): - """Test that search_codec callback returns None for non-EDIFACT.""" - - from ppf.datamatrix import codec_edifact - self.assertTrue(codec_edifact.search_codec_edifact('invalid') is None) - if __name__ == '__main__': # This enables running the unit tests by running this script which is diff --git a/tests/test_text.py b/tests/test_text.py index 8a4fc8a..bc08052 100644 --- a/tests/test_text.py +++ b/tests/test_text.py @@ -7,34 +7,32 @@ .. author: Adrian Schlatter """ +import ppf.datamatrix import unittest -from .common import ASCII -import ppf.datamatrix as put +from .common import ASCII, Codec_Test -class Test_datamatrix_text(unittest.TestCase): +class Test_datamatrix_text(Codec_Test, unittest.TestCase): """Test codecs datamatrix.text.""" - def test_consistency(self): - """Verify that coding + decoding return the original message.""" - for i in range(128): - if 2 * i > len(ASCII): - msg = ASCII[i:] + ASCII[:3 * i - len(ASCII)] - else: - msg = ASCII[i:2 * i] - - code = msg.encode('datamatrix.text') - decoded = code.decode('datamatrix.text') - self.assertEqual(decoded, msg) - - def test_encode_known_short(self): - """ - Encode short string and verify correctness. - - 'short' means: Too short to pack a single word. - """ - code = 'A'.encode('datamatrix.text') - self.assertEqual(code, b'B') + CODEC = 'datamatrix.text' + ALPHABET = ASCII + # Apparently, datamatrix-svg has a problem with '`' (it encodes + # 'A`' to the same as it encode 'A9'). Verified ppf.datamatrix against + # datamatrix-svg without '`' => OK + # ALPHABET_ENC below is what I belief to be the correct encoding of the + # ALPHABET: + ALPHABET_ENC = ( + b'\xef\x00\x01\x06C\x00y\x19\x06\x00\xf1+\xc9\x01i>\x8c\x01' + b'\xe1QO\x02Yd\x12\x02\xd1v\xd5\x03I\x89\x98\x03\xc1\x9c[\x04' + b'9\xaf\x1e\x04\xb1\xc2:\x00*\x06\x92\x12\xed\x07\n%\xb0\x07' + b'\x828s\x07\xfaK6\x08u 83sF\xae\x08\x9ad:\t\x12v\xfd\t\x8b' + b'\x06\x93\x0c\xfb\x19V\rs,\x19\r\xeb>\xdc\x0ecQ\x9f\x0e\xdbd' + b'b\x0fSw%\x0f\xcb\x89\xe8\x10C\x9c\xab\t\xb2\x90\x01\n*\xa2' + b'\xd1Y\xe9m$\x80_\x93\x9a\xa6\xd5\xba\x10\xcdK\xe0\x86\xf3\x9b' + b'\xa9-\x11\x0b\xbb\xf0\xfe') + KNOWN_PAIR = {'msg': 'A', 'enc': b'B'} + CODEC_MODULE_NAME = 'codec_text' def test_encode_known_long(self): """ @@ -50,40 +48,6 @@ def test_return_to_ascii(self): code = 'Hello World!'.encode('datamatrix.text') self.assertEqual(code, b'\xef\r\xd3\xa0E\x13(\xb3\xf2ji\xfe') - def test_encode_ASCII(self): - """Encode ASCII and compare to datamatrix-svg.""" - # Apparently, datamatrix-svg has a problem with '`' (it encodes - # 'A`' to the same as it encode 'A9'). Skip '`' for now: - msg = ASCII[:96] + ASCII[97:] - code = msg.encode('datamatrix.text') - # Note: datamatrix-svg actually encodes ending in '1\xfe\x80', - # not '3\xfe\x80'. But the '3' encodes a dummy 'set1' code, while - # '1' encodes a dummy 'set3' code which is equivalent. "Dummy" - # meaning immediately followed by another swich alphabet code - # (in this case \xFE which means 'return to ascii'). - truth = (b'\xef\x00\x01\x06C\x00y\x19\x06\x00\xf1+\xc9\x01i>\x8c\x01' - b'\xe1QO\x02Yd\x12\x02\xd1v\xd5\x03I\x89\x98\x03\xc1\x9c[\x04' - b'9\xaf\x1e\x04\xb1\xc2:\x00*\x06\x92\x12\xed\x07\n%\xb0\x07' - b'\x828s\x07\xfaK6\x08u 83sF\xae\x08\x9ad:\t\x12v\xfd\t\x8b' - b'\x06\x93\x0c\xfb\x19V\rs,\x19\r\xeb>\xdc\x0ecQ\x9f\x0e\xdbd' - b'b\x0fSw%\x0f\xcb\x89\xe8\x10C\x9c\xab\t\xb2\x90\x01\n*\xa4' - b'\xc0f\xbby\xf6\x8d1\xa0l\xb3\xa7\xc6\xe2\xda\x1d\xedX\x10' - b'\xbb\xafn\x113\xfe\x80') - self.assertTrue(code == truth) - - def test_decode_invalid_TEXT(self): - """Try to decode invalid code.""" - - code = 9 * b'\x00' - with self.assertRaises(ValueError): - code.decode('datamatrix.text') - - def test_search_nonTEXT(self): - """Test that search_codec callback returns None for non-TEXT.""" - - from ppf.datamatrix import codec_text - self.assertTrue(codec_text.search_codec_text('invalid') is None) - if __name__ == '__main__': # This enables running the unit tests by running this script which is