From 89879153de431ebdba1cedf7f2e638f1302953f1 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Wed, 22 Sep 2021 20:54:09 -0400 Subject: [PATCH] implemented Handlers.incAnchorBy --- src/Helpers.hs | 10 ++++++++++ test/HelpersSpec.hs | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Helpers.hs b/src/Helpers.hs index 51ce991..0a8eac1 100644 --- a/src/Helpers.hs +++ b/src/Helpers.hs @@ -31,6 +31,7 @@ module Helpers ( editParam, editAnchor, incParamBy, + incAnchorBy, incStrBy ) where @@ -145,6 +146,15 @@ incParamBy -> Maybe Url incParamBy n p = editParam p $ incStrBy n +-- | Increment an anchor by a given amount (if possible) +incAnchorBy + :: Integer + -- ^ the amount to increment by + -> Url + -- ^ the 'Url' to edit + -> Maybe Url +incAnchorBy n = editAnchor $ 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 690023e..01d51d8 100644 --- a/test/HelpersSpec.hs +++ b/test/HelpersSpec.hs @@ -39,6 +39,7 @@ spec = do editParamSpec editAnchorSpec incParamBySpec + incAnchorBySpec incStrBySpec textToUrlSpec :: Spec @@ -194,6 +195,22 @@ incParamBySpec = describe "incParamBy" $ mapM_ , ( "c", Just "foo" ) ] +incAnchorBySpec :: Spec +incAnchorBySpec = describe "incAnchorBy" $ mapM_ + ( \(desc, n, url, expected) -> context desc $ + it ("should be " ++ show expected) $ + incAnchorBy n url `shouldBe` expected + ) + + -- description, number, url, expected + [ ( "+1", 1, urlWith "1", Just $ urlWith "2" ) + , ( "+2", 2, urlWith "1", Just $ urlWith "3" ) + , ( "empty", 1, simpleUrl, Just simpleUrl ) + , ( "non-numeric", 1, urlWith "foo", Nothing ) + ] + + where urlWith str = simpleUrl { anchor = Just str } + incStrBySpec :: Spec incStrBySpec = describe "incStrBy" $ mapM_ ( \(n, str, expected) ->