-
Notifications
You must be signed in to change notification settings - Fork 0
/
revip_test.go
171 lines (149 loc) · 4 KB
/
revip_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package revip
import (
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
type (
TestConfig struct {
Name string `yaml:"name"`
Amount int `yaml:"amount"`
Provider *TestProviderConfig `yaml:"provider"`
nonce int
}
TestProviderConfig struct {
Type string `yaml:"type"`
Simple *TestSimpleProviderConfig `yaml:"simple"`
Inline *TestInlineProviderConfig `yaml:"inline"`
}
TestBaseProviderConfig struct {
Rate int `yaml:"rate"`
Auto *bool `yaml:"auto"`
Actions []*TestActionConfig `yaml:"actions"`
Handlers map[string]*TestHandlerConfig `yaml:"handlers"`
}
TestActionConfig struct {
Name string `yaml:"name"`
}
TestHandlerConfig struct {
Name string `yaml:"name"`
}
TestSimpleProviderConfig struct {
Base *TestBaseProviderConfig `yaml:"base"`
}
TestInlineProviderConfig struct {
Base *TestBaseProviderConfig `yaml:",inline"`
}
)
func (c *TestConfig) Default() {
if c.Amount == 0 {
c.Amount = 10
}
if c.Provider == nil {
c.Provider = &TestProviderConfig{}
}
}
func (c *TestProviderConfig) Default() {
if c.Type == "" {
c.Type = "simple"
}
if c.Type == "simple" && c.Simple == nil {
c.Simple = &TestSimpleProviderConfig{}
}
if c.Type == "inline" && c.Inline == nil {
c.Inline = &TestInlineProviderConfig{Base: &TestBaseProviderConfig{}}
}
}
func (c *TestBaseProviderConfig) Default() {
if len(c.Handlers) == 0 {
c.Handlers = map[string]*TestHandlerConfig{
"/": {Name: "root"},
}
}
}
func (c *TestProviderConfig) Validate() error {
if c.Type == "" {
return errors.New("type should not be empty")
}
return nil
}
func (c *TestConfig) Validate() error {
if c.Name == "" {
return errors.New("name should not be empty")
}
return nil
}
func (c *TestConfig) Expand() error {
c.nonce += 1
return nil
}
//
func TestConfigDefaults(t *testing.T) {
c := &TestConfig{}
err := Postprocess(c, WithDefaults())
if err != nil {
t.Error(err)
}
assert.Equal(t, "", c.Name)
assert.Equal(t, 10, c.Amount)
assert.NotNil(t, c.Provider)
assert.Equal(t, "simple", c.Provider.Type)
assert.NotNil(t, c.Provider.Simple)
assert.Nil(t, c.Provider.Inline)
assert.Nil(t, c.Provider.Simple.Base)
}
func TestConfigDefaultsDependant(t *testing.T) {
c := &TestConfig{Provider: &TestProviderConfig{Type: "inline"}}
err := Postprocess(c, WithDefaults())
if err != nil {
t.Error(err)
}
assert.Equal(t, "", c.Name)
assert.Equal(t, 10, c.Amount)
assert.NotNil(t, c.Provider)
assert.Equal(t, "inline", c.Provider.Type)
assert.Nil(t, c.Provider.Simple)
assert.NotNil(t, c.Provider.Inline)
assert.NotNil(t, c.Provider.Inline.Base)
assert.Equal(t, "root", c.Provider.Inline.Base.Handlers["/"].Name)
}
//
func TestConfigValidation(t *testing.T) {
c := &TestConfig{}
err := Postprocess(c, WithValidation())
assert.NotNil(t, err)
assert.Equal(t, "postprocessing failed at .TestConfig: name should not be empty", err.Error())
c.Name = "foo" // should not enter nil configurations
err = Postprocess(c, WithValidation())
assert.Nil(t, err)
c.Provider = &TestProviderConfig{}
err = Postprocess(c, WithValidation())
assert.NotNil(t, err)
assert.Equal(t, "postprocessing failed at .TestConfig.TestConfig.Provider: type should not be empty", err.Error())
}
//
func TestConfigNoNilPointers(t *testing.T) {
c := &TestConfig{}
err := Postprocess(c, WithNoNilPointers())
if err != nil {
t.Error(err)
}
assert.NotNil(t, c.Provider)
assert.NotNil(t, c.Provider.Simple)
assert.NotNil(t, c.Provider.Simple.Base)
assert.NotNil(t, c.Provider.Simple.Base.Actions)
assert.NotNil(t, c.Provider.Simple.Base.Handlers)
assert.NotNil(t, c.Provider.Inline)
assert.NotNil(t, c.Provider.Inline.Base)
assert.NotNil(t, c.Provider.Inline.Base.Actions)
assert.NotNil(t, c.Provider.Inline.Base.Handlers)
}
//
func TestConfigExpansion(t *testing.T) {
c := &TestConfig{}
err := Postprocess(c, WithExpansion())
if err != nil {
t.Error(err)
}
assert.Equal(t, 1, c.nonce)
}