-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_player.py
More file actions
147 lines (132 loc) · 6.76 KB
/
test_player.py
File metadata and controls
147 lines (132 loc) · 6.76 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# pylint: skip-file
from io import StringIO
from unittest import TestCase
from unittest.mock import patch
import player
import ship
class MyTestCase(TestCase):
def setUp(self):
self.player_object = player.Player()
self.human_object = player.Human()
self.AI_object = player.AI()
self.battleship = ship.Ship(position=[2, 2])
self.cruiser = ship.Ship(position=[3, 2], length=4, letter="C")
self.player_object.ships = [self.battleship, self.cruiser]
self.shooting_range = {"45": "o", "23": "x", "11": "o"}
def test_clear_console(self):
expected_out = ""
with patch('sys.stdout', new=StringIO()) as fake_out:
player.clear_console()
self.assertEqual(fake_out.getvalue(), expected_out)
def test_set_pause(self):
self.player_object.set_pause_mode(mode=2)
self.assertEqual(self.player_object.pause_mode, 2)
def test_set_pause_mode(self):
self.player_object.set_pause_mode(2)
self.assertEqual(self.player_object.pause_mode, 2)
def test_check_sunken(self):
self.assertEqual(self.player_object.check_sunken(), False)
self.battleship.sunken = True
self.cruiser.sunken = True
self.assertEqual(self.player_object.check_sunken(), True)
def test_get_shot(self):
self.battleship.placed = True
self.battleship.sunken = False
self.assertEqual(self.player_object.get_shot([2, 2], self.shooting_range), "hit")
self.assertEqual(self.player_object.get_shot([9, 2], self.shooting_range), "miss")
self.battleship.length = 1
self.assertEqual(self.player_object.get_shot([2, 2], self.shooting_range), "sink")
self.cruiser.length = 1
self.cruiser.placed = True
self.cruiser.sunken = False
self.assertEqual(self.player_object.get_shot([3, 2], self.shooting_range), "lost")
def test_prepare_ships(self):
self.battleship.length = 1
self.battleship.placed = True
self.cruiser.length = 1
self.cruiser.placed = True
self.assertEqual(self.player_object.prepare_ships(), {'33': 'B', '43': 'C'})
def test_get_segment(self):
return_state = ' |'
self.assertEqual(self.human_object.get_segment([0, 0], "none"), return_state)
def test_print_battlefield(self):
expected_battlefield = """ | a | b | c | d | e | f | g | h | i | j |
1 | | | | | | | | | | |
2 | | | | | | | | | | |
3 | | | | | | | | | | |
4 | | | | | | | | | | |
5 | | | | | | | | | | |
6 | | | | | | | | | | |
7 | | | | | | | | | | |
8 | | | | | | | | | | |
9 | | | | | | | | | | |
10 | | | | | | | | | | |
"""
with patch('sys.stdout', new=StringIO()) as fake_out:
self.player_object.print_battlefield(mode="none")
self.assertEqual(fake_out.getvalue(), expected_battlefield)
self.shooting_range = {}
with patch('sys.stdout', new=StringIO()) as fake_out_two:
self.player_object.print_battlefield(mode="markers")
self.assertEqual(fake_out_two.getvalue(), expected_battlefield)
def test_human_shoot(self):
self.assertEqual(self.human_object.shoot(), "miss")
def test_complete_shoot(self):
expected_out = ""
expected_out_two = """[1;37mUnnamed Player - Shoot Square[0;0m
| a | b | c | d | e | f | g | h | i | j |
1 |[[0;36mo[0;0m]| | | | | | | | | |
2 | | | | | | | | | | | Shooting Cursor Control:
3 | | | | | | | | | | |
4 | | | | | | | | | | | Q W P - show ship view / cursor up / pause
5 | | | | | | | | | | | A S D - move cursor left / down / right
6 | | | | | | | | | | | [Space] - shoot at square
7 | | | | | | | | | | |
8 | | | | | | | | | | |
9 | | | | | | | | | | |
10 | | | | | | | | | | |
"""
expected_out_three = """[1;37mUnnamed Player - Ship View[0;0m
| a | b | c | d | e | f | g | h | i | j |
1 | | | | | | | | | | |
2 | | | | | | | | | | | Player Ship View:
3 | | | | | | | | | | |
4 | | | | | | | | | | | W - return to Shooting Input
5 | | | | | | | | | | |
6 | | | | | | | | | | |
7 | | | | | | | | | | |
8 | | | | | | | | | | |
9 | | | | | | | | | | |
10 | | | | | | | | | | |
"""
with patch('sys.stdout', new=StringIO()) as fake_out:
self.human_object.complete_shoot(key=" ")
self.assertEqual(fake_out.getvalue(), expected_out)
with patch('sys.stdout', new=StringIO()) as fake_out_two:
self.human_object.complete_shoot()
self.assertEqual(fake_out_two.getvalue(), expected_out_two)
with patch('sys.stdout', new=StringIO()) as fake_out_three:
self.human_object.complete_shoot(key="w")
self.assertEqual(fake_out_three.getvalue(), expected_out_two)
with patch('sys.stdout', new=StringIO()) as fake_out_four:
self.human_object.complete_shoot(key="q")
self.assertEqual(fake_out_four.getvalue(), expected_out_three)
def test_draw_place_ships(self):
self.assertEqual(self.human_object.draw_place_ships(), False)
self.assertEqual(self.human_object.draw_place_ships(key=" ", shipper=self.battleship), True)
self.ships = None
self.assertEqual(self.human_object.draw_place_ships(key=" ", shipper=self.battleship), True)
self.battleship.orientation = True
self.assertEqual(self.human_object.draw_place_ships(key="w", shipper=self.battleship), True)
self.battleship.placed = False
self.assertEqual(self.human_object.draw_place_ships(shipper=self.battleship), True)
self.assertEqual(self.human_object.draw_place_ships(key="r", shipper=self.battleship), True)
self.assertEqual(self.human_object.draw_place_ships(key="w", shipper=self.battleship), True)
self.battleship.orientation = False
self.assertEqual(self.human_object.draw_place_ships(key="w", shipper=self.battleship), True)
def test_capture_input_place(self):
self.human_object.pause = True
self.human_object.capture_input_place(self.battleship)
self.assertEqual(self.human_object.pause, True)
def test_AI_shoot(self):
self.assertEqual(self.AI_object.shoot(), "miss")