hslua-core-1.0.0: test/HsLua/Core/AuxiliaryTests.hs
{-# LANGUAGE OverloadedStrings #-}
{-| Tests for the auxiliary library.
-}
module HsLua.Core.AuxiliaryTests (tests) where
import Data.ByteString (ByteString)
import Data.Maybe (fromMaybe)
import HsLua.Core (nth)
import Test.Tasty.HsLua ((?:), (=:), pushLuaExpr, shouldBeResultOf)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit ((@=?))
import qualified HsLua.Core as Lua
-- | Specifications for Attributes parsing functions.
tests :: TestTree
tests = testGroup "Auxiliary"
[ testGroup "getsubtable"
[ "gets a subtable from field" =:
[5, 8] `shouldBeResultOf` do
pushLuaExpr "{foo = {5, 8}}"
_ <- Lua.getsubtable Lua.top "foo"
Lua.rawgeti (nth 1) 1
Lua.rawgeti (nth 2) 2
i1 <- fromMaybe 0 <$> Lua.tointeger (nth 2)
i2 <- fromMaybe 0 <$> Lua.tointeger (nth 1)
return [i1, i2]
, "creates new table at field if necessary" =:
Lua.TypeTable `shouldBeResultOf` do
Lua.newtable
_ <- Lua.getsubtable Lua.top "new"
Lua.getfield (Lua.nth 2) "new"
Lua.ltype Lua.top
, "returns True if a table exists" ?: do
pushLuaExpr "{yep = {}}"
Lua.getsubtable Lua.top "yep"
, "returns False if field does not contain a table" ?: do
pushLuaExpr "{nope = 5}"
not <$> Lua.getsubtable Lua.top "nope"
]
, testGroup "getmetafield'"
[ "gets field from the object's metatable" =:
("testing" :: ByteString) `shouldBeResultOf` do
Lua.newtable
pushLuaExpr "{foo = 'testing'}"
Lua.setmetatable (Lua.nth 2)
_ <- Lua.getmetafield Lua.top "foo"
Lua.tostring' Lua.top
, "returns TypeNil if the object doesn't have a metatable" =:
Lua.TypeNil `shouldBeResultOf` do
Lua.newtable
Lua.getmetafield Lua.top "foo"
]
, testGroup "getmetatable'"
[ "gets table created with newmetatable" =:
("__name" :: ByteString, "testing" :: ByteString) `shouldBeResultOf` do
Lua.newmetatable "testing" *> Lua.pop 1
_ <- Lua.getmetatable' "testing"
Lua.pushnil
Lua.next (nth 2)
key <- Lua.tostring' (nth 2) <* Lua.pop 1
value <- Lua.tostring' (nth 1) <* Lua.pop 1
return (key, value)
, "returns nil if there is no such metatable" =:
Lua.TypeNil `shouldBeResultOf` do
_ <- Lua.getmetatable' "nope"
Lua.ltype Lua.top
, "returns TypeTable if metatable exists" =:
Lua.TypeTable `shouldBeResultOf` do
_ <- Lua.newmetatable "yep"
Lua.getmetatable' "yep"
]
, "loadedTable" =: ("_LOADED" @=? Lua.loadedTableRegistryField)
, "preloadTable" =: ("_PRELOAD" @=? Lua.preloadTableRegistryField)
]