Skip to content

Template Haskell Splice from readme $$(discover) not working in literate Haskell #539

Description

@Maik-Lesch

I have tried to use hedgehog for a tiny bit of property-based testing in
stack ghci as described in your README.

If I define the tests :: IO Bool manually, the test is correctly run and
fails if I deliberately make it fail by, say, adding 1 on one side of
the equation of prop_crossSum.

test-manual.lhs


> -- just administrative boilerplate for hedgehog testing
> {-# LANGUAGE TemplateHaskell #-}
> {-# LANGUAGE OverloadedStrings #-}
> module Assignment04 where
> import           Hedgehog
> import qualified Hedgehog.Gen as Gen
> import qualified Hedgehog.Range as Range


> crossSum :: Integer -> Integer -> Integer
> prop_crossSum :: Property
> prop_crossSum = property $ do
>   number <- forAll $ Gen.integral $ (Range.linear 1 100000)
>   base <- forAll $ Gen.integral $ (Range.linear 2 100)
>   crossSum base number === crossSum base (base*base*number)
> crossSum base num = foldr (+) 0 (go base (abs num) [])
>   where
>     go b n digits =
>       if (n == 0)
>       then digits
>       else go b ((n - (n `mod` b)) `div` b) ((n `mod` b):digits)

> tests :: IO Bool
> tests = checkParallel $ Group "Test.Assignment04" [
>   ("prop_crossSum", prop_crossSum)]

However, I find the explicit enumeration of tests somewhat inelegant,
since I might forget to register a test for a different piece of code.

Your README provides a handy solution using template Haskell, to match
all functions named with the prefix prop_ with $$(discover).

However, trying this creates a function tests, which still passes but
no longer fails when I deliberately make prop_crossSum fail.

test-template.lhs


> -- just administrative boilerplate for hedgehog testing
> {-# LANGUAGE TemplateHaskell #-}
> {-# LANGUAGE OverloadedStrings #-}
> module Assignment04 where
> import           Hedgehog
> import qualified Hedgehog.Gen as Gen
> import qualified Hedgehog.Range as Range


> crossSum :: Integer -> Integer -> Integer
> prop_crossSum :: Property
> prop_crossSum = property $ do
>   number <- forAll $ Gen.integral $ (Range.linear 1 100000)
>   base <- forAll $ Gen.integral $ (Range.linear 2 100)
>   crossSum base number === crossSum base (base*base*number)
> crossSum base num = foldr (+) 0 (go base (abs num) [])
>   where
>     go b n digits =
>       if (n == 0)
>       then digits
>       else go b ((n - (n `mod` b)) `div` b) ((n `mod` b):digits)

> tests :: IO Bool
> tests = checkParallel $$(discover)

Is this an issue of your README or of my understanding?
How would I make template haskell recognize prop_crossSum or any
other prop_* correctly?

(Sorry if this does not conform to your convention - this is my first issue)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions