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
|
{-# LANGUAGE OverloadedStrings #-}
{-|
Module : HsLua.Core.UserdataTests
Copyright : © 2017-2024 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <tarleb@hslua.org>
Tests that any data type can be pushed to Lua.
-}
module HsLua.Core.UserdataTests (tests) where
import HsLua.Core (getfield, pushboolean, setmetatable, tostring)
import HsLua.Core.Userdata
(fromuserdata, newhsuserdatauv, newudmetatable, putuserdata)
import HsLua.Core.Types (nth, top)
import Test.Tasty.HsLua ((=:), shouldBeResultOf)
import Test.Tasty (TestTree, testGroup)
-- | Specifications for Attributes parsing functions.
tests :: TestTree
tests = testGroup "Userdata"
[ "Name is kept in __name" =:
Just "Sample" `shouldBeResultOf` do
newudmetatable "Sample"
getfield top "__name"
tostring top
, "get back pushed value" =:
Just (Sample 0 "zero") `shouldBeResultOf` do
newhsuserdatauv (Sample 0 "zero") 0
newudmetatable "Sample"
setmetatable (nth 2)
fromuserdata top "Sample"
, "fail on boolean" =:
(Nothing :: Maybe Sample) `shouldBeResultOf` do
pushboolean False
fromuserdata top "Sample"
, "fail on wrong userdata" =:
(Nothing :: Maybe Sample) `shouldBeResultOf` do
newhsuserdatauv (5 :: Integer) 0
newudmetatable "Integer"
setmetatable (nth 2)
fromuserdata top "Sample"
, "change wrapped value" =:
Just (Sample 1 "a") `shouldBeResultOf` do
newhsuserdatauv (Sample 5 "five") 0
newudmetatable "Sample"
setmetatable (nth 2)
True <- putuserdata top "Sample" (Sample 1 "a")
fromuserdata top "Sample"
, "change fails on wrong name" =:
Just (Sample 2 "b") `shouldBeResultOf` do
newhsuserdatauv (Sample 2 "b") 0
newudmetatable "Sample"
setmetatable (nth 2)
False <- putuserdata top "WRONG" (Sample 3 "c")
fromuserdata top "Sample"
]
-- | Sample data type.
data Sample = Sample Int String
deriving (Eq, Show)
|