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 #-}
{-|
Module : HsLua.Marshalling.UserdataTests
Copyright : © 2018-2024 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb@hslua.org>
Tests that any data type can be pushed to Lua as userdata.
-}
module HsLua.Marshalling.UserdataTests (tests) where
import Control.Monad (when)
import HsLua.Marshalling.Userdata
import Test.Tasty.HsLua ( (=:), shouldBeResultOf )
import Test.Tasty (TestTree, testGroup)
import qualified HsLua.Core as Lua
-- | Specifications for Attributes parsing functions.
tests :: TestTree
tests = testGroup "Userdata"
[ testGroup "pushIterator"
[ "iterate over list" =:
Just "0,1,1,2,3,5,8,13,21" `shouldBeResultOf` do
let fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
Lua.openlibs
Lua.pushHaskellFunction $
pushIterator (\n -> 1 <$ Lua.pushinteger n) (take 9 fibs)
Lua.setglobal "fibs"
stat <- Lua.dostring $ mconcat
[ "local acc = {}\n"
, "for n in fibs() do\n"
, " table.insert(acc, n)\n"
, "end\n"
, "return table.concat(acc, ',')\n"
]
when (stat /= Lua.OK) Lua.throwErrorAsException
Lua.tostring Lua.top
, "skip entry if value pusher returned 0" =:
Just "1,3,4" `shouldBeResultOf` do
let pushNoTwo 2 = return 0
pushNoTwo i = 1 <$ Lua.pushinteger i
Lua.openlibs
Lua.pushHaskellFunction $ pushIterator pushNoTwo [1..4]
Lua.setglobal "skip"
stat <- Lua.dostring $ mconcat
[ "local acc = {}\n"
, "for n in skip() do table.insert(acc, n) end\n"
, "return table.concat(acc, ',')\n"
]
when (stat /= Lua.OK) Lua.throwErrorAsException
Lua.tostring Lua.top
]
]
|