22# vim: set ft=python ts=4 sw=4 expandtab:
33# pylint: disable=no-self-use,protected-access,too-many-locals,too-many-statements
44
5- from unittest .mock import MagicMock , call
5+ from unittest .mock import MagicMock , call , patch
66
77import pytest
88
99from apologies .game import ADULT_HAND , DECK_SIZE , PAWNS , Card , CardType , Game , GameMode , Pawn , PlayerColor , Position
1010from apologies .rules import Action , ActionType , BoardRules , Move , Rules
1111
12+ _UUID = MagicMock (return_value = MagicMock (hex = "uuid" )) # any call to get a random UUID returns a UUID with hex value "uuid"
13+
1214
1315class TestAction :
1416 def test_constructor (self ):
@@ -21,12 +23,22 @@ def test_constructor(self):
2123
2224
2325class TestMove :
24- def test_constructor (self ):
26+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
27+ def test_constructor_uuid (self ):
2528 card = Card (3 , CardType .CARD_12 )
2629 actions = [Action (ActionType .MOVE_TO_START , pawn = Pawn (PlayerColor .BLUE , 1 , "whatever" ))]
2730 move = Move (card , actions )
2831 assert move .card is card
2932 assert move .actions == actions
33+ assert move .id == "uuid"
34+
35+ def test_constructor_explicit (self ):
36+ card = Card (3 , CardType .CARD_12 )
37+ actions = [Action (ActionType .MOVE_TO_START , pawn = Pawn (PlayerColor .BLUE , 1 , "whatever" ))]
38+ move = Move (card , actions , id = "whatever" )
39+ assert move .card is card
40+ assert move .actions == actions
41+ assert move .id == "whatever"
3042
3143
3244class TestRules :
@@ -84,6 +96,7 @@ def test_start_game_adult(self):
8496 assert game .players [PlayerColor .BLUE ].pawns [0 ].position .square == 19
8597 assert len (game .players [PlayerColor .BLUE ].hand ) == ADULT_HAND
8698
99+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
87100 def test_construct_legal_moves_no_moves_with_card (self ):
88101 card = MagicMock ()
89102
@@ -114,6 +127,7 @@ def test_construct_legal_moves_no_moves_with_card(self):
114127 [call (PlayerColor .RED , card , pawn1 , all_pawns ), call (PlayerColor .RED , card , pawn2 , all_pawns )]
115128 )
116129
130+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
117131 def test_construct_legal_moves_no_moves_no_card (self ):
118132 card = None
119133
@@ -151,6 +165,7 @@ def test_construct_legal_moves_no_moves_no_card(self):
151165 ]
152166 )
153167
168+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
154169 def test_construct_legal_moves_with_moves_with_card (self ):
155170 card = MagicMock ()
156171
@@ -187,6 +202,7 @@ def test_construct_legal_moves_with_moves_with_card(self):
187202 [call (PlayerColor .RED , card , pawn1 , all_pawns ), call (PlayerColor .RED , card , pawn2 , all_pawns )]
188203 )
189204
205+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
190206 def test_construct_legal_moves_with_moves_no_card (self ):
191207 card = None
192208
@@ -498,6 +514,7 @@ def _legal_moves(color, game, index, cardtype):
498514
499515
500516class TestLegalMoves :
517+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
501518 def test_construct_legal_moves_card_1 (self ):
502519 # No legal moves if no pawn in start, on the board, or in safe
503520 game = _setup_game ()
@@ -544,6 +561,7 @@ def test_construct_legal_moves_card_1(self):
544561 card , pawn , view , moves = _legal_moves (RED , game , 0 , "1" )
545562 assert moves == [Move (card , actions = [_square (pawn , 7 )], side_effects = [_bump (view , GREEN , 1 )])]
546563
564+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
547565 def test_construct_legal_moves_card_2 (self ):
548566 # No legal moves if no pawn in start, on the board, or in safe
549567 game = _setup_game ()
@@ -590,6 +608,7 @@ def test_construct_legal_moves_card_2(self):
590608 card , pawn , view , moves = _legal_moves (RED , game , 0 , "2" )
591609 assert moves == [Move (card , actions = [_square (pawn , 8 )], side_effects = [_bump (view , GREEN , 1 )])]
592610
611+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
593612 def test_construct_legal_moves_card_3 (self ):
594613 # No legal moves if no pawn on the board, or in safe
595614 game = _setup_game ()
@@ -617,6 +636,7 @@ def test_construct_legal_moves_card_3(self):
617636 card , pawn , view , moves = _legal_moves (RED , game , 0 , "3" )
618637 assert moves == [Move (card , actions = [_square (pawn , 9 )], side_effects = [_bump (view , GREEN , 1 )])]
619638
639+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
620640 def test_construct_legal_moves_card_4 (self ):
621641 # No legal moves if no pawn on the board, or in safe
622642 game = _setup_game ()
@@ -644,6 +664,7 @@ def test_construct_legal_moves_card_4(self):
644664 card , pawn , view , moves = _legal_moves (RED , game , 0 , "4" )
645665 assert moves == [Move (card , actions = [_square (pawn , 2 )], side_effects = [_bump (view , GREEN , 1 )])]
646666
667+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
647668 def test_construct_legal_moves_card_5 (self ):
648669 # No legal moves if no pawn on the board, or in safe
649670 game = _setup_game ()
@@ -671,6 +692,7 @@ def test_construct_legal_moves_card_5(self):
671692 card , pawn , view , moves = _legal_moves (RED , game , 0 , "5" )
672693 assert moves == [Move (card , actions = [_square (pawn , 11 )], side_effects = [_bump (view , GREEN , 1 )])]
673694
695+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
674696 def test_construct_legal_moves_card_7 (self ):
675697 # No legal moves if no pawn on the board, or in safe
676698 game = _setup_game ()
@@ -742,6 +764,7 @@ def test_construct_legal_moves_card_7(self):
742764 Move (card , actions = [_square (pawn , 12 ), _square (other2 , 56 )], side_effects = []), # split (6, 1)
743765 ]
744766
767+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
745768 def test_construct_legal_moves_card_8 (self ):
746769 # No legal moves if no pawn on the board, or in safe
747770 game = _setup_game ()
@@ -769,6 +792,7 @@ def test_construct_legal_moves_card_8(self):
769792 card , pawn , view , moves = _legal_moves (RED , game , 0 , "8" )
770793 assert moves == [Move (card , actions = [_square (pawn , 14 )], side_effects = [_bump (view , GREEN , 1 )])]
771794
795+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
772796 def test_construct_legal_moves_card_10 (self ):
773797 # No legal moves if no pawn on the board, or in safe
774798 game = _setup_game ()
@@ -821,6 +845,7 @@ def test_construct_legal_moves_card_10(self):
821845 Move (card , actions = [_square (pawn , 4 )], side_effects = [_bump (view , GREEN , 1 )]),
822846 ]
823847
848+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
824849 def test_construct_legal_moves_card_11 (self ):
825850 # No legal moves if no pawn on the board, or in safe
826851 game = _setup_game ()
@@ -866,6 +891,7 @@ def test_construct_legal_moves_card_11(self):
866891 Move (card , actions = [_square (pawn , 26 )], side_effects = []),
867892 ]
868893
894+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
869895 def test_construct_legal_moves_card_12 (self ):
870896 # No legal moves if no pawn on the board, or in safe
871897 game = _setup_game ()
@@ -893,6 +919,7 @@ def test_construct_legal_moves_card_12(self):
893919 card , pawn , view , moves = _legal_moves (RED , game , 0 , "12" )
894920 assert moves == [Move (card , actions = [_square (pawn , 18 )], side_effects = [_bump (view , GREEN , 1 )])]
895921
922+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
896923 def test_construct_legal_moves_card_apologies (self ):
897924 # No legal moves if no pawn in start
898925 game = _setup_game ()
@@ -914,6 +941,7 @@ def test_construct_legal_moves_card_apologies(self):
914941 Move (card , actions = [_square (pawn , 19 ), _bump (view , BLUE , 1 )], side_effects = []),
915942 ]
916943
944+ @patch ("apologies.rules.uuid.uuid4" , new = _UUID )
917945 def test_construct_legal_moves_special (self ):
918946 # Move pawn into safe zone
919947 game = _setup_game ()
0 commit comments