Skip to content

Commit c5e95d6

Browse files
committed
Ensure types of generator/cycle stay the same.
1 parent 308741e commit c5e95d6

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

axelrod/player.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,17 @@ def __eq__(self, other):
149149
generator, original_value = itertools.tee(value)
150150
other_generator, original_other_value = itertools.tee(other_value)
151151

152-
setattr(self, attribute,
153-
(ele for ele in original_value))
154-
setattr(other, attribute,
155-
(ele for ele in original_other_value))
152+
153+
if isinstance(value, types.GeneratorType):
154+
setattr(self, attribute,
155+
(ele for ele in original_value))
156+
setattr(other, attribute,
157+
(ele for ele in original_other_value))
158+
else:
159+
setattr(self, attribute,
160+
itertools.cycle(original_value))
161+
setattr(other, attribute,
162+
itertools.cycle(original_other_value))
156163

157164
if not (all(next(generator) == next(other_generator)
158165
for _ in range(200))):

axelrod/tests/strategies/test_player.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ def test_equality_for_generator(self):
207207
self.assertEqual(list(p1.generator), list(range(10)))
208208
self.assertEqual(list(p2.generator), list(range(1, 10)))
209209

210+
# Check that type is generator
211+
self.assertIsInstance(p2.generator, types.GeneratorType)
212+
210213
def test_equality_for_cycle(self):
211214
"""Test equality works with cycle attribute and that the cycle attribute
212215
is not altered during checking of equality"""
@@ -231,11 +234,15 @@ def test_equality_for_cycle(self):
231234
self.assertEqual(next(p1.cycle), 0)
232235
self.assertEqual(next(p2.cycle), 1)
233236

237+
# Check that type is cycle
238+
self.assertIsInstance(p2.cycle, itertools.cycle)
239+
234240
def test_equaity_on_init(self):
235241
"""Test all instances of a strategy are equal on init"""
236242
for s in axelrod.strategies:
237243
p1, p2 = s(), s()
238-
# Check twice (so testing equality doesn't change anything)
244+
# Check three times (so testing equality doesn't change anything)
245+
self.assertEqual(p1, p2)
239246
self.assertEqual(p1, p2)
240247
self.assertEqual(p1, p2)
241248

0 commit comments

Comments
 (0)