hspec-expectations-0.8.4: Catchy combinators for HUnit
Safe HaskellSafe-Inferred
LanguageHaskell2010

Test.Hspec.Expectations.Contrib

Description

Experimental combinators, that may become part of the main distribution, if they turn out to be useful for a wider audience.

Synopsis

Predicates

(useful in combination with shouldSatisfy)

isLeft :: Either a b -> Bool #

Return True if the given value is a Left-value, False otherwise.

Examples

Expand

Basic usage:

>>> isLeft (Left "foo")
True
>>> isLeft (Right 3)
False

Assuming a Left value signifies some sort of error, we can use isLeft to write a very simple error-reporting function that does absolutely nothing in the case of success, and outputs "ERROR" if any error occurred.

This example shows how isLeft might be used to avoid pattern matching when one does not care about the value contained in the constructor:

>>> import Control.Monad ( when )
>>> let report e = when (isLeft e) $ putStrLn "ERROR"
>>> report (Right 1)
>>> report (Left "parse error")
ERROR

Since: base-4.7.0.0

isRight :: Either a b -> Bool #

Return True if the given value is a Right-value, False otherwise.

Examples

Expand

Basic usage:

>>> isRight (Left "foo")
False
>>> isRight (Right 3)
True

Assuming a Left value signifies some sort of error, we can use isRight to write a very simple reporting function that only outputs "SUCCESS" when a computation has succeeded.

This example shows how isRight might be used to avoid pattern matching when one does not care about the value contained in the constructor:

>>> import Control.Monad ( when )
>>> let report e = when (isRight e) $ putStrLn "SUCCESS"
>>> report (Left "parse error")
>>> report (Right 1)
SUCCESS

Since: base-4.7.0.0

Annotating expectations

annotate :: String -> IO a -> IO a Source #

If you have a test case that has multiple assertions, you can use the annotate function to provide a string message that will be attached to the Expectation.

describe "annotate" $ do
  it "adds the message" $ do
    annotate "obvious falsehood" $ do
      True shouldBe False

========>

1) annotate, adds the message
      obvious falsehood
      expected: False
       but got: True

Since: 0.8.3