rematch-0.1.0.2: A simple api for matchers

Safe HaskellNone

Control.Rematch

Contents

Description

This module defines an api for matchers: rules that can pass or fail, and describe their failure and success conditions for humans to read.

This module also exports some useful matchers for things in the Prelude, and some combinators that are useful for combining several matchers into one.

Synopsis

Documentation

data Matcher a Source

The basic api for a matcher

Constructors

Matcher 

Fields

match :: a -> Bool

A function that returns True if the matcher should pass, False if it should fail

description :: String

A description of the matcher (usually of its success conditions)

describeMismatch :: a -> String

A description to be shown if the match fails.

Useful functions for running matchers

expect :: a -> Matcher a -> AssertionSource

Run a matcher as an HUnit assertion

Example output:

 Expected:
 equalTo "a"
 but:  was "b"

runMatch :: Matcher a -> a -> MatchSource

Run a matcher, producing a Match with a good error string

Basic Matchers

is :: (Show a, Eq a) => a -> Matcher aSource

Matcher on equality

equalTo :: (Show a, Eq a) => a -> Matcher aSource

Matcher on equality

Matchers on lists

isEmpty :: Show a => Matcher [a]Source

Matches if the input list is empty

hasSize :: Show a => Int -> Matcher [a]Source

Matches if the input list has the required size

everyItem :: Matcher a -> Matcher [a]Source

Matches if every item in the input list passes a matcher

hasItem :: Matcher a -> Matcher [a]Source

Matches if any of the items in the input list passes the provided matcher

Matchers on Ord

greaterThan :: (Ord a, Show a) => a -> Matcher aSource

Matches if the input is greater than the required number

greaterThanOrEqual :: (Ord a, Show a) => a -> Matcher aSource

Matches if the input is greater than or equal to the required number

lessThan :: (Ord a, Show a) => a -> Matcher aSource

Matches if the input is less than the required number

lessThanOrEqual :: (Ord a, Show a) => a -> Matcher aSource

Matches if the input is less than or equal to the required number

Matchers on Maybe

isJust :: Show a => Matcher (Maybe a)Source

Matches if the input is (Just a)

isNothing :: Show a => Matcher (Maybe a)Source

Matches if the input is Nothing

Matchers on Either

isRight :: (Show a, Show b) => Matcher (Either a b)Source

Matches if an Either is Right

isLeft :: (Show a, Show b) => Matcher (Either a b)Source

Matches if an Either is Left

Matcher combinators

no :: Matcher a -> Matcher aSource

Inverts a matcher, so success becomes failure, and failure becomes success

allOf :: [Matcher a] -> Matcher aSource

Matches if all of a list of matchers pass

anyOf :: [Matcher a] -> Matcher aSource

Matches if any of a list of matchers pass

Utility functions for writing your own matchers

matcherOn :: Show a => String -> (a -> a -> Bool) -> a -> Matcher aSource

Builds a Matcher a out of a name and a function from (a -> a -> Bool) Succeeds if the function returns true, fails if the function returns false

matchList :: [Matcher a] -> a -> [Bool]Source

Utility function for running a list of matchers

standardMismatch :: Show a => a -> StringSource

A standard mismatch description on (Show a): standardMismatch 1 == was 1