-
Notifications
You must be signed in to change notification settings - Fork 72
/
runner_test.go
110 lines (99 loc) · 2.44 KB
/
runner_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
package tomltest
import (
"context"
"fmt"
"os"
"strings"
"testing"
"testing/fstest"
)
func notInList(t *testing.T, list []string, str string) {
t.Helper()
for _, item := range list {
if item == str {
t.Fatalf("error: %q in list", str)
}
}
}
func TestVersion(t *testing.T) {
_, err := Runner{Version: "0.9", Files: os.DirFS("./tests")}.Run()
if err == nil {
t.Fatal("expected an error for version 0.9")
}
r := Runner{Version: "1.0.0", Files: os.DirFS("./tests")}
ls, err := r.List()
if err != nil {
t.Fatal()
}
notInList(t, ls, "valid/string/escape-esc")
r = Runner{Version: "1.0.0", Files: os.DirFS("./tests")}
ls, err = r.List()
if err != nil {
t.Fatal()
}
notInList(t, ls, "valid/string/escape-esc")
}
type testParser struct{}
func (t *testParser) Encode(ctx context.Context, input string) (output string, outputIsError bool, err error) {
switch input {
case `a=1`:
return `{"a": {"type":"integer","value":"1"}}`, false, nil
case `a=`, `c=`:
return `oh noes: error one`, true, nil
case `b=`:
return `error two`, true, nil
default:
panic(fmt.Sprintf("unreachable: %q", input))
}
}
func (t testParser) Decode(ctx context.Context, input string) (string, bool, error) {
return t.Encode(ctx, input)
}
func TestErrors(t *testing.T) {
r := Runner{
Parser: &testParser{},
Files: fstest.MapFS{
"valid/a.toml": &fstest.MapFile{Data: []byte(`a=1`)},
"valid/a.json": &fstest.MapFile{Data: []byte(`{"a": {"type":"integer","value":"1"}}`)},
"invalid/a.toml": &fstest.MapFile{Data: []byte(`a=`)},
"invalid/b.toml": &fstest.MapFile{Data: []byte(`b=`)},
"invalid/dir/c.toml": &fstest.MapFile{Data: []byte(`c=`)},
},
Errors: map[string]string{
"invalid/a": "oh noes",
"invalid/b": "don't match",
"dir/c.toml": "oh noes",
},
}
tt, err := r.Run()
if err != nil {
t.Error(err)
}
for _, test := range tt.Tests {
if test.Path == "invalid/b" {
if !test.Failed() {
t.Errorf("expected failure for %q, but got none", test.Path)
}
continue
}
if test.Failed() {
t.Errorf("\n%s: %s", test.Path, test.Failure)
}
}
t.Run("non-existent", func(t *testing.T) {
r := Runner{
Parser: &testParser{},
Files: fstest.MapFS{},
Errors: map[string]string{
"file/doesn/exist": "oh noes",
},
}
_, err := r.Run()
if err == nil {
t.Fatal("error is nil")
}
if !strings.Contains(err.Error(), "didn't match anything") {
t.Fatalf("wrong error: %s", err)
}
})
}