-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
annotations_test.go
151 lines (118 loc) · 2.85 KB
/
annotations_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
139
140
141
142
143
144
145
146
147
148
149
150
151
package docs
import (
"errors"
"math/rand"
"os"
"path/filepath"
"reflect"
"runtime"
"testing"
"testing/quick"
)
const (
examplesDir = "./examples"
)
func TestUnitMapAnnotationsInPath(t *testing.T) {
t.Parallel()
o := prepForInitCallStack(t)
_ = o.MapAnnotationsInPath(examplesDir)
}
func TestUnitMapAnnotationsInPathErr(t *testing.T) {
t.Parallel()
o := (*OAS)(nil)
err := o.MapAnnotationsInPath(examplesDir)
if err == nil {
t.Error("expected an error, got none")
}
}
func TestUnitScanForChangesInPathErrFnWD(t *testing.T) {
t.Parallel()
wd := func() (dir string, err error) {
return "", errors.New("test")
}
_, err := scanForChangesInPath("", wd, walkFilepath)
if err == nil {
t.Error("expected an error, got none")
}
}
func TestUnitScanForChangesInPathErrWalk(t *testing.T) {
t.Parallel()
wd := func() (dir string, err error) {
return "~@!", nil
}
wdErr := func() (dir string, err error) {
return "~@!", errors.New("walkDir error")
}
pathWalkerErr := func(path string, walker walkerFn) ([]string, error) {
return []string{}, errors.New("triggered")
}
_, err := scanForChangesInPath("", wd, pathWalkerErr)
if err == nil {
t.Error("expected an error, got none")
}
o := prepForInitCallStack(t)
errConfig := configAnnotation{
getWD: wdErr,
}
err = o.MapAnnotationsInPath(".", errConfig)
if err == nil {
t.Error("expected an error, got none")
}
walkFnErr := func(root string, walkFn filepath.WalkFunc) error {
return errors.New("triggered")
}
_, err = walkFilepath("", walkFnErr)
if err == nil {
t.Error("expected an error, got none")
}
}
type triggerNil struct {
trigger bool
}
func TestQuickUnitGetWD(t *testing.T) {
t.Parallel()
config := quick.Config{
Values: func(values []reflect.Value, rand *rand.Rand) {
ca := configAnnotation{}
rndNm := rand.Int()
tn := triggerNil{trigger: false}
if rndNm%2 == 0 {
tn.trigger = true
ca.getWD = func() (dir string, err error) {
return "", nil
}
} else {
ca.getWD = os.Getwd
}
values[0] = reflect.ValueOf(ca)
values[1] = reflect.ValueOf(tn)
},
}
gwdFetcher := func(ca configAnnotation, tn triggerNil) bool {
got := ca.getCurrentDirFetcher()
return reflect.TypeOf(got) == reflect.TypeOf(ca.getWD)
}
if err := quick.Check(gwdFetcher, &config); err != nil {
t.Errorf("Check failed: %#v", err)
}
}
func TestUnitGWD(t *testing.T) {
t.Parallel()
ca := configAnnotation{
getWD: func() (dir string, err error) {
return "", nil
},
}
got := ca.getCurrentDirFetcher()
if reflect.TypeOf(got) != reflect.TypeOf(ca.getWD) {
t.Error("functions differ")
}
caNil := configAnnotation{}.getCurrentDirFetcher()
if gFnName(t, caNil) != gFnName(t, os.Getwd) {
t.Error("functions differ")
}
}
func gFnName(t *testing.T, i interface{}) string {
t.Helper()
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}