/
unit.py
185 lines (130 loc) · 6.79 KB
/
unit.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import unittest
from hamcrest.core.assert_that import assert_that
from hamcrest.core.core.allof import all_of
from hamcrest.core.core.isequal import equal_to
from hamcrest.core.core.isnot import is_not
from hamcrest.library.collection.isdict_containing import has_entry
from hamcrest.library.number.ordering_comparison import greater_than_or_equal_to, less_than_or_equal_to
from hamcrest.library.text.stringcontains import contains_string
from pyDoubles.framework import *
from robot import *
__author__ = 'rubenbp'
class RobotInitializeTest(unittest.TestCase):
def setUp(self):
self.server_proxy = spy(ServerProxy(None))
when(self.server_proxy.move).then_return(("OK", 10))
next_cell_calculator = stub(NextCellCalculator(0, 100))
when(next_cell_calculator.next).then_return(5)
self.robot = Robot(
"robocop",self.server_proxy, next_cell_calculator)
def test_initial_score_is_zero(self):
assert_that(self.robot.score, equal_to(0))
def test_initialize_the_game(self):
self.robot.start(max_moves = 1)
assert_that_method(self.server_proxy.init
).was_called().with_args("robocop")
def test_with_gameover_no_movements_are_made(self):
when(self.server_proxy.init).then_return("GameOver")
self.robot.start(max_moves = 2)
assert_that_method(self.server_proxy.move).was_never_called()
class RobotMovesTests(unittest.TestCase):
def setUp(self):
self.server_proxy_spy = spy(ServerProxy)
self.next_cell_calculator = spy(NextCellCalculator(0, 100))
when(self.next_cell_calculator.next).then_return(10)
self.robot = Robot(
"robocop", self.server_proxy_spy, self.next_cell_calculator)
def test_move_to_cell_severals_times(self):
when(self.server_proxy_spy.move).then_return(("OK", 10))
self.robot.start(max_moves = 2)
assert_that_method(self.server_proxy_spy.move).was_called().times(2)
def test_game_over_in_a_move(self):
when(self.server_proxy_spy.move).then_return(("GameOver", 0))
self.robot.start(max_moves = 10)
assert_that(self.robot.status, equal_to("GameOver"))
assert_that(self.robot.total_moves, equal_to(1))
def test_stop_game_with_nogame_status(self):
when(self.server_proxy_spy.move).then_return(("NoGame", 0))
self.robot.start(max_moves = 10)
assert_that(self.robot.status, equal_to("NoGame"))
assert_that(self.robot.total_moves, equal_to(1))
def test_win_in_a_move(self):
when(self.server_proxy_spy.move).then_return(("YouWin",10))
self.robot.start(max_moves = 2)
assert_that(self.robot.status, equal_to("YouWin"))
assert_that(self.robot.total_moves, equal_to(1))
class RobotUsingNextCellCalculatorTests(unittest.TestCase):
def setUp(self):
self.server_proxy_stub = stub(ServerProxy)
when(self.server_proxy_stub.move).then_return(("OK", 10))
self.next_cell_calculator = spy(NextCellCalculator(0, 100))
when(self.next_cell_calculator.next).then_return(10)
self.robot = Robot(
"robocop", self.server_proxy_stub, self.next_cell_calculator)
def test_robot_use_next_cell_calculator(self):
self.robot.start(max_moves = 1)
assert_that_method(self.next_cell_calculator.next).was_called()
def test_register_movement_in_cell_calculator(self):
self.robot.start(max_moves = 1)
assert_that_method(self.next_cell_calculator.register_cell_score
).was_called().with_args(10, 10)
class NextCellCalculatorTests(unittest.TestCase):
def setUp(self):
self.next_cell_calculator = NextCellCalculator(0, 100)
self.next_cell_calculator.jump = 1
def test_return_next_cell_in_valid_range(self):
for x in range(999):
assert_that(
self.next_cell_calculator.next(),
all_of(greater_than_or_equal_to(0),
less_than_or_equal_to(100)))
def test_dont_return_twice_the_same_number(self):
for x in range(500):
assert_that(
self.next_cell_calculator.next(),
is_not(equal_to(self.next_cell_calculator.next())))
def test_register_severals_cells(self):
self.next_cell_calculator.register_cell_score(1, 10)
self.next_cell_calculator.register_cell_score(2, 30)
assert_that(len(self.next_cell_calculator.cell_scores), equal_to(2))
assert_that(self.next_cell_calculator.cell_scores, has_entry(1, 10))
assert_that(self.next_cell_calculator.cell_scores, has_entry(2, 30))
def test_dont_register_same_cell_twice(self):
self.next_cell_calculator.register_cell_score(1, 10)
self.next_cell_calculator.register_cell_score(1, 10)
assert_that(len(self.next_cell_calculator.cell_scores), equal_to(1))
def test_register_last_cell_score(self):
self.next_cell_calculator.register_cell_score(1, 10)
self.next_cell_calculator.register_cell_score(2, 30)
assert_that(self.next_cell_calculator.last_cell.index, equal_to(2))
assert_that(self.next_cell_calculator.last_cell.score, equal_to(30))
def test_move_to_high_cell_score(self):
self.next_cell_calculator.cell_scores = {1:10, 2:50, 3:5}
self.next_cell_calculator.seek = 3
self.next_cell_calculator.last_cell = Cell(3,5)
assert_that(self.next_cell_calculator.next(), equal_to(2))
def test_dont_move_to_zero_cell_score(self):
self.next_cell_calculator.cell_scores = {2:0, 3:0}
self.next_cell_calculator.seek = 1
self.next_cell_calculator.last_cell = Cell(1,5)
assert_that(self.next_cell_calculator.next(), equal_to(4))
def test_dont_move_to_bad_cell_score(self):
self.next_cell_calculator.cell_scores = {2:8, 3:10}
self.next_cell_calculator.seek = 1
self.next_cell_calculator.min_cell_score_to_move = 12
self.next_cell_calculator.last_cell = Cell(1,5)
assert_that(self.next_cell_calculator.next(), equal_to(4))
class UrlGeneratorTest(unittest.TestCase):
def test_make_move_url(self):
url_generator = UrlGenerator("http://fake/")
result_url = url_generator.move_url("robocop", 30)
assert_that(result_url, contains_string("http://fake/"))
assert_that(result_url, contains_string("player=robocop"))
assert_that(result_url, contains_string("command=Move"))
assert_that(result_url, contains_string("position=30"))
def test_make_init_url(self):
url_generator = UrlGenerator("http://fake/")
result_url = url_generator.init_url("robocop")
assert_that(result_url, contains_string("http://fake/"))
assert_that(result_url, contains_string("player=robocop"))
assert_that(result_url, contains_string("command=Init"))