This repository was archived by the owner on Sep 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest.js
More file actions
67 lines (60 loc) · 2.01 KB
/
test.js
File metadata and controls
67 lines (60 loc) · 2.01 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
const fs = require('fs')
const path = require('path')
const luainjs = require('..')
let exitCode = 0
{
const rootPath = './tests/starlight/'
const luaEnv = luainjs.createEnv({
fileExists: p => fs.existsSync(path.join(rootPath, p)),
loadFile: p => fs.readFileSync(path.join(rootPath, p), { encoding: 'utf8' }),
osExit: code => (exitCode += code)
})
luaEnv.parseFile('test-runner.lua').exec()
}
// TODO: make more official lua 5.3 tests pass (most of them don't pass because they `require "debug"`)
{
const rootPath = './tests/lua-5.3/'
const luaEnv = luainjs.createEnv({
fileExists: p => fs.existsSync(path.join(rootPath, p)),
loadFile: p => fs.readFileSync(path.join(rootPath, p), { encoding: 'utf8' }),
osExit: code => process.exit(code)
})
luaEnv.parseFile('goto.lua').exec()
luaEnv.parseFile('bwcoercion.lua').exec()
luaEnv.parseFile('logical_operations.lua').exec()
}
{
const luaEnv = luainjs.createEnv()
function helloBuilder(name) {
const NAME = luainjs.utils.coerceArgToString(name, 'sayHi', 1)
return `Hello ${NAME}!`
}
const myLib = new luainjs.Table({ helloBuilder })
luaEnv.loadLib('myLib', myLib)
const str = luaEnv.parse(`return myLib.helloBuilder('John')`).exec()
if (str !== 'Hello John!') {
throw Error("Strings don't match!")
}
}
{
const luaEnv = luainjs.createEnv()
const ext = new luainjs.Table({ foo: () => 'bar' })
luaEnv.extendLib('math', ext)
const val = luaEnv.parse('return math.foo()').exec()
if (val !== 'bar') {
throw Error('extendLib failed!')
}
}
{
const luaEnv = luainjs.createEnv()
let str
try {
str = luaEnv.parse('return "Backtick `literals` in strings work"').exec()
} catch (e) {
throw Error('Backticks in strings transpile into invalid code!')
}
if (str !== 'Backtick `literals` in strings work') {
throw Error('Backticks in strings transpile incorrectly!')
}
}
process.exit(exitCode)