forked from toml-lang/toml-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
212 lines (182 loc) · 4.4 KB
/
results.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/BurntSushi/toml"
"os"
"os/exec"
)
type result struct {
testName string
err error
valid bool
failure string
key string
}
func (r result) errorf(format string, v ...interface{}) result {
r.err = fmt.Errorf(format, v...)
return r
}
func (r result) failedf(format string, v ...interface{}) result {
r.failure = fmt.Sprintf(format, v...)
return r
}
func (r result) mismatch(expected string, got interface{}) result {
return r.failedf("Type mismatch for key '%s'. Expected %s but got %T.",
r.key, expected, got)
}
func (r result) valMismatch(expected string, got string) result {
return r.failedf("Type mismatch for key '%s'. Expected %s but got %s.",
r.key, expected, got)
}
func (r result) kjoin(key string) result {
if len(r.key) == 0 {
r.key = key
} else {
r.key += "." + key
}
return r
}
func (r result) failed() bool {
return r.err != nil || len(r.failure) > 0
}
func (r result) pathTest() string {
ext := "toml"
if flagEncoder {
ext = "json"
}
if r.valid {
return vPath("%s.%s", r.testName, ext)
}
return invPath("%s.%s", r.testName, ext)
}
func (r result) pathGold() string {
if !r.valid {
panic("Invalid tests do not have a 'correct' version.")
}
if flagEncoder {
return vPath("%s.toml", r.testName)
}
return vPath("%s.json", r.testName)
}
func runInvalidTest(name string) result {
r := result{
testName: name,
valid: false,
}
_, stderr, err := runParser(r.pathTest())
if err != nil {
// Errors here are OK if it's just an exit error.
if _, ok := err.(*exec.ExitError); ok {
return r
}
// Otherwise, something has gone horribly wrong.
return r.errorf(err.Error())
}
if stderr != nil { // test has passed!
return r
}
return r.failedf("Expected an error, but no error was reported.")
}
func runValidTest(name string) result {
r := result{
testName: name,
valid: true,
}
stdout, stderr, err := runParser(r.pathTest())
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
switch {
case stderr != nil && stderr.Len() > 0:
return r.failedf(stderr.String())
case stdout != nil && stdout.Len() > 0:
return r.failedf(stdout.String())
}
}
return r.errorf(err.Error())
}
if stdout == nil {
return r.errorf("Parser does not satisfy interface. stdout is " +
"empty, but the process exited successfully.")
}
if flagEncoder {
tomlExpected, err := loadToml(r.pathGold())
if err != nil {
return r.errorf(err.Error())
}
var tomlTest interface{}
if _, err := toml.DecodeReader(stdout, &tomlTest); err != nil {
return r.errorf(
"Could not decode TOML output from encoder: %s", err)
}
return r.cmpToml(tomlExpected, tomlTest)
} else {
jsonExpected, err := loadJson(r.pathGold())
if err != nil {
return r.errorf(err.Error())
}
var jsonTest interface{}
if err := json.NewDecoder(stdout).Decode(&jsonTest); err != nil {
return r.errorf(
"Could not decode JSON output from parser: %s", err)
}
return r.cmpJson(jsonExpected, jsonTest)
}
}
func runParser(testFile string) (*bytes.Buffer, *bytes.Buffer, error) {
f, err := os.Open(testFile)
if err != nil {
return nil, nil, err
}
stdout := new(bytes.Buffer)
stderr := new(bytes.Buffer)
c := exec.Command(parserCmd)
c.Stdin = f
c.Stdout = stdout
c.Stderr = stderr
if err := c.Run(); err != nil {
return stdout, stderr, err
}
return stdout, nil, nil
}
func loadJson(fp string) (interface{}, error) {
fjson, err := os.Open(fp)
if err != nil {
return nil, fmt.Errorf(
"Could not find expected JSON output at %s.", fp)
}
var vjson interface{}
if err := json.NewDecoder(fjson).Decode(&vjson); err != nil {
return nil, fmt.Errorf(
"Could not decode expected JSON output at %s: %s", fp, err)
}
return vjson, nil
}
func loadToml(fp string) (interface{}, error) {
var vtoml interface{}
if _, err := toml.DecodeFile(fp, &vtoml); err != nil {
return nil, fmt.Errorf(
"Could not decode expected TOML output at %s: %s", fp, err)
}
return vtoml, nil
}
func (r result) String() string {
buf := new(bytes.Buffer)
p := func(s string, v ...interface{}) { fmt.Fprintf(buf, s, v...) }
validStr := "invalid"
if r.valid {
validStr = "valid"
}
p("Test: %s (%s)\n\n", r.testName, validStr)
if r.err != nil {
p("Error running test: %s", r.err)
return buf.String()
}
if len(r.failure) > 0 {
p(r.failure)
return buf.String()
}
p("PASSED.")
return buf.String()
}