1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
{-# LANGUAGE OverloadedStrings #-}
{-|
Module : Test.Tasty.HsLua
Copyright : © 2017-2024 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb@hslua.org>
Stability : beta
Portability : non-portable (depends on GHC)
Utilities for testing of HsLua operations.
-}
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
-- | Takes a Lua expression as a 'ByteString', evaluates it and pushes
-- the result to the stack.
--
-- > -- will return "12"
-- > run $ do
-- > pushLuaExpr "7 + 5"
-- > tointeger top
pushLuaExpr :: LuaError e => ByteString -> LuaE e ()
pushLuaExpr expr = loadstring ("return " `append` expr) *> call 0 multret
-- | Takes a value and a 'Lua' operation and turns them into an
-- 'Assertion' which checks that the operation produces the given value.
shouldBeResultOf :: (HasCallStack, Eq a, Show a)
=> a -> Lua a -> Assertion
shouldBeResultOf expected luaOp = do
errOrRes <- runEither luaOp
case errOrRes of
Left (Lua.Exception msg) -> assertFailure $ "Lua operation failed with "
++ "message: '" ++ msg ++ "'"
Right res -> res @?= expected
-- | Checks whether a 'Lua' operation fails with the given string as
-- error message.
shouldBeErrorMessageOf :: (HasCallStack, Show a)
=> String -> Lua a -> Assertion
shouldBeErrorMessageOf expectedErrMsg luaOp = do
errOrRes <- runEither luaOp
case errOrRes of
Left (Lua.Exception msg) -> msg @?= expectedErrMsg
Right res ->
assertFailure ("Lua operation succeeded unexpectedly and returned "
++ show res)
-- | Checks whether the return value of an operation holds for the given
-- predicate.
shouldHoldForResultOf :: (HasCallStack, Show a)
=> (a -> Bool) -> Lua a -> Assertion
shouldHoldForResultOf predicate luaOp = do
errOrRes <- runEither luaOp
case errOrRes of
Left (Lua.Exception msg) -> assertFailure $ "Lua operation failed with "
++ "message: '" ++ msg ++ "'"
Right res -> assertBool ("predicate doesn't hold for " ++ show res)
(predicate res)
-- | Checks whether the operation returns 'True'.
assertLuaBool :: HasCallStack => LuaE e Bool -> Assertion
assertLuaBool luaOp = assertBool "" =<< run luaOp
-- | Creates a new test case with the given name, checking whether the
-- operation returns 'True'.
luaTestBool :: HasCallStack => String -> LuaE e Bool -> TestTree
luaTestBool msg luaOp = testCase msg $
assertBool "Lua operation returned false" =<< run luaOp
-- | Infix alias for 'testCase'.
(=:) :: String -> Assertion -> TestTree
(=:) = testCase
infix 3 =:
-- | Infix alias for 'luaTestBool'.
(?:) :: HasCallStack => String -> LuaE e Bool -> TestTree
(?:) = luaTestBool
infixr 3 ?:
|