Skip to content

Commit 14e6a6b

Browse files
committed
Fix test for calculator + implemented player eq
This implements a player equality which is needed in this particular case.
1 parent 6e622f2 commit 14e6a6b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

axelrod/player.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from collections import defaultdict
22
from functools import wraps
3+
import types
34
import random
45
import copy
6+
import numpy as np
57

68
from axelrod.actions import Actions, flip_action, Action
79
from .game import DefaultGame
@@ -189,3 +191,21 @@ def reset(self):
189191
self.cooperations = 0
190192
self.defections = 0
191193
self.state_distribution = defaultdict(int)
194+
195+
def __eq__(self, other):
196+
"""
197+
Test if two players are equal.
198+
"""
199+
check = self.__repr__() == other.__repr__()
200+
for attribute, value in self.__dict__.items():
201+
other_value = getattr(other, attribute)
202+
203+
if isinstance(value, np.ndarray):
204+
check = check and np.array_equal(value, other_value)
205+
206+
elif isinstance(value, types.GeneratorType):
207+
check = check and all(next(value) == next(other_value)
208+
for _ in range(10))
209+
else:
210+
check = check and value == other_value
211+
return check

axelrod/tests/unit/test_match_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def test_init_with_clone(self):
3232
self.assertEqual(tt.turns, test_turns)
3333
player = tt.players[0]
3434
opponent = tt.opponents[0]
35-
self.assertEqual(player.name, opponent.name)
36-
self.assertNotEqual(player, opponent)
35+
self.assertEqual(player, opponent)
36+
self.assertIsNot(player, opponent)
3737
# Check that the two player instances are wholly independent
3838
opponent.name = 'Test'
39-
self.assertNotEqual(player.name, opponent.name)
39+
self.assertNotEqual(player, opponent)
4040

4141
def test_len(self):
4242
tt = axelrod.MatchGenerator(

0 commit comments

Comments
 (0)