-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.go
138 lines (117 loc) · 3.1 KB
/
helper.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
package test
import (
compilerPkg "github.com/iafisher/venice/src/compiler"
"github.com/iafisher/venice/src/vm"
"strings"
"testing"
)
func B(b bool) *vm.VeniceBoolean {
return &vm.VeniceBoolean{b}
}
func F(n float64) *vm.VeniceRealNumber {
return &vm.VeniceRealNumber{n}
}
func L(values ...vm.VeniceValue) *vm.VeniceList {
return &vm.VeniceList{values}
}
func I(n int) *vm.VeniceInteger {
return &vm.VeniceInteger{n}
}
func S(s string) *vm.VeniceString {
return &vm.VeniceString{s}
}
func Some(v vm.VeniceValue) *vm.VeniceEnumObject {
return &vm.VeniceEnumObject{
Label: "Some",
Values: []vm.VeniceValue{v},
}
}
func None() *vm.VeniceEnumObject {
return &vm.VeniceEnumObject{
Label: "None",
Values: []vm.VeniceValue{},
}
}
func Tup(values ...vm.VeniceValue) *vm.VeniceTuple {
return &vm.VeniceTuple{values}
}
func assertEqual(t *testing.T, program string, result vm.VeniceValue) {
parsedFile, err := compilerPkg.ParseString(program)
if err != nil {
t.Helper()
t.Fatalf("Parse error: %s\n\nInput:\n\n%s", err, program)
}
compiler := compilerPkg.NewCompiler()
compiledProgram, err := compiler.Compile(parsedFile)
if err != nil {
t.Helper()
t.Fatalf("Compile error: %s\n\nInput:\n\n%s", err, program)
}
virtualMachine := vm.NewVirtualMachine()
value, err := virtualMachine.Execute(compiledProgram, false)
if err != nil {
t.Helper()
t.Fatalf("Execution error: %s\n\nInput:\n\n%s", err, program)
}
if value == nil {
t.Helper()
t.Fatalf("Code snippet did not return a value\n\nInput:\n\n%s", program)
}
if !value.Equals(result) {
t.Helper()
t.Fatalf(
"Expected %s, got %s\n\nInput:\n\n%s",
result.String(),
value.String(),
program,
)
}
}
func assertTypecheckError(t *testing.T, program string, errorMessage string) {
parsedFile, err := compilerPkg.ParseString(program)
if err != nil {
t.Helper()
t.Fatalf("Parse error: %s\n\nInput:\n\n%s", err, program)
}
compiler := compilerPkg.NewCompiler()
_, err = compiler.Compile(parsedFile)
if err == nil {
t.Helper()
t.Fatalf(
"Expected compile error, but program compiled without error\n\nInput:\n\n%s",
program,
)
}
if !strings.Contains(err.Error(), errorMessage) {
t.Helper()
t.Fatalf(
"Expected compile error to contain substring %q, but it did not\n\nError: %s\n\nInput:\n\n%s",
errorMessage,
err.Error(),
program,
)
}
}
func assertPanic(t *testing.T, program string) {
parsedFile, err := compilerPkg.ParseString(program)
if err != nil {
t.Helper()
t.Fatalf("Parse error: %s\n\nInput:\n\n%s", err, program)
}
compiler := compilerPkg.NewCompiler()
compiledProgram, err := compiler.Compile(parsedFile)
if err != nil {
t.Helper()
t.Fatalf("Compile error: %s\n\nInput:\n\n%s", err, program)
}
virtualMachine := vm.NewVirtualMachine()
_, err = virtualMachine.Execute(compiledProgram, false)
if err == nil {
t.Helper()
t.Fatalf("Expected panic, but program ran without panicking: %s\n\nInput:\n\n%s", err, program)
}
if _, ok := err.(*vm.PanicError); !ok {
t.Helper()
t.Fatalf("Expected panic, but program suffered an internal error: %s\n\nInput:\n\n%s", err, program)
}
}