Skip to content

Commit 131f965

Browse files
authored
Merge pull request #1033 from Axelrod-Python/884-hunter
Refactor tests for hunter.
2 parents a166645 + 0c34fbc commit 131f965

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

axelrod/tests/strategies/test_axelrod_first.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,6 @@ def test_strategy(self):
253253
# Test TFT-type initial play
254254
self.first_play_test(C)
255255

256-
# self.responses_test([D], [C, D], [D, C])
257-
258256
# Test trailing post-round 3 play
259257

260258
actions = [(C, C)] * 9

axelrod/tests/strategies/test_hunter.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ class TestDefectorHunter(TestPlayer):
4747

4848
def test_strategy(self):
4949
self.first_play_test(C)
50-
for i in range(3):
51-
self.responses_test([C], [C] * i, [D] * i)
52-
self.responses_test([D], [C] * 4, [D] * 4)
5350

51+
actions = [(C, D)] * 4 + [(D, D)] * 10
52+
self.versus_test(opponent=axelrod.Defector(), expected_actions=actions)
53+
54+
actions = [(C, C)] * 14
55+
self.versus_test(opponent=axelrod.Cooperator(),
56+
expected_actions=actions)
5457

5558
class TestCooperatorHunter(TestPlayer):
5659

@@ -67,10 +70,13 @@ class TestCooperatorHunter(TestPlayer):
6770
}
6871

6972
def test_strategy(self):
70-
self.first_play_test(C)
71-
for i in range(3):
72-
self.responses_test([C], [C] * i, [C] * i)
73-
self.responses_test([D], [C] * 4, [C] * 4)
73+
actions = [(C, C)] * 4 + [(D, C)] * 10
74+
self.versus_test(opponent=axelrod.Cooperator(),
75+
expected_actions=actions)
76+
77+
actions = [(C, D)] * 14
78+
self.versus_test(opponent=axelrod.Defector(),
79+
expected_actions=actions)
7480

7581

7682
class TestAlternatorHunter(TestPlayer):
@@ -89,14 +95,13 @@ class TestAlternatorHunter(TestPlayer):
8995

9096
def test_strategy(self):
9197
self.first_play_test(C)
92-
self.responses_test([C], [C] * 2, [C, D], attrs={'is_alt': False})
93-
self.responses_test([C], [C] * 3, [C, D, C], attrs={'is_alt': False})
94-
self.responses_test([C], [C] * 4, [C, D] * 2, attrs={'is_alt': False})
95-
self.responses_test([C], [C] * 5, [C, D] * 2 + [C],
96-
attrs={'is_alt': False})
97-
self.responses_test([D], [C] * 6, [C, D] * 3, attrs={'is_alt': True})
98-
self.responses_test([D], [C] * 7, [C, D] * 3 + [C],
99-
attrs={'is_alt': True})
98+
actions = [(C, C), (C, D)] * 3 + [(D, C), (D, D)] * 5
99+
self.versus_test(opponent=axelrod.Alternator(),
100+
expected_actions=actions, attrs={'is_alt': True})
101+
102+
actions = [(C, D)] * 14
103+
self.versus_test(opponent=axelrod.Defector(),
104+
expected_actions=actions, attrs={'is_alt': False})
100105

101106
def test_reset_attr(self):
102107
p = self.player()
@@ -200,7 +205,9 @@ class TestMathConstantHunter(TestPlayer):
200205
}
201206

202207
def test_strategy(self):
203-
self.responses_test([D], [C] * 8, [C] * 7 + [D])
208+
opponent = axelrod.MockPlayer([C] * 7 + [D] * 3)
209+
actions = [(C, C)] * 7 + [(C, D)]
210+
self.versus_test(opponent=opponent, expected_actions=actions)
204211

205212

206213
class TestRandomHunter(TestPlayer):
@@ -220,16 +227,15 @@ class TestRandomHunter(TestPlayer):
220227
def test_strategy(self):
221228

222229
# We should catch the alternator here.
223-
self.responses_test([D], [C] * 12, [C, D] * 6)
224-
225-
# It is still possible for this test to fail, but very unlikely.
226-
history1 = [C] * 100
227-
history2 = [random.choice([C, D]) for i in range(100)]
228-
self.responses_test(D, history1, history2)
229-
230-
history1 = [D] * 100
231-
history2 = [random.choice([C, D]) for i in range(100)]
232-
self.responses_test(D, history1, history2)
230+
actions = [(C, C), (C, D)] * 5 + [(C, C), (D, D), (D, C)]
231+
self.versus_test(opponent=axelrod.Alternator(),
232+
expected_actions=actions, attrs={"countCC": 5,
233+
"countDD": 0})
234+
235+
actions = [(C, D)] * 14
236+
self.versus_test(opponent=axelrod.Defector(),
237+
expected_actions=actions, attrs={"countCC": 0,
238+
"countDD": 0})
233239

234240
def test_reset(self):
235241
player = self.player()

axelrod/tests/strategies/test_player.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ def test_init_kwargs(self):
337337

338338

339339
def test_responses(test_class, player1, player2, responses, history1=None,
340-
history2=None, seed=None, attrs=None):
340+
history2=None, seed=None):
341341
"""
342342
Test responses to arbitrary histories. Used for the following tests
343343
in TestPlayer: first_play_test, second_play_test, and responses_test.
@@ -359,9 +359,6 @@ def test_responses(test_class, player1, player2, responses, history1=None,
359359
for response in responses:
360360
s1, s2 = simulate_play(player1, player2)
361361
test_class.assertEqual(s1, response)
362-
if attrs:
363-
for attr, value in attrs.items():
364-
test_class.assertEqual(getattr(player1, attr), value)
365362

366363

367364
class TestOpponent(Player):
@@ -541,7 +538,6 @@ def versus_test(self, opponent, expected_actions,
541538

542539
def responses_test(self, responses, player_history=None,
543540
opponent_history=None, seed=None, length=200,
544-
attrs=None,
545541
init_args=None, init_kwargs=None):
546542
"""Test responses to arbitrary histories. A match is played where the
547543
histories are enforced and the sequence of plays in responses is
@@ -576,19 +572,19 @@ def responses_test(self, responses, player_history=None,
576572
player2 = MockPlayer()
577573
player2.set_match_attributes(length=length)
578574
test_responses(self, player1, player2, responses, player_history,
579-
opponent_history, seed=seed, attrs=attrs)
575+
opponent_history, seed=seed)
580576

581577
# Test that we get the same sequence after a reset
582578
player1.reset()
583579
player2.reset()
584580
test_responses(self, player1, player2, responses, player_history,
585-
opponent_history, seed=seed, attrs=attrs)
581+
opponent_history, seed=seed)
586582

587583
# Test that we get the same sequence after a clone
588584
player1 = player1.clone()
589585
player2 = player2.clone()
590586
test_responses(self, player1, player2, responses, player_history,
591-
opponent_history, seed=seed, attrs=attrs)
587+
opponent_history, seed=seed)
592588

593589
def classifier_test(self, expected_class_classifier=None):
594590
"""Test that the keys in the expected_classifier dictionary give the

0 commit comments

Comments
 (0)