rematch-0.1.1.0: 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)

hasJust :: Matcher a -> Matcher (Maybe a)Source

Matcher combinator, turns Matcher a to Matcher (Maybe a) Fails if the Maybe is Nothing, otherwise tries the original matcher on the content of the Maybe

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

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

Matcher combinator: turns a Matcher b into a Matcher on the Right side of an Either a b

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

Matches if an Either is Left

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

Matcher combinator: turns a Matcher a into a Matcher on the Left side of an Either a b

Matcher combinators

isNot :: 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

on :: Matcher b -> (a -> b, String) -> Matcher aSource

A combinator that translates Matcher a to Matcher b using a function :: (a -> b) Takes a name of the function for better error messages

Using this as an infix operator gets you some nice syntax: expect ((is 1) on (length, length)) []

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