-------------------------------------------------------------------------------
-- 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.RWS as S


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

data TestEvent = TCleanUpError
               | TQuitGame
               | TSetupDisplay
               | TShutdownDisplay
               | TStartGame
               | TStartEvents
               | TStopEvents
        deriving (TestEvent -> TestEvent -> Bool
(TestEvent -> TestEvent -> Bool)
-> (TestEvent -> TestEvent -> Bool) -> Eq TestEvent
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
(Int -> TestEvent -> ShowS)
-> (TestEvent -> String)
-> ([TestEvent] -> ShowS)
-> Show TestEvent
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)

-- r: ()
-- w: [TestEvents]
-- s: [GTest]
newtype Test a = Test (S.RWS () [TestEvent] GRec a)
               deriving (a -> Test b -> Test a
(a -> b) -> Test a -> Test b
(forall a b. (a -> b) -> Test a -> Test b)
-> (forall a b. a -> Test b -> Test a) -> Functor Test
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
<$ :: a -> Test b -> Test a
$c<$ :: forall a b. a -> Test b -> Test a
fmap :: (a -> b) -> Test a -> Test b
$cfmap :: forall a b. (a -> b) -> Test a -> Test b
Functor, Functor Test
a -> Test a
Functor Test
-> (forall a. a -> Test a)
-> (forall a b. Test (a -> b) -> Test a -> Test b)
-> (forall a b c. (a -> b -> c) -> Test a -> Test b -> Test c)
-> (forall a b. Test a -> Test b -> Test b)
-> (forall a b. Test a -> Test b -> Test a)
-> Applicative Test
Test a -> Test b -> Test b
Test a -> Test b -> Test a
Test (a -> b) -> Test a -> Test b
(a -> b -> c) -> Test a -> Test b -> Test c
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
<* :: Test a -> Test b -> Test a
$c<* :: forall a b. Test a -> Test b -> Test a
*> :: Test a -> Test b -> Test b
$c*> :: forall a b. Test a -> Test b -> Test b
liftA2 :: (a -> b -> c) -> Test a -> Test b -> Test c
$cliftA2 :: forall a b c. (a -> b -> c) -> Test a -> Test b -> Test c
<*> :: Test (a -> b) -> Test a -> Test b
$c<*> :: forall a b. Test (a -> b) -> Test a -> Test b
pure :: a -> Test a
$cpure :: forall a. a -> Test a
$cp1Applicative :: Functor Test
Applicative, Applicative Test
a -> Test a
Applicative Test
-> (forall a b. Test a -> (a -> Test b) -> Test b)
-> (forall a b. Test a -> Test b -> Test b)
-> (forall a. a -> Test a)
-> Monad Test
Test a -> (a -> Test b) -> Test b
Test a -> Test b -> Test b
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 :: a -> Test a
$creturn :: forall a. a -> Test a
>> :: Test a -> Test b -> Test b
$c>> :: forall a b. Test a -> Test b -> Test b
>>= :: Test a -> (a -> Test b) -> Test b
$c>>= :: forall a b. Test a -> (a -> Test b) -> Test b
$cp1Monad :: Applicative Test
Monad,
                         S.MonadState GRec,
                         S.MonadWriter [TestEvent])

runTest :: Test a -> GRec -> (a, [TestEvent])
runTest :: Test a -> GRec -> (a, [TestEvent])
runTest (Test RWS () [TestEvent] GRec a
m) GRec
es = RWS () [TestEvent] GRec a -> () -> GRec -> (a, [TestEvent])
forall r w s a. RWS r w s a -> r -> s -> (a, w)
S.evalRWS RWS () [TestEvent] GRec a
m () GRec
es


-----------
-- CLASS --
-----------

tconst :: a -> Test a
tconst :: a -> Test a
tconst a
a = RWS () [TestEvent] GRec a -> Test a
forall a. RWS () [TestEvent] GRec a -> Test a
Test (RWS () [TestEvent] GRec a -> Test a)
-> RWS () [TestEvent] GRec a -> Test a
forall a b. (a -> b) -> a -> b
$ a -> RWS () [TestEvent] GRec a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a

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

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

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

instance MonadException Test where
    cleanUpErr :: Test a -> Test b -> Test a
cleanUpErr Test a
a Test b
_ = [TestEvent] -> Test ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
S.tell [TestEvent
TCleanUpError] Test () -> Test a -> Test a
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Test a
a
    throwExc :: ATGException -> Test a
throwExc ATGException
e = String -> Test a
forall a. HasCallStack => String -> a
error (String -> Test a)
-> (ATGException -> String) -> ATGException -> Test a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ATGException -> String
forall a. Show a => a -> String
show (ATGException -> Test a) -> ATGException -> Test a
forall a b. (a -> b) -> a -> b
$ ATGException
e

instance MonadLogic Test where
    checkQuit :: (s -> Bool) -> s -> Test Bool
checkQuit s -> Bool
_ s
_ = (GRec -> Bool) -> Test Bool
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
S.gets GRec -> Bool
isOver

instance MonadDisplay Test where
    setupDisplay :: Test ()
setupDisplay = () () -> Test () -> Test ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [TestEvent] -> Test ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
S.tell [TestEvent
TSetupDisplay]
    clearDisplay :: Test ()
clearDisplay = () -> Test ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    displaySize :: Test (Maybe Dimensions)
displaySize = RWS () [TestEvent] GRec (Maybe Dimensions)
-> Test (Maybe Dimensions)
forall a. RWS () [TestEvent] GRec a -> Test a
Test (RWS () [TestEvent] GRec (Maybe Dimensions)
 -> Test (Maybe Dimensions))
-> RWS () [TestEvent] GRec (Maybe Dimensions)
-> Test (Maybe Dimensions)
forall a b. (a -> b) -> a -> b
$ (GRec -> (Maybe Dimensions, GRec))
-> RWS () [TestEvent] GRec (Maybe Dimensions)
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
_ = () -> Test ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    shutdownDisplay :: Test ()
shutdownDisplay = () () -> Test () -> Test ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ [TestEvent] -> Test ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
S.tell [TestEvent
TShutdownDisplay]