-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_tuple_utils.py
80 lines (69 loc) · 2.74 KB
/
test_tuple_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import unittest
import unittest.mock
from io import StringIO
import tuple_utils
class TupleUtilsTest(unittest.TestCase):
def test_tic_tac_toe_finish(self):
board_in = (
['O', 'X', ''],
['X', 'X', ''],
['', '', '']
)
board_out = (
['O', 'X', ''],
['X', 'X', 'X'],
['', '', '']
)
tuple_utils.tic_tac_toe_finish(board_in, 1, 2, 'X')
self.assertEqual(board_in, board_out)
def test2_tic_tac_toe_finish(self):
board_in = (
['', '', ''],
['X', 'O', ''],
['X', '', 'O']
)
board_out = (
['O', '', ''],
['X', 'O', ''],
['X', '', 'O']
)
tuple_utils.tic_tac_toe_finish(board_in, 0, 0, 'O')
self.assertEqual(board_in, board_out)
def test_count_instances(self):
test_cases = [
((1, 1, 1, 0), 1, 3),
(('A', 'B', 'B', 'C'), 'B', 2)
]
for tuple_to_evaluate, item_to_count, expected in test_cases:
with self.subTest(f"{tuple_to_evaluate}, {item_to_count} -> {expected}"):
self.assertEqual(expected, tuple_utils.count_instances(tuple_to_evaluate, item_to_count))
@unittest.mock.patch('sys.stdout', new_callable=StringIO)
def test_print_indexes_and_entries(self, mock_stdout):
indexes = ('Bilbo', 'Thorin', 'Gandalf')
entries = ('Burglar', 'Leader', 'Wizard')
tuple_utils.print_indexes_and_entries(indexes, entries)
expected = "Index: Bilbo Entry: Burglar\n" \
"Index: Thorin Entry: Leader\n" \
"Index: Gandalf Entry: Wizard\n"
self.assertEqual(expected, mock_stdout.getvalue())
@unittest.mock.patch('sys.stdout', new_callable=StringIO)
def test2_print_indexes_and_entries(self, mock_stdout):
indexes = (1, 2, 3)
entries = ('A', 'B', 'C')
tuple_utils.print_indexes_and_entries(indexes, entries)
expected = "Index: 1 Entry: A\n" \
"Index: 2 Entry: B\n" \
"Index: 3 Entry: C\n"
self.assertEqual(expected, mock_stdout.getvalue())
@unittest.mock.patch('sys.stdout', new_callable=StringIO)
def test_print_items_with_index(self, mock_stdout):
items = ('Pride', 'Envy', 'Gluttony', 'Lust', 'Anger', 'Greed', 'Sloth')
tuple_utils.print_items_with_index(items)
expected = "1: Pride\n" \
"2: Envy\n" \
"3: Gluttony\n" \
"4: Lust\n" \
"5: Anger\n" \
"6: Greed\n" \
"7: Sloth\n"
self.assertEqual(expected, mock_stdout.getvalue())