number.go
5.9 KB
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
package httpexpect
import (
"math"
)
// Number provides methods to inspect attached float64 value
// (Go representation of JSON number).
type Number struct {
chain chain
value float64
}
// NewNumber returns a new Number given a reporter used to report
// failures and value to be inspected.
//
// reporter should not be nil.
//
// Example:
// number := NewNumber(t, 123.4)
func NewNumber(reporter Reporter, value float64) *Number {
return &Number{makeChain(reporter), value}
}
// Raw returns underlying value attached to Number.
// This is the value originally passed to NewNumber.
//
// Example:
// number := NewNumber(t, 123.4)
// assert.Equal(t, 123.4, number.Raw())
func (n *Number) Raw() float64 {
return n.value
}
// Path is similar to Value.Path.
func (n *Number) Path(path string) *Value {
return getPath(&n.chain, n.value, path)
}
// Schema is similar to Value.Schema.
func (n *Number) Schema(schema interface{}) *Number {
checkSchema(&n.chain, n.value, schema)
return n
}
// Equal succeeds if number is equal to given value.
//
// value should have numeric type convertible to float64. Before comparison,
// it is converted to float64.
//
// Example:
// number := NewNumber(t, 123)
// number.Equal(float64(123))
// number.Equal(int32(123))
func (n *Number) Equal(value interface{}) *Number {
v, ok := canonNumber(&n.chain, value)
if !ok {
return n
}
if !(n.value == v) {
n.chain.fail("\nexpected number equal to:\n %v\n\nbut got:\n %v",
v, n.value)
}
return n
}
// NotEqual succeeds if number is not equal to given value.
//
// value should have numeric type convertible to float64. Before comparison,
// it is converted to float64.
//
// Example:
// number := NewNumber(t, 123)
// number.NotEqual(float64(321))
// number.NotEqual(int32(321))
func (n *Number) NotEqual(value interface{}) *Number {
v, ok := canonNumber(&n.chain, value)
if !ok {
return n
}
if !(n.value != v) {
n.chain.fail("\nexpected number not equal to:\n %v\n\nbut got:\n %v",
v, n.value)
}
return n
}
// EqualDelta succeeds if two numerals are within delta of each other.
//
// Example:
// number := NewNumber(t, 123.0)
// number.EqualDelta(123.2, 0.3)
func (n *Number) EqualDelta(value, delta float64) *Number {
if math.IsNaN(n.value) || math.IsNaN(value) || math.IsNaN(delta) {
n.chain.fail("\nexpected number equal to:\n %v\n\nbut got:\n %v\n\ndelta:\n %v",
value, n.value, delta)
return n
}
diff := (n.value - value)
if diff < -delta || diff > delta {
n.chain.fail("\nexpected number equal to:\n %v\n\nbut got:\n %v\n\ndelta:\n %v",
value, n.value, delta)
return n
}
return n
}
// NotEqualDelta succeeds if two numerals are not within delta of each other.
//
// Example:
// number := NewNumber(t, 123.0)
// number.NotEqualDelta(123.2, 0.1)
func (n *Number) NotEqualDelta(value, delta float64) *Number {
if math.IsNaN(n.value) || math.IsNaN(value) || math.IsNaN(delta) {
n.chain.fail(
"\nexpected number not equal to:\n %v\n\nbut got:\n %v\n\ndelta:\n %v",
value, n.value, delta)
return n
}
diff := (n.value - value)
if !(diff < -delta || diff > delta) {
n.chain.fail(
"\nexpected number not equal to:\n %v\n\nbut got:\n %v\n\ndelta:\n %v",
value, n.value, delta)
return n
}
return n
}
// Gt succeeds if number is greater than given value.
//
// value should have numeric type convertible to float64. Before comparison,
// it is converted to float64.
//
// Example:
// number := NewNumber(t, 123)
// number.Gt(float64(122))
// number.Gt(int32(122))
func (n *Number) Gt(value interface{}) *Number {
v, ok := canonNumber(&n.chain, value)
if !ok {
return n
}
if !(n.value > v) {
n.chain.fail("\nexpected number > then:\n %v\n\nbut got:\n %v",
v, n.value)
}
return n
}
// Ge succeeds if number is greater than or equal to given value.
//
// value should have numeric type convertible to float64. Before comparison,
// it is converted to float64.
//
// Example:
// number := NewNumber(t, 123)
// number.Ge(float64(122))
// number.Ge(int32(122))
func (n *Number) Ge(value interface{}) *Number {
v, ok := canonNumber(&n.chain, value)
if !ok {
return n
}
if !(n.value >= v) {
n.chain.fail("\nexpected number >= then:\n %v\n\nbut got:\n %v",
v, n.value)
}
return n
}
// Lt succeeds if number is lesser than given value.
//
// value should have numeric type convertible to float64. Before comparison,
// it is converted to float64.
//
// Example:
// number := NewNumber(t, 123)
// number.Lt(float64(124))
// number.Lt(int32(124))
func (n *Number) Lt(value interface{}) *Number {
v, ok := canonNumber(&n.chain, value)
if !ok {
return n
}
if !(n.value < v) {
n.chain.fail("\nexpected number < then:\n %v\n\nbut got:\n %v",
v, n.value)
}
return n
}
// Le succeeds if number is lesser than or equal to given value.
//
// value should have numeric type convertible to float64. Before comparison,
// it is converted to float64.
//
// Example:
// number := NewNumber(t, 123)
// number.Le(float64(124))
// number.Le(int32(124))
func (n *Number) Le(value interface{}) *Number {
v, ok := canonNumber(&n.chain, value)
if !ok {
return n
}
if !(n.value <= v) {
n.chain.fail("\nexpected number <= then:\n %v\n\nbut got:\n %v",
v, n.value)
}
return n
}
// InRange succeeds if number is in given range [min; max].
//
// min and max should have numeric type convertible to float64. Before comparison,
// they are converted to float64.
//
// Example:
// number := NewNumber(t, 123)
// number.InRange(float32(100), int32(200)) // success
// number.InRange(100, 200) // success
// number.InRange(123, 123) // success
func (n *Number) InRange(min, max interface{}) *Number {
a, ok := canonNumber(&n.chain, min)
if !ok {
return n
}
b, ok := canonNumber(&n.chain, max)
if !ok {
return n
}
if !(n.value >= a && n.value <= b) {
n.chain.fail("\nexpected number in range:\n [%v; %v]\n\nbut got:\n %v",
a, b, n.value)
}
return n
}