-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrefs_test.go
133 lines (118 loc) · 2.62 KB
/
refs_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
package main
import (
"bytes"
"encoding/json"
"log"
"os"
"path/filepath"
"reflect"
"sort"
"testing"
)
func assertString(t *testing.T, got, expected string) bool {
if got == expected {
return true
}
t.Errorf("got: %q, expected: %q", got, expected)
return false
}
func TestLoc(t *testing.T) {
t.Logf(".URL().String()")
assertString(t, (&loc{Path: "x.yml"}).URL().String(), "x.yml")
assertString(t, (&loc{Path: "/tmp/x.yml", Ptr: "/info"}).URL().String(), "file:///tmp/x.yml#/info")
t.Logf(".String()")
assertString(t, (&loc{Path: "x.yml"}).String(), "x.yml")
assertString(t, (&loc{Path: "/tmp/x.yml", Ptr: "/info"}).String(), "/tmp/x.yml#/info")
}
func TestExpandRefs(t *testing.T) {
runAllExpandRefs(t)
}
func BenchmarkExpandRefs(b *testing.B) {
runAllExpandRefs(b)
}
func runAllExpandRefs(t interface {
testing.TB
}) {
dir, err := os.Open("testdata")
if err != nil {
log.Fatal(err)
}
all, err := dir.Readdir(-1)
dir.Close()
if err != nil {
log.Fatal(err)
}
sort.SliceStable(all, func(i, j int) bool {
return all[i].Name() < all[j].Name()
})
for _, f := range all {
if !f.IsDir() {
continue
}
name := f.Name()
if name[0] < '0' || name[0] > '9' {
continue
}
switch t := t.(type) {
case *testing.T:
t.Run(name, func(t *testing.T) {
runExpandRefs(t, "testdata/"+name)
})
case *testing.B:
runExpandRefs(t, "testdata/"+name)
}
}
}
func runExpandRefs(t testing.TB, path string) {
var inputPath string
for _, ext := range []string{".yml", ".yaml", ".json"} {
p := path + "/input" + ext
t.Log(p)
_, err := os.Stat(p)
if err == nil {
inputPath = p
break
}
if os.IsNotExist(err) {
continue
}
log.Fatalf("%s: %v", p, err)
}
if inputPath == "" {
t.Fatal("no input file")
}
expected, err := loadFile(filepath.Join(filepath.FromSlash(path), "result.json"))
if err != nil {
t.Fatalf("%s/result.json: %v", path, err)
}
switch tb := t.(type) {
case *testing.T:
var out interface{}
err = processFile(inputPath, func(result interface{}) error {
out = result
return nil
}, &debugFlags{})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(out, expected) {
b, err := json.Marshal(out)
_ = err
var bFmt bytes.Buffer
json.Indent(&bFmt, b, "", " ")
t.Errorf("output doesn't match:\n%s", bFmt.String())
}
case *testing.B:
for i := 0; i < tb.N; i++ {
_ = processFile(inputPath, func(interface{}) error {
return nil
}, &debugFlags{})
}
}
}
func TestInlineIndirect(t *testing.T) {
runExpandRefs(t, "testdata/41-inline-indirect")
}
func Benchmark43(b *testing.B) {
runExpandRefs(b, "testdata/43-inline-overrides-deep")
}