-
Notifications
You must be signed in to change notification settings - Fork 171
/
goby_test.go
195 lines (146 loc) · 4.47 KB
/
goby_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"github.com/goby-lang/goby/vm"
)
func init() {
_, err := os.Stat("./goby")
if err == nil {
err := exec.Command("rm", "./goby").Run()
if err != nil {
panic(err)
}
}
fmt.Println("Building Goby binary")
err = exec.Command("make", "build").Run()
if err != nil {
panic(err)
}
}
func execGoby(t *testing.T, args ...string) (in io.WriteCloser, out io.ReadCloser, e io.ReadCloser) {
t.Helper()
cmd := exec.Command("./goby", args...)
in, err := cmd.StdinPipe()
if err != nil {
t.Fatalf("Error getting stdin\n%s", err.Error())
}
out, err = cmd.StdoutPipe()
if err != nil {
t.Fatalf("Error getting stdout\n%s", err.Error())
}
e, err = cmd.StderrPipe()
if err != nil {
t.Fatalf("Error getting stderr\n%s", err.Error())
}
err = cmd.Start()
if err != nil {
t.Fatalf("Error running goby\n%s", err.Error())
}
return
}
func partialReport() (md string) {
md += fmt.Sprintf("### Goby version\n%s\n", vm.Version)
md += fmt.Sprintf("### GOBY_ROOT\n%s\n", os.Getenv("GOBY_ROOT"))
md += fmt.Sprintf("### Go version\n%s\n", runtime.Version())
md += fmt.Sprintf("### GOROOT\n%s\n", os.Getenv("GOROOT"))
md += fmt.Sprintf("### GOPATH\n%s\n", os.Getenv("GOPATH"))
md += fmt.Sprintf("### Operating system\n%s\n", runtime.GOOS)
return
}
func TestArgE(t *testing.T) {
_, out, _ := execGoby(t, "-e", "samples/error-report.gb")
byt, err := ioutil.ReadAll(out)
if err != nil {
t.Fatalf("Couldn't read from pipe: %s", err.Error())
}
if !strings.Contains(string(byt), partialReport()) {
t.Fatalf("Interpreter -e output incorrect:\nExpected \n`%s` in string `\n%s`", partialReport(), string(byt))
}
}
func TestArgI(t *testing.T) {
in, out, _ := execGoby(t, "-i")
fmt.Fprintln(in, `puts "hello world"`)
fmt.Fprintln(in, `exit`)
expectedOutput := "hello world\n"
byt, err := ioutil.ReadAll(out)
if err != nil {
t.Fatalf("Couldn't read from pipe: %s", err.Error())
}
if !strings.HasSuffix(string(byt), expectedOutput) {
t.Fatalf("Interpreter output incorrect. Expect '%s' to contain '%s'", string(byt), expectedOutput)
}
}
func TestArgV(t *testing.T) {
_, out, _ := execGoby(t, "-v")
byt, err := ioutil.ReadAll(out)
if err != nil {
t.Fatalf("Couldn't read from pipe: %s", err.Error())
}
if !strings.Contains(string(byt), vm.Version) {
t.Fatalf("Interpreter -v output incorrect:\nExpected '%s' in string '%s'.", vm.Version, string(byt))
}
}
func TestArgProfileCPU(t *testing.T) {
_, out, _ := execGoby(t, "-profile-cpu", "samples/one_thousand_threads.gb")
byt, err := ioutil.ReadAll(out)
if err != nil {
t.Fatalf("Couldn't read from pipe: %s", err.Error())
}
if string(byt) != "500500\n" {
t.Fatalf("Test failed, excpected 500500, got %s", string(byt))
}
}
func TestArgProfileMem(t *testing.T) {
_, out, _ := execGoby(t, "-profile-mem", "samples/one_thousand_threads.gb")
byt, err := ioutil.ReadAll(out)
if err != nil {
t.Fatalf("Couldn't read from pipe: %s", err.Error())
}
if string(byt) != "500500\n" {
t.Fatalf("Test failed, excpected 500500, got %s", string(byt))
}
}
func TestExecFileWithError(t *testing.T) {
expectedError := "NoMethodError: Undefined Method 'foo' for "
_, _, stderr := execGoby(t, "test_fixtures/file_with_error.gb")
output, _ := ioutil.ReadAll(stderr)
if !strings.Contains(string(output), expectedError) {
t.Fatalf("Expect to see error: '%s'. But got: '%s' instead", expectedError, string(output))
}
}
func TestTestCommand(t *testing.T) {
// Folder name with slash
_, out, _ := execGoby(t, "test", "test_fixtures/test_command_test/")
byt, err := ioutil.ReadAll(out)
if err != nil {
t.Fatalf("Couldn't read from pipe: %s", err.Error())
}
if !strings.Contains(string(byt), "Spec test 2") {
t.Fatalf("Test files by giving folder name with slash failed, got: %s", string(byt))
}
// Folder name
_, out, _ = execGoby(t, "test", "test_fixtures/test_command_test")
byt, err = ioutil.ReadAll(out)
if err != nil {
t.Fatalf("Couldn't read from pipe: %s", err.Error())
}
if !strings.Contains(string(byt), "Spec test 2") {
t.Fatalf("Test files by giving folder name failed, got: %s", string(byt))
}
// File name
_, out, _ = execGoby(t, "test", "test_fixtures/test_command_test/test_spec.gb")
byt, err = ioutil.ReadAll(out)
if err != nil {
t.Fatalf("Couldn't read from pipe: %s", err.Error())
}
if !strings.Contains(string(byt), "Spec") {
t.Fatalf("Test files by giving file name failed, got: %s", string(byt))
}
}