easytest-0.3: Simple, expressive testing library

Copyright(c) Joel Burget 2018-2019
LicenseMIT
Maintainerjoelburget@gmail.com
Stabilityexperimental
Safe HaskellNone
LanguageHaskell98

EasyTest.Internal

Contents

Description

This module defines the core internals and interface of easytest.

Synopsis

Structuring tests

tests :: [Test] -> Test Source #

Run a list of tests

scope :: String -> Test -> Test Source #

Label a test. Can be nested. A "." is placed between nested scopes, so scope "foo" . scope "bar" is equivalent to scope "foo.bar"

skip :: Test -> Test Source #

Explicitly skip this set of tests.

example :: HasCallStack => PropertyT IO () -> Test Source #

Run a unit test (same as unitTest). Example:

>>> run $ example $ 1 === 2
> ━━━ run ━━━
>   ✗ (unnamed) failed after 1 test.
>
>        ┏━━ tests/Suite.hs ━━━
>     26 ┃ main :: IO ()
>     27 ┃ main = do
>     28 ┃   run $ example $ 1 === (2 :: Int)
>        ┃   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>        ┃   │ Failed (- lhs =/= + rhs)
>        ┃   │ - 1
>        ┃   │ + 2
>
>     This failure can be reproduced by running:
>     > recheck (Size 0) (Seed 2914818620245020776 12314041441884757111) (unnamed)
>
>   ✗ 1 failed.

unitTest :: HasCallStack => PropertyT IO () -> Test Source #

Run a unit test (same as example). Example:

>>> run $ unitTest $ 1 === 2
> ━━━ run ━━━
>   ✗ (unnamed) failed after 1 test.
>
>        ┏━━ tests/Suite.hs ━━━
>     26 ┃ main :: IO ()
>     27 ┃ main = do
>     28 ┃   run $ unitTest $ 1 === (2 :: Int)
>        ┃   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>        ┃   │ Failed (- lhs =/= + rhs)
>        ┃   │ - 1
>        ┃   │ + 2
>
>     This failure can be reproduced by running:
>     > recheck (Size 0) (Seed 2914818620245020776 12314041441884757111) (unnamed)
>
>   ✗ 1 failed.

property :: HasCallStack => PropertyT IO () -> Test Source #

Run a property test. Example:

>>> run $ scope "list reversal" $ property $ do
>..   list <- forAll $ Gen.list @_ @Int (Range.linear 0 100)
>..     (Gen.element [0..100])
>..   reverse (reverse list) === list
> ━━━ run ━━━
>   ✓ list reversal passed 100 tests.
>   ✓ 1 succeeded.

propertyWith :: HasCallStack => PropertyConfig -> PropertyT IO () -> Test Source #

Run a property test with a custom configuration. This allows you to configure the propertyTestLimit, propertyDiscardLimit, propertyShrinkLimit, or propertyShrinkRetries. Example:

>>> run $ scope "list reversal" $ propertyWith (defaultConfig { propertyTestLimit = 500 }) $ do
>..   list <- forAll $ Gen.list @_ @Int (Range.linear 0 100)
>..     (Gen.element [0..100])
>..   reverse (reverse list) === list
> ━━━ run ━━━
>   ✓ list reversal passed 500 tests.
>   ✓ 1 succeeded.

Assertions for unit tests

matches :: HasCallStack => Prism' s a -> s -> PropertyT IO () Source #

Test whether a Prism matches. Example:

>>> main
> ━━━ run ━━━
>   ✓ (unnamed) passed 1 test.
>   ✗ (unnamed) failed after 1 test.
>
>        ┏━━ tests/Suite.hs ━━━
>     48 ┃ main :: IO ()
>     49 ┃ main = do
>     50 ┃   _ <- run $ tests
>     51 ┃     [ example $ matches _Left (Left 1   :: Either Int ())
>     52 ┃     , example $ matches _Left (Right () :: Either Int ())
>        ┃     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>     53 ┃     ]
>     54 ┃   pure ()
>
>     Prism failed to match
>
>     This failure can be reproduced by running:
>     > recheck (Size 0) (Seed 14003809197113786240 2614482618840800713) (unnamed)
>
>   ✗ 1 failed, 1 succeeded.

Use with _Just, _Nothing, _Left, _Right, or Control.Lens.Prism

doesn'tMatch :: HasCallStack => Prism' s a -> s -> PropertyT IO () Source #

Test whether a Prism doesn't match. Compare with matches.

pending :: String -> PropertyT IO () Source #

Mark a test as pending.

crash :: HasCallStack => String -> PropertyT IO () Source #

Record a failure with a given message

Running tests

run :: Test -> IO Summary Source #

Run all tests

runOnly :: String -> Test -> IO Summary Source #

Run all tests whose scope starts with the given prefix.

>>> runOnly "components.a" tests

rerun :: Seed -> Test -> IO Summary Source #

Rerun all tests with the given seed

>>> rerun (Seed 2914818620245020776 12314041441884757111) tests

rerunOnly :: String -> Seed -> Test -> IO Summary Source #

Rerun all tests with the given seed and whose scope starts with the given prefix

>>> rerunOnly "components.a" (Seed 2914818620245020776 12314041441884757111) tests

Bracketed tests (requiring setup / teardown)

bracket :: IO a -> (a -> IO ()) -> (a -> PropertyT IO ()) -> PropertyT IO () Source #

Make a test with setup and teardown steps.

bracket_ :: IO a -> IO b -> PropertyT IO () -> PropertyT IO () Source #

A variant of bracket where the return value from the setup step is not required.

finally :: PropertyT IO () -> IO a -> PropertyT IO () Source #

A specialised variant of bracket with just a teardown step.

Cabal test suite

cabalTestSuite :: IO Summary -> IO () Source #

Make this a cabal test suite for use with exitcode-stdio-1.0 test-suites.

This simply checks to see if any tests failed and if so exits with exitFailure.

Internal

type Prism s t a b = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t) Source #

A prism embodies one constructor of a sum type (as a lens embodies one part of a product type). See _Just, _Nothing, _Left, and _Right for examples. See Control.Lens.Prism for more explanation.

type Prism' s a = Prism s s a a Source #

A type-restricted prism. See Control.Lens.Prism for more explanation.

data TestType Source #

Unit- or property- test.

Constructors

Unit 
Prop PropertyConfig 

data Test Source #

A set of unit- and property-tests

Constructors

NamedTests ![(String, Test)]

A set of named (scoped) tests

Sequence ![Test]

A sequence of tests

Leaf !TestType !(PropertyT IO ())

An atomic unit- or property-test

Skipped !Test

A set of tests marked to skip

Hedgehog re-exports

data Property #

A property test, along with some configurable limits like how many times to run the test.

data PropertyT (m :: Type -> Type) a #

The property monad transformer allows both the generation of test inputs and the assertion of expectations.

Instances
Distributive PropertyT 
Instance details

Defined in Hedgehog.Internal.Property

Associated Types

type Transformer f PropertyT m :: Constraint #

Methods

distribute :: Transformer f PropertyT m => PropertyT (f m) a -> f (PropertyT m) a #

MonadTrans PropertyT 
Instance details

Defined in Hedgehog.Internal.Property

Methods

lift :: Monad m => m a -> PropertyT m a #

MonadBase b m => MonadBase b (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftBase :: b α -> PropertyT m α #

MonadState s m => MonadState s (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

get :: PropertyT m s #

put :: s -> PropertyT m () #

state :: (s -> (a, s)) -> PropertyT m a #

MonadReader r m => MonadReader r (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

ask :: PropertyT m r #

local :: (r -> r) -> PropertyT m a -> PropertyT m a #

reader :: (r -> a) -> PropertyT m a #

MonadError e m => MonadError e (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

throwError :: e -> PropertyT m a #

catchError :: PropertyT m a -> (e -> PropertyT m a) -> PropertyT m a #

Monad m => Monad (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

(>>=) :: PropertyT m a -> (a -> PropertyT m b) -> PropertyT m b #

(>>) :: PropertyT m a -> PropertyT m b -> PropertyT m b #

return :: a -> PropertyT m a #

fail :: String -> PropertyT m a #

Functor m => Functor (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

fmap :: (a -> b) -> PropertyT m a -> PropertyT m b #

(<$) :: a -> PropertyT m b -> PropertyT m a #

Monad m => Applicative (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

pure :: a -> PropertyT m a #

(<*>) :: PropertyT m (a -> b) -> PropertyT m a -> PropertyT m b #

liftA2 :: (a -> b -> c) -> PropertyT m a -> PropertyT m b -> PropertyT m c #

(*>) :: PropertyT m a -> PropertyT m b -> PropertyT m b #

(<*) :: PropertyT m a -> PropertyT m b -> PropertyT m a #

MonadIO m => MonadIO (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftIO :: IO a -> PropertyT m a #

MonadPlus m => Alternative (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

empty :: PropertyT m a #

(<|>) :: PropertyT m a -> PropertyT m a -> PropertyT m a #

some :: PropertyT m a -> PropertyT m [a] #

many :: PropertyT m a -> PropertyT m [a] #

MonadPlus m => MonadPlus (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

mzero :: PropertyT m a #

mplus :: PropertyT m a -> PropertyT m a -> PropertyT m a #

MonadCatch m => MonadCatch (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

catch :: Exception e => PropertyT m a -> (e -> PropertyT m a) -> PropertyT m a #

MonadThrow m => MonadThrow (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

throwM :: Exception e => e -> PropertyT m a #

Monad m => MonadTest (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> PropertyT m a #

PrimMonad m => PrimMonad (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Associated Types

type PrimState (PropertyT m) :: Type #

Methods

primitive :: (State# (PrimState (PropertyT m)) -> (#State# (PrimState (PropertyT m)), a#)) -> PropertyT m a #

MFunctor PropertyT 
Instance details

Defined in Hedgehog.Internal.Property

Methods

hoist :: Monad m => (forall a. m a -> n a) -> PropertyT m b -> PropertyT n b #

type Transformer t PropertyT m 
Instance details

Defined in Hedgehog.Internal.Property

type PrimState (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

class Monad m => MonadTest (m :: Type -> Type) #

Minimal complete definition

liftTest

Instances
MonadTest m => MonadTest (MaybeT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> MaybeT m a #

MonadTest m => MonadTest (ResourceT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> ResourceT m a #

Monad m => MonadTest (PropertyT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> PropertyT m a #

Monad m => MonadTest (TestT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> TestT m a #

MonadTest m => MonadTest (IdentityT m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> IdentityT m a #

MonadTest m => MonadTest (ExceptT x m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> ExceptT x m a #

(MonadTest m, Monoid w) => MonadTest (WriterT w m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> WriterT w m a #

MonadTest m => MonadTest (StateT s m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> StateT s m a #

MonadTest m => MonadTest (StateT s m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> StateT s m a #

(MonadTest m, Monoid w) => MonadTest (WriterT w m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> WriterT w m a #

MonadTest m => MonadTest (ReaderT r m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> ReaderT r m a #

MonadTest m => MonadTest (ContT r m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> ContT r m a #

(MonadTest m, Monoid w) => MonadTest (RWST r w s m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> RWST r w s m a #

(MonadTest m, Monoid w) => MonadTest (RWST r w s m) 
Instance details

Defined in Hedgehog.Internal.Property

Methods

liftTest :: Test a -> RWST r w s m a #

(===) :: (MonadTest m, Eq a, Show a, HasCallStack) => a -> a -> m () infix 4 #

Fails the test if the two arguments provided are not equal.

(/==) :: (MonadTest m, Eq a, Show a, HasCallStack) => a -> a -> m () infix 4 #

Fails the test if the two arguments provided are equal.

data Seed #

A splittable random number generator.

Instances
Eq Seed 
Instance details

Defined in Hedgehog.Internal.Seed

Methods

(==) :: Seed -> Seed -> Bool #

(/=) :: Seed -> Seed -> Bool #

Ord Seed 
Instance details

Defined in Hedgehog.Internal.Seed

Methods

compare :: Seed -> Seed -> Ordering #

(<) :: Seed -> Seed -> Bool #

(<=) :: Seed -> Seed -> Bool #

(>) :: Seed -> Seed -> Bool #

(>=) :: Seed -> Seed -> Bool #

max :: Seed -> Seed -> Seed #

min :: Seed -> Seed -> Seed #

Read Seed 
Instance details

Defined in Hedgehog.Internal.Seed

Show Seed 
Instance details

Defined in Hedgehog.Internal.Seed

Methods

showsPrec :: Int -> Seed -> ShowS #

show :: Seed -> String #

showList :: [Seed] -> ShowS #

RandomGen Seed 
Instance details

Defined in Hedgehog.Internal.Seed

Methods

next :: Seed -> (Int, Seed) #

genRange :: Seed -> (Int, Int) #

split :: Seed -> (Seed, Seed) #

data Summary #

A summary of all the properties executed.

Instances
Show Summary 
Instance details

Defined in Hedgehog.Internal.Report

Semigroup Summary 
Instance details

Defined in Hedgehog.Internal.Report

Monoid Summary 
Instance details

Defined in Hedgehog.Internal.Report

defaultConfig :: PropertyConfig #

The default configuration for a property test.