forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration_test.go
314 lines (279 loc) · 7.01 KB
/
duration_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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package httpexpect
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDuration_FailedChain(t *testing.T) {
chain := newMockChain(t, flagFailed)
tm := time.Second
value := newDuration(chain, &tm)
value.chain.assert(t, failure)
value.Alias("foo")
value.IsEqual(tm)
value.NotEqual(tm)
value.InRange(tm, tm)
value.NotInRange(tm, tm)
value.InList(tm)
value.NotInList(tm)
value.Gt(tm)
value.Ge(tm)
value.Lt(tm)
value.Le(tm)
}
func TestDuration_Constructors(t *testing.T) {
tm := time.Second
t.Run("reporter", func(t *testing.T) {
reporter := newMockReporter(t)
value := NewDuration(reporter, tm)
value.IsEqual(tm)
value.chain.assert(t, success)
})
t.Run("config", func(t *testing.T) {
reporter := newMockReporter(t)
value := NewDurationC(Config{
Reporter: reporter,
}, tm)
value.IsEqual(tm)
value.chain.assert(t, success)
})
t.Run("chain", func(t *testing.T) {
chain := newMockChain(t)
value := newDuration(chain, &tm)
assert.NotSame(t, value.chain, chain)
assert.Equal(t, value.chain.context.Path, chain.context.Path)
})
}
func TestDuration_Raw(t *testing.T) {
reporter := newMockReporter(t)
value := NewDuration(reporter, time.Second)
assert.Equal(t, time.Second, value.Raw())
value.chain.assert(t, success)
}
func TestDuration_Alias(t *testing.T) {
reporter := newMockReporter(t)
value := NewDuration(reporter, time.Second)
assert.Equal(t, []string{"Duration()"}, value.chain.context.Path)
assert.Equal(t, []string{"Duration()"}, value.chain.context.AliasedPath)
value.Alias("foo")
assert.Equal(t, []string{"Duration()"}, value.chain.context.Path)
assert.Equal(t, []string{"foo"}, value.chain.context.AliasedPath)
}
func TestDuration_IsEqual(t *testing.T) {
cases := []struct {
name string
duration time.Duration
value time.Duration
wantEqual chainResult
}{
{
name: "compare equivalent durations",
duration: time.Second,
value: time.Second,
wantEqual: success,
},
{
name: "compare non-equivalent durations",
duration: time.Second,
value: time.Minute,
wantEqual: failure,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reporter := newMockReporter(t)
NewDuration(reporter, tc.duration).IsEqual(tc.value).
chain.assert(t, tc.wantEqual)
NewDuration(reporter, tc.duration).NotEqual(tc.value).
chain.assert(t, !tc.wantEqual)
})
}
}
func TestDuration_IsGreater(t *testing.T) {
cases := []struct {
name string
duration time.Duration
value time.Duration
wantGt chainResult
wantGe chainResult
}{
{
name: "duration is lesser",
duration: time.Second,
value: time.Second + 1,
wantGt: failure,
wantGe: failure,
},
{
name: "duration is equal",
duration: time.Second,
value: time.Second,
wantGt: failure,
wantGe: success,
},
{
name: "duration is greater",
duration: time.Second,
value: time.Second - 1,
wantGt: success,
wantGe: success,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reporter := newMockReporter(t)
NewDuration(reporter, tc.duration).Gt(tc.value).
chain.assert(t, tc.wantGt)
NewDuration(reporter, tc.duration).Ge(tc.value).
chain.assert(t, tc.wantGe)
})
}
}
func TestDuration_IsLesser(t *testing.T) {
cases := []struct {
name string
duration time.Duration
value time.Duration
wantLt chainResult
wantLe chainResult
}{
{
name: "duration is lesser",
duration: time.Second,
value: time.Second + 1,
wantLt: success,
wantLe: success,
},
{
name: "duration is equal",
duration: time.Second,
value: time.Second,
wantLt: failure,
wantLe: success,
},
{
name: "duration is greater",
duration: time.Second,
value: time.Second - 1,
wantLt: failure,
wantLe: failure,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reporter := newMockReporter(t)
NewDuration(reporter, tc.duration).Lt(tc.value).
chain.assert(t, tc.wantLt)
NewDuration(reporter, tc.duration).Le(tc.value).
chain.assert(t, tc.wantLe)
})
}
}
func TestDuration_InRange(t *testing.T) {
cases := []struct {
name string
value time.Duration
min time.Duration
max time.Duration
wantInRange chainResult
wantNotInRange chainResult
}{
{
name: "value equal to both min and max",
value: time.Second,
min: time.Second,
max: time.Second,
wantInRange: success,
wantNotInRange: failure,
},
{
name: "value greater than min and equal to max",
value: time.Second,
min: time.Second - 1,
max: time.Second,
wantInRange: success,
wantNotInRange: failure,
},
{
name: "value equal to min and smaller than max",
value: time.Second,
min: time.Second,
max: time.Second + 1,
wantInRange: success,
wantNotInRange: failure,
},
{
name: "value smaller than min",
value: time.Second,
min: time.Second + 1,
max: time.Second + 2,
wantInRange: failure,
wantNotInRange: success,
},
{
name: "value greater than max",
value: time.Second,
min: time.Second - 2,
max: time.Second - 1,
wantInRange: failure,
wantNotInRange: success,
},
{
name: "min smaller than max",
value: time.Second,
min: time.Second + 1,
max: time.Second - 1,
wantInRange: failure,
wantNotInRange: success,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reporter := newMockReporter(t)
NewDuration(reporter, tc.value).InRange(tc.min, tc.max).
chain.assert(t, tc.wantInRange)
NewDuration(reporter, tc.value).NotInRange(tc.min, tc.max).
chain.assert(t, tc.wantNotInRange)
})
}
}
func TestDuration_InList(t *testing.T) {
cases := []struct {
name string
value time.Duration
list []time.Duration
wantInList chainResult
wantNotInList chainResult
}{
{
name: "empty list",
value: time.Second,
list: []time.Duration{},
wantInList: failure,
wantNotInList: failure,
},
{
name: "value present in list",
value: time.Second,
list: []time.Duration{time.Second, time.Minute},
wantInList: success,
wantNotInList: failure,
},
{
name: "value not present in list",
value: time.Second,
list: []time.Duration{time.Second - 1, time.Second + 1},
wantInList: failure,
wantNotInList: success,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
reporter := newMockReporter(t)
NewDuration(reporter, tc.value).InList(tc.list...).
chain.assert(t, tc.wantInList)
NewDuration(reporter, tc.value).NotInList(tc.list...).
chain.assert(t, tc.wantNotInList)
})
}
}