forked from robpike/ivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivy_test.go
138 lines (129 loc) · 3.11 KB
/
ivy_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
// 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 (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"robpike.io/ivy/mobile" // The mobile package has the handy Eval function.
)
// 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)
}
}
dir, err := os.Open("testdata")
check()
names, err := dir.Readdirnames(0)
check()
for _, name := range names {
if !strings.HasSuffix(name, ".ivy") {
continue
}
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, lines)
if input == nil {
break
}
if !runTest(t, path, lineNum, input, output) {
errCount++
if errCount > 3 {
t.Fatal("too many errors")
}
}
lines = lines[length:]
lineNum += length
}
}
}
func runTest(t *testing.T, name string, lineNum int, input, output []string) bool {
shouldFail := strings.HasSuffix(name, "_fail.ivy")
mobile.Reset()
in := strings.Join(input, "\n")
result, err := mobile.Eval(in)
if shouldFail {
if err == nil {
t.Fatalf("\nexpected execution failure at %s:%d:\n%s", name, lineNum, in)
}
return true
}
if err != nil {
t.Fatalf("\nexecution failure (%s) at %s:%d:\n%s", err, name, lineNum, in)
}
if shouldFail {
return true
}
if !equal(strings.Split(result, "\n"), output) {
t.Errorf("\n%s:%d:\n%s\ngot:\n%swant:\n%s",
name, lineNum,
strings.Join(input, "\n"), result, strings.Join(output, "\n"))
return false
}
return true
}
func equal(a, b []string) bool {
// Split leaves an empty traililng 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, lines []string) (input, output []string, length int) {
// Skip blank and initial comment lines.
for _, line := range lines {
if len(line) > 0 && !strings.HasPrefix(line, "#") {
break
}
length++
}
// Input starts in left column.
for _, line := range lines[length:] {
if len(line) == 0 {
t.Fatalf("%s:%d: unexpected empty line", fileName, lineNum+length)
}
if strings.HasPrefix(line, "\t") {
break
}
input = append(input, line)
length++
}
// Output is indented by a tab.
for _, line := range lines[length:] {
length++
if len(line) == 0 {
break
}
if !strings.HasPrefix(line, "\t") {
t.Fatalf("%s:%d: output not indented", fileName, lineNum+length)
}
output = append(output, line[1:])
}
return // Will return nil if no more tests exist.
}