From 8c8a2d52a69cb838f216cef84cb2accc64dd9846 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Wed, 25 Sep 2019 02:28:48 -0400 Subject: [PATCH] implemented modifyPlayer --- src/Mtlstats/Types.hs | 18 +++++++++++++++++- test/TypesSpec.hs | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Mtlstats/Types.hs b/src/Mtlstats/Types.hs index d31399a..c385b64 100644 --- a/src/Mtlstats/Types.hs +++ b/src/Mtlstats/Types.hs @@ -126,7 +126,8 @@ module Mtlstats.Types ( -- ** Player Helpers pPoints, playerSearch, - playerSearchExact + playerSearchExact, + modifyPlayer ) where import Control.Monad.Trans.State (StateT) @@ -692,3 +693,18 @@ playerSearchExact sStr = filter (match sStr) . zip [0..] 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) diff --git a/test/TypesSpec.hs b/test/TypesSpec.hs index e617f45..13999c5 100644 --- a/test/TypesSpec.hs +++ b/test/TypesSpec.hs @@ -57,6 +57,7 @@ spec = describe "Mtlstats.Types" $ do pPointsSpec playerSearchSpec playerSearchExactSpec + modifyPlayerSpec Menu.spec playerSpec :: Spec @@ -552,6 +553,31 @@ playerSearchExactSpec = describe "playerSearchExact" $ mapM_ , ( "", 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 = newPlayer 2 "Joe" "center"