{-# LANGUAGE TemplateHaskell #-}
{-|
Module      : Test.Tasty.Lua.Module
Copyright   : © 2019–2020 Albert Krewinkel
License     : MIT
Maintainer  : Albert Krewinkel <albert+hslua@zeitkraut.de>
Stability   : alpha
Portability : Requires TemplateHaskell

Tasty Lua module, providing the functions necessary to write tasty tests
in Lua scripts.
-}
module Test.Tasty.Lua.Module
  ( pushModule )
where

import Data.ByteString (ByteString)
import Data.FileEmbed
import Foreign.Lua (Lua, NumResults, Status (OK), dostring, throwTopMessage)

-- | Tasty Lua script
tastyScript :: ByteString
tastyScript :: ByteString
tastyScript = $(embedFile "tasty.lua")

-- | Push the Aeson module on the Lua stack.
pushModule :: Lua NumResults
pushModule :: Lua NumResults
pushModule = do
  Status
result <- ByteString -> Lua Status
dostring ByteString
tastyScript
  if Status
result Status -> Status -> Bool
forall a. Eq a => a -> a -> Bool
== Status
OK
    then NumResults -> Lua NumResults
forall (m :: * -> *) a. Monad m => a -> m a
return NumResults
1
    else Lua NumResults
forall a. Lua a
throwTopMessage
{-# INLINABLE pushModule #-}