{-# LANGUAGE TemplateHaskell #-}
{-|
Module      : Test.Tasty.Lua.Module
Copyright   : © 2019–2021 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 HsLua.Core
  (LuaE, LuaError, NumResults, Status (OK), dostring, throwErrorAsException)

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

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