{- 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 ActionsSpec (spec) where import Control.Monad (replicateM) import Lens.Micro ((^.), (&), (.~), (?~), (%~)) import System.Random (randomRIO) import Test.Hspec (Spec, context, describe, it, shouldBe, shouldNotBe) import Mtlstats.Actions import Mtlstats.Types spec :: Spec spec = describe "Mtlstats.Actions" $ do startNewSeasonSpec startNewGameSpec resetYtdSpec addCharSpec removeCharSpec overtimeCheckSpec startNewSeasonSpec :: Spec startNewSeasonSpec = describe "startNewSeason" $ do let s = newProgState & database . dbGames .~ 1 & startNewSeason it "should set the progState to NewSeason" $ s ^. progMode `shouldBe` NewSeason it "should set the number of games to 0" $ s ^. database . dbGames `shouldBe` 0 startNewGameSpec :: Spec startNewGameSpec = describe "startNewGame" $ do let s = startNewGame newProgState it "should increment the number of games" $ s ^. database . dbGames `shouldBe` 1 it "should set the mode to NewGame" $ s ^. progMode `shouldBe` NewGame newGameState resetYtdSpec :: Spec resetYtdSpec = describe "resetYtd" $ it "should reset the year-to-date stats for all players" $ do ps <- replicateM 2 makePlayer gs <- replicateM 2 makeGoalie let s = newProgState & database . dbPlayers .~ ps & database . dbGoalies .~ gs & resetYtd mapM_ (\p -> do let ytd = p ^. pYtd lt = p ^. pLifetime ytd ^. psGoals `shouldBe` 0 ytd ^. psAssists `shouldBe` 0 ytd ^. psPMin `shouldBe` 0 lt ^. psGoals `shouldNotBe` 0 lt ^. psAssists `shouldNotBe` 0 lt ^. psPMin `shouldNotBe` 0) $ s ^. database . dbPlayers mapM_ (\g -> do let ytd = g ^. gYtd lt = g ^. gLifetime ytd ^. gsGames `shouldBe` 0 ytd ^. gsMinsPlayed `shouldBe` 0 ytd ^. gsGoalsAllowed `shouldBe` 0 ytd ^. gsGoalsAgainst `shouldBe` 0 ytd ^. gsWins `shouldBe` 0 ytd ^. gsLosses `shouldBe` 0 ytd ^. gsTies `shouldBe` 0 lt ^. gsGames `shouldNotBe` 0 lt ^. gsMinsPlayed `shouldNotBe` 0 lt ^. gsGoalsAllowed `shouldNotBe` 0 lt ^. gsGoalsAgainst `shouldNotBe` 0 lt ^. gsWins `shouldNotBe` 0 lt ^. gsLosses `shouldNotBe` 0 lt ^. gsTies `shouldNotBe` 0) $ s ^. database . dbGoalies addCharSpec :: Spec addCharSpec = describe "addChar" $ it "should add the character to the input buffer" $ let s = newProgState & inputBuffer .~ "foo" & addChar 'd' in s ^. inputBuffer `shouldBe` "food" removeCharSpec :: Spec removeCharSpec = describe "removeChar" $ do context "empty" $ it "should remove the character from the input buffer" $ let s = removeChar newProgState in s ^. inputBuffer `shouldBe` "" context "not empty" $ it "should remove the character from the input buffer" $ let s = newProgState & inputBuffer .~ "foo" & removeChar in s ^. inputBuffer `shouldBe` "fo" overtimeCheckSpec = describe "overtimeCheck" $ do context "tie game" $ do let s = newProgState & progMode.gameStateL %~ (gameType ?~ HomeGame) . (homeScore ?~ 1) . (awayScore ?~ 1) & overtimeCheck it "should clear the home score" $ s^.progMode.gameStateL.homeScore `shouldBe` Nothing it "should clear the away score" $ s^.progMode.gameStateL.awayScore `shouldBe` Nothing it "should leave the overtimeFlag blank" $ s^.progMode.gameStateL.overtimeFlag `shouldBe` Nothing context "game won" $ do let s = newProgState & progMode.gameStateL %~ (gameType ?~ HomeGame) . (homeScore ?~ 2) . (awayScore ?~ 1) & overtimeCheck it "should not change the home score" $ s^.progMode.gameStateL.homeScore `shouldBe` Just 2 it "should not change the away score" $ s^.progMode.gameStateL.awayScore `shouldBe` Just 1 it "should set the overtimeCheck flag to False" $ s^.progMode.gameStateL.overtimeFlag `shouldBe` Just False context "game lost" $ do let s = newProgState & progMode.gameStateL %~ (gameType ?~ HomeGame) . (homeScore ?~ 1) . (awayScore ?~ 2) & overtimeCheck it "should not change the home score" $ s^.progMode.gameStateL.homeScore `shouldBe` Just 1 it "should not change the away score" $ s^.progMode.gameStateL.awayScore `shouldBe` Just 2 it "should leave the overtimeCheck flag blank" $ s^.progMode.gameStateL.overtimeFlag `shouldBe` Nothing makePlayer :: IO Player makePlayer = Player <$> makeNum <*> makeName <*> makeName <*> makePlayerStats <*> makePlayerStats makeGoalie :: IO Goalie makeGoalie = Goalie <$> makeNum <*> makeName <*> makeGoalieStats <*> makeGoalieStats makePlayerStats :: IO PlayerStats makePlayerStats = PlayerStats <$> makeNum <*> makeNum <*> makeNum makeGoalieStats :: IO GoalieStats makeGoalieStats = GoalieStats <$> makeNum <*> makeNum <*> makeNum <*> makeNum <*> makeNum <*> makeNum <*> makeNum makeNum :: IO Int makeNum = randomRIO (1, 10) makeName :: IO String makeName = replicateM 10 $ randomRIO ('A', 'Z')