implemented editGoalieName

This commit is contained in:
Jonathan Lamothe 2019-11-13 23:50:02 -05:00
parent fceba7eed1
commit 61ba781c5d
2 changed files with 107 additions and 61 deletions

View File

@ -37,13 +37,7 @@ editGoalieNumber
-- ^ New goalie number -- ^ New goalie number
-> ProgState -> ProgState
-> ProgState -> ProgState
editGoalieNumber n s = fromMaybe s $ do editGoalieNumber num = editGoalie (gNumber .~ num) EGMenu
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
-- | Edits a goalie's name -- | Edits a goalie's name
editGoalieName editGoalieName
@ -51,4 +45,12 @@ editGoalieName
-- ^ The new name -- ^ The new name
-> ProgState -> ProgState
-> ProgState -> ProgState
editGoalieName = undefined editGoalieName name = editGoalie (gName .~ name) EGMenu
editGoalie :: (Goalie -> Goalie) -> EditGoalieMode -> ProgState -> ProgState
editGoalie f mode s = fromMaybe s $ do
gid <- s^.progMode.editGoalieStateL.egsSelectedGoalie
void $ nth gid $ s^.database.dbGoalies
Just $ s
& database.dbGoalies %~ modifyNth gid f
& progMode.editGoalieStateL.egsMode .~ mode

View File

@ -30,80 +30,124 @@ import Mtlstats.Types
import Mtlstats.Util import Mtlstats.Util
spec :: Spec spec :: Spec
spec = describe "EditGoalie" spec = describe "EditGoalie" $ do
editGoalieNumberSpec editGoalieNumberSpec
editGoalieNameSpec
editGoalieNumberSpec :: Spec editGoalieNumberSpec :: Spec
editGoalieNumberSpec = describe "editGoalieNumber" $ do editGoalieNumberSpec = describe "editGoalieNumber" $ editTest
let (editGoalieNumber 5)
joe = newGoalie 2 "Joe" EGNumber
bob = newGoalie 3 "Bob" (uncurry newGoalie)
db = newDatabase & dbGoalies .~ [joe, bob] [ ( "set Joe"
, Just 0
, (5, "Joe")
, (3, "Bob")
, EGMenu
)
, ( "set Bob"
, Just 1
, (2, "Joe")
, (5, "Bob")
, EGMenu
)
, ( "out of bounds"
, Just 2
, (2, "Joe")
, (3, "Bob")
, EGNumber
)
, ( "no goalie selected"
, Nothing
, (2, "Joe")
, (3, "Bob")
, EGNumber
)
]
progState m = newProgState editGoalieNameSpec :: Spec
& progMode .~ m editGoalieNameSpec = describe "editGoalieName" $ editTest
& database .~ db (editGoalieName "foo")
& editGoalieNumber 5 EGName
(uncurry newGoalie)
[ ( "set Joe"
, Just 0
, ( 2, "foo" )
, ( 3, "Bob" )
, EGMenu
)
, ( "set Bob"
, Just 1
, ( 2, "Joe" )
, ( 3, "foo" )
, EGMenu
)
, ( "out of bounds"
, Just 2
, ( 2, "Joe" )
, ( 3, "Bob" )
, EGName
)
, ( "no goalie selected"
, Nothing
, ( 2, "Joe" )
, ( 3, "Bob" )
, EGName
)
]
editTest
:: (ProgState -> ProgState)
-> EditGoalieMode
-> (a -> Goalie)
-> [(String, Maybe Int, a, a, EditGoalieMode)]
-> Spec
editTest func setMode mkGoalie params = do
mapM_ mapM_
(\(setLabel, setGid, mode, joeData, bobData) -> context setLabel $ do (\(setLabel, setGid, joeData, bobData, expectMode) -> context setLabel $ do
let let
egs = newEditGoalieState egs = newEditGoalieState
& egsSelectedGoalie .~ setGid & egsSelectedGoalie .~ setGid
& egsMode .~ EGNumber & egsMode .~ setMode
pm = EditGoalie egs ps = func $ progState $ EditGoalie egs
ps = progState pm
mapM_ mapM_
(\(chkLabel, chkGid, (number, name)) -> context chkLabel $ let (\(chkLabel, chkGid, goalieData) -> context chkLabel $ let
g = fromJust $ nth chkGid $ ps^.database.dbGoalies actual = fromJust $ nth chkGid $ ps^.database.dbGoalies
expected = mkGoalie goalieData
in context "check goalie" $ let
expected = newGoalie number name
in it ("should be " ++ show expected) $ in it ("should be " ++ show expected) $
g `shouldBe` expected) actual `shouldBe` expected)
-- label, goalie ID, goalie data
-- label, goalie ID, data
[ ( "check Joe", 0, joeData ) [ ( "check Joe", 0, joeData )
, ( "check Bob", 1, bobData ) , ( "check Bob", 1, bobData )
] ]
context "check mode" $ context "check mode" $
it ("should be " ++ show mode) $ it ("should be " ++ show expectMode) $
ps^.progMode.editGoalieStateL.egsMode `shouldBe` mode) ps^.progMode.editGoalieStateL.egsMode `shouldBe` expectMode)
[ ( "set Joe" params
, 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 context "wrong progMode" $ do
let ps = progState MainMenu let ps = func $ progState MainMenu
it "should not change the database" $ it "should not change the database" $
ps^.database `shouldBe` db ps^.database `shouldBe` db
it "should not change the progMode" $ it "should not change the progMode" $
show (ps^.progMode) `shouldBe` "MainMenu" show (ps^.progMode) `shouldBe` "MainMenu"
joe :: Goalie
joe = newGoalie 2 "Joe"
bob :: Goalie
bob = newGoalie 3 "Bob"
db :: Database
db = newDatabase & dbGoalies .~ [joe, bob]
progState :: ProgMode -> ProgState
progState mode = newProgState
& progMode .~ mode
& database .~ db