Skip to content

Commit 7a65c17

Browse files
authored
Merge pull request #1021 from Axelrod-Python/884-punisher
Refactor tests for punisher.
2 parents 592823b + edb39c6 commit 7a65c17

File tree

2 files changed

+67
-50
lines changed

2 files changed

+67
-50
lines changed

axelrod/strategies/punisher.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ def strategy(self, opponent: Player) -> Action:
5252
if self.grudged:
5353
self.grudge_memory += 1
5454
return D
55+
5556
elif D in opponent.history[-1:]:
5657
self.mem_length = (opponent.defections * 20) // len(opponent.history)
5758
self.grudged = True
5859
return D
60+
5961
return C
6062

6163
def reset(self):

axelrod/tests/strategies/test_punisher.py

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,36 @@ class TestPunisher(TestPlayer):
2222

2323
def test_init(self):
2424
"""Tests for the __init__ method."""
25-
P1 = axelrod.Punisher()
26-
self.assertEqual(P1.history, [])
27-
self.assertEqual(P1.mem_length, 1)
28-
self.assertEqual(P1.grudged, False)
29-
self.assertEqual(P1.grudge_memory, 1)
25+
player = axelrod.Punisher()
26+
self.assertEqual(player.mem_length, 1)
27+
self.assertFalse(player.grudged)
28+
self.assertEqual(player.grudge_memory, 1)
3029

3130
def test_strategy(self):
32-
self.responses_test([C], [], [], attrs={"grudged": False})
33-
self.responses_test([C], [C], [C], attrs={"grudged": False})
34-
self.responses_test([D], [C], [D], attrs={"grudged": True})
35-
for i in range(10):
36-
self.responses_test([D], [C, C] + [D] * i, [C, D] + [C] * i,
37-
attrs={"grudged": True, "grudge_memory": i,
38-
"mem_length": 10})
31+
32+
opponent = axelrod.Alternator()
33+
actions = [(C, C), (C, D), (D, C)]
34+
self.versus_test(opponent=opponent, expected_actions=actions,
35+
attrs={"grudged": True, "grudge_memory": 0})
36+
37+
opponent = axelrod.MockPlayer([C, D] + [C] * 10)
38+
actions = [(C, C), (C, D)] + [(D, C)] * 11
39+
self.versus_test(opponent=opponent, expected_actions=actions,
40+
attrs={"grudged": True, "grudge_memory": 10})
41+
3942
# Eventually the grudge is dropped
40-
i = 11
41-
self.responses_test([C], [C, C] + [D] * i, [C, D] + [C] * i,
42-
attrs={"grudged": False, "grudge_memory": 0,
43-
"mem_length": 10})
43+
opponent = axelrod.MockPlayer([C, D] + [C] * 10)
44+
actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, D)]
45+
self.versus_test(opponent=opponent, expected_actions=actions,
46+
attrs={"grudged": False, "grudge_memory": 0,
47+
"mem_length": 10})
4448

4549
# Grudged again on opponent's D
46-
self.responses_test([D], [C, C] + [D] * i + [C], [C, D] + [C] * i + [D],
47-
attrs={"grudged": True, "grudge_memory": 0,
48-
"mem_length": 2})
50+
opponent = axelrod.MockPlayer([C, D] + [C] * 11)
51+
actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, C), (C, D), (D, C)]
52+
self.versus_test(opponent=opponent, expected_actions=actions,
53+
attrs={"grudged": True, "grudge_memory": 0,
54+
"mem_length": 2})
4955

5056

5157
class TestInversePunisher(TestPlayer):
@@ -64,34 +70,36 @@ class TestInversePunisher(TestPlayer):
6470

6571
def test_init(self):
6672
"""Tests for the __init__ method."""
67-
P1 = axelrod.InversePunisher()
68-
self.assertEqual(P1.history, [])
69-
self.assertEqual(P1.mem_length, 1)
70-
self.assertEqual(P1.grudged, False)
71-
self.assertEqual(P1.grudge_memory, 1)
73+
player = axelrod.InversePunisher()
74+
self.assertEqual(player.mem_length, 1)
75+
self.assertFalse(player.grudged)
76+
self.assertEqual(player.grudge_memory, 1)
7277

7378
def test_strategy(self):
74-
self.responses_test([C], [], [], attrs={"grudged": False})
75-
self.responses_test([C], [C], [C], attrs={"grudged": False})
76-
self.responses_test([D], [C], [D], attrs={"grudged": True})
77-
for i in range(10):
78-
self.responses_test([D], [C, C] + [D] * i, [C, D] + [C] * i,
79-
attrs={"grudged": True, "grudge_memory": i,
80-
"mem_length": 10})
79+
opponent = axelrod.Alternator()
80+
actions = [(C, C), (C, D), (D, C)]
81+
self.versus_test(opponent=opponent, expected_actions=actions,
82+
attrs={"grudged": True, "grudge_memory": 0})
83+
84+
opponent = axelrod.MockPlayer([C, D] + [C] * 10)
85+
actions = [(C, C), (C, D)] + [(D, C)] * 11
86+
self.versus_test(opponent=opponent, expected_actions=actions,
87+
attrs={"grudged": True, "grudge_memory": 10})
88+
8189
# Eventually the grudge is dropped
82-
i = 11
83-
self.responses_test([C], [C, C] + [D] * i, [C, D] + [C] * i,
84-
attrs={"grudged": False, "grudge_memory": 0,
85-
"mem_length": 10})
90+
opponent = axelrod.MockPlayer([C, D] + [C] * 10)
91+
actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, D)]
92+
self.versus_test(opponent=opponent, expected_actions=actions,
93+
attrs={"grudged": False, "grudge_memory": 0,
94+
"mem_length": 10})
95+
8696
# Grudged again on opponent's D
87-
self.responses_test([D], [C, C] + [D] * i + [C], [C, D] + [C] * i + [D],
88-
attrs={"grudged": True, "grudge_memory": 0,
89-
"mem_length": 17})
97+
opponent = axelrod.MockPlayer([C, D] + [C] * 11)
98+
actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, C), (C, D), (D, C)]
99+
self.versus_test(opponent=opponent, expected_actions=actions,
100+
attrs={"grudged": True, "grudge_memory": 0,
101+
"mem_length": 17})
90102

91-
# Test a different grudge length period
92-
self.responses_test([D], [C] * 5, [C] * 4 + [D],
93-
attrs={"grudged": True, "grudge_memory": 0,
94-
"mem_length": 16})
95103

96104
class TestLevelPunisher(TestPlayer):
97105

@@ -111,11 +119,18 @@ def test_strategy(self):
111119
# Starts by Cooperating
112120
self.first_play_test(C)
113121

114-
# Defects if the turns played are less than 10.
115-
self.responses_test([C], [C], [C])
116-
self.responses_test([C], [C] * 4, [C, D, C, D])
117-
118-
# Check for the number of rounds greater than 10.
119-
self.responses_test([C], [C] * 10, [C, C, C, C, D, C, C, C, C, D])
120-
#Check if number of defections by opponent is greater than 20%
121-
self.responses_test([D], [C] * 10, [D, D, D, D, D, C, D, D, D, D])
122+
# Cooperates if the turns played are less than 10.
123+
actions = [(C, C)] * 9
124+
self.versus_test(opponent=axelrod.Cooperator(),
125+
expected_actions=actions)
126+
127+
# After 10 rounds
128+
# Check if number of defections by opponent is greater than 20%
129+
opponent = axelrod.MockPlayer([C] * 4 + [D] * 2 + [C] * 3 + [D])
130+
actions = [(C, C)] * 4 + [(C, D)] * 2 + [(C, C)] * 3 + [(C, D), (D, C)]
131+
self.versus_test(opponent=opponent, expected_actions=actions)
132+
133+
# Check if number of defections by opponent is less than 20%
134+
opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 4 + [D])
135+
actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 4 + [(C, D), (C, C)]
136+
self.versus_test(opponent=opponent, expected_actions=actions)

0 commit comments

Comments
 (0)