This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
forked from mtabini/go-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
145 lines (135 loc) · 4.02 KB
/
parser_test.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package lua
import (
"os/exec"
"path/filepath"
"reflect"
"runtime/debug"
"strings"
"testing"
)
func load(l *State, t *testing.T, fileName string) *luaClosure {
if err := LoadFile(l, fileName, "bt"); err != nil {
return nil
}
return l.ToValue(-1).(*luaClosure)
}
func TestParser(t *testing.T) {
l := NewState()
OpenLibraries(l)
bin := load(l, t, "fixtures/fib.bin")
l.Pop(1)
closure := load(l, t, "fixtures/fib.lua")
p := closure.prototype
if p == nil {
t.Fatal("prototype was nil")
}
validate("@fixtures/fib.lua", p.source, "as source file name", t)
if !p.isVarArg {
t.Error("expected main function to be var arg, but wasn't")
}
if len(closure.upValues) != len(closure.prototype.upValues) {
t.Error("upvalue count doesn't match", len(closure.upValues), "!=", len(closure.prototype.upValues))
}
compareClosures(t, bin, closure)
l.Call(0, 0)
}
func TestEmptyString(t *testing.T) {
l := NewState()
if err := LoadString(l, ""); err != nil {
t.Fatal(err.Error())
}
l.Call(0, 0)
}
func TestParserExhaustively(t *testing.T) {
_, err := exec.LookPath("luac")
if err != nil {
t.Skipf("exhaustively testing the parser requires luac: %s", err)
}
l := NewState()
matches, err := filepath.Glob(filepath.Join("lua-tests", "*.lua"))
if err != nil {
t.Fatal(err)
}
blackList := map[string]bool{"math.lua": true}
for _, source := range matches {
if _, ok := blackList[filepath.Base(source)]; ok {
continue
}
protectedTestParser(l, t, source)
}
}
func protectedTestParser(l *State, t *testing.T, source string) {
defer func() {
if x := recover(); x != nil {
t.Error(x)
t.Log(string(debug.Stack()))
}
}()
t.Log("Compiling " + source)
binary := strings.TrimSuffix(source, ".lua") + ".bin"
if err := exec.Command("luac", "-o", binary, source).Run(); err != nil {
t.Fatalf("luac failed to compile %s: %s", source, err)
}
t.Log("Parsing " + source)
bin := load(l, t, binary)
l.Pop(1)
src := load(l, t, source)
l.Pop(1)
t.Log(source)
compareClosures(t, src, bin)
}
func expectEqual(t *testing.T, x, y interface{}, m string) {
if x != y {
t.Errorf("%s doesn't match: %v, %v\n", m, x, y)
}
}
func expectDeepEqual(t *testing.T, x, y interface{}, m string) bool {
if reflect.DeepEqual(x, y) {
return true
}
if reflect.TypeOf(x).Kind() == reflect.Slice && reflect.ValueOf(y).Len() == 0 && reflect.ValueOf(x).Len() == 0 {
return true
}
t.Errorf("%s doesn't match: %v, %v\n", m, x, y)
return false
}
func compareClosures(t *testing.T, a, b *luaClosure) {
expectEqual(t, a.upValueCount(), b.upValueCount(), "upvalue count")
comparePrototypes(t, a.prototype, b.prototype)
}
func comparePrototypes(t *testing.T, a, b *prototype) {
expectEqual(t, a.isVarArg, b.isVarArg, "var arg")
expectEqual(t, a.lineDefined, b.lineDefined, "line defined")
expectEqual(t, a.lastLineDefined, b.lastLineDefined, "last line defined")
expectEqual(t, a.parameterCount, b.parameterCount, "parameter count")
expectEqual(t, a.maxStackSize, b.maxStackSize, "max stack size")
expectEqual(t, a.source, b.source, "source")
expectEqual(t, len(a.code), len(b.code), "code length")
if !expectDeepEqual(t, a.code, b.code, "code") {
for i := range a.code {
if a.code[i] != b.code[i] {
t.Errorf("%d: %v != %v\n", a.lineInfo[i], a.code[i], b.code[i])
}
}
for _, i := range []int{3, 197, 198, 199, 200, 201} {
t.Errorf("%d: %#v, %#v\n", i, a.constants[i], b.constants[i])
}
for _, i := range []int{202, 203, 204} {
t.Errorf("%d: %#v\n", i, b.constants[i])
}
}
if !expectDeepEqual(t, a.constants, b.constants, "constants") {
for i := range a.constants {
if a.constants[i] != b.constants[i] {
t.Errorf("%d: %#v != %#v\n", i, a.constants[i], b.constants[i])
}
}
}
expectDeepEqual(t, a.lineInfo, b.lineInfo, "line info")
expectDeepEqual(t, a.upValues, b.upValues, "upvalues")
expectDeepEqual(t, a.localVariables, b.localVariables, "local variables")
expectEqual(t, len(a.prototypes), len(b.prototypes), "prototypes length")
for i := range a.prototypes {
comparePrototypes(t, &a.prototypes[i], &b.prototypes[i])
}
}