Skip to content

Commit ab84b83

Browse files
author
gaffneytj
committed
Add makes_use_of function
1 parent 95c221d commit ab84b83

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

axelrod/makes_use_of.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import inspect
2+
import re
3+
from typing import Set, Text, Type
4+
5+
from axelrod.player import Player
6+
7+
8+
def makes_use_of(player: Type[Player]) -> Set[Text]:
9+
result = set()
10+
for method in inspect.getmembers(player, inspect.ismethod):
11+
if method[0] == "__init__":
12+
continue
13+
method_code = inspect.getsource(method[1])
14+
attr_string = r"self.match_attributes\[\"(\w+)\"\]"
15+
all_attrs = re.findall(attr_string, method_code)
16+
for attr in all_attrs:
17+
result.add(attr)
18+
return result
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Tests for makes_use_of."""
2+
3+
import unittest
4+
5+
import axelrod as axl
6+
from axelrod.makes_use_of import makes_use_of
7+
8+
9+
class TestMakesUseOfLengthAndGamePlayer(axl.Player):
10+
"""
11+
Should have some function that uses length
12+
"""
13+
14+
def first_function(self): # pragma: no cover
15+
x = 1 + 2
16+
x * 5
17+
18+
def second_function(self): # pragma: no cover
19+
# We put this in the second function to make sure both are checked.
20+
x = 1 + self.match_attributes["length"]
21+
22+
# Should only add once.
23+
y = 2 + self.match_attributes["length"]
24+
25+
# Should also add game.
26+
self.match_attributes["game"]
27+
28+
29+
class TestMakesUseOfNothingPlayer(axl.Player):
30+
"""
31+
Doesn't use match_attributes
32+
"""
33+
34+
def only_function(self): # pragma: no cover
35+
1 + 2 + 3
36+
print("=6")
37+
38+
39+
class TestMakesUseOf(unittest.TestCase):
40+
def test_makes_use_of_length_and_game(self):
41+
self.assertEqual(
42+
makes_use_of(TestMakesUseOfLengthAndGamePlayer()),
43+
{"length", "game"},
44+
)
45+
46+
def test_makes_use_of_empty(self):
47+
self.assertEqual(makes_use_of(TestMakesUseOfNothingPlayer()), set())

0 commit comments

Comments
 (0)