-------------------------------------------------------------------------------
-- Layer 2 (mockable IO), as per
-- https://www.parsonsmatt.org/2018/03/22/three_layer_haskell_cake.html
-- 2019 Francesco Ariis GPLv3
-------------------------------------------------------------------------------

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Terminal.Game.Layer.Object.Test where

-- Test (pure) MonadGame* typeclass implementation for testing purposes.

import Terminal.Game.Layer.Object.Interface
import Terminal.Game.Layer.Object.Primitive

import qualified Control.Monad.Except as E
import qualified Control.Monad.RWS as S
import qualified Data.Bifunctor as B


-----------
-- TYPES --
-----------

data TestEvent = TCleanUpError
               | TException ATGException
               | TQuitGame
               | TSetupDisplay
               | TShutdownDisplay
               | TStartGame
               | TStartEvents
               | TStopEvents
        deriving (TestEvent -> TestEvent -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TestEvent -> TestEvent -> Bool
$c/= :: TestEvent -> TestEvent -> Bool
== :: TestEvent -> TestEvent -> Bool
$c== :: TestEvent -> TestEvent -> Bool
Eq, Int -> TestEvent -> ShowS
[TestEvent] -> ShowS
TestEvent -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TestEvent] -> ShowS
$cshowList :: [TestEvent] -> ShowS
show :: TestEvent -> String
$cshow :: TestEvent -> String
showsPrec :: Int -> TestEvent -> ShowS
$cshowsPrec :: Int -> TestEvent -> ShowS
Show)

-- e: ()
-- r: ()
-- w: [TestEvents]
-- s: [GTest]
newtype Test a = Test (E.ExceptT () (S.RWS () [TestEvent] GRec) a)
               deriving (forall a b. a -> Test b -> Test a
forall a b. (a -> b) -> Test a -> Test b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Test b -> Test a
$c<$ :: forall a b. a -> Test b -> Test a
fmap :: forall a b. (a -> b) -> Test a -> Test b
$cfmap :: forall a b. (a -> b) -> Test a -> Test b
Functor, Functor Test
forall a. a -> Test a
forall a b. Test a -> Test b -> Test a
forall a b. Test a -> Test b -> Test b
forall a b. Test (a -> b) -> Test a -> Test b
forall a b c. (a -> b -> c) -> Test a -> Test b -> Test c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. Test a -> Test b -> Test a
$c<* :: forall a b. Test a -> Test b -> Test a
*> :: forall a b. Test a -> Test b -> Test b
$c*> :: forall a b. Test a -> Test b -> Test b
liftA2 :: forall a b c. (a -> b -> c) -> Test a -> Test b -> Test c
$cliftA2 :: forall a b c. (a -> b -> c) -> Test a -> Test b -> Test c
<*> :: forall a b. Test (a -> b) -> Test a -> Test b
$c<*> :: forall a b. Test (a -> b) -> Test a -> Test b
pure :: forall a. a -> Test a
$cpure :: forall a. a -> Test a
Applicative, Applicative Test
forall a. a -> Test a
forall a b. Test a -> Test b -> Test b
forall a b. Test a -> (a -> Test b) -> Test b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> Test a
$creturn :: forall a. a -> Test a
>> :: forall a b. Test a -> Test b -> Test b
$c>> :: forall a b. Test a -> Test b -> Test b
>>= :: forall a b. Test a -> (a -> Test b) -> Test b
$c>>= :: forall a b. Test a -> (a -> Test b) -> Test b
Monad,
                         E.MonadError (), S.MonadState GRec,
                         S.MonadWriter [TestEvent])

runTest :: Test a -> GRec -> (Maybe a, [TestEvent])
runTest :: forall a. Test a -> GRec -> (Maybe a, [TestEvent])
runTest (Test ExceptT () (RWS () [TestEvent] GRec) a
em) GRec
es = let m :: RWS () [TestEvent] GRec (Either () a)
m = forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
E.runExceptT ExceptT () (RWS () [TestEvent] GRec) a
em
                           t :: (Either () a, [TestEvent])
t = forall r w s a. RWS r w s a -> r -> s -> (a, w)
S.evalRWS RWS () [TestEvent] GRec (Either () a)
m () GRec
es in
                       forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
B.first (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just) (Either () a, [TestEvent])
t

-------------------------------------------------------------------------------
-- Class

mockHandle :: InputHandle
mockHandle :: InputHandle
mockHandle = MVar [Event] -> [ThreadId] -> InputHandle
InputHandle (forall a. HasCallStack => String -> a
error String
"mock handle keyMvar")
                         (forall a. HasCallStack => String -> a
error String
"mock handle threads")

instance MonadInput Test where
    startEvents :: TPS -> Test InputHandle
startEvents TPS
_ = forall w (m :: * -> *). MonadWriter w m => w -> m ()
S.tell [TestEvent
TStartEvents] forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                    forall (m :: * -> *) a. Monad m => a -> m a
return InputHandle
mockHandle
    pollEvents :: MVar [Event] -> Test [Event]
pollEvents MVar [Event]
_ = forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
S.state GRec -> ([Event], GRec)
getPolled
    stopEvents :: [ThreadId] -> Test ()
stopEvents [ThreadId]
_ = forall w (m :: * -> *). MonadWriter w m => w -> m ()
S.tell [TestEvent
TStopEvents]
    areEventsOver :: Test Bool
areEventsOver = forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
S.gets GRec -> Bool
isOver

instance MonadTimer Test where
    getTime :: Test TPS
getTime = forall (m :: * -> *) a. Monad m => a -> m a
return TPS
1
    sleepABit :: TPS -> Test ()
sleepABit TPS
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()

instance MonadException Test where
    cleanUpErr :: forall a b. Test a -> Test b -> Test a
cleanUpErr Test a
a Test b
_ = forall w (m :: * -> *). MonadWriter w m => w -> m ()
S.tell [TestEvent
TCleanUpError] forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Test a
a
    throwExc :: forall a. ATGException -> Test a
throwExc ATGException
e = forall w (m :: * -> *). MonadWriter w m => w -> m ()
S.tell [ATGException -> TestEvent
TException ATGException
e] forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                 forall e (m :: * -> *) a. MonadError e m => e -> m a
E.throwError ()

instance MonadDisplay Test where
    setupDisplay :: Test ()
setupDisplay = () forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall w (m :: * -> *). MonadWriter w m => w -> m ()
S.tell [TestEvent
TSetupDisplay]
    clearDisplay :: Test ()
clearDisplay = forall (m :: * -> *) a. Monad m => a -> m a
return ()
    displaySize :: Test (Maybe Dimensions)
displaySize = forall a. ExceptT () (RWS () [TestEvent] GRec) a -> Test a
Test forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *) a. MonadState s m => (s -> (a, s)) -> m a
S.state GRec -> (Maybe Dimensions, GRec)
getDims
    blitPlane :: Maybe Plane -> Plane -> Test ()
blitPlane Maybe Plane
_ Plane
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
    shutdownDisplay :: Test ()
shutdownDisplay = () forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall w (m :: * -> *). MonadWriter w m => w -> m ()
S.tell [TestEvent
TShutdownDisplay]