-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathModuleTests.hs
More file actions
126 lines (111 loc) · 3.52 KB
/
ModuleTests.hs
File metadata and controls
126 lines (111 loc) · 3.52 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-|
Module : HsLua.Packaging.ModuleTests
Copyright : © 2019-2021 Albert Krewinkel
License : MIT
Maintainer : Albert Krewinkel <albert+hslua@zeitkraut.de>
Stability : alpha
Portability : Requires GHC 8 or later.
Tests creating and loading of modules with Haskell.
-}
module HsLua.Packaging.ModuleTests (tests) where
import HsLua.Marshalling
(Result (Success), peekIntegral, peekString, pushIntegral, pushText, runPeek)
import HsLua.Packaging.Function
import HsLua.Packaging.Module
import Test.Tasty.HsLua ((=:), pushLuaExpr, shouldBeResultOf)
import Test.Tasty (TestTree, testGroup)
import qualified HsLua.Core as Lua
-- | Specifications for Attributes parsing functions.
tests :: TestTree
tests = testGroup "Module"
[ testGroup "requirehs"
[ "pushes module to stack" =:
1 `shouldBeResultOf` do
Lua.openlibs
old <- Lua.gettop
Lua.requirehs "foo" (Lua.pushnumber 5.0)
new <- Lua.gettop
return (new - old)
, "module can be loaded with `require`" =:
let testModule = "string as a module"
in Just testModule `shouldBeResultOf` do
Lua.openlibs
Lua.requirehs "test.module" (Lua.pushstring testModule)
pushLuaExpr "require 'test.module'"
Lua.tostring Lua.top
]
, testGroup "preloadhs"
[ "does not modify the stack" =:
0 `shouldBeResultOf` do
Lua.openlibs
old <- Lua.gettop
Lua.preloadhs "foo" (1 <$ Lua.pushnumber 5.0)
new <- Lua.gettop
return (new - old)
, "module can be loaded with `require`" =:
let testModule = "string as a module"
in Just testModule `shouldBeResultOf` do
Lua.openlibs
Lua.preloadhs "test.module" (1 <$ Lua.pushstring testModule)
pushLuaExpr "require 'test.module'"
Lua.tostring Lua.top
]
, testGroup "creation helpers"
[ "create produces a table" =:
Lua.TypeTable `shouldBeResultOf` do
Lua.newtable
Lua.ltype Lua.top
]
, testGroup "module type"
[ "register module" =:
1 `shouldBeResultOf` do
Lua.openlibs
old <- Lua.gettop
registerModule mymath
new <- Lua.gettop
return (new - old)
, "call module function" =:
Success 24 `shouldBeResultOf` do
Lua.openlibs
registerModule mymath
_ <- Lua.dostring $ mconcat
[ "local mymath = require 'mymath'\n"
, "return mymath.factorial(4)"
]
runPeek $ peekIntegral @Integer Lua.top
, "call module as function" =:
Success "call me maybe" `shouldBeResultOf` do
Lua.openlibs
registerModule mymath
_ <- Lua.dostring "return (require 'mymath')()"
runPeek $ peekString Lua.top
]
]
mymath :: Module Lua.Exception
mymath = Module
{ moduleName = "mymath"
, moduleDescription = "A math module."
, moduleFields = []
, moduleFunctions = [factorial]
, moduleOperations =
[ (,) Call $ lambda
### (1 <$ pushText "call me maybe")
=?> "call result"
]
}
factorial :: DocumentedFunction Lua.Exception
factorial =
defun "factorial"
### liftPure (\n -> product [1..n])
<#> factorialParam
=#> factorialResult
factorialParam :: Parameter Lua.Exception Integer
factorialParam =
parameter (peekIntegral @Integer) "integer"
"n"
"number for which the factorial is computed"
factorialResult :: FunctionResults Lua.Exception Integer
factorialResult =
functionResult (pushIntegral @Integer) "integer" "factorial"