| Safe Haskell | Safe |
|---|---|
| Language | Haskell98 |
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.
- 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
Constructors
| Matcher | |
Fields
| |
Useful functions for running matchers
Basic Matchers
Matchers on lists
hasItem :: Matcher a -> Matcher [a] #
Matches if any of the items in the input list passes the provided matcher
Matchers on Ord
greaterThan :: (Ord a, Show a) => a -> Matcher a #
Matches if the input is greater than the required number
greaterThanOrEqual :: (Ord a, Show a) => a -> Matcher a #
Matches if the input is greater than or equal to the required number
lessThan :: (Ord a, Show a) => a -> Matcher a #
Matches if the input is less than the required number
lessThanOrEqual :: (Ord a, Show a) => a -> Matcher a #
Matches if the input is less than or equal to the required number
Matchers on Maybe
hasJust :: Matcher a -> Matcher (Maybe a) #
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) #
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) #
Matcher combinator: turns a Matcher a into a Matcher on the Left side of an Either a b
Matcher combinators
isNot :: Matcher a -> Matcher a #
Inverts a matcher, so success becomes failure, and failure becomes success
on :: Matcher b -> (a -> b, String) -> Matcher a #
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 a #
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 -> String #
A standard mismatch description on (Show a): standardMismatch 1 == "was 1"