hspec / hspec-hedgehog

A library to integrate hedgehog tests into your hspec test suite.
https://hackage.haskell.org/package/hspec-hedgehog
Other
28 stars 6 forks source link

How would I use hedgehog-classes with this? #8

Closed kozross closed 4 years ago

kozross commented 4 years ago

It's not immediately apparent to me how I could use this to test various type class laws in Hspec. Would some support be possible?

parsonsmatt commented 4 years ago

The functions in hedgehog-classes return IO Bool, where True indicates that the laws pass. hspec tests let you run IO actions in the example blocks.

spec :: Spec
spec = do
  describe "Monoid Ordering" $ do
    it "satisfies the laws" $ do
      result <- lawsCheck (monoidLaws genOrdering)
      result `shouldBe` True

You can simplify this to:

it "satisfies the laws" $ do
  lawsCheck (monoidLaws genOrdering)
    `shouldReturn` True

Or define a helper function:

hedgeLaws :: String -> IO Bool -> IO ()
hedgeLaws message action = 
  it message $ 
    action `shouldReturn` Bool

spec :: Spec
spec = do
  describe "Monoid ordering" $ do
    hedgeLaws "works" $ monoidLaws genOrdering