| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Test.Hspec.Meta
Synopsis
- type Spec = SpecWith ()
- type SpecWith a = SpecM a ()
- type family Arg e
- class Example e
- type Expectation = Assertion
- expectationFailure :: HasCallStack => String -> Expectation
- shouldBe :: (HasCallStack, Show a, Eq a) => a -> a -> Expectation
- shouldSatisfy :: (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
- shouldStartWith :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation
- shouldEndWith :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation
- shouldContain :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation
- shouldMatchList :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation
- shouldReturn :: (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation
- shouldNotBe :: (HasCallStack, Show a, Eq a) => a -> a -> Expectation
- shouldNotSatisfy :: (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation
- shouldNotContain :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation
- shouldNotReturn :: (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation
- shouldThrow :: (HasCallStack, Exception e) => IO a -> Selector e -> Expectation
- type Selector a = a -> Bool
- anyException :: Selector SomeException
- anyErrorCall :: Selector ErrorCall
- anyIOException :: Selector IOException
- anyArithException :: Selector ArithException
- errorCall :: String -> Selector ErrorCall
- type HasCallStack = ?callStack :: CallStack
- it :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- specify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- describe :: HasCallStack => String -> SpecWith a -> SpecWith a
- context :: HasCallStack => String -> SpecWith a -> SpecWith a
- example :: Expectation -> Expectation
- parallel :: SpecWith a -> SpecWith a
- runIO :: IO r -> SpecM a r
- pending :: HasCallStack => Expectation
- pendingWith :: HasCallStack => String -> Expectation
- xit :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- xspecify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- xdescribe :: HasCallStack => String -> SpecWith a -> SpecWith a
- xcontext :: HasCallStack => String -> SpecWith a -> SpecWith a
- focus :: SpecWith a -> SpecWith a
- fit :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- fspecify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a)
- fdescribe :: HasCallStack => String -> SpecWith a -> SpecWith a
- fcontext :: HasCallStack => String -> SpecWith a -> SpecWith a
- type ActionWith a = a -> IO ()
- before :: IO a -> SpecWith a -> Spec
- before_ :: IO () -> SpecWith a -> SpecWith a
- beforeWith :: (b -> IO a) -> SpecWith a -> SpecWith b
- beforeAll :: IO a -> SpecWith a -> Spec
- beforeAll_ :: IO () -> SpecWith a -> SpecWith a
- beforeAllWith :: (b -> IO a) -> SpecWith a -> SpecWith b
- after :: ActionWith a -> SpecWith a -> SpecWith a
- after_ :: IO () -> SpecWith a -> SpecWith a
- afterAll :: HasCallStack => ActionWith a -> SpecWith a -> SpecWith a
- afterAll_ :: HasCallStack => IO () -> SpecWith a -> SpecWith a
- around :: (ActionWith a -> IO ()) -> SpecWith a -> Spec
- around_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a
- aroundWith :: (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
- aroundAll :: (ActionWith a -> IO ()) -> SpecWith a -> Spec
- aroundAll_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a
- aroundAllWith :: forall a b. (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b
- mapSubject :: (b -> a) -> SpecWith a -> SpecWith b
- ignoreSubject :: SpecWith () -> SpecWith a
- hspec :: Spec -> IO ()
- type Spec = SpecWith ()
- hspec :: Spec -> IO ()
- class IsFormatter a where
- toFormatter :: a -> IO Formatter
- hspecWithFormatter :: IsFormatter a => a -> Spec -> IO ()
- postProcessSpec :: FilePath -> Spec -> Spec
- describe :: HasCallStack => String -> SpecWith a -> SpecWith a
- hspecResult :: Spec -> IO Summary
Types
Instances
| type Arg Bool Source # | |
Defined in Test.Hspec.Core.Example | |
| type Arg Property Source # | |
Defined in Test.Hspec.Core.Example | |
| type Arg Expectation Source # | |
Defined in Test.Hspec.Core.Example | |
| type Arg (a -> Property) Source # | |
Defined in Test.Hspec.Core.Example | |
| type Arg (a -> Expectation) Source # | |
Defined in Test.Hspec.Core.Example | |
| type Arg (a -> Bool) Source # | |
Defined in Test.Hspec.Core.Example | |
A type class for examples
Minimal complete definition
evaluateExample
Instances
| Example Bool Source # | |
Defined in Test.Hspec.Core.Example Methods evaluateExample :: Bool -> Params -> (ActionWith (Arg Bool) -> IO ()) -> ProgressCallback -> IO Result | |
| Example Property Source # | |
Defined in Test.Hspec.Core.Example Methods evaluateExample :: Property -> Params -> (ActionWith (Arg Property) -> IO ()) -> ProgressCallback -> IO Result | |
| Example Expectation Source # | |
Defined in Test.Hspec.Core.Example Associated Types type Arg Expectation Source # Methods evaluateExample :: Expectation -> Params -> (ActionWith (Arg Expectation) -> IO ()) -> ProgressCallback -> IO Result | |
| Example (a -> Property) Source # | |
Defined in Test.Hspec.Core.Example Methods evaluateExample :: (a -> Property) -> Params -> (ActionWith (Arg (a -> Property)) -> IO ()) -> ProgressCallback -> IO Result | |
| Example (a -> Expectation) Source # | |
Defined in Test.Hspec.Core.Example Associated Types type Arg (a -> Expectation) Source # Methods evaluateExample :: (a -> Expectation) -> Params -> (ActionWith (Arg (a -> Expectation)) -> IO ()) -> ProgressCallback -> IO Result | |
| Example (a -> Bool) Source # | |
Defined in Test.Hspec.Core.Example Methods evaluateExample :: (a -> Bool) -> Params -> (ActionWith (Arg (a -> Bool)) -> IO ()) -> ProgressCallback -> IO Result | |
Setting expectations
Setting expectations
type Expectation = Assertion Source #
expectationFailure :: HasCallStack => String -> Expectation Source #
shouldBe :: (HasCallStack, Show a, Eq a) => a -> a -> Expectation infix 1 Source #
actual `shouldBe` expected sets the expectation that actual is equal
to expected.
shouldSatisfy :: (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation infix 1 Source #
v `shouldSatisfy` p sets the expectation that p v is True.
shouldStartWith :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation infix 1 Source #
list `shouldStartWith` prefix sets the expectation that list starts with prefix,
shouldEndWith :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation infix 1 Source #
list `shouldEndWith` suffix sets the expectation that list ends with suffix,
shouldContain :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation infix 1 Source #
list `shouldContain` sublist sets the expectation that sublist is contained,
wholly and intact, anywhere in list.
shouldMatchList :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation infix 1 Source #
xs `shouldMatchList` ys sets the expectation that xs has the same
elements that ys has, possibly in another order
shouldReturn :: (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation infix 1 Source #
action `shouldReturn` expected sets the expectation that action
returns expected.
shouldNotBe :: (HasCallStack, Show a, Eq a) => a -> a -> Expectation infix 1 Source #
actual `shouldNotBe` notExpected sets the expectation that actual is not
equal to notExpected
shouldNotSatisfy :: (HasCallStack, Show a) => a -> (a -> Bool) -> Expectation infix 1 Source #
v `shouldNotSatisfy` p sets the expectation that p v is False.
shouldNotContain :: (HasCallStack, Show a, Eq a) => [a] -> [a] -> Expectation infix 1 Source #
list `shouldNotContain` sublist sets the expectation that sublist is not
contained anywhere in list.
shouldNotReturn :: (HasCallStack, Show a, Eq a) => IO a -> a -> Expectation infix 1 Source #
action `shouldNotReturn` notExpected sets the expectation that action
does not return notExpected.
Expecting exceptions
shouldThrow :: (HasCallStack, Exception e) => IO a -> Selector e -> Expectation infix 1 Source #
action `shouldThrow` selector sets the expectation that action throws
an exception. The precise nature of the expected exception is described
with a Selector.
Selecting exceptions
type Selector a = a -> Bool Source #
A Selector is a predicate; it can simultaneously constrain the type and
value of an exception.
Predefined type-based selectors
There are predefined selectors for some standard exceptions. Each selector
is just const True with an appropriate type.
Combinators for defining value-based selectors
Some exceptions (most prominently ErrorCall) have no Eq instance.
Selecting a specific value would require pattern matching.
For such exceptions, combinators that construct selectors are provided. Each combinator corresponds to a constructor; it takes the same arguments, and has the same name (but starting with a lower-case letter).
Re-exports
type HasCallStack = ?callStack :: CallStack #
Request a CallStack.
NOTE: The implicit parameter ?callStack :: CallStack is an
implementation detail and should not be considered part of the
CallStack API, we may decide to change the implementation in the
future.
Since: base-4.9.0.0
Defining a spec
it :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
The it function creates a spec item.
A spec item consists of:
- a textual description of a desired behavior
- an example for that behavior
describe "absolute" $ do
it "returns a positive number when given a negative number" $
absolute (-1) == 1specify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
specify is an alias for it.
describe :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
The describe function combines a list of specs into a larger spec.
context :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
context is an alias for describe.
example :: Expectation -> Expectation Source #
example is a type restricted version of id. It can be used to get better
error messages on type mismatches.
Compare e.g.
it "exposes some behavior" $ example $ do putStrLn
with
it "exposes some behavior" $ do putStrLn
parallel :: SpecWith a -> SpecWith a Source #
parallel marks all spec items of the given spec to be safe for parallel
evaluation.
runIO :: IO r -> SpecM a r Source #
Run an IO action while constructing the spec tree.
SpecM is a monad to construct a spec tree, without executing any spec
items. runIO allows you to run IO actions during this construction phase.
The IO action is always run when the spec tree is constructed (e.g. even
when --dry-run is specified).
If you do not need the result of the IO action to construct the spec tree,
beforeAll may be more suitable for your use case.
Pending spec items
During a test run a pending spec item is:
- not executed
- reported as "pending"
pending :: HasCallStack => Expectation Source #
pending can be used to mark a spec item as pending.
If you want to textually specify a behavior but do not have an example yet, use this:
describe "fancyFormatter" $ do
it "can format text in a way that everyone likes" $
pendingpendingWith :: HasCallStack => String -> Expectation Source #
pendingWith is similar to pending, but it takes an additional string
argument that can be used to specify the reason for why the spec item is pending.
xspecify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
xspecify is an alias for xit.
xcontext :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
xcontext is an alias for xdescribe.
Focused spec items
During a test run, when a spec contains focused spec items, all other spec items are ignored.
fit :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
fit is an alias for fmap focus . it
fspecify :: (HasCallStack, Example a) => String -> a -> SpecWith (Arg a) Source #
fspecify is an alias for fit.
fdescribe :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
fdescribe is an alias for fmap focus . describe
fcontext :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
fcontext is an alias for fdescribe.
Hooks
type ActionWith a = a -> IO () Source #
An IO action that expects an argument of type a
beforeWith :: (b -> IO a) -> SpecWith a -> SpecWith b Source #
Run a custom action before every spec item.
beforeAll_ :: IO () -> SpecWith a -> SpecWith a Source #
Run a custom action before the first spec item.
beforeAllWith :: (b -> IO a) -> SpecWith a -> SpecWith b Source #
Run a custom action with an argument before the first spec item.
after :: ActionWith a -> SpecWith a -> SpecWith a Source #
Run a custom action after every spec item.
afterAll :: HasCallStack => ActionWith a -> SpecWith a -> SpecWith a Source #
Run a custom action after the last spec item.
afterAll_ :: HasCallStack => IO () -> SpecWith a -> SpecWith a Source #
Run a custom action after the last spec item.
around :: (ActionWith a -> IO ()) -> SpecWith a -> Spec Source #
Run a custom action before and/or after every spec item.
around_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a Source #
Run a custom action before and/or after every spec item.
aroundWith :: (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b Source #
Run a custom action before and/or after every spec item.
aroundAll :: (ActionWith a -> IO ()) -> SpecWith a -> Spec Source #
Wrap an action around the given spec.
aroundAll_ :: (IO () -> IO ()) -> SpecWith a -> SpecWith a Source #
Wrap an action around the given spec.
aroundAllWith :: forall a b. (ActionWith a -> ActionWith b) -> SpecWith a -> SpecWith b Source #
Wrap an action around the given spec. Changes the arg type inside.
mapSubject :: (b -> a) -> SpecWith a -> SpecWith b Source #
Modify the subject under test.
Note that this resembles a contravariant functor on the first type parameter
of SpecM. This is because the subject is passed inwards, as an argument
to the spec item.
ignoreSubject :: SpecWith () -> SpecWith a Source #
Ignore the subject under test for a given spec.
Running a spec
hspec :: Spec -> IO () Source #
Run a given spec and write a report to stdout.
Exit with exitFailure if at least one spec item fails.
Note: hspec handles command-line options and reads config files. This
is not always desired. Use runSpec if you need more control over these
aspects.
hspec :: Spec -> IO () Source #
Run a given spec and write a report to stdout.
Exit with exitFailure if at least one spec item fails.
Note: hspec handles command-line options and reads config files. This
is not always desired. Use runSpec if you need more control over these
aspects.
class IsFormatter a where Source #
Methods
toFormatter :: a -> IO Formatter Source #
hspecWithFormatter :: IsFormatter a => a -> Spec -> IO () Source #
describe :: HasCallStack => String -> SpecWith a -> SpecWith a Source #
The describe function combines a list of specs into a larger spec.
hspecResult :: Spec -> IO Summary Source #
Run given spec and returns a summary of the test run.
Note: hspecResult does not exit with exitFailure on failing spec
items. If you need this, you have to check the Summary yourself and act
accordingly.