Skip to content

Commit

Permalink
test fixes. branch switch
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-s-s authored and drvinceknight committed Aug 3, 2017
1 parent c3421fb commit 4fb8685
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
2 changes: 1 addition & 1 deletion axelrod/strategy_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def strategy(self, opponent):
proposed_action = PlayerClass.strategy(self, opponent)

if strategy_wrapper == dual_wrapper:
# after dual_wrapper_figures calls the strategy, it returns
# after dual_wrapper calls the strategy, it returns
# the Player to its original state.
flip_play_attributes(self)

Expand Down
52 changes: 27 additions & 25 deletions axelrod/tests/unit/test_pickling.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ def strategy(self, opponent):
)


@st.IdentityTransformer()
@st.DualTransformer(name_prefix=None)
@st.FlipTransformer(name_prefix=None)
@st.DualTransformer(name_prefix=None)
class TestDualTransformerIssues(axl.Cooperator):
pass


@st.IdentityTransformer()
@st.DualTransformer()
@st.FlipTransformer()
@st.DualTransformer()
class TestDualTransformerIssues(axl.Cooperator):
class TestDualTransformerIssues2(axl.Cooperator):
pass


@st.FlipTransformer()
class MyCooperator(axl.Player):
def strategy(self, opponent):
Expand Down Expand Up @@ -169,6 +179,7 @@ def assert_orignal_equals_pickled(self, player, turns=10):
self.assert_mutated_instance_same_as_pickled(player)

def assert_original_plays_same_as_pickled(self, player, turns=10):
player.reset()
copy = pickle.loads(pickle.dumps(player))
opponent_1 = axl.CyclerCCCDCD()
opponent_2 = axl.CyclerCCCDCD()
Expand Down Expand Up @@ -230,28 +241,27 @@ def test_created_on_the_spot_multiple_transformers(self):
klass = st.FlipTransformer()(axl.Cooperator)
klass = st.DualTransformer()(klass)
player = st.FinalTransformer((C, D))(klass)()
copy = pickle.loads(pickle.dumps(player))

self.assertEqual(player, copy)
self.assert_mutated_instance_same_as_pickled(player)
self.assert_orignal_equals_pickled(player)

def test_dual_transformer_special_case(self):
player = TestDualTransformerIssues()
self.assert_orignal_equals_pickled(player)

player = TestDualTransformerIssues2()
self.assert_orignal_equals_pickled(player)

def test_class_and_instance_name_different_single_flip(self):
player = SingleFlip()
self.assertEqual(player.__class__.__name__, 'FlippedSingleFlip')

self.assert_mutated_instance_same_as_pickled(player)
self.assert_original_plays_same_as_pickled(player, turns=10)
self.assert_orignal_equals_pickled(player)

def test_class_and_instance_name_different_double_flip(self):
player = DoubleFlip()
self.assertEqual(player.__class__.__name__, 'FlippedFlippedDoubleFlip')

self.assert_mutated_instance_same_as_pickled(player)
self.assert_original_plays_same_as_pickled(player, turns=10)
self.assert_orignal_equals_pickled(player)

def test_class_and_instance_name_different_built_from_player_class(self):
player = MyCooperator()
Expand All @@ -261,8 +271,7 @@ def test_class_and_instance_name_different_built_from_player_class(self):
['FlippedMyCooperator', 'MyCooperator', 'Player', 'object']
)

self.assert_original_plays_same_as_pickled(player, turns=10)
self.assert_mutated_instance_same_as_pickled(player)
self.assert_orignal_equals_pickled(player)

def test_pointer_to_class_derived_from_strategy(self):
player = PointerToWrappedStrategy()
Expand All @@ -274,8 +283,7 @@ def test_pointer_to_class_derived_from_strategy(self):
'Player', 'object']
)

self.assert_original_plays_same_as_pickled(player, turns=10)
self.assert_mutated_instance_same_as_pickled(player)
self.assert_orignal_equals_pickled(player)

def test_pointer_to_class_derived_from_Player(self):
player = PointerToWrappedClassNotInStrategies()
Expand All @@ -287,23 +295,17 @@ def test_pointer_to_class_derived_from_Player(self):
'Player', 'object']
)

self.assert_original_plays_same_as_pickled(player, turns=10)
self.assert_mutated_instance_same_as_pickled(player)
self.assert_orignal_equals_pickled(player)

def test_local_class_unpicklable(self):
"""An unpickle-able AND transformed class will not raise an error until
it is un-pickled. This is different from the original class that raises
an error when it is pickled."""
class LocalCooperator(axl.Player):
def __init__(self):
super(LocalCooperator, self).__init__()

def strategy(self, opponent):
return C
class LocalCooperator(axl.Cooperator):
pass

un_transformed = LocalCooperator()
# for coverage
self.assertEqual(un_transformed.strategy(axl.Cooperator()), C)

self.assertRaises(AttributeError, pickle.dumps, un_transformed)

Expand All @@ -314,22 +316,22 @@ def strategy(self, opponent):
def test_with_various_name_prefixes(self):
no_prefix = Flip()
self.assertEqual(no_prefix.__class__.__name__, 'Flip')
self.assert_mutated_instance_same_as_pickled(no_prefix)
self.assert_orignal_equals_pickled(no_prefix)

default_prefix = st.FlipTransformer()(axl.Cooperator)()
self.assertEqual(default_prefix.__class__.__name__,
'FlippedCooperator')
self.assert_mutated_instance_same_as_pickled(default_prefix)
self.assert_orignal_equals_pickled(default_prefix)

fliptastic = st.FlipTransformer(name_prefix='Fliptastic')
new_prefix = fliptastic(axl.Cooperator)()
self.assertEqual(new_prefix.__class__.__name__,
'FliptasticCooperator')
self.assert_mutated_instance_same_as_pickled(new_prefix)
self.assert_orignal_equals_pickled(new_prefix)

def test_dynamic_class_no_name_prefix(self):
player = st.FlipTransformer(name_prefix=None)(axl.Cooperator)()

self.assertEqual(player.__class__.__name__, 'Cooperator')
self.assert_mutated_instance_same_as_pickled(player)
self.assert_orignal_equals_pickled(player)

0 comments on commit 4fb8685

Please sign in to comment.