implemented Helpers.editHost

This commit is contained in:
Jonathan Lamothe 2021-09-22 14:50:30 -04:00
parent 34ee4a57bc
commit b7737766f6
2 changed files with 29 additions and 0 deletions

View File

@ -25,6 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
module Helpers (
textToUrl,
urlToText,
editHost
) where
import qualified Data.List as L
@ -62,6 +63,17 @@ urlToText url = T.pack $
Just str -> "#" ++ str
Nothing -> ""
-- | Edit the 'host' value of a 'Url'
editHost
:: (String -> Maybe String)
-- ^ the transformation function
-> Url
-- ^ the Url being modified
-> Maybe Url
editHost f url = do
host' <- f $ host url
Just url { host = host' }
subToUrl :: T.Text -> Maybe Url
subToUrl text = case T.splitOn "://" text of
[protT, raw] -> do

View File

@ -33,6 +33,7 @@ spec :: Spec
spec = do
textToUrlSpec
urlToTextSpec
editHostSpec
textToUrlSpec :: Spec
textToUrlSpec = describe "textToUrl" $ mapM_
@ -64,6 +65,22 @@ urlToTextSpec = describe "urlToText" $ mapM_
, ( anchorUrl, anchorTxt )
]
editHostSpec ::Spec
editHostSpec = describe "editHost" $ mapM_
( \(desc, f, url, expected) -> context desc $
it ("should be " ++ show expected) $
editHost f url `shouldBe` expected
)
-- description, function, url, expected
[ ( "reverse", Just . reverse, simpleUrl, reversed )
, ( "fail", const Nothing, simpleUrl, Nothing )
]
where
reversed = Just simpleUrl
{ host = reverse $ host simpleUrl }
simpleTxt :: T.Text
simpleTxt = "http://example.com/"