hslua-marshalling-2.0.0: test/HsLua/Marshalling/UserdataTests.hs
{-# LANGUAGE OverloadedStrings #-}
{-|
Module : HsLua.Marshalling.UserdataTests
Copyright : © 2018-2021 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb+hslua@zeitkraut.de>
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
]
]