diff --git a/src/Helpers.hs b/src/Helpers.hs index a2c0c21..b98560f 100644 --- a/src/Helpers.hs +++ b/src/Helpers.hs @@ -29,6 +29,7 @@ module Helpers ( editHost, dropParam, editParam, + incParamBy, incStrBy ) where @@ -119,6 +120,17 @@ editParam pName f url = do ) $ params url 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 -- amount (if possible) incStrBy diff --git a/test/HelpersSpec.hs b/test/HelpersSpec.hs index 97ed12b..9f74a63 100644 --- a/test/HelpersSpec.hs +++ b/test/HelpersSpec.hs @@ -37,6 +37,7 @@ spec = do editHostSpec dropParamSpec editParamSpec + incParamBySpec incStrBySpec textToUrlSpec :: Spec @@ -150,6 +151,29 @@ editParamSpec = describe "editParam" $ mapM_ , ( "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 = describe "incStrBy" $ mapM_ ( \(n, str, expected) ->