Skip to content

Commit b43c81c

Browse files
authored
Merge pull request #1024 from Axelrod-Python/884-oncebitten
Refactor tests for oncebitten
2 parents 1d3d81f + 970ac13 commit b43c81c

File tree

1 file changed

+54
-29
lines changed

1 file changed

+54
-29
lines changed

axelrod/tests/strategies/test_oncebitten.py

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,23 @@ def test_strategy(self):
2626
"""If opponent defects at any point then the player will defect
2727
forever."""
2828
# Become grudged if the opponent defects twice in a row
29-
self.responses_test([C], attrs={"grudged": False})
30-
self.responses_test([C], [C], [C], attrs={"grudged": False})
31-
self.responses_test([C], [C, C], [C, C], attrs={"grudged": False})
32-
self.responses_test([C], [C, C, C], [C, C, D], attrs={"grudged": False})
33-
self.responses_test([D], [C, C, C, C], [C, C, D, D],
34-
attrs={"grudged": True})
35-
36-
mem_length = self.player().mem_length
37-
for i in range(mem_length - 1):
38-
self.responses_test([D], [C, C, C, C] + [D] * i,
39-
[C, C, D, D] + [D] * i,
40-
attrs={"grudged": True, "grudge_memory": i})
41-
i = mem_length + 1
42-
self.responses_test([C], [C, C, C, C] + [D] * i, [C, C, D, D] + [C] * i,
43-
attrs={"grudged": False, "grudge_memory": 0})
29+
opponent = axelrod.MockPlayer([C, C, C, D])
30+
actions = [(C, C), (C, C), (C, C), (C, D), (C, C)]
31+
self.versus_test(opponent=opponent, expected_actions=actions,
32+
attrs={"grudged": False, "grudge_memory": 0})
33+
34+
opponent = axelrod.MockPlayer([C, C, C, D, D, D])
35+
actions = [(C, C), (C, C), (C, C), (C, D), (C, D), (D, D),
36+
(D, C), (D, C), (D, C), (D, D), (D, D)]
37+
self.versus_test(opponent=opponent, expected_actions=actions,
38+
attrs={"grudged": True, "grudge_memory": 5})
39+
40+
# After 10 rounds of being grudged: forgives
41+
opponent = axelrod.MockPlayer([C, D, D, C] + [C] * 10)
42+
actions = [(C, C), (C, D), (C, D), (D, C)] + [(D, C)] * 10 + [(C, C)]
43+
self.versus_test(opponent=opponent, expected_actions=actions,
44+
attrs={"grudged": False, "grudge_memory": 0})
45+
4446

4547
def test_reset(self):
4648
"""Check that grudged gets reset properly"""
@@ -71,10 +73,20 @@ class TestFoolMeOnce(TestPlayer):
7173
def test_strategy(self):
7274
self.first_play_test(C)
7375
# If opponent defects more than once, defect forever
74-
self.responses_test([C], [C], [D])
75-
self.responses_test([D], [C, C], [D, D])
76-
self.responses_test([C], [C, C], [D, C])
77-
self.responses_test([D], [C, C, C], [D, D, D])
76+
actions = [(C, C)] * 10
77+
self.versus_test(opponent=axelrod.Cooperator(),
78+
expected_actions=actions)
79+
80+
opponent = axelrod.MockPlayer([D] + [C] * 9)
81+
actions = [(C, D)] + [(C, C)] * 9
82+
self.versus_test(opponent=opponent, expected_actions=actions)
83+
84+
actions = [(C, D)] * 2 + [(D, D)] * 8
85+
self.versus_test(opponent=axelrod.Defector(), expected_actions=actions)
86+
87+
opponent = axelrod.MockPlayer([D, D] + [C] * 9)
88+
actions = [(C, D)] * 2 + [(D, C)] * 8
89+
self.versus_test(opponent=opponent, expected_actions=actions)
7890

7991

8092
class TestForgetfulFoolMeOnce(TestPlayer):
@@ -95,11 +107,15 @@ def test_strategy(self):
95107
# Test that will forgive one D but will grudge after 2 Ds, randomly
96108
# forgets count.
97109
self.first_play_test(C)
98-
self.responses_test([C], [C], [D], seed=2)
99-
self.responses_test([D], [C, C], [D, D])
110+
111+
actions = [(C, C), (C, D), (C, C), (C, D), (D, C)]
112+
self.versus_test(opponent=axelrod.Alternator(),
113+
expected_actions=actions, seed=2, attrs={"D_count": 2})
114+
100115
# Sometime eventually forget count:
101-
self.responses_test([D] * 18 + [C], [C, C], [D, D], seed=2,
102-
attrs={"D_count": 0})
116+
actions = [(C, D), (C, D)] + [(D, D)] * 18 + [(C, D)]
117+
self.versus_test(opponent=axelrod.Defector(),
118+
expected_actions=actions, seed=2, attrs={"D_count": 0})
103119

104120
def test_reset(self):
105121
"""Check that count gets reset properly"""
@@ -129,9 +145,18 @@ class TestFoolMeForever(TestPlayer):
129145
}
130146

131147
def test_strategy(self):
132-
# If opponent defects more than once, defect forever.
133-
self.responses_test([D])
134-
self.responses_test([C], [D], [D])
135-
self.responses_test([D], [D], [C])
136-
self.responses_test([C], [D, C], [D, C])
137-
self.responses_test([C], [D, C, C], [D, C, C])
148+
# If opponent defects more than once, cooperate forever.
149+
actions = [(D, C)] * 20
150+
self.versus_test(opponent=axelrod.Cooperator(),
151+
expected_actions=actions)
152+
153+
actions = [(D, C), (D, D)] + [(C, C), (C, D)] * 20
154+
self.versus_test(opponent=axelrod.Alternator(),
155+
expected_actions=actions)
156+
157+
opponent = axelrod.MockPlayer([D] + [C] * 19)
158+
actions = [(D, D)] + [(C, C)] * 19
159+
self.versus_test(opponent=opponent, expected_actions=actions)
160+
161+
actions = [(D, D)] + [(C, D)] * 19
162+
self.versus_test(opponent=axelrod.Defector(), expected_actions=actions)

0 commit comments

Comments
 (0)