Skip to content

Commit f8af82f

Browse files
committed
feat: add comparison assert functions.
1 parent 9d75e99 commit f8af82f

File tree

4 files changed

+629
-1
lines changed

4 files changed

+629
-1
lines changed

builtin.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package assert
22

33
import (
4+
"fmt"
45
"regexp"
56
"testing"
67
)
@@ -86,6 +87,176 @@ func ContainsStringNow(t *testing.T, str, substr string, message ...any) error {
8687
return tryContainsString(t, true, str, substr, message...)
8788
}
8889

90+
// Gt compares the values and sets the result to false if the first value is not greater than to
91+
// the second value.
92+
//
93+
// Gt(t, 2, 1) // success
94+
// Gt(t, 3.14, 1.68) // success
95+
// Gt(t, "BCD", "ABC") // success
96+
// Gt(t, 2, 2) // fail
97+
// Gt(t, 1, 2) // fail
98+
func Gt(t *testing.T, v1, v2 any, message ...string) error {
99+
t.Helper()
100+
101+
return tryCompareOrderableValues(
102+
t,
103+
false,
104+
compareTypeGreater,
105+
v1, v2,
106+
fmt.Sprintf(defaultErrMessageGt, v1, v2),
107+
message...,
108+
)
109+
}
110+
111+
// GtNow compares the values and sets the result to false if the first value is not greater than to
112+
// the second value. It will panic if they do not match the expected result.
113+
//
114+
// GtNow(t, 2, 1) // success
115+
// GtNow(t, 3.14, 1.68) // success
116+
// GtNow(t, "BCD", "ABC") // success
117+
// GtNow(t, 1, 2) // fail and terminate
118+
// // never runs
119+
func GtNow(t *testing.T, v1, v2 any, message ...string) error {
120+
t.Helper()
121+
122+
return tryCompareOrderableValues(
123+
t,
124+
true,
125+
compareTypeGreater,
126+
v1, v2,
127+
fmt.Sprintf(defaultErrMessageGt, v1, v2),
128+
message...,
129+
)
130+
}
131+
132+
// Gte compares the values and sets the result to false if the first value is not greater than or
133+
// equal to the second value.
134+
//
135+
// Gte(t, 2, 1) // success
136+
// Gte(t, 3.14, 1.68) // success
137+
// Gte(t, "BCD", "ABC") // success
138+
// Gte(t, 2, 2) // success
139+
// Gte(t, 1, 2) // fail
140+
func Gte(t *testing.T, v1, v2 any, message ...string) error {
141+
t.Helper()
142+
143+
return tryCompareOrderableValues(
144+
t,
145+
false,
146+
compareTypeEqual|compareTypeGreater,
147+
v1, v2,
148+
fmt.Sprintf(defaultErrMessageGte, v1, v2),
149+
message...,
150+
)
151+
}
152+
153+
// GteNow compares the values and sets the result to false if the first value is not greater than
154+
// or equal to the second value. It will panic if they do not match the expected result.
155+
//
156+
// GteNow(t, 2, 1) // success
157+
// GteNow(t, 3.14, 1.68) // success
158+
// GteNow(t, "BCD", "ABC") // success
159+
// GteNow(t, 2, 2) // success
160+
// GteNow(t, 1, 2) // fail and terminate
161+
// // never runs
162+
func GteNow(t *testing.T, v1, v2 any, message ...string) error {
163+
t.Helper()
164+
165+
return tryCompareOrderableValues(
166+
t,
167+
true,
168+
compareTypeEqual|compareTypeGreater,
169+
v1, v2,
170+
fmt.Sprintf(defaultErrMessageGte, v1, v2),
171+
message...,
172+
)
173+
}
174+
175+
// Lt compares the values and sets the result to false if the first value is not less than the
176+
// second value.
177+
//
178+
// Lt(t, 1, 2) // success
179+
// Lt(t, 1.68, 3.14) // success
180+
// Lt(t, "ABC", "BCD") // success
181+
// Lt(t, 2, 2) // fail
182+
// Lt(t, 2, 1) // fail
183+
func Lt(t *testing.T, v1, v2 any, message ...string) error {
184+
t.Helper()
185+
186+
return tryCompareOrderableValues(
187+
t,
188+
false,
189+
compareTypeLess,
190+
v1, v2,
191+
fmt.Sprintf(defaultErrMessageLt, v1, v2),
192+
message...,
193+
)
194+
}
195+
196+
// LtNow compares the values and sets the result to false if the first value is not less than the
197+
// second value. It will panic if they do not match the expected result.
198+
//
199+
// LtNow(t, 1, 2) // success
200+
// LtNow(t, 1.68, 3.14) // success
201+
// LtNow(t, "ABC", "BCD") // success
202+
// LtNow(t, 2, 1) // fail and terminate
203+
// // never runs
204+
func LtNow(t *testing.T, v1, v2 any, message ...string) error {
205+
t.Helper()
206+
207+
return tryCompareOrderableValues(
208+
t,
209+
true,
210+
compareTypeLess,
211+
v1, v2,
212+
fmt.Sprintf(defaultErrMessageLt, v1, v2),
213+
message...,
214+
)
215+
}
216+
217+
// Lte compares the values and sets the result to false if the first value is not less than or
218+
// equal to the second value.
219+
//
220+
// Lte(t, 1, 2) // success
221+
// Lte(t, 1.68, 3.14) // success
222+
// Lte(t, "ABC", "BCD") // success
223+
// Lte(t, 2, 2) // success
224+
// Lte(t, 2, 1) // fail
225+
func Lte(t *testing.T, v1, v2 any, message ...string) error {
226+
t.Helper()
227+
228+
return tryCompareOrderableValues(
229+
t,
230+
false,
231+
compareTypeEqual|compareTypeLess,
232+
v1, v2,
233+
fmt.Sprintf(defaultErrMessageLte, v1, v2),
234+
message...,
235+
)
236+
}
237+
238+
// LteNow compares the values and sets the result to false if the first value is not less than or
239+
// equal to the second value. It will panic if they do not match the expected result.
240+
//
241+
// LteNow(t, 1, 2) // success
242+
// LteNow(t, 1.68, 3.14) // success
243+
// LteNow(t, "ABC", "BCD") // success
244+
// LteNow(t, 2, 2) // success
245+
// LteNow(t, 2, 1) // fail and terminate
246+
// // never runs
247+
func LteNow(t *testing.T, v1, v2 any, message ...string) error {
248+
t.Helper()
249+
250+
return tryCompareOrderableValues(
251+
t,
252+
true,
253+
compareTypeEqual|compareTypeLess,
254+
v1, v2,
255+
fmt.Sprintf(defaultErrMessageLte, v1, v2),
256+
message...,
257+
)
258+
}
259+
89260
// NotContainsString tests whether the string contains the substring or not, and it set the result
90261
// to fail if the string contains the substring.
91262
//

error.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const (
2828
defaultErrMessageNotMapHasKey string = "expect map has no key %v"
2929
defaultErrMessageMapHasValue string = "expect map has value %v"
3030
defaultErrMessageNotMapHasValue string = "expect map has no value %v"
31+
defaultErrMessageGt string = "%v must greater than %v"
32+
defaultErrMessageGte string = "%v must greater than or equal to %v"
33+
defaultErrMessageLt string = "%v must less then %v"
34+
defaultErrMessageLte string = "%v must less then or equal to %v"
3135
)
3236

3337
var (

0 commit comments

Comments
 (0)