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
|
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-deprecations #-}
{-|
Module : HsLua.Core.ClosuresTests
Copyright : © 2017-2024 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb@hslua.org>
Stability : stable
Portability : portable
Test exposing Haskell functions to Lua.
-}
module HsLua.Core.ClosuresTests (tests) where
import Control.Monad (forM_, void)
import Data.Maybe (fromMaybe)
import HsLua.Core as Lua
import Test.Tasty.HsLua ((=:), (?:), shouldBeResultOf)
import Test.Tasty (TestTree, testGroup)
-- | Specifications for Attributes parsing functions.
tests :: TestTree
tests = testGroup "Closures"
[ "Haskell functions are callable from Lua" =:
Just (113 :: Lua.Integer) `shouldBeResultOf` do
-- add 23
pushHaskellFunction $ do
i <- tointeger (nthBottom 1)
pushinteger (fromMaybe 0 i + 42)
return (NumResults 1)
pushinteger 71
call 1 1
tointeger top
, "Haskell functions have the Lua type C function" ?: do
pushHaskellFunction (return 0 :: Lua NumResults)
iscfunction top
-- The following test case will hang if there are issues with the way
-- functions are garbage collection.
, "function garbage collection" =:
() `shouldBeResultOf` do
let pushAndPopAdder n = do
let fn :: Lua NumResults
fn = do
x <- fromMaybe 0 <$> tointeger (nthBottom 1)
pushinteger (x + n)
return (NumResults 1)
pushHaskellFunction fn
pop 1
forM_ [1..5000::Lua.Integer] pushAndPopAdder
void $ gc Lua.GCCollect
]
|