Skip to content

Commit

Permalink
Extra expectations: shouldInclude & shouldIncludeAll
Browse files Browse the repository at this point in the history
 * Add a new `shouldInclude` (hspec#38 / hspec#35)
 * Also, a more general `shouldIncludeAll` which provides subset-like expectations (generalised to any `Foldable`s), with helpful (hopefully) messaging about missing elements
 * Add some tests around these
 * Replicate a few unexported helper functions, rather than mess with the overall structure
 * Update Hpack / Cabal

Closes hspec#38
  • Loading branch information
declension committed Apr 28, 2021
1 parent 5d3243d commit c2f0a07
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/Test/Hspec/Expectations/ContribSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Test.Hspec.Expectations.ContribSpec (spec) where

import Test.Hspec (Spec, describe, it)

import Test.Hspec.Expectations hiding (HasCallStack)
import Test.Hspec.Expectations.Contrib
import Test.HUnit.Lang
import Data.CallStack
import qualified Data.Set as S


expectationFailed :: HasCallStack => FailureReason -> HUnitFailure -> Bool
expectationFailed msg (HUnitFailure l m) = m == msg && (fmap setColumn l) == (fmap setColumn location)
where
location = case reverse callStack of
[] -> Nothing
(_, loc) : _ -> Just loc
location :: Maybe SrcLoc

setColumn loc_ = loc_{srcLocStartCol = 0, srcLocEndCol = 0}


one :: Int
one = 1

spec :: Spec
spec = do
describe "shouldInclude" $ do
it "fails for an empty list" $ do
([] `shouldInclude` one) `shouldThrow` expectationFailed (Reason "[] does not include 1")

it "succeeds for a single item list" $ do
[one] `shouldInclude` one

it "succeeds for a longer list" $ do
[1, 2, 2, 3] `shouldInclude` one

it "succeeds with repeated inclusion" $ do
[1, 2, 1] `shouldInclude` one

describe "shouldIncludeAll" $ do
it "should pass for lists in order" $
[1 :: Int, 2, 3] `shouldIncludeAll` [1, 3]

it "should fail with a nice message for lists with extra item" $
([one, 3] `shouldIncludeAll` [1, 2, 3]) `shouldThrow` expectationFailed (Reason "[1,3] did not include all of [1,2,3] - missing: 2")

it "should pass for lists out of order" $
[0, one, 2, 3] `shouldIncludeAll` [3, 2, 0, 1]

it "should pass for sets out of order" $
S.fromList [0 :: Int, 1, 2] `shouldIncludeAll` [2, 1]

0 comments on commit c2f0a07

Please sign in to comment.