implemented modifyPlayer

This commit is contained in:
Jonathan Lamothe 2019-09-25 02:28:48 -04:00
parent 625d9c616a
commit 8c8a2d52a6
2 changed files with 43 additions and 1 deletions

View File

@ -126,7 +126,8 @@ module Mtlstats.Types (
-- ** Player Helpers -- ** Player Helpers
pPoints, pPoints,
playerSearch, playerSearch,
playerSearchExact playerSearchExact,
modifyPlayer
) where ) where
import Control.Monad.Trans.State (StateT) import Control.Monad.Trans.State (StateT)
@ -692,3 +693,18 @@ playerSearchExact sStr =
filter (match sStr) . filter (match sStr) .
zip [0..] zip [0..]
where match sStr (_, p) = p^.pName == sStr where match sStr (_, p) = p^.pName == sStr
-- | Modifies a player with a given name
modifyPlayer
:: (Player -> Player)
-- ^ The modification function
-> String
-- ^ The player's name
-> [Player]
-- ^ The list of players to modify
-> [Player]
-- ^ The modified list
modifyPlayer f n = map
(\p -> if p^.pName == n
then f p
else p)

View File

@ -57,6 +57,7 @@ spec = describe "Mtlstats.Types" $ do
pPointsSpec pPointsSpec
playerSearchSpec playerSearchSpec
playerSearchExactSpec playerSearchExactSpec
modifyPlayerSpec
Menu.spec Menu.spec
playerSpec :: Spec playerSpec :: Spec
@ -552,6 +553,31 @@ playerSearchExactSpec = describe "playerSearchExact" $ mapM_
, ( "", Nothing ) , ( "", Nothing )
] ]
modifyPlayerSpec :: Spec
modifyPlayerSpec = describe "modifyPlayer" $ mapM_
(\(pName, j, b, s) -> let
modifier = pLifetime.psGoals .~ 1
players = modifyPlayer modifier pName [joe, bob, steve]
in context ("modify " ++ pName) $ do
context "Joe's lifetime goals" $
it ("should be " ++ show j) $
head players ^. pLifetime.psGoals `shouldBe` j
context "Bob's lifetime goals" $
it ("should be " ++ show b) $
(players !! 1) ^. pLifetime.psGoals `shouldBe` b
context "Steve's lifetime goals" $
it ("should be " ++ show s) $
last players ^. pLifetime.psGoals `shouldBe` s)
-- player name, Joe's goals, Bob's goals, Steve's goals
[ ( "Joe", 1, 0, 0 )
, ( "Bob", 0, 1, 0 )
, ( "Steve", 0, 0, 1 )
, ( "Sam", 0, 0, 0 )
]
joe :: Player joe :: Player
joe = newPlayer 2 "Joe" "center" joe = newPlayer 2 "Joe" "center"