mtlstats/test/ActionsSpec.hs
2019-08-22 10:25:23 -04:00

177 lines
4.7 KiB
Haskell

{-
mtlstats
Copyright (C) 2019 Rhéal Lamothe
<rheal.lamothe@gmail.com>
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 <https://www.gnu.org/licenses/>.
-}
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
setHomeGameSpec
setAwayGameSpec
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 = newProgState
& database . dbGames .~ 1
& startNewGame
it "should set the number of games to 0" $
s ^. database . dbGames `shouldBe` 0
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
setHomeGameSpec :: Spec
setHomeGameSpec = describe "setHomeGame" $ do
let m = NewGame $ newGameState & gameType ?~ HomeGame
context "unexpected mode" $
it "should set the game type" $ let
s = setHomeGame newProgState
in s ^. progMode `shouldBe` m
context "NewGame mode" $
it "should set the game type" $ let
s = newProgState
& progMode .~ NewGame newGameState
& setHomeGame
in s ^. progMode `shouldBe` m
setAwayGameSpec :: Spec
setAwayGameSpec = describe "setAwayGame" $ do
let m = NewGame $ newGameState & gameType ?~ AwayGame
context "unexpected mode" $
it "should set the game type" $ let
s = setAwayGame newProgState
in s ^. progMode `shouldBe` m
context "NewGame mode" $
it "should set the game type" $ let
s = newProgState
& progMode .~ NewGame newGameState
& setAwayGame
in s ^. progMode `shouldBe` m
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')