{-# options_haddock hide #-}

module Polysemy.Test.Hedgehog where

import qualified Control.Monad.Trans.Writer.Lazy as MTL
import qualified Hedgehog as Native
import Hedgehog.Internal.Property (Failure, Journal, TestT(TestT), failWith)
import Polysemy.Writer (Writer, tell)

import qualified Polysemy.Test.Data.Hedgehog as Hedgehog
import Polysemy.Test.Data.Hedgehog (Hedgehog, liftH)

-- |Interpret 'Hedgehog' into @'TestT' IO@ by simple embedding of the native combinators.
interpretHedgehog ::
  Member (Embed (TestT m)) r =>
  InterpreterFor (Hedgehog m) r
interpretHedgehog :: InterpreterFor (Hedgehog m) r
interpretHedgehog =
  (forall x (rInitial :: EffectRow).
 Hedgehog m (Sem rInitial) x -> Sem r x)
-> Sem (Hedgehog m : r) a -> Sem r a
forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: EffectRow). e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret \case
    Hedgehog.LiftH t ->
      TestT m x -> Sem r x
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed TestT m x
t

-- |Interpret 'Hedgehog' in terms of @'Error' 'Failure'@ and @'Writer' 'Journal'@, which correspond to the monad stack
-- wrapped by 'TestT'.
rewriteHedgehog ::
  Members [Error Failure, Writer Journal, Embed m] r =>
  InterpreterFor (Hedgehog m) r
rewriteHedgehog :: InterpreterFor (Hedgehog m) r
rewriteHedgehog =
  (forall x (rInitial :: EffectRow).
 Hedgehog m (Sem rInitial) x -> Sem r x)
-> Sem (Hedgehog m : r) a -> Sem r a
forall (e :: Effect) (r :: EffectRow) a.
FirstOrder e "interpret" =>
(forall x (rInitial :: EffectRow). e (Sem rInitial) x -> Sem r x)
-> Sem (e : r) a -> Sem r a
interpret \case
    Hedgehog.LiftH (TestT t) -> do
      (Either Failure x
result, Journal
journal) <- m (Either Failure x, Journal) -> Sem r (Either Failure x, Journal)
forall (m :: * -> *) (r :: EffectRow) a.
Member (Embed m) r =>
m a -> Sem r a
embed (WriterT Journal m (Either Failure x)
-> m (Either Failure x, Journal)
forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
MTL.runWriterT (ExceptT Failure (WriterT Journal m) x
-> WriterT Journal m (Either Failure x)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT ExceptT Failure (WriterT Journal m) x
t))
      Journal -> Sem r ()
forall o (r :: EffectRow).
MemberWithError (Writer o) r =>
o -> Sem r ()
tell Journal
journal
      Either Failure x -> Sem r x
forall e (r :: EffectRow) a.
Member (Error e) r =>
Either e a -> Sem r a
fromEither Either Failure x
result

-- |Embeds 'Hedgehog.assert'.
assert ::
   m r .
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  Bool ->
  Sem r ()
assert :: Bool -> Sem r ()
assert Bool
a =
  (HasCallStack => Sem r ()) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r ()) -> Sem r ())
-> (HasCallStack => Sem r ()) -> Sem r ()
forall a b. (a -> b) -> a -> b
$ TestT m () -> Sem r ()
forall (m :: * -> *) a (r :: EffectRow).
Member (Hedgehog m) r =>
TestT m a -> Sem r a
liftH (Bool -> TestT m ()
forall (m :: * -> *). (MonadTest m, HasCallStack) => Bool -> m ()
Native.assert Bool
a)

infix 4 ===

-- |Embeds 'Hedgehog.==='.
--
-- >>> 5 === 6
-- 5 === 6
-- ^^^^^^^
-- │ ━━━ Failed (- lhs) (+ rhs) ━━━
-- │ - 5
-- │ + 6
(===) ::
   a m r .
  Monad m =>
  Eq a =>
  Show a =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  a ->
  Sem r ()
a
a === :: a -> a -> Sem r ()
=== a
b =
  (HasCallStack => Sem r ()) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r ()) -> Sem r ())
-> (HasCallStack => Sem r ()) -> Sem r ()
forall a b. (a -> b) -> a -> b
$ TestT m () -> Sem r ()
forall (m :: * -> *) a (r :: EffectRow).
Member (Hedgehog m) r =>
TestT m a -> Sem r a
liftH (a
a a -> a -> TestT m ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
Native.=== a
b)

infix 4 /==

-- |Prefix variant of '(===)'.
assertEq ::
   a m r .
  Monad m =>
  Eq a =>
  Show a =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  a ->
  Sem r ()
assertEq :: a -> a -> Sem r ()
assertEq a
a a
b =
  (HasCallStack => Sem r ()) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r ()) -> Sem r ())
-> (HasCallStack => Sem r ()) -> Sem r ()
forall a b. (a -> b) -> a -> b
$ TestT m () -> Sem r ()
forall (m :: * -> *) a (r :: EffectRow).
Member (Hedgehog m) r =>
TestT m a -> Sem r a
liftH (a
a a -> a -> TestT m ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
Native.=== a
b)

-- |Embeds 'Hedgehog./=='.
--
-- >>> 5 /== 5
-- 5 /== 5
-- ^^^^^^^
-- │ ━━━ Failed (no differences) ━━━
-- │ 5
(/==) ::
   a m r .
  Monad m =>
  Eq a =>
  Show a =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  a ->
  Sem r ()
a
a /== :: a -> a -> Sem r ()
/== a
b =
  (HasCallStack => Sem r ()) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r ()) -> Sem r ())
-> (HasCallStack => Sem r ()) -> Sem r ()
forall a b. (a -> b) -> a -> b
$ TestT m () -> Sem r ()
forall (m :: * -> *) a (r :: EffectRow).
Member (Hedgehog m) r =>
TestT m a -> Sem r a
liftH (a
a a -> a -> TestT m ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
Native./== a
b)

-- |Prefix variant of '(===)'.
assertNeq ::
   a m r .
  Monad m =>
  Eq a =>
  Show a =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  a ->
  Sem r ()
assertNeq :: a -> a -> Sem r ()
assertNeq a
a a
b =
  (HasCallStack => Sem r ()) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r ()) -> Sem r ())
-> (HasCallStack => Sem r ()) -> Sem r ()
forall a b. (a -> b) -> a -> b
$ TestT m () -> Sem r ()
forall (m :: * -> *) a (r :: EffectRow).
Member (Hedgehog m) r =>
TestT m a -> Sem r a
liftH (a
a a -> a -> TestT m ()
forall (m :: * -> *) a.
(MonadTest m, Eq a, Show a, HasCallStack) =>
a -> a -> m ()
Native./== a
b)

-- |Embeds 'Hedgehog.evalEither'.
evalEither ::
   a m e r .
  Show e =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  Either e a ->
  Sem r a
evalEither :: Either e a -> Sem r a
evalEither Either e a
e =
  (HasCallStack => Sem r a) -> Sem r a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r a) -> Sem r a)
-> (HasCallStack => Sem r a) -> Sem r a
forall a b. (a -> b) -> a -> b
$ TestT m a -> Sem r a
forall (m :: * -> *) a (r :: EffectRow).
Member (Hedgehog m) r =>
TestT m a -> Sem r a
liftH (Either e a -> TestT m a
forall (m :: * -> *) x a.
(MonadTest m, Show x, HasCallStack) =>
Either x a -> m a
Native.evalEither Either e a
e)

-- |Given a reference value, unpacks an 'Either' with 'evalEither' and applies '===' to the result in the
-- 'Right' case, and produces a test failure in the 'Left' case.
assertRight ::
   a m e r .
  Eq a =>
  Show e =>
  Show a =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  Either e a ->
  Sem r ()
assertRight :: a -> Either e a -> Sem r ()
assertRight a
a =
  (HasCallStack => Either e a -> Sem r ()) -> Either e a -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Either e a -> Sem r ())
 -> Either e a -> Sem r ())
-> (HasCallStack => Either e a -> Sem r ())
-> Either e a
-> Sem r ()
forall a b. (a -> b) -> a -> b
$ (a
a a -> a -> Sem r ()
forall a (m :: * -> *) (r :: EffectRow).
(Monad m, Eq a, Show a, HasCallStack, Member (Hedgehog m) r) =>
a -> a -> Sem r ()
===) (a -> Sem r ())
-> (Either e a -> Sem r a) -> Either e a -> Sem r ()
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Either e a -> Sem r a
forall a (m :: * -> *) e (r :: EffectRow).
(Show e, Monad m, HasCallStack, Member (Hedgehog m) r) =>
Either e a -> Sem r a
evalEither

-- |Like 'assertRight', but for two nested Eithers.
assertRight2 ::
   a m e1 e2 r .
  Eq a =>
  Show e1 =>
  Show e2 =>
  Show a =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  Either e1 (Either e2 a) ->
  Sem r ()
assertRight2 :: a -> Either e1 (Either e2 a) -> Sem r ()
assertRight2 a
a =
  (HasCallStack => Either e1 (Either e2 a) -> Sem r ())
-> Either e1 (Either e2 a) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Either e1 (Either e2 a) -> Sem r ())
 -> Either e1 (Either e2 a) -> Sem r ())
-> (HasCallStack => Either e1 (Either e2 a) -> Sem r ())
-> Either e1 (Either e2 a)
-> Sem r ()
forall a b. (a -> b) -> a -> b
$ a -> Either e2 a -> Sem r ()
forall a (m :: * -> *) e (r :: EffectRow).
(Eq a, Show e, Show a, Monad m, HasCallStack,
 Member (Hedgehog m) r) =>
a -> Either e a -> Sem r ()
assertRight a
a (Either e2 a -> Sem r ())
-> (Either e1 (Either e2 a) -> Sem r (Either e2 a))
-> Either e1 (Either e2 a)
-> Sem r ()
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Either e1 (Either e2 a) -> Sem r (Either e2 a)
forall a (m :: * -> *) e (r :: EffectRow).
(Show e, Monad m, HasCallStack, Member (Hedgehog m) r) =>
Either e a -> Sem r a
evalEither

-- |Like 'assertRight', but for three nested Eithers.
assertRight3 ::
   a m e1 e2 e3 r .
  Eq a =>
  Show e1 =>
  Show e2 =>
  Show e3 =>
  Show a =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  Either e1 (Either e2 (Either e3 a)) ->
  Sem r ()
assertRight3 :: a -> Either e1 (Either e2 (Either e3 a)) -> Sem r ()
assertRight3 a
a =
  (HasCallStack => Either e1 (Either e2 (Either e3 a)) -> Sem r ())
-> Either e1 (Either e2 (Either e3 a)) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Either e1 (Either e2 (Either e3 a)) -> Sem r ())
 -> Either e1 (Either e2 (Either e3 a)) -> Sem r ())
-> (HasCallStack =>
    Either e1 (Either e2 (Either e3 a)) -> Sem r ())
-> Either e1 (Either e2 (Either e3 a))
-> Sem r ()
forall a b. (a -> b) -> a -> b
$ a -> Either e2 (Either e3 a) -> Sem r ()
forall a (m :: * -> *) e1 e2 (r :: EffectRow).
(Eq a, Show e1, Show e2, Show a, Monad m, HasCallStack,
 Member (Hedgehog m) r) =>
a -> Either e1 (Either e2 a) -> Sem r ()
assertRight2 a
a (Either e2 (Either e3 a) -> Sem r ())
-> (Either e1 (Either e2 (Either e3 a))
    -> Sem r (Either e2 (Either e3 a)))
-> Either e1 (Either e2 (Either e3 a))
-> Sem r ()
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Either e1 (Either e2 (Either e3 a))
-> Sem r (Either e2 (Either e3 a))
forall a (m :: * -> *) e (r :: EffectRow).
(Show e, Monad m, HasCallStack, Member (Hedgehog m) r) =>
Either e a -> Sem r a
evalEither

-- |Like 'evalEither', but for 'Left'.
evalLeft ::
   a m e r .
  Show a =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  Either e a ->
  Sem r e
evalLeft :: Either e a -> Sem r e
evalLeft = \case
  Right a
a ->
    (HasCallStack => Sem r e) -> Sem r e
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r e) -> Sem r e)
-> (HasCallStack => Sem r e) -> Sem r e
forall a b. (a -> b) -> a -> b
$ TestT m e -> Sem r e
forall (m :: * -> *) a (r :: EffectRow).
Member (Hedgehog m) r =>
TestT m a -> Sem r a
liftH (TestT m e -> Sem r e) -> TestT m e -> Sem r e
forall a b. (a -> b) -> a -> b
$ Maybe Diff -> String -> TestT m e
forall (m :: * -> *) a.
(MonadTest m, HasCallStack) =>
Maybe Diff -> String -> m a
failWith Maybe Diff
forall a. Maybe a
Nothing (String -> TestT m e) -> String -> TestT m e
forall a b. (a -> b) -> a -> b
$ a -> String
forall b a. (Show a, IsString b) => a -> b
show a
a
  Left e
e ->
    e -> Sem r e
forall (f :: * -> *) a. Applicative f => a -> f a
pure e
e

-- |Like 'assertRight', but for 'Left'.
assertLeft ::
   a m e r .
  Eq e =>
  Show e =>
  Show a =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  e ->
  Either e a ->
  Sem r ()
assertLeft :: e -> Either e a -> Sem r ()
assertLeft e
e =
  (HasCallStack => Either e a -> Sem r ()) -> Either e a -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Either e a -> Sem r ())
 -> Either e a -> Sem r ())
-> (HasCallStack => Either e a -> Sem r ())
-> Either e a
-> Sem r ()
forall a b. (a -> b) -> a -> b
$ (e
e e -> e -> Sem r ()
forall a (m :: * -> *) (r :: EffectRow).
(Monad m, Eq a, Show a, HasCallStack, Member (Hedgehog m) r) =>
a -> a -> Sem r ()
===) (e -> Sem r ())
-> (Either e a -> Sem r e) -> Either e a -> Sem r ()
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Either e a -> Sem r e
forall a (m :: * -> *) e (r :: EffectRow).
(Show a, Monad m, HasCallStack, Member (Hedgehog m) r) =>
Either e a -> Sem r e
evalLeft

data ValueIsNothing =
  ValueIsNothing
  deriving Int -> ValueIsNothing -> ShowS
[ValueIsNothing] -> ShowS
ValueIsNothing -> String
(Int -> ValueIsNothing -> ShowS)
-> (ValueIsNothing -> String)
-> ([ValueIsNothing] -> ShowS)
-> Show ValueIsNothing
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ValueIsNothing] -> ShowS
$cshowList :: [ValueIsNothing] -> ShowS
show :: ValueIsNothing -> String
$cshow :: ValueIsNothing -> String
showsPrec :: Int -> ValueIsNothing -> ShowS
$cshowsPrec :: Int -> ValueIsNothing -> ShowS
Show

-- |Like 'evalEither', but for 'Maybe'.
evalMaybe ::
   a m r .
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  Maybe a ->
  Sem r a
evalMaybe :: Maybe a -> Sem r a
evalMaybe Maybe a
ma =
  (HasCallStack => Sem r a) -> Sem r a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r a) -> Sem r a)
-> (HasCallStack => Sem r a) -> Sem r a
forall a b. (a -> b) -> a -> b
$ Either ValueIsNothing a -> Sem r a
forall a (m :: * -> *) e (r :: EffectRow).
(Show e, Monad m, HasCallStack, Member (Hedgehog m) r) =>
Either e a -> Sem r a
evalEither (ValueIsNothing -> Maybe a -> Either ValueIsNothing a
forall l r. l -> Maybe r -> Either l r
maybeToRight ValueIsNothing
ValueIsNothing Maybe a
ma)

-- |Given a reference value, asserts that the scrutinee is 'Just' and its contained value matches the target.
assertJust ::
   a m r .
  Eq a =>
  Show a =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  Maybe a ->
  Sem r ()
assertJust :: a -> Maybe a -> Sem r ()
assertJust a
target Maybe a
ma =
  (HasCallStack => Sem r ()) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r ()) -> Sem r ())
-> (HasCallStack => Sem r ()) -> Sem r ()
forall a b. (a -> b) -> a -> b
$ a -> Either ValueIsNothing a -> Sem r ()
forall a (m :: * -> *) e (r :: EffectRow).
(Eq a, Show e, Show a, Monad m, HasCallStack,
 Member (Hedgehog m) r) =>
a -> Either e a -> Sem r ()
assertRight a
target (ValueIsNothing -> Maybe a -> Either ValueIsNothing a
forall l r. l -> Maybe r -> Either l r
maybeToRight ValueIsNothing
ValueIsNothing Maybe a
ma)

-- |Run a Polysemy 'Error' effect and assert its result.
evalError ::
   e a m r .
  Show e =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  Sem (Error e : r) a ->
  Sem r a
evalError :: Sem (Error e : r) a -> Sem r a
evalError Sem (Error e : r) a
sem =
  (HasCallStack => Sem r a) -> Sem r a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r a) -> Sem r a)
-> (HasCallStack => Sem r a) -> Sem r a
forall a b. (a -> b) -> a -> b
$ Either e a -> Sem r a
forall a (m :: * -> *) e (r :: EffectRow).
(Show e, Monad m, HasCallStack, Member (Hedgehog m) r) =>
Either e a -> Sem r a
evalEither (Either e a -> Sem r a) -> Sem r (Either e a) -> Sem r a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Sem (Error e : r) a -> Sem r (Either e a)
forall e (r :: EffectRow) a.
Sem (Error e : r) a -> Sem r (Either e a)
runError Sem (Error e : r) a
sem

-- |Assert that two numeric values are closer to each other than the specified @delta@.
assertCloseBy ::
   a m r .
  Num a =>
  Ord a =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  a ->
  a ->
  Sem r ()
assertCloseBy :: a -> a -> a -> Sem r ()
assertCloseBy a
delta a
target a
scrutinee =
  (HasCallStack => Sem r ()) -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => Sem r ()) -> Sem r ())
-> (HasCallStack => Sem r ()) -> Sem r ()
forall a b. (a -> b) -> a -> b
$ Bool -> Sem r ()
forall (m :: * -> *) (r :: EffectRow).
(Monad m, HasCallStack, Member (Hedgehog m) r) =>
Bool -> Sem r ()
assert (a -> a
forall a. Num a => a -> a
abs (a
scrutinee a -> a -> a
forall a. Num a => a -> a -> a
- a
target) a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
delta)

-- |Assert that two fractional values are closer to each other than @0.001@.
assertClose ::
   a m r .
  Ord a =>
  Fractional a =>
  Monad m =>
  HasCallStack =>
  Member (Hedgehog m) r =>
  a ->
  a ->
  Sem r ()
assertClose :: a -> a -> Sem r ()
assertClose =
  (HasCallStack => a -> a -> Sem r ()) -> a -> a -> Sem r ()
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => a -> a -> Sem r ()) -> a -> a -> Sem r ())
-> (HasCallStack => a -> a -> Sem r ()) -> a -> a -> Sem r ()
forall a b. (a -> b) -> a -> b
$ a -> a -> a -> Sem r ()
forall a (m :: * -> *) (r :: EffectRow).
(Num a, Ord a, Monad m, HasCallStack, Member (Hedgehog m) r) =>
a -> a -> a -> Sem r ()
assertCloseBy a
0.001