For our Hakyll codebase, I've written a few helper methods and have started adding some HSpec unit tests around newer ones e.g.:
-- | Reject an item unless @fieldName@ is set to "true"
unlessEnabled :: MonadMetadata m
=> String
-> Item a
-> m Bool
unlessEnabled fieldName item = do
maybeValue <- getMetadataBool (itemIdentifier item) fieldName
return $ maybe True not maybeValue
-- | Try to look up a boolean field ("true" maps to @Just True@)
getMetadataBool :: MonadMetadata m
=> Identifier
-> String
-> m (Maybe Bool)
getMetadataBool ident name = do
maybeString <- getMetadataField ident name
return $ ((== "true") . map toLower) <$> maybeString
Now creating an Item or Identifier for testing is easy enough, but I'm not sure where to go with the MonadMetadata when running an Hspec.
I've seen testCompiler which feels like it might be copyable / useful (Compiler has a MonadMetadata instance), but I'm out of my Haskell-depth here...
Eventually (and thanks to @Bergi's suggestions) I got something working, and then tidied up:
Which allowed simple unit tests like: