Skip to content

Commit 8aa4714

Browse files
author
Kirill V. Lyadvinsky
committed
Added a few unit tests
1 parent 058107a commit 8aa4714

File tree

5 files changed

+91
-7
lines changed

5 files changed

+91
-7
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.PHONY: init test clean coverage lint
22

33
test:
4-
python3 -m unittest discover -v
4+
python3 -m unittest discover -v -b
55

66
clean:
77
rm -rf test/*.pyc

test/test_hobeta.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,27 @@
1212

1313

1414
class TestHobeta(unittest.TestCase):
15+
def test_args_parser(self):
16+
with self.assertRaises(SystemExit):
17+
args = hobeta.parse_args(("-h", "-v"))
18+
19+
temp_in_file = tempfile.mkstemp()[1]
20+
input_file = open(temp_in_file, "w")
21+
input_file.close()
22+
temp_out_file = tempfile.mkstemp()[1]
23+
try:
24+
args = hobeta.parse_args(("info", temp_in_file))
25+
self.assertEqual(args.func, hobeta.show_info)
26+
args.hobeta_file.close()
27+
28+
args = hobeta.parse_args(("strip", temp_in_file, temp_out_file))
29+
self.assertEqual(args.func, hobeta.strip_header)
30+
args.hobeta_file.close()
31+
args.output_file.close()
32+
finally:
33+
os.remove(temp_in_file)
34+
os.remove(temp_out_file)
35+
1536
def test_ckechsum(self):
1637
data = b'F.load.AC\x00\x80\xf9\x06\x00\x07'
1738
self.assertEqual(hobeta.calc_checksum(data), 20661)

test/test_zeus2txt.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,78 @@
77
import tempfile
88
import unittest
99
from collections import namedtuple
10+
import logging
1011

1112
from zxtools import zeus2txt
1213

1314

1415
class TestZeus2Txt(unittest.TestCase):
15-
def test_convert(self):
16-
test_file = io.BytesIO(self.test_data)
16+
def test_args_parser(self):
17+
with self.assertRaises(SystemExit):
18+
args = zeus2txt.parse_args(("-h", "-v"))
19+
20+
temp_in_file = tempfile.mkstemp()[1]
21+
input_file = open(temp_in_file, "w")
22+
input_file.close()
23+
temp_out_file = tempfile.mkstemp()[1]
24+
try:
25+
args = zeus2txt.parse_args(("info", temp_in_file))
26+
self.assertEqual(args.func, zeus2txt.show_info)
27+
args.zeus_file.close()
28+
29+
args = zeus2txt.parse_args(("convert", temp_in_file, temp_out_file))
30+
self.assertEqual(args.func, zeus2txt.convert_file)
31+
args.zeus_file.close()
32+
args.output_file.close()
33+
finally:
34+
os.remove(temp_in_file)
35+
os.remove(temp_out_file)
36+
37+
@staticmethod
38+
def prepare_convert_args(test_data):
39+
test_file = io.BytesIO(test_data)
1740
temp_output_path = tempfile.mkstemp()[1]
1841
temp_output_file = open(temp_output_path, "w")
1942

2043
Args = namedtuple('Args', "zeus_file output_file")
2144
parsed_args = Args(test_file, temp_output_file)
22-
zeus2txt.convert_file(parsed_args)
45+
return parsed_args, temp_output_path, temp_output_file
46+
47+
def test_undefined_token(self):
48+
logging.basicConfig(level=logging.DEBUG)
49+
args, temp_output_path, temp_output_file = self.prepare_convert_args(
50+
b"\x0A\x00\x0A\x06\xFF\x2C\x34\x32\x00\xFF\xFF")
51+
52+
try:
53+
zeus2txt.convert_file(args)
54+
temp_output_file.close()
55+
temp_output_file = open(temp_output_path, "r")
56+
lines = temp_output_file.read().splitlines()
57+
self.assertEqual(lines, ["00010 ,42",""])
58+
finally:
59+
temp_output_file.close()
60+
os.remove(temp_output_path)
61+
62+
def test_no_eof(self):
63+
args, temp_output_path, temp_output_file = self.prepare_convert_args(
64+
b"\x0A\x00\x0A\x06\x82\x87\x2C\x34\x32\x00")
65+
66+
try:
67+
zeus2txt.convert_file(args)
68+
temp_output_file.close()
69+
temp_output_file = open(temp_output_path, "r")
70+
lines = temp_output_file.read().splitlines()
71+
self.assertEqual(lines, ["00010 ADD BC,42",])
72+
finally:
73+
temp_output_file.close()
74+
os.remove(temp_output_path)
75+
76+
def test_convert(self):
77+
args, temp_output_path, temp_output_file = self.prepare_convert_args(
78+
self.test_data)
2379

2480
try:
81+
zeus2txt.convert_file(args)
2582
temp_output_file.close()
2683
temp_output_file = open(temp_output_path, "rb")
2784
lines = temp_output_file.read().splitlines()

zxtools/hobeta.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ def strip_header(parsed_args):
107107
length -= len(data)
108108
print("Created file %s, %d bytes copied." %
109109
(dst_file.name, bytes_to_copy-length))
110+
dst_file.close()
111+
src_file.close()
110112
return bytes_to_copy-length
111113

112114

zxtools/zeus2txt.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def read_file(src_file):
3939

4040

4141
def convert_file(parsed_args):
42+
""" Convert Zeus Z80 assembler file specified in zeus_file to the plain text and print it to the output_file """
43+
logger = logging.getLogger('convert_file')
44+
4245
process_string = False
4346
strnum_lo = False, 0
4447
tab = False
@@ -63,9 +66,9 @@ def convert_file(parsed_args):
6366
continue
6467
try:
6568
print(ASM_META[b-ASM_FIRST_TOKEN], end="", file=output)
66-
except KeyError:
67-
print("Token not defined: 0x%02X (%d), at line %05d"
68-
% (b, b, strnum))
69+
except IndexError:
70+
logger.warning("Token not defined: 0x%02X (%d), at line %05d"
71+
% (b, b, strnum))
6972
else:
7073
if not strnum_lo[0]:
7174
strnum_lo = True, b
@@ -76,6 +79,7 @@ def convert_file(parsed_args):
7679
break
7780
print("%05d" % strnum, end=" ", file=output)
7881
process_string = True
82+
output.close()
7983

8084

8185
def parse_args(args):

0 commit comments

Comments
 (0)