implemented Helpers.incParamBy

This commit is contained in:
Jonathan Lamothe 2021-09-22 19:01:10 -04:00
parent 515109f45d
commit 06b768ce15
2 changed files with 36 additions and 0 deletions

View File

@ -29,6 +29,7 @@ module Helpers (
editHost, editHost,
dropParam, dropParam,
editParam, editParam,
incParamBy,
incStrBy incStrBy
) where ) where
@ -119,6 +120,17 @@ editParam pName f url = do
) $ params url ) $ params url
Just url { params = params' } Just url { params = params' }
-- | Increment a parameter of a 'Url' by a given amount (if possible)
incParamBy
:: Integer
-- ^ the amount to increment by
-> String
-- ^ the parameter
-> Url
-- ^ the 'Url' to edit
-> Maybe Url
incParamBy n p = editParam p $ incStrBy n
-- | Increment a 'String' representation of an 'Integer' by a given -- | Increment a 'String' representation of an 'Integer' by a given
-- amount (if possible) -- amount (if possible)
incStrBy incStrBy

View File

@ -37,6 +37,7 @@ spec = do
editHostSpec editHostSpec
dropParamSpec dropParamSpec
editParamSpec editParamSpec
incParamBySpec
incStrBySpec incStrBySpec
textToUrlSpec :: Spec textToUrlSpec :: Spec
@ -150,6 +151,29 @@ editParamSpec = describe "editParam" $ mapM_
, ( "c", Nothing ) , ( "c", Nothing )
] ]
incParamBySpec :: Spec
incParamBySpec = describe "incParamBy" $ mapM_
( \(desc, n, pName, expected) -> context desc $
it ("should be " ++ show expected) $
incParamBy n pName url `shouldBe` expected
)
-- description, number, param, expected
[ ( "+1", 1, "a", Just $ urlWith "3" )
, ( "+2", 2, "a", Just $ urlWith "4" )
, ( "non-numeric", 1, "c", Nothing )
, ( "missing", 1, "d", Just url )
]
where
url = urlWith "2"
urlWith n = simpleUrl { params = mkParams n }
mkParams n =
[ ( "a", Just n )
, ( "b", Just "2" )
, ( "c", Just "foo" )
]
incStrBySpec :: Spec incStrBySpec :: Spec
incStrBySpec = describe "incStrBy" $ mapM_ incStrBySpec = describe "incStrBy" $ mapM_
( \(n, str, expected) -> ( \(n, str, expected) ->