From 3546e0c4bb5169aff242d73fe964d5dded6fd6f5 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Sat, 23 Jul 2016 22:09:35 +0200 Subject: [PATCH] improve the exceptions also adds a stump for the tests for MagicEncode --- src/escpos/exceptions.py | 16 ++++++------ test/test_magicencode.py | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 test/test_magicencode.py diff --git a/src/escpos/exceptions.py b/src/escpos/exceptions.py index d0e2bd6..0f9f058 100644 --- a/src/escpos/exceptions.py +++ b/src/escpos/exceptions.py @@ -87,7 +87,7 @@ class BarcodeCodeError(Error): self.resultcode = 30 def __str__(self): - return "No Barcode code was supplied" + return "No Barcode code was supplied ({msg})".format(msg=self.msg) class ImageSizeError(Error): @@ -101,7 +101,7 @@ class ImageSizeError(Error): self.resultcode = 40 def __str__(self): - return "Image height is longer than 255px and can't be printed" + return "Image height is longer than 255px and can't be printed ({msg})".format(msg=self.msg) class TextError(Error): @@ -116,7 +116,7 @@ class TextError(Error): self.resultcode = 50 def __str__(self): - return "Text string must be supplied to the text() method" + return "Text string must be supplied to the text() method ({msg})".format(msg=self.msg) class CashDrawerError(Error): @@ -131,7 +131,7 @@ class CashDrawerError(Error): self.resultcode = 60 def __str__(self): - return "Valid pin must be set to send pulse" + return "Valid pin must be set to send pulse ({msg})".format(msg=self.msg) class TabPosError(Error): @@ -146,7 +146,7 @@ class TabPosError(Error): self.resultcode = 70 def __str__(self): - return "Valid tab positions must be in the range 0 to 16" + return "Valid tab positions must be in the range 0 to 16 ({msg})".format(msg=self.msg) class CharCodeError(Error): @@ -161,7 +161,7 @@ class CharCodeError(Error): self.resultcode = 80 def __str__(self): - return "Valid char code must be set" + return "Valid char code must be set ({msg})".format(msg=self.msg) class USBNotFoundError(Error): @@ -176,7 +176,7 @@ class USBNotFoundError(Error): self.resultcode = 90 def __str__(self): - return "USB device not found" + return "USB device not found ({msg})".format(msg=self.msg) class SetVariableError(Error): @@ -191,7 +191,7 @@ class SetVariableError(Error): self.resultcode = 100 def __str__(self): - return "Set variable out of range" + return "Set variable out of range ({msg})".format(msg=self.msg) # Configuration errors diff --git a/test/test_magicencode.py b/test/test_magicencode.py new file mode 100644 index 0000000..403bc75 --- /dev/null +++ b/test/test_magicencode.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +"""tests for panel button function + +:author: `Patrick Kanzler `_ +:organization: `python-escpos `_ +:copyright: Copyright (c) 2016 `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 + +from nose.tools import with_setup + +import escpos.printer as printer +import os + +devfile = 'testfile' + + +def setup_testfile(): + """create a testfile as devfile""" + fhandle = open(devfile, 'a') + try: + os.utime(devfile, None) + finally: + fhandle.close() + + +def teardown_testfile(): + """destroy testfile again""" + os.remove(devfile) + + +@with_setup(setup_testfile, teardown_testfile) +def test_function_panel_button_on(): + """test the panel button function (enabling) by comparing output""" + instance = printer.File(devfile=devfile) + instance.panel_buttons() + instance.flush() + with open(devfile, "rb") as f: + assert(f.read() == b'\x1B\x63\x35\x00') + + +@with_setup(setup_testfile, teardown_testfile) +def test_function_panel_button_off(): + """test the panel button function (disabling) by comparing output""" + instance = printer.File(devfile=devfile) + instance.panel_buttons(False) + instance.flush() + with open(devfile, "rb") as f: + assert(f.read() == b'\x1B\x63\x35\x01')