Safe Haskell | Safe-Inferred |
---|
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.
- data Matcher a = Matcher {
- match :: a -> Bool
- description :: String
- describeMismatch :: a -> String
- runMatch :: Matcher a -> a -> Match
- is :: (Show a, Eq a) => a -> Matcher a
- equalTo :: (Show a, Eq a) => a -> Matcher a
- isEmpty :: Show a => Matcher [a]
- hasSize :: Show a => Int -> Matcher [a]
- everyItem :: Matcher a -> Matcher [a]
- hasItem :: Matcher a -> Matcher [a]
- greaterThan :: (Ord a, Show a) => a -> Matcher a
- greaterThanOrEqual :: (Ord a, Show a) => a -> Matcher a
- lessThan :: (Ord a, Show a) => a -> Matcher a
- lessThanOrEqual :: (Ord a, Show a) => a -> Matcher a
- isJust :: Show a => Matcher (Maybe a)
- hasJust :: Matcher a -> Matcher (Maybe a)
- isNothing :: Show a => Matcher (Maybe a)
- isRight :: (Show a, Show b) => Matcher (Either a b)
- hasRight :: (Show a, Show b) => Matcher b -> Matcher (Either a b)
- isLeft :: (Show a, Show b) => Matcher (Either a b)
- hasLeft :: (Show a, Show b) => Matcher a -> Matcher (Either a b)
- isNot :: Matcher a -> Matcher a
- allOf :: [Matcher a] -> Matcher a
- anyOf :: [Matcher a] -> Matcher a
- on :: Matcher b -> (a -> b, String) -> Matcher a
- matcherOn :: Show a => String -> (a -> a -> Bool) -> a -> Matcher a
- matchList :: [Matcher a] -> a -> [Bool]
- standardMismatch :: Show a => a -> String
Documentation
The basic api for a matcher
Matcher | |
|
Useful functions for running matchers
Basic Matchers
Matchers on lists
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
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
Matchers on Either
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
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
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
standardMismatch :: Show a => a -> StringSource
A standard mismatch description on (Show a): standardMismatch 1 == was 1