Skip to content

Commit 398052a

Browse files
committed
Add tests to improve coverage
1 parent f8821fc commit 398052a

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

axelrod/strategies/meta.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,9 @@ class MetaMixer(MetaPlayer):
544544

545545
def __init__(self, team=None, distribution=None):
546546
super().__init__(team=team)
547+
# Check that distribution is not all zeros, which will make numpy unhappy.
548+
if distribution and all(x == 0 for x in distribution):
549+
distribution = None
547550
self.distribution = distribution
548551

549552
def _post_init(self):

axelrod/tests/strategies/test_meta.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ def test_clone(self, seed):
8888
self.assertEqual(len(player1.history), turns)
8989
self.assertEqual(player1.history, player2.history)
9090

91+
def test_update_histories(self):
92+
"""Artificial test to ensure that an exception is thrown."""
93+
p = axl.MetaHunter()
94+
with self.assertRaises(TypeError):
95+
p.update_histories(C)
96+
9197

9298
class TestMetaMajority(TestMetaPlayer):
9399
name = "Meta Majority"
@@ -150,6 +156,15 @@ class TestMetaWinnerEnsemble(TestMetaPlayer):
150156
name = "Meta Winner Ensemble"
151157
player = axl.MetaWinnerEnsemble
152158

159+
def test_singularity(self):
160+
"""Test meta_strategy when the player is singular."""
161+
team = [axl.Cooperator]
162+
player = axl.MetaWinnerEnsemble(team=team)
163+
self.assertFalse(Classifiers["stochastic"](player))
164+
coplayer = axl.Defector()
165+
match = axl.Match((player, coplayer), turns=10)
166+
match.play()
167+
153168
def test_stochasticity(self):
154169
# One player teams may be stochastic or not
155170
team = [axl.Cooperator]
@@ -548,6 +563,28 @@ def test_stochasticity(self):
548563
self.assertTrue(Classifiers["stochastic"](player))
549564

550565
def test_strategy(self):
566+
# Distribution = None
567+
team = [axl.TitForTat, axl.Cooperator, axl.Grudger]
568+
distribution = None
569+
570+
actions = [(C, C)] * 20
571+
self.versus_test(
572+
opponent=axl.Cooperator(),
573+
expected_actions=actions,
574+
init_kwargs={"team": team, "distribution": distribution},
575+
)
576+
577+
# Distribution = [0, 0, 0]
578+
team = [axl.TitForTat, axl.Cooperator, axl.Grudger]
579+
distribution = [0, 0, 0]
580+
581+
actions = [(C, C)] * 20
582+
self.versus_test(
583+
opponent=axl.Cooperator(),
584+
expected_actions=actions,
585+
init_kwargs={"team": team, "distribution": distribution},
586+
)
587+
551588
team = [axl.TitForTat, axl.Cooperator, axl.Grudger]
552589
distribution = [0.2, 0.5, 0.3]
553590

axelrod/tests/strategies/test_player.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pickle
33
import types
44
import unittest
5+
import warnings
56

67
import axelrod as axl
78
import numpy as np
@@ -56,6 +57,12 @@ class TestPlayerClass(unittest.TestCase):
5657
player = axl.Player
5758
classifier = {"stochastic": False}
5859

60+
def test_seed_warning(self):
61+
"""Test that the user is warned if a null seed is given."""
62+
player = self.Player()
63+
with warnings.catch_warnings():
64+
player._set_seed(seed=None)
65+
5966
def test_play(self):
6067
player1, player2 = self.player(), self.player()
6168
player1.strategy = cooperate

axelrod/tests/strategies/test_punisher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_strategy(self):
155155
class TestTrickyLevelPunisher(TestPlayer):
156156

157157
name = "Level Punisher"
158-
player = axl.LevelPunisher
158+
player = axl.TrickyLevelPunisher
159159
expected_classifier = {
160160
"memory_depth": float("inf"), # Long memory
161161
"stochastic": False,
@@ -192,3 +192,4 @@ def test_strategy(self):
192192
opponent = axl.MockPlayer([C] * 10)
193193
actions = [(C, C)] * 5
194194
self.versus_test(opponent=opponent, expected_actions=actions)
195+

0 commit comments

Comments
 (0)