Skip to content

Commit ea2181c

Browse files
committed
refactor tests for new code
1 parent 160a841 commit ea2181c

File tree

2 files changed

+85
-97
lines changed

2 files changed

+85
-97
lines changed

spiral_matrix/spiral_matrix.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class SpiralMatrix():
7474
}
7575

7676
def __init__(self, dimension=None, bearing='E', turn=False,
77-
start=1, step=1, filename=None, words=None, test=False):
77+
start=1, step=1, filename=None, words=None, testing=False):
7878
'''
7979
Generate a new instance of SpiralMatrix.
8080
@@ -101,10 +101,10 @@ def __init__(self, dimension=None, bearing='E', turn=False,
101101
self.turn = 'right' if turn else 'left'
102102
self.series = self._series(
103103
filename, words, self._start(start), self._step(step))
104-
self.width = self._width()
104+
self.width = self._width(self.series)
105105

106106
# Build the matrix structure that conforms to the attributes.
107-
if not test:
107+
if not testing:
108108
self._build()
109109

110110
def _dimension(self, dimension):
@@ -252,15 +252,13 @@ def _series_from_integers(self, start, step):
252252

253253
return series
254254

255-
def _width(self):
255+
def _width(self, series):
256256
'''
257257
Set cell-width for printing, equal to the widest element in the series.
258258
259259
Return the width integer.
260260
'''
261261

262-
series = self.series
263-
264262
width = 0
265263
for i in range(len(series)):
266264
if len(str(series[i])) > width:

test/test_spiral_matrix.py

+81-91
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from spiral_matrix.spiral_matrix import SpiralMatrix
99

1010
################################################################################
11-
class SpiralMatrixInstantiationTestCase(unittest.TestCase):
11+
class SpiralMatrixTestCase(unittest.TestCase):
1212

13-
def test_simple_instantiation(self):
13+
def test_01_simple_instantiation(self):
1414

1515
pass_configs = [3, 5, 23, 1.21e2, '3']
1616
for config in pass_configs:
@@ -55,94 +55,81 @@ def test_build(self):
5555
################################################################################
5656
class SpiralMatrixMethodsTestCase(unittest.TestCase):
5757

58-
def setUp(self):
59-
60-
self.matrix_dimension = 5
61-
62-
def test_series(self):
63-
64-
m = SpiralMatrix(self.matrix_dimension)
58+
def test_01_series_from_integers(self):
6559

6660
pass_configs = [
67-
{ 'start': 1, 'step': 1,
61+
{ 'dimension': 5, 'start': 1, 'step': 1,
6862
'want_0_0_value': 17 },
69-
{ 'start': 1000, 'step': 2,
63+
{ 'dimension': 5, 'start': 1000, 'step': 2,
7064
'want_0_0_value': 1032 },
71-
{ 'start': -100, 'step': 3,
65+
{ 'dimension': 5, 'start': -100, 'step': 3,
7266
'want_0_0_value': -52 },
7367
]
7468
for config in pass_configs:
7569
with self.subTest(config=config):
76-
m.start, m.step, want_0_0_value = config.values()
77-
m.series = m._series()
70+
dimension, start, step, want_0_0_value = config.values()
71+
m = SpiralMatrix(dimension)
72+
m.series = m._series_from_integers(start, step)
7873
self.assertIsInstance(m.series, range)
7974
self.assertEqual(len(m.series), m.max)
8075
m._build()
8176
self.assertEqual(m.matrix[0][0], want_0_0_value)
8277

83-
def test_series_from_string(self):
84-
85-
m = SpiralMatrix(self.matrix_dimension)
78+
def test_02_series_from_string(self):
8679

8780
pass_configs = [
88-
{ 'words': 'vulture duck crocodile elephant tiger',
81+
{ 'dimension': 5, 'words': 'vulture duck crocodile elephant tiger',
8982
'want_0_0_value': 'duck' },
90-
{ 'words': '10 4 2 45 31 8.88 22 -7 0 94',
83+
{ 'dimension': 5, 'words': '10 4 2 45 31 8.88 22 -7 0 94',
9184
'want_0_0_value': '22' },
92-
{ 'words': '~ @#$ ) ( !*% +!',
85+
{ 'dimension': 5, 'words': '~ @#$ ) ( !*% +!',
9386
'want_0_0_value': '!*%' },
9487
]
9588
for config in pass_configs:
9689
with self.subTest(config=config):
97-
m.words, want_0_0_value = config.values()
98-
m.series = m._series_from_string()
90+
dimension, words, want_0_0_value = config.values()
91+
m = SpiralMatrix(dimension)
92+
m.series = m._series_from_string(words)
9993
self.assertIsInstance(m.series, list)
10094
self.assertEqual(len(m.series), m.max)
10195
m._build()
10296
self.assertEqual(m.matrix[0][0], want_0_0_value)
10397

104-
def test_series_from_file(self):
98+
def test_03_series_from_file(self):
10599

106-
m = SpiralMatrix(self.matrix_dimension)
107-
108-
# import os
109-
# file_path = '%s/input' % (os.path.dirname(os.path.abspath(__file__)))
110-
# pass_configs = ['tests/input/%s' % (f) for f in os.listdir(file_path)]
100+
from os import path
101+
cwd = path.dirname(__file__)
111102

112103
pass_configs = [
113-
{ 'filename': 'test/test-input/5-letter-words.txt',
114-
'want_0_0_value': 'alike' },
115-
{ 'filename': 'test/test-input/3-letter-words.txt',
116-
'want_0_0_value': 'alt' },
117-
{ 'filename': 'test/test-input/lorem-ipsum.txt',
118-
'want_0_0_value': 'at.' },
104+
{ 'filename': f'{cwd}/test-input/3-letter-words.txt',
105+
'want_77th_element': 'cam' },
106+
{ 'filename': f'{cwd}/test-input/5-letter-words.txt',
107+
'want_77th_element': 'cable' },
108+
{ 'filename': f'{cwd}/test-input/lorem-ipsum.txt',
109+
'want_77th_element': 'Integer' },
119110
]
111+
120112
for config in pass_configs:
121113
with self.subTest(config=config):
122-
filename, want_0_0_value = config.values()
123-
with open(filename) as file:
124-
m.file = file
125-
m.series = m._series_from_file()
126-
self.assertIsInstance(m.series, list)
127-
self.assertEqual(len(m.series), m.max)
128-
m._build()
129-
self.assertEqual(m.matrix[0][0], want_0_0_value)
130-
131-
# fail_configs = [
132-
# { 'filename': 'tests/input/binary.dat' },
133-
# { 'filename': 'tests/input/empty.txt' },
134-
# ]
135-
# for config in fail_configs:
136-
# with self.subTest(config=config):
137-
# filename, = config.values()
138-
# with open(filename) as file:
139-
# m.file = file
140-
# with self.assertRaises(argparse.ArgumentError):
141-
# m.series = m._series_from_file()
142-
143-
def test_width(self):
144-
145-
m = SpiralMatrix(self.matrix_dimension)
114+
filename, want_77th_element = config.values()
115+
m = SpiralMatrix(9)
116+
series = m._series_from_file(filename)
117+
self.assertIsInstance(series, list)
118+
self.assertEqual(len(series), m.max)
119+
self.assertEqual(series[76], want_77th_element)
120+
121+
fail_configs = [
122+
{ 'filename': f'{cwd}/test-input/binary.dat' },
123+
{ 'filename': f'{cwd}/test-input/empty.txt' },
124+
]
125+
for config in fail_configs:
126+
with self.subTest(config=config):
127+
filename, = config.values()
128+
m = SpiralMatrix(3)
129+
with self.assertRaises(AttributeError):
130+
series = m._series_from_file(filename)
131+
132+
def test_04_width(self):
146133

147134
pass_configs = [
148135
{ 'series': ['vulture', 'duck', 'crocodile', 'elephant', 'tiger'],
@@ -152,79 +139,82 @@ def test_width(self):
152139
]
153140
for config in pass_configs:
154141
with self.subTest(config=config):
155-
m.series, want_width = config.values()
156-
m.width = m._width()
157-
self.assertIsInstance(m.width, int)
158-
self.assertEqual(m.width, want_width)
159-
160-
def test_fill(self):
142+
series, want_width = config.values()
143+
m = SpiralMatrix(5)
144+
width = m._width(series)
145+
self.assertIsInstance(width, int)
146+
self.assertEqual(width, want_width)
161147

162-
m = SpiralMatrix(self.matrix_dimension)
148+
def test_05_fill(self):
163149

164150
pass_configs = [
165-
{ 'series': ['vulture', 'duck', 'crocodile', 'elephant', 'tiger'],
166-
'index': 2, 'coords': (0, 2) },
151+
{ 'series': ['emu', 'duck', 'crocodile', 'shark', 'tiger', 'worm'],
152+
'index': 12,
153+
'want_coords': (1, 4) },
167154
{ 'series': range(30, 50, 2),
168-
'index': 7, 'coords': (1, 0) },
155+
'index': 21,
156+
'want_coords': (4, 0) },
169157
]
170158
for config in pass_configs:
171159
with self.subTest(config=config):
172-
m.series, index, coords = config.values()
173-
new_index = m._fill(coords, index)
160+
series, index, want_coords = config.values()
161+
m = SpiralMatrix(5)
162+
m.series = (list(series) * 5)[:m.max]
163+
new_index = m._fill(want_coords, index)
174164
self.assertIsInstance(new_index, int)
175165
self.assertEqual(new_index, index + 1)
176-
self.assertEqual(m.matrix[coords[0]][coords[1]],
177-
m.series[index])
178-
179-
def test_look(self):
166+
self.assertEqual(m.series[index],
167+
m.matrix[want_coords[0]][want_coords[1]])
180168

181-
m = SpiralMatrix(self.matrix_dimension)
169+
def test_06_look(self):
182170

183171
pass_configs = [
184-
{ 'cell': (1, 2), 'bearing': m.N, 'look': 'left',
172+
{ 'cell': (1, 2), 'bearing': 'N', 'look': 'left',
185173
'want_cell': (1, 1) },
186-
{ 'cell': (0, 0), 'bearing': m.E, 'look': 'right',
174+
{ 'cell': (0, 0), 'bearing': 'E', 'look': 'right',
187175
'want_cell': (1, 0) },
188-
### TODO: better handle a look outside the matrix. expect error?
189-
{ 'cell': (0, 1), 'bearing': m.E, 'look': 'left',
176+
{ 'cell': (0, 1), 'bearing': 'E', 'look': 'left',
190177
'want_cell': (-1, 1) },
191178
]
192179
for config in pass_configs:
193180
with self.subTest(config=config):
194181
cell, bearing, look, want_cell = config.values()
182+
m = SpiralMatrix(5)
183+
bearing = m.compass[bearing]
195184
y, x = m._look(cell, look, bearing)
196185
self.assertEqual(y, want_cell[0])
197186
self.assertEqual(x, want_cell[1])
198187

199-
def test_turn(self):
200-
201-
m = SpiralMatrix(self.matrix_dimension)
188+
def test_07_turn(self):
202189

203190
pass_configs = [
204-
{ 'cell': (1, 1), 'turn': 'left', 'bearing': m.N,
205-
'want_bearing': m.W },
206-
{ 'cell': (1, 0), 'turn': 'right', 'bearing': m.S,
207-
'want_bearing': m.W },
191+
{ 'cell': (1, 1), 'turn': 'left', 'bearing': 'N',
192+
'want_bearing': 'W' },
193+
{ 'cell': (1, 0), 'turn': 'right', 'bearing': 'S',
194+
'want_bearing': 'W' },
208195
]
209196
for config in pass_configs:
210197
with self.subTest(config=config):
211198
cell, turn, bearing, want_bearing = config.values()
199+
m = SpiralMatrix(5)
200+
bearing = m.compass[bearing]
201+
want_bearing = m.compass[want_bearing]
212202
new_bearing = m._turn(turn, bearing)
213203
self.assertEqual(new_bearing, want_bearing)
214204

215-
def test_move(self):
216-
217-
m = SpiralMatrix(self.matrix_dimension)
205+
def test_08_move(self):
218206

219207
pass_configs = [
220-
{ 'cell': (1, 1), 'bearing': m.N,
208+
{ 'cell': (1, 1), 'bearing': 'N',
221209
'want_coords': (0, 1) },
222-
{ 'cell': (0, 2), 'bearing': m.S,
210+
{ 'cell': (0, 2), 'bearing': 'S',
223211
'want_coords': (1, 2) },
224212
]
225213
for config in pass_configs:
226214
with self.subTest(config=config):
227215
cell, bearing, want_coords = config.values()
216+
m = SpiralMatrix(5)
217+
bearing = m.compass[bearing]
228218
y, x = m._move(cell, bearing)
229219
self.assertEqual((y, x), want_coords)
230220

0 commit comments

Comments
 (0)