forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration.go
176 lines (163 loc) · 4.16 KB
/
duration.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
package httpexpect
import (
"time"
)
// Duration provides methods to inspect attached time.Duration value.
type Duration struct {
chain chain
value *time.Duration
}
// NewDuration returns a new Duration object given a reporter used to report
// failures and time.Duration value to be inspected.
//
// reporter should not be nil.
//
// Example:
// d := NewDuration(reporter, time.Second)
// d.Le(time.Minute)
func NewDuration(reporter Reporter, value time.Duration) *Duration {
return &Duration{makeChain(reporter), &value}
}
// Raw returns underlying time.Duration value attached to Duration.
// This is the value originally passed to NewDuration.
//
// Example:
// d := NewDuration(t, duration)
// assert.Equal(t, timestamp, d.Raw())
func (d *Duration) Raw() time.Duration {
if d.value == nil {
return 0
}
return *d.value
}
// IsSet succeeds if Duration is set.
//
// Example:
// d := NewDuration(t, time.Second)
// d.IsSet()
func (d *Duration) IsSet() *Duration {
if d.value == nil {
d.chain.fail("expected duration is set, but it is not")
}
return d
}
// NotSet succeeds if Duration is not set.
func (d *Duration) NotSet() *Duration {
if d.value != nil {
d.chain.fail("expected duration is not set, but it is")
}
return d
}
// Equal succeeds if Duration is equal to given value.
//
// Example:
// d := NewDuration(t, time.Second)
// d.Equal(time.Second)
func (d *Duration) Equal(value time.Duration) *Duration {
if d.value == nil {
d.chain.fail("expected duration is set, but it is not")
return d
}
if !(*d.value == value) {
d.chain.fail("\nexpected duration equal to:\n %s\n\nbut got:\n %s",
value, *d.value)
}
return d
}
// NotEqual succeeds if Duration is not equal to given value.
//
// Example:
// d := NewDuration(t, time.Second)
// d.NotEqual(time.Minute)
func (d *Duration) NotEqual(value time.Duration) *Duration {
if d.value == nil {
d.chain.fail("expected duration is set, but it is not")
return d
}
if !(*d.value != value) {
d.chain.fail("\nexpected duration not equal to:\n %s", value)
}
return d
}
// Gt succeeds if Duration is greater than given value.
//
// Example:
// d := NewDuration(t, time.Minute)
// d.Gt(time.Second)
func (d *Duration) Gt(value time.Duration) *Duration {
if d.value == nil {
d.chain.fail("expected duration is set, but it is not")
return d
}
if !(*d.value > value) {
d.chain.fail("\nexpected duration > then:\n %s\n\nbut got:\n %s",
value, *d.value)
}
return d
}
// Ge succeeds if Duration is greater than or equal to given value.
//
// Example:
// d := NewDuration(t, time.Minute)
// d.Ge(time.Second)
func (d *Duration) Ge(value time.Duration) *Duration {
if d.value == nil {
d.chain.fail("expected duration is set, but it is not")
return d
}
if !(*d.value >= value) {
d.chain.fail("\nexpected duration >= then:\n %s\n\nbut got:\n %s",
value, *d.value)
}
return d
}
// Lt succeeds if Duration is lesser than given value.
//
// Example:
// d := NewDuration(t, time.Second)
// d.Lt(time.Minute)
func (d *Duration) Lt(value time.Duration) *Duration {
if d.value == nil {
d.chain.fail("expected duration is set, but it is not")
return d
}
if !(*d.value < value) {
d.chain.fail("\nexpected duration < then:\n %s\n\nbut got:\n %s",
value, *d.value)
}
return d
}
// Le succeeds if Duration is lesser than or equal to given value.
//
// Example:
// d := NewDuration(t, time.Second)
// d.Le(time.Minute)
func (d *Duration) Le(value time.Duration) *Duration {
if d.value == nil {
d.chain.fail("expected duration is set, but it is not")
return d
}
if !(*d.value <= value) {
d.chain.fail("\nexpected duration <= then:\n %s\n\nbut got:\n %s",
value, *d.value)
}
return d
}
// InRange succeeds if Duration is in given range [min; max].
//
// Example:
// d := NewDuration(t, time.Minute)
// d.InRange(time.Second, time.Hour)
// d.InRange(time.Minute, time.Minute)
func (d *Duration) InRange(min, max time.Duration) *Duration {
if d.value == nil {
d.chain.fail("expected duration is set, but it is not")
return d
}
if !(*d.value >= min && *d.value <= max) {
d.chain.fail(
"\nexpected duration in range:\n min: %s\n max: %s\n\nbut got: %s",
min, max, *d.value)
}
return d
}