Skip to content

Commit ef9d93f

Browse files
committed
Make utility function return a boolean.
1 parent ff721b8 commit ef9d93f

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

axelrod/match_generator.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,20 @@ def estimated_size(self):
169169
return size
170170

171171

172-
def test_graph_is_connected(edges, players):
172+
def graph_is_connected(edges, players):
173173
"""
174174
Test if a set of edges defines a complete graph on a set of players.
175175
176-
This is used by the spatial tournaments and will raise a ValueError if the
177-
graph would have some players not playing anyone.
176+
This is used by the spatial tournaments.
178177
179178
Parameters:
180179
-----------
180+
edges : a list of 2 tuples
181+
players : a list of player names
181182
182-
edges : a list of 2 tuples
183-
players : a list of player names
183+
Returns:
184+
--------
185+
boolean : True if the graph is connected
184186
"""
185187
# Check if all players are connected.
186188
player_indices = set(range(len(players)))
@@ -189,8 +191,7 @@ def test_graph_is_connected(edges, players):
189191
for node in edge:
190192
node_indices.add(node)
191193

192-
if player_indices != node_indices:
193-
raise ValueError("The graph edges do not include all players.")
194+
return player_indices == node_indices
194195

195196

196197

@@ -217,7 +218,8 @@ class SpatialMatches(RoundRobinMatches):
217218

218219
def __init__(self, players, turns, game, repetitions, edges):
219220

220-
test_graph_is_connected(edges, players)
221+
if not graph_is_connected(edges, players):
222+
raise ValueError("The graph edges do not include all players.")
221223
self.edges = edges
222224
super(SpatialMatches, self).__init__(players, turns, game, repetitions)
223225

@@ -254,7 +256,8 @@ class ProbEndSpatialMatches(SpatialMatches, ProbEndRoundRobinMatches):
254256

255257
def __init__(self, players, prob_end, game, repetitions, noise, edges):
256258

257-
test_graph_is_connected(edges, players)
259+
if not graph_is_connected(edges, players):
260+
raise ValueError("The graph edges do not include all players.")
258261
self.edges = edges
259262
ProbEndRoundRobinMatches.__init__(self, players, prob_end,
260263
game, repetitions, noise)

axelrod/tests/unit/test_match_generator.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from hypothesis.strategies import floats, integers
66

77
import axelrod
8-
from axelrod.match_generator import test_graph_is_connected
8+
from axelrod.match_generator import graph_is_connected
99

1010

1111
test_strategies = [
@@ -270,6 +270,14 @@ def test_build_match_chunks(self, repetitions, prob_end, noise):
270270

271271
self.assertEqual(match_definitions, expected_match_definitions)
272272

273+
def test_raise_error_if_not_connected(self):
274+
edges = [(0, 0), (0, 1), (1, 1)]
275+
players = ["Cooperator", "Defector", "Alternator"]
276+
noise = 0
277+
self.assertRaises(ValueError, axelrod.ProbEndSpatialMatches,
278+
players, test_turns, test_game, test_repetitions,
279+
noise, edges)
280+
273281
def test_len(self):
274282
edges = [(0, 1), (1, 2), (3, 4)]
275283
noise = 0
@@ -326,9 +334,9 @@ class TestUtilityFunctions(unittest.TestCase):
326334
def test_connected_graph(self):
327335
edges = [(0, 0), (0, 1), (1, 1)]
328336
players = ["Cooperator", "Defector"]
329-
self.assertEqual(test_graph_is_connected(edges, players), None)
337+
self.assertTrue(graph_is_connected(edges, players))
330338

331339
def test_unconnected_graph(self):
332340
edges = [(0, 0), (0, 1), (1, 1)]
333341
players = ["Cooperator", "Defector", "Alternator"]
334-
self.assertRaises(ValueError, test_graph_is_connected, edges, players)
342+
self.assertFalse(graph_is_connected(edges, players))

0 commit comments

Comments
 (0)