forked from goby-lang/goby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goby_test.go
179 lines (136 loc) · 4.05 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
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 {
fmt.Println("Goby binary not found, building")
cmd := exec.Command("go", "build", ".")
err = cmd.Run()
if err != nil {
fmt.Println("Could not build binary\n", err.Error())
panic(err)
}
fmt.Println("Built. Testing ./goby")
} else {
fmt.Println("Using existing Goby binary. Testing ./goby")
}
}
func execGoby(t *testing.T, args ...string) (in io.WriteCloser, out io.ReadCloser) {
exec.Command("rm ./goby")
exec.Command("make build")
cmd := exec.Command("./goby", args...)
var err error
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())
}
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 incorect:\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`)
byt, err := ioutil.ReadAll(out)
if err != nil {
t.Fatalf("Couldn't read from pipe: %s", err.Error())
}
if strings.HasSuffix(string(byt), "hello world\nBye") {
t.Fatalf("Interpreter output incorect")
}
}
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 incorect:\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 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))
}
}