skeletest-0.1.0: Batteries-included, opinionated test framework
Safe HaskellNone
LanguageGHC2021

Skeletest.Predicate

Synopsis

Documentation

data Predicate (m :: Type -> Type) a Source #

General

anything :: forall (m :: Type -> Type) a. Monad m => Predicate m a Source #

Ord

eq :: forall a (m :: Type -> Type). (Eq a, Monad m) => a -> Predicate m a Source #

gt :: forall a (m :: Type -> Type). (Ord a, Monad m) => a -> Predicate m a Source #

gte :: forall a (m :: Type -> Type). (Ord a, Monad m) => a -> Predicate m a Source #

lt :: forall a (m :: Type -> Type). (Ord a, Monad m) => a -> Predicate m a Source #

lte :: forall a (m :: Type -> Type). (Ord a, Monad m) => a -> Predicate m a Source #

Data types

just :: forall (m :: Type -> Type) a. Monad m => Predicate m a -> Predicate m (Maybe a) Source #

nothing :: forall (m :: Type -> Type) a. Monad m => Predicate m (Maybe a) Source #

left :: forall (m :: Type -> Type) a b. Monad m => Predicate m a -> Predicate m (Either a b) Source #

right :: forall (m :: Type -> Type) b a. Monad m => Predicate m b -> Predicate m (Either a b) Source #

tup :: forall a (m :: Type -> Type). (IsPredTuple m a, Monad m) => ToPredTuple m a -> Predicate m a Source #

Matches a tuple satisfying the given predicates. Works for tuples up to 6 elements.

P.tup (P.eq 1, P.gt 2, P.hasPrefix "hello ")

con :: forall a (m :: Type -> Type). a -> Predicate m a Source #

A predicate for checking that a value matches the given constructor.

It takes one argument, which is the constructor, except with all fields taking a Predicate instead of the normal value. Skeletest will rewrite the expression so it typechecks correctly.

>>> user `shouldSatisfy` P.con User{name = P.eq "user1", email = P.contains "@"}

Record fields that are omitted are not checked at all; i.e. P.con Foo{} and P.con Foo{a = P.anything} are equivalent.

Numeric

approx :: forall a (m :: Type -> Type). (Fractional a, Ord a, Monad m) => Tolerance -> a -> Predicate m a Source #

A predicate for checking that a value is equal within some tolerance.

Useful for checking equality with floats, which might not be exactly equal. For more information, see: https://jvns.ca/blog/2023/01/13/examples-of-floating-point-problems/.

>>> (0.1 + 0.2) `shouldSatisfy` P.approx P.tol 0.3
>>> (0.1 + 0.2) `shouldSatisfy` P.approx P.tol{P.rel = Just 1e-6} 0.3
>>> (0.1 + 0.2) `shouldSatisfy` P.approx P.tol{P.abs = 1e-12} 0.3
>>> (0.1 + 0.2) `shouldSatisfy` P.approx P.tol{P.rel = Just 1e-6, P.abs = 1e-12} 0.3
>>> (0.1 + 0.2) `shouldSatisfy` P.approx P.tol{P.rel = Nothing} 0.3
>>> (0.1 + 0.2) `shouldSatisfy` P.approx P.tol{P.rel = Nothing, P.abs = 1e-12} 0.3

data Tolerance Source #

Constructors

Tolerance 

Fields

Combinators

(<<<) :: forall (m :: Type -> Type) a b. Monad m => Predicate m a -> (b -> a) -> Predicate m b infixr 1 Source #

(>>>) :: forall (m :: Type -> Type) b a. Monad m => (b -> a) -> Predicate m a -> Predicate m b infixr 1 Source #

not :: forall (m :: Type -> Type) a. Monad m => Predicate m a -> Predicate m a Source #

(&&) :: forall (m :: Type -> Type) a. Monad m => Predicate m a -> Predicate m a -> Predicate m a Source #

(||) :: forall (m :: Type -> Type) a. Monad m => Predicate m a -> Predicate m a -> Predicate m a Source #

and :: forall (m :: Type -> Type) a. Monad m => [Predicate m a] -> Predicate m a Source #

or :: forall (m :: Type -> Type) a. Monad m => [Predicate m a] -> Predicate m a Source #

Containers

any :: forall t (m :: Type -> Type) a. (Foldable t, Monad m) => Predicate m a -> Predicate m (t a) Source #

all :: forall t (m :: Type -> Type) a. (Foldable t, Monad m) => Predicate m a -> Predicate m (t a) Source #

elem :: forall a t (m :: Type -> Type). (Eq a, Foldable t, Monad m) => a -> Predicate m (t a) Source #

Subsequences

class HasSubsequences a where Source #

Methods

isPrefixOf :: a -> a -> Bool Source #

isInfixOf :: a -> a -> Bool Source #

isSuffixOf :: a -> a -> Bool Source #

Instances

Instances details
HasSubsequences Text Source # 
Instance details

Defined in Skeletest.Internal.Predicate

Eq a => HasSubsequences [a] Source # 
Instance details

Defined in Skeletest.Internal.Predicate

Methods

isPrefixOf :: [a] -> [a] -> Bool Source #

isInfixOf :: [a] -> [a] -> Bool Source #

isSuffixOf :: [a] -> [a] -> Bool Source #

hasPrefix :: forall a (m :: Type -> Type). (HasSubsequences a, Monad m) => a -> Predicate m a Source #

hasInfix :: forall a (m :: Type -> Type). (HasSubsequences a, Monad m) => a -> Predicate m a Source #

hasSuffix :: forall a (m :: Type -> Type). (HasSubsequences a, Monad m) => a -> Predicate m a Source #

IO

returns :: MonadIO m => Predicate m a -> Predicate m (m a) Source #

Functions

(===) :: (a -> b) -> (a -> b) -> IsoChecker a b infix 2 Source #

Verify if two functions are isomorphic.

prop "reverse . reverse === id" $ do
  let genList = Gen.list (Gen.linear 0 10) $ Gen.int (Gen.linear 0 1000)
  (reverse . reverse) === id `shouldSatisfy` P.isoWith genList

Snapshot testing

matchesSnapshot :: forall a (m :: Type -> Type). (Typeable a, MonadIO m) => Predicate m a Source #