-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathcompiler_test.go
152 lines (133 loc) · 3.09 KB
/
compiler_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
146
147
148
149
150
151
152
package compiler
import (
"testing"
"github.com/goby-lang/goby/compiler/bytecode"
"github.com/goby-lang/goby/compiler/parser"
)
type testInstruction struct {
actionName string
opCode uint8
sourceLine int
paramsLen int
}
func verifyInstructions(i *bytecode.Instruction, e testInstruction, t *testing.T) {
t.Helper()
if i.ActionName() != e.actionName || i.Opcode != e.opCode || i.SourceLine() != e.sourceLine || len(i.Params) != e.paramsLen {
t.Fatalf("Line %d: expect ActionName: `%s`, opCode: %d, SourceLine: %d, paramsLen: %d. got: ActionName: `%s`, opCode: %d, SourceLine: %d, paramsLen: %d", i.Line(),
e.actionName, e.opCode, e.sourceLine, e.paramsLen,
i.ActionName(), i.Opcode, i.SourceLine(), len(i.Params))
}
}
func TestCompileToInstructionsNormalMode(t *testing.T) {
is, err := CompileToInstructions(`
def bar(a)
99 + a
end
while true do
end`, parser.NormalMode)
if err != nil {
t.Fatal(err.Error())
}
tests := []struct {
line int
expected testInstruction
}{
{
0,
testInstruction{actionName: "putobject", opCode: 10, sourceLine: 3, paramsLen: 1},
},
{
1,
testInstruction{actionName: "getlocal", opCode: 0, sourceLine: 3, paramsLen: 2},
},
{
2,
testInstruction{actionName: "send", opCode: 24, sourceLine: 3, paramsLen: 4},
},
{
3,
testInstruction{actionName: "leave", opCode: 29, sourceLine: 2, paramsLen: 0},
},
}
for _, tt := range tests {
i := is[0].Instructions[tt.line]
verifyInstructions(i, tt.expected, t)
}
}
func TestCompileToInstructionsTESTMode(t *testing.T) {
is, err := CompileToInstructions(`
module Foo
end
`, parser.TestMode)
if err != nil {
t.Fatal(err.Error())
}
tests := []struct {
line int
expected testInstruction
}{
{
0,
testInstruction{actionName: "leave", opCode: 29, sourceLine: 2, paramsLen: 0},
},
}
for _, tt := range tests {
i := is[0].Instructions[tt.line]
verifyInstructions(i, tt.expected, t)
}
}
func TestCompileToInstructionsREPLMode(t *testing.T) {
is, err := CompileToInstructions(`
def bar(a)
99 + a
end
while true do
end
`, parser.REPLMode)
if err != nil {
t.Fatal(err.Error())
}
tests := []struct {
line int
expected testInstruction
}{
{
0,
testInstruction{actionName: "putobject", opCode: 10, sourceLine: 3, paramsLen: 1},
},
{
1,
testInstruction{actionName: "getlocal", opCode: 0, sourceLine: 3, paramsLen: 2},
},
{
2,
testInstruction{actionName: "send", opCode: 24, sourceLine: 3, paramsLen: 4},
},
{
3,
testInstruction{actionName: "leave", opCode: 29, sourceLine: 2, paramsLen: 0},
},
}
for _, tt := range tests {
i := is[0].Instructions[tt.line]
verifyInstructions(i, tt.expected, t)
}
}
// TODO: The tests needs to be updated with igb/repl.go
func TestCompileToInstructionsREPLModeFail(t *testing.T) {
tests := []struct {
input string
expected string
}{
{`
iff
end
`, "unexpected end Line: 2"},
}
for _, tt := range tests {
_, err := CompileToInstructions(tt.input, parser.REPLMode)
if err.Error() != tt.expected {
t.Fatalf("Expect `%s` error. got: %s", tt.expected, err.Error())
}
}
}