Skip to content

Commit

Permalink
Show less context on failure (fixes #26)
Browse files Browse the repository at this point in the history
  • Loading branch information
sol committed Jul 27, 2024
1 parent 34dc340 commit 5c93445
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ jobs:
- name: Check for trailing whitespace
run: '! git grep -I "\s\+$"'
- run: curl -sSL https://raw.githubusercontent.com/sol/hpack/main/get-hpack.sh | bash
- run: hpack && git diff --exit-code
- run: hpack && git diff --color --exit-code
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
steps:
- uses: actions/checkout@v3

- run: cabal check
- run: cabal check --ignore=missing-upper-bounds

- uses: sol/haskell-autotag@v1
id: autotag
Expand Down
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog for hspec-hedgehog

## 0.2.0.0
- [#29](https://github.com/parsonsmatt/hspec-hedgehog/pull/29) @sol
- Show less context on failure.

## 0.1.1.0
- [#30](https://github.com/parsonsmatt/hspec-hedgehog/pull/30) @sol
- Show classification on success
Expand Down
6 changes: 3 additions & 3 deletions hspec-hedgehog.cabal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spec-version: 0.36.0

name: hspec-hedgehog
version: 0.1.1.0
version: 0.2.0.0
synopsis: Integrate Hedgehog and Hspec!
description: Please see the README on GitHub at <https://github.com/hspec/hspec-hedgehog#readme>
category: Testing
Expand All @@ -19,7 +19,7 @@ dependencies:
- base >= 4.7 && < 5
- hspec >= 2.11.0 && < 3
- hspec-core >= 2.11.0 && < 3
- hedgehog >= 1.0.2 && < 2
- hedgehog >= 1.5

library:
source-dirs: src
Expand Down
19 changes: 17 additions & 2 deletions src/Test/Hspec/Hedgehog.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ module Test.Hspec.Hedgehog
) where

import Control.Monad.IO.Class (liftIO)
import Data.Char (isSpace)
import Data.Coerce (coerce)
import Data.IORef (newIORef, readIORef, writeIORef)
import GHC.Stack (withFrozenCallStack)
Expand All @@ -105,7 +106,6 @@ import Hedgehog.Internal.Property (DiscardLimit (..), Property (..),
TerminationCriteria (..),
TestCount (..), TestLimit (..))
import Hedgehog.Internal.Report hiding (renderResult)
import qualified Hedgehog.Internal.Report as Hedge
import Hedgehog.Internal.Runner (checkReport)
import qualified Hedgehog.Internal.Seed as Seed
import Hedgehog.Internal.Source (ColumnNo (..), LineNo (..),
Expand Down Expand Up @@ -203,7 +203,14 @@ instance (m ~ IO) => Example (a -> PropertyT m ()) where
Just (rng, _) -> pure (uncurry Seed (unseedSMGen (coerce rng)))
hedgeResult <- checkReport propConfig size seed (propertyTest prop) cb

let renderResult color = Hedge.renderResult color (Just "property") hedgeResult
let
config = defaultConfig {
configContext = Context 3
, configPrintFailedAtLocation = False
, configPrintReproduceMessage = False
, configPrintPrefixIcons = DisablePrefixIcons
}
renderResult color = unlines . unindent . lines . dropWhileEnd isSpace <$> renderResultWith config color (Just "") hedgeResult

case reportStatus hedgeResult of
Failed FailureReport{..} -> do
Expand All @@ -223,3 +230,11 @@ instance (m ~ IO) => Example (a -> PropertyT m ()) where
ppresult <- renderResult DisableColor
writeIORef ref $ Result ppresult Success
readIORef ref

dropWhileEnd :: (a -> Bool) -> [a] -> [a]
dropWhileEnd p = reverse . dropWhile p . reverse

unindent :: [String] -> [String]
unindent xs = map (drop indentation) xs
where
indentation = minimum $ map (length . takeWhile (== ' ')) xs
26 changes: 11 additions & 15 deletions test/Test/Hspec/HedgehogSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,31 @@ spec = do
context "on Success" $ do
it "includes the number of passed tests" $ do
eval success `shouldReturn` Result
" ✓ property passed 100 tests."
"passed 100 tests.\n"
Success

it "includes classification" $ do
eval (label "foo" >> success) `shouldReturn` Result (joinLines [
" ✓ property passed 100 tests."
, " foo 100% ████████████████████"
"passed 100 tests."
, "foo 100% ████████████████████\n"
]) Success

context "on Failure" $ do
it "includes the number of discarded tests" $ do
eval discard `shouldReturn` Result "" (Failure Nothing (Reason
" ⚐ property gave up after 10 discards, passed 0 tests."
"gave up after 10 discards, passed 0 tests.\n"
))

it "provides a detailed failure message" $ do
Result "" (Failure (Just _loc) (ColorizedReason reason)) <- eval failingProperty
let line delta = " " <> show (failingPropertyLine + delta)
let line delta = "" <> show (failingPropertyLine + delta)
stripAnsi reason `shouldBe` joinLines [
" ✗ property failed at test/Test/Hspec/HedgehogSpec.hs:" <> show failingPropertyLine <> ":19"
, " after 1 test."
, " shrink path: 1:"
, " "
, " ┏━━ test/Test/Hspec/HedgehogSpec.hs ━━━"
"failed after 1 test."
, "shrink path: 1:"
, ""
, " ┏━━ test/Test/Hspec/HedgehogSpec.hs ━━━"
, line -1 <> " ┃ failingProperty :: PropertyT IO ()"
, line 0 <> " ┃ failingProperty = failure"
, " ┃ ^^^^^^^^^^^^^^^^^^^^^^^^^"
, " "
, " This failure can be reproduced by running:"
, " > recheckAt (Seed 14375056955115587481 16778118630780010967) \"1:\" property"
, " "
, " ┃ ^^^^^^^^^^^^^^^^^^^^^^^^^"
, ""
]

0 comments on commit 5c93445

Please sign in to comment.