Skip to content

Commit b85297c

Browse files
committed
Refactor tests for punisher.
Contributes towards #884
1 parent f8231e5 commit b85297c

File tree

2 files changed

+69
-50
lines changed

2 files changed

+69
-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: 67 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,37 @@ 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.history, [])
27+
self.assertEqual(player.mem_length, 1)
28+
self.assertFalse(player.grudged)
29+
self.assertEqual(player.grudge_memory, 1)
3030

3131
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})
32+
33+
opponent = axelrod.Alternator()
34+
actions = [(C, C), (C, D), (D, C)]
35+
self.versus_test(opponent=opponent, expected_actions=actions,
36+
attrs={"grudged": True, "grudge_memory": 0})
37+
38+
opponent = axelrod.MockPlayer([C, D] + [C] * 10)
39+
actions = [(C, C), (C, D)] + [(D, C)] * 11
40+
self.versus_test(opponent=opponent, expected_actions=actions,
41+
attrs={"grudged": True, "grudge_memory": 10})
42+
3943
# 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})
44+
opponent = axelrod.MockPlayer([C, D] + [C] * 10)
45+
actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, D)]
46+
self.versus_test(opponent=opponent, expected_actions=actions,
47+
attrs={"grudged": False, "grudge_memory": 0,
48+
"mem_length": 10})
4449

4550
# 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})
51+
opponent = axelrod.MockPlayer([C, D] + [C] * 11)
52+
actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, C), (C, D), (D, C)]
53+
self.versus_test(opponent=opponent, expected_actions=actions,
54+
attrs={"grudged": True, "grudge_memory": 0,
55+
"mem_length": 2})
4956

5057

5158
class TestInversePunisher(TestPlayer):
@@ -64,34 +71,37 @@ class TestInversePunisher(TestPlayer):
6471

6572
def test_init(self):
6673
"""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)
74+
player = axelrod.InversePunisher()
75+
self.assertEqual(player.history, [])
76+
self.assertEqual(player.mem_length, 1)
77+
self.assertFalse(player.grudged)
78+
self.assertEqual(player.grudge_memory, 1)
7279

7380
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})
81+
opponent = axelrod.Alternator()
82+
actions = [(C, C), (C, D), (D, C)]
83+
self.versus_test(opponent=opponent, expected_actions=actions,
84+
attrs={"grudged": True, "grudge_memory": 0})
85+
86+
opponent = axelrod.MockPlayer([C, D] + [C] * 10)
87+
actions = [(C, C), (C, D)] + [(D, C)] * 11
88+
self.versus_test(opponent=opponent, expected_actions=actions,
89+
attrs={"grudged": True, "grudge_memory": 10})
90+
8191
# 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})
92+
opponent = axelrod.MockPlayer([C, D] + [C] * 10)
93+
actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, D)]
94+
self.versus_test(opponent=opponent, expected_actions=actions,
95+
attrs={"grudged": False, "grudge_memory": 0,
96+
"mem_length": 10})
97+
8698
# 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})
99+
opponent = axelrod.MockPlayer([C, D] + [C] * 11)
100+
actions = [(C, C), (C, D)] + [(D, C)] * 11 + [(C, C), (C, D), (D, C)]
101+
self.versus_test(opponent=opponent, expected_actions=actions,
102+
attrs={"grudged": True, "grudge_memory": 0,
103+
"mem_length": 17})
90104

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})
95105

96106
class TestLevelPunisher(TestPlayer):
97107

@@ -111,11 +121,18 @@ def test_strategy(self):
111121
# Starts by Cooperating
112122
self.first_play_test(C)
113123

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])
124+
# Cooperates if the turns played are less than 10.
125+
actions = [(C, C)] * 9
126+
self.versus_test(opponent=axelrod.Cooperator(),
127+
expected_actions=actions)
128+
129+
# After 10 rounds
130+
# Check if number of defections by opponent is greater than 20%
131+
opponent = axelrod.MockPlayer([C] * 4 + [D] * 2 + [C] * 3 + [D])
132+
actions = [(C, C)] * 4 + [(C, D)] * 2 + [(C, C)] * 3 + [(C, D), (D, C)]
133+
self.versus_test(opponent=opponent, expected_actions=actions)
134+
135+
# Check if number of defections by opponent is less than 20%
136+
opponent = axelrod.MockPlayer([C] * 4 + [D] + [C] * 4 + [D])
137+
actions = [(C, C)] * 4 + [(C, D)] + [(C, C)] * 4 + [(C, D), (C, C)]
138+
self.versus_test(opponent=opponent, expected_actions=actions)

0 commit comments

Comments
 (0)