Test.Hspec.Core.Formatters

hspec-core-2.4.3: A Testing Framework for Haskell

Stability | experimental
Safe Haskell | None
Language | Haskell2010

Test.Hspec.Core.Formatters

Contents

- Formatters
- Implementing a custom Formatter
  - Accessing the runner state
  - Appending to the gerenated report
  - Dealing with colors
  - Helpers

Description

This module contains formatters that can be used with `hspecWith`.

Synopsis

- silent :: Formatter
- specdoc :: Formatter
- progress :: Formatter
- failed_examples :: Formatter
- data Formatter = Formatter {
  - headerFormatter :: FormatM ()
  - exampleGroupStarted :: [String] -> String -> FormatM ()
  - exampleGroupDone :: FormatM ()
  - exampleProgress :: Handle -> Path -> Progress -> IO ()
  - exampleSucceeded :: Path -> FormatM ()
  - exampleFailed :: Path -> Either SomeException FailureReason -> FormatM ()
  - examplePending :: Path -> Maybe String -> FormatM ()
  - failedFormatter :: FormatM ()
  - footerFormatter :: FormatM ()
  }
- data FailureReason
  - = NoReason
  - | Reason String
  - | ExpectedButGot (Maybe String) String String
- type FormatM = Free FormatF
- getSuccessCount :: FormatM Int
- getPendingCount :: FormatM Int
- getFailCount :: FormatM Int
- getTotalCount :: FormatM Int
- data FailureRecord = FailureRecord {
  - failureRecordLocation :: Maybe Location
  - failureRecordPath :: Path
  - failureRecordMessage :: Either SomeException FailureReason
  }
- getFailMessages :: FormatM [FailureRecord]
- usedSeed :: FormatM Integer
- getCPUTime :: FormatM (Maybe Double)
- getRealTime :: FormatM Double
- write :: String -> FormatM ()
- writeLine :: String -> FormatM ()
- withInfoColor :: FormatM a -> FormatM a
- withSuccessColor :: FormatM a -> FormatM a
- withPendingColor :: FormatM a -> FormatM a
- withFailColor :: FormatM a -> FormatM a
- extraChunk :: String -> FormatM ()
- missingChunk :: String -> FormatM ()
- formatException :: SomeException -> String

# Formatters

silent :: Formatter #

specdoc :: Formatter #

progress :: Formatter #

failed_examples :: Formatter #

# Implementing a custom Formatter

A formatter is a set of actions. Each action is evaluated when a certain situation is encountered during a test run.

Actions live in the `FormatM` monad. It provides access to the runner state and primitives for appending to the generated report.

data Formatter #

Constructors

Formatter |
Fields headerFormatter :: FormatM () exampleGroupStarted :: [String] -> String -> FormatM () evaluated before each test group The given number indicates the position within the parent group. exampleGroupDone :: FormatM () exampleProgress :: Handle -> Path -> Progress -> IO () used to notify the progress of the currently evaluated example Note: This is only called when interactive/color mode. exampleSucceeded :: Path -> FormatM () evaluated after each successful example exampleFailed :: Path -> Either SomeException FailureReason -> FormatM () evaluated after each failed example examplePending :: Path -> Maybe String -> FormatM () evaluated after each pending example failedFormatter :: FormatM () evaluated after a test run footerFormatter :: FormatM () evaluated after `failuresFormatter`

data FailureReason #

Constructors

NoReason |
Reason String |
ExpectedButGot (Maybe String) String String |

Instances

Eq FailureReason # |
Methods (==) :: FailureReason -> FailureReason -> Bool # (/=) :: FailureReason -> FailureReason -> Bool #
Read FailureReason # |
Methods readsPrec :: Int -> ReadS FailureReason # readList :: ReadS [FailureReason] # readPrec :: ReadPrec FailureReason # readListPrec :: ReadPrec [FailureReason] #
Show FailureReason # |
Methods showsPrec :: Int -> FailureReason -> ShowS # show :: FailureReason -> String # showList :: [FailureReason] -> ShowS #
NFData FailureReason # |
Methods rnf :: FailureReason -> () #

type FormatM = Free FormatF #

## Accessing the runner state

getSuccessCount :: FormatM Int #

Get the number of successful examples encountered so far.

getPendingCount :: FormatM Int #

Get the number of pending examples encountered so far.

getFailCount :: FormatM Int #

Get the number of failed examples encountered so far.

getTotalCount :: FormatM Int #

Get the total number of examples encountered so far.

data FailureRecord #

Constructors

FailureRecord |
Fields failureRecordLocation :: Maybe Location failureRecordPath :: Path failureRecordMessage :: Either SomeException FailureReason

getFailMessages :: FormatM [FailureRecord] #

Get the list of accumulated failure messages.

usedSeed :: FormatM Integer #

The random seed that is used for QuickCheck.

getCPUTime :: FormatM (Maybe Double) #

Get the used CPU time since the test run has been started.

getRealTime :: FormatM Double #

Get the passed real time since the test run has been started.

## Appending to the gerenated report

write :: String -> FormatM () #

Append some output to the report.

writeLine :: String -> FormatM () #

The same as `write`, but adds a newline character.

## Dealing with colors

withInfoColor :: FormatM a -> FormatM a #

Set output color to cyan, run given action, and finally restore the default color.

withSuccessColor :: FormatM a -> FormatM a #

Set output color to green, run given action, and finally restore the default color.

withPendingColor :: FormatM a -> FormatM a #

Set output color to yellow, run given action, and finally restore the default color.

withFailColor :: FormatM a -> FormatM a #

Set output color to red, run given action, and finally restore the default color.

extraChunk :: String -> FormatM () #

Output given chunk in red.

missingChunk :: String -> FormatM () #

Output given chunk in green.

## Helpers

formatException :: SomeException -> String #

The function `formatException` converts an exception to a string.

This is different from `show`. The type of the exception is included, e.g.:

    >>> formatException (toException DivideByZero)
    "ArithException (divide by zero)"

For `IOException`s the `IOErrorType` is included, as well.