Skip to content

Commit 09a02f5

Browse files
committed
Add more tests to improve coverage.
1 parent 698bf64 commit 09a02f5

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

axelrod/classifier.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,12 @@ def classify_player_for_this_classifier(
194194
if key in player.classifier:
195195
return player.classifier[key]
196196

197-
if player.name not in cls.all_player_dicts:
197+
try:
198+
player_classifiers = cls.all_player_dicts[player.name]
199+
except:
198200
return None
199-
player_classifiers = cls.all_player_dicts[player.name]
200201

201-
if key not in player_classifiers:
202-
return None
203-
return player_classifiers[key]
202+
return player_classifiers.get(key, None)
204203

205204
return classify_player_for_this_classifier
206205

axelrod/tests/unit/test_classification.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@
77
import yaml
88

99
import axelrod as axl
10-
from axelrod.classifier import Classifier, Classifiers, rebuild_classifier_table
10+
from axelrod.classifier import (
11+
Classifier,
12+
Classifiers,
13+
_Classifiers,
14+
memory_depth,
15+
rebuild_classifier_table,
16+
)
17+
from axelrod.player import Player
18+
19+
20+
class TestTitForTat(Player):
21+
"""
22+
Same name as TitForTat, but with empty classifier.
23+
"""
24+
25+
# Classifiers are looked up by name, so only the name matters.
26+
name = "Tit For Tat"
27+
classifier = {}
1128

1229

1330
class TestClassification(unittest.TestCase):
@@ -33,11 +50,23 @@ def test_classifier_build(self):
3350
)
3451

3552
def test_singletonity_of_classifiers_class(self):
36-
classifiers_1 = Classifiers
37-
classifiers_2 = Classifiers
53+
classifiers_1 = _Classifiers()
54+
classifiers_2 = _Classifiers()
3855

3956
self.assertIs(classifiers_1, classifiers_2)
4057

58+
def test_get_name_from_classifier(self):
59+
# Should be able to take a string or a Classifier instance.
60+
self.assertEqual(Classifiers["memory_depth"](axl.TitForTat), 1)
61+
self.assertEqual(Classifiers[memory_depth](axl.TitForTat), 1)
62+
63+
def test_key_error_on_uknown_classifier(self):
64+
with self.assertRaises(KeyError):
65+
Classifiers["invalid_key"](axl.TitForTat)
66+
67+
def test_will_lookup_key_in_dict(self):
68+
self.assertEqual(Classifiers["memory_depth"](TestTitForTat), 1)
69+
4170
def test_known_classifiers(self):
4271
# A set of dimensions that are known to have been fully applied
4372
known_keys = [

0 commit comments

Comments
 (0)