{-# LANGUAGE OverloadedStrings #-}
module Test.Tasty.HsLua
( assertLuaBool
, pushLuaExpr
, shouldBeErrorMessageOf
, shouldBeResultOf
, shouldHoldForResultOf
, (=:)
, (?:)
) where
import Data.ByteString (ByteString, append)
import HsLua.Core
(Lua, LuaE, LuaError, run, runEither, loadstring, call, multret)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit
(Assertion, HasCallStack, assertBool, assertFailure, testCase, (@?=))
import qualified HsLua.Core as Lua
pushLuaExpr :: LuaError e => ByteString -> LuaE e ()
pushLuaExpr :: forall e. LuaError e => ByteString -> LuaE e ()
pushLuaExpr ByteString
expr = ByteString -> LuaE e Status
forall e. ByteString -> LuaE e Status
loadstring (ByteString
"return " ByteString -> ByteString -> ByteString
`append` ByteString
expr) LuaE e Status -> LuaE e () -> LuaE e ()
forall a b. LuaE e a -> LuaE e b -> LuaE e b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> NumArgs -> NumResults -> LuaE e ()
forall e. LuaError e => NumArgs -> NumResults -> LuaE e ()
call NumArgs
0 NumResults
multret
shouldBeResultOf :: (HasCallStack, Eq a, Show a)
=> a -> Lua a -> Assertion
shouldBeResultOf :: forall a. (HasCallStack, Eq a, Show a) => a -> Lua a -> Assertion
shouldBeResultOf a
expected Lua a
luaOp = do
Either Exception a
errOrRes <- Lua a -> IO (Either Exception a)
forall e a. Exception e => LuaE e a -> IO (Either e a)
runEither Lua a
luaOp
case Either Exception a
errOrRes of
Left (Lua.Exception [Char]
msg) -> [Char] -> Assertion
forall a. HasCallStack => [Char] -> IO a
assertFailure ([Char] -> Assertion) -> [Char] -> Assertion
forall a b. (a -> b) -> a -> b
$ [Char]
"Lua operation failed with "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"message: '" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
msg [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"'"
Right a
res -> a
res a -> a -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= a
expected
shouldBeErrorMessageOf :: (HasCallStack, Show a)
=> String -> Lua a -> Assertion
shouldBeErrorMessageOf :: forall a. (HasCallStack, Show a) => [Char] -> Lua a -> Assertion
shouldBeErrorMessageOf [Char]
expectedErrMsg Lua a
luaOp = do
Either Exception a
errOrRes <- Lua a -> IO (Either Exception a)
forall e a. Exception e => LuaE e a -> IO (Either e a)
runEither Lua a
luaOp
case Either Exception a
errOrRes of
Left (Lua.Exception [Char]
msg) -> [Char]
msg [Char] -> [Char] -> Assertion
forall a. (Eq a, Show a, HasCallStack) => a -> a -> Assertion
@?= [Char]
expectedErrMsg
Right a
res ->
[Char] -> Assertion
forall a. HasCallStack => [Char] -> IO a
assertFailure ([Char]
"Lua operation succeeded unexpectedly and returned "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
res)
shouldHoldForResultOf :: (HasCallStack, Show a)
=> (a -> Bool) -> Lua a -> Assertion
shouldHoldForResultOf :: forall a.
(HasCallStack, Show a) =>
(a -> Bool) -> Lua a -> Assertion
shouldHoldForResultOf a -> Bool
predicate Lua a
luaOp = do
Either Exception a
errOrRes <- Lua a -> IO (Either Exception a)
forall e a. Exception e => LuaE e a -> IO (Either e a)
runEither Lua a
luaOp
case Either Exception a
errOrRes of
Left (Lua.Exception [Char]
msg) -> [Char] -> Assertion
forall a. HasCallStack => [Char] -> IO a
assertFailure ([Char] -> Assertion) -> [Char] -> Assertion
forall a b. (a -> b) -> a -> b
$ [Char]
"Lua operation failed with "
[Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"message: '" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
msg [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"'"
Right a
res -> HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool ([Char]
"predicate doesn't hold for " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ a -> [Char]
forall a. Show a => a -> [Char]
show a
res)
(a -> Bool
predicate a
res)
assertLuaBool :: HasCallStack => LuaE e Bool -> Assertion
assertLuaBool :: forall e. HasCallStack => LuaE e Bool -> Assertion
assertLuaBool LuaE e Bool
luaOp = HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"" (Bool -> Assertion) -> IO Bool -> Assertion
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LuaE e Bool -> IO Bool
forall e a. LuaE e a -> IO a
run LuaE e Bool
luaOp
luaTestBool :: HasCallStack => String -> LuaE e Bool -> TestTree
luaTestBool :: forall e. HasCallStack => [Char] -> LuaE e Bool -> TestTree
luaTestBool [Char]
msg LuaE e Bool
luaOp = [Char] -> Assertion -> TestTree
testCase [Char]
msg (Assertion -> TestTree) -> Assertion -> TestTree
forall a b. (a -> b) -> a -> b
$
HasCallStack => [Char] -> Bool -> Assertion
[Char] -> Bool -> Assertion
assertBool [Char]
"Lua operation returned false" (Bool -> Assertion) -> IO Bool -> Assertion
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< LuaE e Bool -> IO Bool
forall e a. LuaE e a -> IO a
run LuaE e Bool
luaOp
(=:) :: String -> Assertion -> TestTree
=: :: [Char] -> Assertion -> TestTree
(=:) = [Char] -> Assertion -> TestTree
testCase
infix 3 =:
(?:) :: HasCallStack => String -> LuaE e Bool -> TestTree
?: :: forall e. HasCallStack => [Char] -> LuaE e Bool -> TestTree
(?:) = [Char] -> LuaE e Bool -> TestTree
forall e. HasCallStack => [Char] -> LuaE e Bool -> TestTree
luaTestBool
infixr 3 ?: