Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haddock tests #5

Merged
merged 6 commits into from
Jun 28, 2023
Merged

Haddock tests #5

merged 6 commits into from
Jun 28, 2023

Conversation

blujupiter32
Copy link
Owner

PR created in fork for CI testing.

spec :: Spec
spec = describe "EntityHaddockSpec" $ do
it "generates entity Haddock" $ do
maybeDoc <- runQ $ getDoc (DeclDoc 'CommentModel)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grepping ghc codebase for Template Haskell failure gets us this:

badIO :: String -> IO a
badIO op = do   { qReport True ("Can't do `" ++ op ++ "' in the IO monad")
                ; fail "Template Haskell failure" }

Which is kind of an awful error - the actual reason is interleaved within the test output, here.

I think what you'll want to do here is splice the doc into the file, and then you can write assertions on the doc in a spec. Like,

[d|
commentModelDoc :: String
commentModelDoc = 
  $(lift =<< getDoc (DeclDoc 'CommentModel))
|]

spec = describe "EntityHaddockSpec" $ do
    it "generates entity haddock" $ do
      commentModelDoc `shouldBe` "..."

Or, if you want a compilation failure, you could write the whole "test" in the splice.

module Test where
 
import Doc

do
    maybeDoc <- getDoc (DeclDoc 'CommentModel)
    case maybeDoc of
        Nothing -> reportError "Expected doc for CommentModel"
        Just doc -> do
            let expected = "..."
            when (doc /= expected) $ do
                reportError "Expected doc to be different..."

    pure [] -- to satisfy the `Q [Dec]` type for a top level splice   

But this is probably much less easy to write, and won't get you nice error message rendering. But it will fail faster, so idk. hspec-compilation is begging to be born I guess lol.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've completed EntityHaddockSpec using the former method. However, the test output shows that it only finds the field comment added using putDoc - attempting to get the Haddock for CommentModel (added using withDecDoc) returns Nothing.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now fixed - I'd put 'CommentModel (constructor Name) instead of ''CommentModel (type Name) in the splice.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh goodness - I guess you can write, like,

-- | commentary for type
data Thing = 
    -- | commentary for constructor
    MkThing 

Great catch 😄

CommentSpec is compiled with -haddock to ensure that documentation
added using putDoc and withDecDoc is available.
@blujupiter32 blujupiter32 merged commit 2029f45 into entity-haddocks Jun 28, 2023
@blujupiter32 blujupiter32 deleted the haddock-tests branch July 14, 2023 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants