-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathparser_test.go
More file actions
172 lines (161 loc) · 4.73 KB
/
parser_test.go
File metadata and controls
172 lines (161 loc) · 4.73 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package lua
import (
"math"
"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("luac5.2")
if err != nil {
t.Skipf("exhaustively testing the parser requires luac5.2: %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("luac5.2", "-o", binary, source).Run(); err != nil {
t.Fatalf("luac5.2 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) {
t.Helper()
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 {
t.Helper()
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
}
// The following exception is necessary because Go stdlib math
// functions may be less precise than the C equivalents. In
// particular constants[153] in lua-tests/attrib.lua:360
// which is the result of "10^33" gives different results. See
// https://github.com/golang/go/issues/25270 for discussion.
if m == "constants" {
xc, yc := x.([]value), y.([]value)
if len(xc) != len(yc) {
return false
}
for i := range xc {
if fx, ok := xc[i].(float64); ok {
fy, ok2 := yc[i].(float64)
if !ok2 || !similarFloat64(fx, fy) {
return false
}
}
}
return true
}
t.Errorf("%s doesn't match: %v, %v\n", m, x, y)
return false
}
func similarFloat64(x, y float64) bool {
return x == y || math.Nextafter(x, y) == y
}
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])
}
}