From 0b3d70e7c39bd6678aa2737aad377d34e8d78753 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Wed, 13 Nov 2019 01:07:23 -0500 Subject: [PATCH] implemented setGoalieNumber --- src/Mtlstats/Actions/EditGoalie.hs | 13 +++- test/Actions/EditGoalieSpec.hs | 109 +++++++++++++++++++++++++++++ test/ActionsSpec.hs | 2 + 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 test/Actions/EditGoalieSpec.hs diff --git a/src/Mtlstats/Actions/EditGoalie.hs b/src/Mtlstats/Actions/EditGoalie.hs index ebc46a1..fdc599e 100644 --- a/src/Mtlstats/Actions/EditGoalie.hs +++ b/src/Mtlstats/Actions/EditGoalie.hs @@ -23,7 +23,12 @@ module Mtlstats.Actions.EditGoalie ( setGoalieNumber ) where +import Control.Monad (void) +import Data.Maybe (fromMaybe) +import Lens.Micro ((^.), (&), (.~), (%~)) + import Mtlstats.Types +import Mtlstats.Util -- | Sets a goalie's number setGoalieNumber @@ -31,4 +36,10 @@ setGoalieNumber -- ^ New goalie number -> ProgState -> ProgState -setGoalieNumber = undefined +setGoalieNumber n s = fromMaybe s $ do + gid <- s^.progMode.editGoalieStateL.egsSelectedGoalie + void $ nth gid $ s^.database.dbGoalies + let updateGoalie = gNumber .~ n + Just $ s + & database.dbGoalies %~ modifyNth gid updateGoalie + & progMode.editGoalieStateL.egsMode .~ EGMenu diff --git a/test/Actions/EditGoalieSpec.hs b/test/Actions/EditGoalieSpec.hs new file mode 100644 index 0000000..5dcb9ac --- /dev/null +++ b/test/Actions/EditGoalieSpec.hs @@ -0,0 +1,109 @@ +{- + +mtlstats +Copyright (C) 2019 Rhéal Lamothe + + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or (at +your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +-} + +module Actions.EditGoalieSpec (spec) where + +import Data.Maybe (fromJust) +import Lens.Micro ((^.), (&), (.~)) +import Test.Hspec (Spec, context, describe, it, shouldBe) + +import Mtlstats.Actions.EditGoalie +import Mtlstats.Types +import Mtlstats.Util + +spec :: Spec +spec = describe "EditGoalie" + setGoalieNumberSpec + +setGoalieNumberSpec :: Spec +setGoalieNumberSpec = describe "setGoalieNumber" $ do + let + joe = newGoalie 2 "Joe" + bob = newGoalie 3 "Bob" + db = newDatabase & dbGoalies .~ [joe, bob] + + progState m = newProgState + & progMode .~ m + & database .~ db + & setGoalieNumber 5 + + mapM_ + (\(setLabel, setGid, mode, joeData, bobData) -> context setLabel $ do + let + egs = newEditGoalieState + & egsSelectedGoalie .~ setGid + & egsMode .~ EGNumber + + pm = EditGoalie egs + ps = progState pm + + mapM_ + (\(chkLabel, chkGid, (number, name)) -> context chkLabel $ let + g = fromJust $ nth chkGid $ ps^.database.dbGoalies + + in context "check goalie" $ let + expected = newGoalie number name + in it ("should be " ++ show expected) $ + g `shouldBe` expected) + + -- label, goalie ID, data + [ ( "check Joe", 0, joeData ) + , ( "check Bob", 1, bobData ) + ] + + context "check mode" $ + it ("should be " ++ show mode) $ + ps^.progMode.editGoalieStateL.egsMode `shouldBe` mode) + + [ ( "set Joe" + , Just 0 + , EGMenu + , (5, "Joe") + , (3, "Bob") + ) + , ( "set Bob" + , Just 1 + , EGMenu + , (2, "Joe") + , (5, "Bob") + ) + , ( "out of bounds" + , Just 2 + , EGNumber + , (2, "Joe") + , (3, "Bob") + ) + , ( "no goalie selected" + , Nothing + , EGNumber + , (2, "Joe") + , (3, "Bob") + ) + ] + + context "wrong progMode" $ do + let ps = progState MainMenu + + it "should not change the database" $ + ps^.database `shouldBe` db + + it "should not change the progMode" $ + show (ps^.progMode) `shouldBe` "MainMenu" diff --git a/test/ActionsSpec.hs b/test/ActionsSpec.hs index b6883c9..19a16f3 100644 --- a/test/ActionsSpec.hs +++ b/test/ActionsSpec.hs @@ -38,6 +38,7 @@ import Test.Hspec import Mtlstats.Actions import Mtlstats.Types +import qualified Actions.EditGoalieSpec as EditGoalie import qualified Actions.NewGameSpec as NewGame import qualified TypesSpec as TS @@ -60,6 +61,7 @@ spec = describe "Mtlstats.Actions" $ do scrollUpSpec scrollDownSpec NewGame.spec + EditGoalie.spec startNewSeasonSpec :: Spec startNewSeasonSpec = describe "startNewSeason" $ do