mtlstats/test/ActionsSpec.hs

435 lines
13 KiB
Haskell
Raw Normal View History

2019-08-20 11:31:03 -04:00
{-
mtlstats
2023-05-23 17:22:14 -04:00
Copyright (C) Rhéal Lamothe
2019-08-20 11:31:03 -04:00
<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/>.
-}
2019-10-11 23:13:00 -04:00
{-# LANGUAGE LambdaCase #-}
2019-08-20 11:31:03 -04:00
module ActionsSpec (spec) where
2019-08-21 15:57:52 -04:00
import Control.Monad (replicateM)
2020-01-11 02:29:17 -05:00
import Lens.Micro ((^.), (&), (.~), (?~), (%~))
2019-10-11 23:13:00 -04:00
import Test.Hspec
( Spec
, context
, describe
, it
, shouldBe
, shouldNotBe
, shouldSatisfy
)
2019-08-20 12:26:24 -04:00
import Mtlstats.Actions
import Mtlstats.Types
2019-08-20 11:31:03 -04:00
import qualified Actions.NewGameSpec as NewGame
import qualified Actions.EditStandingsSpec as EditStandings
import qualified TypesSpec as TS
2019-08-20 11:31:03 -04:00
spec :: Spec
spec = describe "Mtlstats.Actions" $ do
2019-08-20 12:26:24 -04:00
startNewSeasonSpec
startNewGameSpec
2019-08-21 15:57:52 -04:00
resetYtdSpec
2020-01-11 02:29:17 -05:00
clearRookiesSpec
2019-11-14 11:21:52 -05:00
resetStandingsSpec
2019-08-24 17:01:18 -04:00
addCharSpec
removeCharSpec
createPlayerSpec
2019-10-25 00:37:14 -04:00
createGoalieSpec
2019-12-17 12:16:26 -05:00
editSpec
2019-11-01 04:00:29 -04:00
editPlayerSpec
editSelectedPlayerSpec
2019-11-12 23:48:31 -05:00
editGoalieSpec
editSelectedGoalieSpec
addPlayerSpec
2019-10-26 02:05:55 -04:00
addGoalieSpec
resetCreatePlayerStateSpec
resetCreateGoalieStateSpec
2019-10-11 23:13:00 -04:00
backHomeSpec
NewGame.spec
EditStandings.spec
2019-08-20 12:26:24 -04:00
startNewSeasonSpec :: Spec
startNewSeasonSpec = describe "startNewSeason" $ do
let
s = newProgState
& database . dbGames .~ 1
& startNewSeason
it "should set the progState to NewSeason" $
2019-09-13 02:26:03 -04:00
show (s^.progMode) `shouldBe` "NewSeason"
2019-08-20 11:31:03 -04:00
2019-08-20 12:26:24 -04:00
it "should set the number of games to 0" $
s ^. database . dbGames `shouldBe` 0
2019-08-20 11:31:03 -04:00
2019-08-20 12:26:24 -04:00
startNewGameSpec :: Spec
2019-08-22 01:18:02 -04:00
startNewGameSpec = describe "startNewGame" $ do
let s = startNewGame newProgState
2019-08-22 01:18:02 -04:00
it "should increment the number of games" $
s ^. database . dbGames `shouldBe` 1
2019-08-22 01:18:02 -04:00
it "should set the mode to NewGame" $
2019-09-13 02:26:03 -04:00
show (s^.progMode) `shouldBe` "NewGame"
2019-08-21 15:57:52 -04:00
resetYtdSpec :: Spec
resetYtdSpec = describe "resetYtd" $
it "should reset the year-to-date stats for all players" $ do
ps <- replicateM 2 TS.makePlayer
gs <- replicateM 2 TS.makeGoalie
2019-08-21 15:57:52 -04:00
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 ^. gsWins `shouldBe` 0
ytd ^. gsLosses `shouldBe` 0
ytd ^. gsTies `shouldBe` 0
lt ^. gsGames `shouldNotBe` 0
lt ^. gsMinsPlayed `shouldNotBe` 0
lt ^. gsGoalsAllowed `shouldNotBe` 0
lt ^. gsWins `shouldNotBe` 0
lt ^. gsLosses `shouldNotBe` 0
lt ^. gsTies `shouldNotBe` 0) $
s ^. database . dbGoalies
2020-01-11 02:29:17 -05:00
clearRookiesSpec :: Spec
clearRookiesSpec = describe "clearRookies" $ do
let
players =
[ newPlayer 1 "Joe" "centre" & pRookie .~ True
, newPlayer 2 "Bob" "centre" & pRookie .~ False
]
goalies =
[ newGoalie 3 "Bill" & gRookie .~ True
, newGoalie 4 "Doug" & gRookie .~ False
]
ps = newProgState
& database
%~ (dbPlayers .~ players)
. (dbGoalies .~ goalies)
ps' = clearRookies ps
context "Players" $ mapM_
(\p -> let
name = p^.pName
rFlag = p^.pRookie
in context name $
it "should not be a rookie" $
rFlag `shouldBe` False)
(ps'^.database.dbPlayers)
context "Goalies" $ mapM_
(\g -> let
name = g^.gName
rFlag = g^.gRookie
in context name $
it "should not be a rookie" $
rFlag `shouldBe` False)
(ps'^.database.dbGoalies)
2019-11-14 11:21:52 -05:00
resetStandingsSpec :: Spec
resetStandingsSpec = describe "resetStandings" $ do
let
home = GameStats
{ _gmsWins = 1
, _gmsLosses = 2
, _gmsOvertime = 3
, _gmsGoalsFor = 4
, _gmsGoalsAgainst = 5
}
away = GameStats
{ _gmsWins = 6
, _gmsLosses = 7
, _gmsOvertime = 8
, _gmsGoalsFor = 9
, _gmsGoalsAgainst = 10
}
db = newDatabase
& dbHomeGameStats .~ home
& dbAwayGameStats .~ away
ps = newProgState
& database .~ db
& resetStandings
context "home standings" $
it "should be reset" $
ps^.database.dbHomeGameStats `shouldBe` newGameStats
context "away standings" $
it "should be reset" $
ps^.database.dbAwayGameStats `shouldBe` newGameStats
2019-08-24 17:01:18 -04:00
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"
createPlayerSpec :: Spec
createPlayerSpec = describe "createPlayer" $
it "should change the mode appropriately" $ let
s = createPlayer newProgState
2019-09-13 02:26:03 -04:00
in show (s^.progMode) `shouldBe` "CreatePlayer"
2019-10-25 00:37:14 -04:00
createGoalieSpec :: Spec
createGoalieSpec = describe "createGoalie" $
it "should change the mode appropriately" $ let
s = createGoalie newProgState
in show (s^.progMode) `shouldBe` "CreateGoalie"
2019-12-17 12:16:26 -05:00
editSpec :: Spec
editSpec = describe "edit" $
it "should change the mode to EditMenu" $ let
ps = edit newProgState
in show (ps^.progMode) `shouldBe` "EditMenu"
2019-11-01 04:00:29 -04:00
editPlayerSpec :: Spec
editPlayerSpec = describe "editPlayer" $
it "should change the mode appropriately" $ let
s = editPlayer newProgState
in show (s^.progMode) `shouldBe` "EditPlayer"
editSelectedPlayerSpec :: Spec
editSelectedPlayerSpec = describe "editSelectedPlayer" $ mapM_
(\(label, pState, expected) -> context label $
it "should edit the players appropriately" $ let
pState' = editSelectedPlayer (pName .~ "foo") pState
players' = pState'^.database.dbPlayers
in players' `shouldBe` expected)
-- label, initial state, expected
[ ( "wrong mode", baseState, players )
, ( "not selected", changePlayer Nothing, players )
, ( "player 0", changePlayer $ Just 0, changed0 )
, ( "player 1", changePlayer $ Just 1, changed1 )
, ( "out of bounds", changePlayer $ Just 2, players )
]
where
baseState = newProgState & database.dbPlayers .~ players
changePlayer n = baseState
& (progMode.editPlayerStateL.epsSelectedPlayer .~ n)
players = [ player 0, player 1 ]
changed0 = [ player' 0, player 1 ]
changed1 = [ player 0, player' 1 ]
player n = newPlayer n ("Player " ++ show n) "pos"
player' n = newPlayer n "foo" "pos"
2019-11-12 23:48:31 -05:00
editGoalieSpec :: Spec
editGoalieSpec = describe "editGoalie" $
it "should change the mode appropriately" $ let
s = editGoalie newProgState
in show (s^.progMode) `shouldBe` "EditGoalie"
editSelectedGoalieSpec :: Spec
editSelectedGoalieSpec = describe "editSelectedGoalie" $ mapM_
(\(label, pState, expected) -> context label $
it "should edit the goalies appropriately" $ let
pState' = editSelectedGoalie (gName .~ "foo") pState
goalies' = pState'^.database.dbGoalies
in goalies' `shouldBe` expected)
-- label, initial state, expected
[ ( "wrong mode", baseState, goalies )
, ( "not selected", changeGoalie Nothing, goalies )
2020-01-09 01:43:35 -05:00
, ( "goalie 0", changeGoalie $ Just 0, changed0 )
, ( "goalie 1", changeGoalie $ Just 1, changed1 )
, ( "out of bounds", changeGoalie $ Just 2, goalies )
]
where
baseState = newProgState & database.dbGoalies .~ goalies
changeGoalie n = baseState
& (progMode.editGoalieStateL.egsSelectedGoalie .~ n)
goalies = [ goalie 0, goalie 1 ]
changed0 = [ goalie' 0, goalie 1 ]
changed1 = [ goalie 0, goalie' 1 ]
goalie n = newGoalie n ("Player " ++ show n)
goalie' n = newGoalie n "foo"
addPlayerSpec :: Spec
2020-02-13 10:35:35 -05:00
addPlayerSpec = describe "addPlayer" $ mapM_
(\(label, expectation, pm, players) -> context label $
it expectation $ let
ps = newProgState
& progMode .~ pm
& database.dbPlayers .~ [joe]
ps' = addPlayer ps
in ps'^.database.dbPlayers `shouldBe` players)
2020-02-13 23:40:43 -05:00
-- label, expectation, progMode, players
[ ( "wrong mode", failure, MainMenu, [joe] )
, ( "missing number", failure, noNum, [joe] )
, ( "missing rookie flag", failure, noRookie, [joe] )
, ( "missing active flag", failure, noActive, [joe] )
, ( "rookie", success, mkRookie, [joe, rookie] )
, ( "retired", success, mkRetired, [joe, retired] )
, ( "normal player", success, mkNormal, [joe, normal] )
2020-02-13 10:35:35 -05:00
]
2020-02-13 10:35:35 -05:00
where
failure = "should not create the player"
success = "should create the player"
2020-02-13 23:40:43 -05:00
noNum = mkpm Nothing (Just False) (Just True)
noRookie = mkpm (Just 3) Nothing (Just True)
noActive = mkpm (Just 3) (Just False) Nothing
mkRookie = mkpm (Just 3) (Just True) (Just True)
mkRetired = mkpm (Just 3) (Just False) (Just False)
mkNormal = mkpm (Just 3) (Just False) (Just True)
2020-02-13 10:35:35 -05:00
joe = newPlayer 2 "Joe" "centre"
2020-02-13 23:55:00 -05:00
rookie = player True True
retired = player False False
normal = player False True
2020-02-13 23:40:43 -05:00
2020-02-13 23:55:00 -05:00
player r a = newPlayer 3 "Bob" "defense"
2020-02-13 23:40:43 -05:00
& pRookie .~ r
& pActive .~ a
mkpm n r a = CreatePlayer $ newCreatePlayerState
2020-02-13 10:35:35 -05:00
& cpsNumber .~ n
& cpsName .~ "Bob"
& cpsPosition .~ "defense"
2020-02-13 23:40:43 -05:00
& cpsRookieFlag .~ r
& cpsActiveFlag .~ a
2019-10-26 02:05:55 -04:00
addGoalieSpec :: Spec
addGoalieSpec = describe "addGoalie" $ mapM_
(\(label, expectation, pm, goalies) -> context label $
it expectation $ let
ps = newProgState
& progMode .~ pm
& database.dbGoalies .~ [joe]
ps' = addGoalie ps
in ps'^.database.dbGoalies `shouldBe` goalies)
2020-02-13 23:55:00 -05:00
-- label, expectation, progMode, expected goalies
[ ( "wrong mode", failure, MainMenu, [joe] )
, ( "no number", failure, noNum, [joe] )
, ( "no rookie flag", failure, noRookie, [joe] )
, ( "no active flag", failure, noActive, [joe] )
, ( "rookie", success, mkRookie, [joe, rookie] )
, ( "retired", success, mkRetired, [joe, retired] )
, ( "normal goalie", success, mkNormal, [joe, normal] )
]
where
2020-02-13 23:55:00 -05:00
failure = "should not create the goalie"
success = "should create the goalie"
noNum = cgs Nothing (Just False) (Just True)
noRookie = cgs (Just 3) Nothing (Just True)
noActive = cgs (Just 3) (Just False) Nothing
mkRookie = cgs (Just 3) (Just True) (Just True)
mkRetired = cgs (Just 3) (Just False) (Just False)
mkNormal = cgs (Just 3) (Just False) (Just True)
joe = newGoalie 2 "Joe"
rookie = goalie True True
retired = goalie False False
normal = goalie False True
goalie r a = newGoalie 3 "Bob"
& gRookie .~ r
& gActive .~ a
cgs n r a = CreateGoalie $ newCreateGoalieState
& cgsNumber .~ n
& cgsName .~ "Bob"
2020-02-13 23:55:00 -05:00
& cgsRookieFlag .~ r
& cgsActiveFlag .~ a
2019-10-26 02:05:55 -04:00
resetCreatePlayerStateSpec :: Spec
resetCreatePlayerStateSpec = describe "resetCreatePlayerState" $ let
cps = newCreatePlayerState
& cpsNumber ?~ 1
& cpsName .~ "Joe"
& cpsPosition .~ "centre"
ps = resetCreatePlayerState $
newProgState & progMode.createPlayerStateL .~ cps
in TS.compareTest (ps^.progMode.createPlayerStateL) newCreatePlayerState
resetCreateGoalieStateSpec :: Spec
resetCreateGoalieStateSpec = describe "resetCreateGoalieState" $ let
cgs = newCreateGoalieState
& cgsNumber ?~ 1
& cgsName .~ "Joe"
ps = resetCreateGoalieState $
newProgState & progMode.createGoalieStateL .~ cgs
in TS.compareTest (ps^.progMode.createGoalieStateL) newCreateGoalieState
2019-10-11 23:13:00 -04:00
backHomeSpec :: Spec
backHomeSpec = describe "backHome" $ do
let
input = newProgState
& progMode.gameStateL .~ newGameState
& inputBuffer .~ "foo"
result = backHome input
it "should set the program mode back to MainMenu" $
result^.progMode `shouldSatisfy` \case
MainMenu -> True
_ -> False
it "should clear the input buffer" $
result^.inputBuffer `shouldBe` ""