-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmeta_test.go
More file actions
119 lines (108 loc) · 2.69 KB
/
Copy pathmeta_test.go
File metadata and controls
119 lines (108 loc) · 2.69 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
// M11 metatable + pcall end-to-end tests.
package wangshu_test
import (
"strings"
"testing"
"github.com/Liam0205/wangshu"
)
func TestMeta_IndexTable(t *testing.T) {
got := runOne(t, `
local base = { greeting = "hello" }
local derived = setmetatable({}, { __index = base })
return derived.greeting
`)
if !got.IsString() || got.Str() != "hello" {
t.Errorf("got %v, want 'hello'", got.Display())
}
}
func TestMeta_IndexFunction(t *testing.T) {
got := runOne(t, `
local t = setmetatable({}, { __index = function(tbl, key) return key .. "!" end })
return t.boom
`)
if !got.IsString() || got.Str() != "boom!" {
t.Errorf("got %v, want 'boom!'", got.Display())
}
}
func TestMeta_NewIndex(t *testing.T) {
got := runOne(t, `
local log = {}
local t = setmetatable({}, { __newindex = function(tbl, k, v) rawset(log, k, v) end })
t.x = 42
return rawget(log, "x")
`)
if !got.IsNumber() || got.Number() != 42 {
t.Errorf("got %v, want 42", got.Display())
}
}
func TestMeta_Add(t *testing.T) {
got := runOne(t, `
local mt = { __add = function(a, b) return a.v + b.v end }
local x = setmetatable({ v = 3 }, mt)
local y = setmetatable({ v = 4 }, mt)
return x + y
`)
if !got.IsNumber() || got.Number() != 7 {
t.Errorf("got %v, want 7", got.Display())
}
}
func TestMeta_GetMetatable(t *testing.T) {
got := runOne(t, `
local mt = {}
local t = setmetatable({}, mt)
return getmetatable(t) == mt
`)
if !got.IsBool() || !got.Bool() {
t.Errorf("got %v, want true", got.Display())
}
}
func TestPcall_Success(t *testing.T) {
got := runOne(t, `
local ok, v = pcall(function() return 99 end)
if ok then return v end
return -1
`)
if !got.IsNumber() || got.Number() != 99 {
t.Errorf("got %v, want 99", got.Display())
}
}
func TestPcall_CatchError(t *testing.T) {
got := runOne(t, `
local ok, err = pcall(function() error("kaboom") end)
if ok then return "no-error" end
return err
`)
// 5.1 semantics: error(string) automatically prepends a chunkname:line: prefix
if !got.IsString() || !strings.HasSuffix(got.Str(), ": kaboom") {
t.Errorf("got %v, want suffix ': kaboom'", got.Display())
}
}
func TestPcall_CatchRuntimeError(t *testing.T) {
got := runOne(t, `
local ok, err = pcall(function()
local x
return x + 1
end)
return ok
`)
if !got.IsBool() || got.Bool() {
t.Errorf("got %v, want false", got.Display())
}
}
func TestPcall_StateContinues(t *testing.T) {
prog, err := wangshu.Compile([]byte(`
local ok = pcall(function() error("x") end)
return 7 + 1
`), "cont")
if err != nil {
t.Fatalf("compile: %v", err)
}
st := wangshu.NewState(wangshu.Options{})
results, err := prog.Run(st)
if err != nil {
t.Fatalf("run: %v", err)
}
if results[0].Number() != 8 {
t.Errorf("got %v, want 8", results[0].Display())
}
}