-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathivy_test.go
More file actions
205 lines (188 loc) · 4.77 KB
/
ivy_test.go
File metadata and controls
205 lines (188 loc) · 4.77 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"robpike.io/ivy/config"
"robpike.io/ivy/exec"
"robpike.io/ivy/lib"
"robpike.io/ivy/run"
"robpike.io/ivy/value"
)
const verbose = false
var testConf config.Config
func init() {
value.MaxParallelismForTesting()
lib.Testing(true)
}
// Note: These tests share some infrastructure and cannot run in parallel.
func TestAll(t *testing.T) {
var err error
check := func() {
if err != nil {
t.Fatal(err)
}
}
var debugConf config.Config
value.SetDebugContext(exec.NewContext(&debugConf))
dir, err := os.Open("testdata")
check()
names, err := dir.Readdirnames(0)
check()
for _, name := range names {
if !strings.HasSuffix(name, ".ivy") {
continue
}
t.Log(name)
shouldFail := strings.HasSuffix(name, "_fail.ivy")
var data []byte
path := filepath.Join("testdata", name)
data, err = ioutil.ReadFile(path)
check()
text := string(data)
lines := strings.Split(text, "\n")
// Will have a trailing empty string.
if len(lines) > 0 && lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
lineNum := 1
errCount := 0
for len(lines) > 0 {
// Assemble the input to one example.
input, output, length := getText(t, path, lineNum, shouldFail, lines)
if input == nil {
break
}
if verbose {
fmt.Printf("%s:%d: %s\n", path, lineNum, input)
}
if !runTest(t, path, lineNum, shouldFail, input, output) {
errCount++
if errCount > 5 { // TODO
t.Fatal("too many errors")
return
}
}
lines = lines[length:]
lineNum += length
}
}
}
func runTest(t *testing.T, name string, lineNum int, shouldFail bool, input, output []string) bool {
reset()
in := strings.Join(input, "\n")
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
run.Ivy(exec.NewContext(&testConf), in, stdout, stderr)
if shouldFail {
if stderr.Len() == 0 {
t.Fatalf("\nexpected execution failure at %s:%d:\n%s", name, lineNum, in)
}
for _, line := range input {
if strings.HasPrefix(line, "# Expect: ") {
expect := line[len("# Expect: "):]
if expect != "" && !strings.Contains(stderr.String(), expect) {
t.Errorf("\n%s:%d: missing execution failure message (%s) running\n\t%s\n",
name, lineNum,
expect,
input)
t.Errorf("got:\n\t%s", stderr)
t.Fatalf("expected:\n\t%s\n", expect)
}
}
}
// Tricky corner case: Failed tests with no output have a blank line.
if len(output) == 1 && output[0] == "" {
output = nil
}
} else if stderr.Len() != 0 {
t.Fatalf("\n%s:%d: execution failure (%s) running\n\t%s\n",
name, lineNum,
strings.TrimSpace(stderr.String()), // For final newline.
input)
}
result := strings.Split(stdout.String(), "\n")
if !equal(result, output) {
t.Errorf("\n%s:%d:\n\t%s\ngot:\n\t%s\nwant:\n\t%s",
name, lineNum,
strings.Join(input, "\n\t"),
strings.Join(result, "\n\t"),
strings.Join(output, "\n\t"))
return false
}
return true
}
func equal(a, b []string) bool {
// Split leaves an empty trailing line.
if len(a) > 0 && a[len(a)-1] == "" {
a = a[:len(a)-1]
}
if len(a) != len(b) {
return false
}
for i, s := range a {
if strings.TrimSpace(s) != strings.TrimSpace(b[i]) {
return false
}
}
return true
}
func getText(t *testing.T, fileName string, lineNum int, shouldFail bool, lines []string) (input, output []string, length int) {
// Skip blank and initial comment lines, except keep leading comment for failure checks.
if !shouldFail {
for _, line := range lines {
if len(line) > 0 && !strings.HasPrefix(line, "#") {
break
}
length++
}
}
// Input ends at tab-indented line.
for _, line := range lines[length:] {
line = strings.TrimRight(line, " \t")
if strings.HasPrefix(line, "\t") {
break
}
input = append(input, line)
length++
}
// Output ends at non-blank, non-tab-indented line.
// Indented "#" is expected blank line in output.
for _, line := range lines[length:] {
line = strings.TrimRight(line, " \t")
if line != "" && !strings.HasPrefix(line, "\t") {
break
}
output = append(output, strings.TrimPrefix(line, "\t"))
length++
}
for len(output) > 0 && output[len(output)-1] == "" {
output = output[:len(output)-1]
}
for i, line := range output {
if line == "#" {
output[i] = ""
}
}
return // Will return nil if no more tests exist.
}
func reset() {
testConf = config.Config{}
testConf.SetFloatPrec(256)
testConf.SetFormat("")
testConf.SetMaxBits(1e9)
testConf.SetMaxDigits(1e4)
testConf.SetMaxStack(100000)
testConf.SetOrigin(1)
testConf.SetPrompt("")
testConf.SetBase(0, 0)
testConf.SetRandomSeed(0)
testConf.SetLocation("UTC")
}