@@ -52,6 +52,38 @@ func False(t Testing, condition bool, messages ...string) bool {
5252 return true
5353}
5454
55+ // Same asserts that two pointers reference the same object.
56+ //
57+ // Both arguments must be pointer variables. Pointer variable equality is
58+ // determined based on the equality of both type and value.
59+ func Same [T Reference ](t Testing , actual , expected T , messages ... string ) bool {
60+ t .Helper ()
61+
62+ if valid , ok := same [T ](expected , actual ); ! ok {
63+ return Failf (t , "%sShould be pointers\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
64+ } else if ! valid {
65+ return Failf (t , "%sShould be same\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
66+ }
67+
68+ return true
69+ }
70+
71+ // NotSame asserts that two pointers do NOT reference the same object.
72+ //
73+ // Both arguments must be pointer variables. Pointer variable equality is
74+ // determined based on the equality of both type and value.
75+ func NotSame [T Reference ](t Testing , actual , expected T , messages ... string ) bool {
76+ t .Helper ()
77+
78+ if valid , ok := same (expected , actual ); ! ok {
79+ Failf (t , "%sShould be pointers\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
80+ } else if valid {
81+ return Failf (t , "%sShould not be same\n actual: %s" , message (messages ), print (actual ))
82+ }
83+
84+ return true
85+ }
86+
5587// Equal asserts that two objects are equal.
5688//
5789// Pointer variable equality is determined based on the equality of the
@@ -93,33 +125,58 @@ func EqualDelta[T Numeric](t Testing, actual, expected, delta T, messages ...str
93125 return true
94126}
95127
96- // Same asserts that two pointers reference the same object .
128+ // NotEqualDelta asserts that two numeric values difference is greater than delta .
97129//
98- // Both arguments must be pointer variables. Pointer variable equality is
99- // determined based on the equality of both type and value.
100- func Same [T Reference ](t Testing , actual , expected T , messages ... string ) bool {
130+ // Panics if delta is negative.
131+ func NotEqualDelta [T Numeric ](t Testing , actual , expected , delta T , messages ... string ) bool {
101132 t .Helper ()
102133
103- if valid , ok := same [T ](expected , actual ); ! ok {
104- return Failf (t , "%sShould be pointers\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
105- } else if ! valid {
106- return Failf (t , "%sShould be same\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
134+ if equalDelta (actual , expected , delta ) {
135+ return Failf (t , "%sShould not be equal in delta:\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
107136 }
108137
109138 return true
110139}
111140
112- // NotSame asserts that two pointers do NOT reference the same object.
113- //
114- // Both arguments must be pointer variables. Pointer variable equality is
115- // determined based on the equality of both type and value.
116- func NotSame [T Reference ](t Testing , actual , expected T , messages ... string ) bool {
141+ // Greater asserts that actual is greater than expected.
142+ func Greater [T Numeric ](t Testing , actual , expected T , messages ... string ) bool {
117143 t .Helper ()
118144
119- if valid , ok := same (expected , actual ); ! ok {
120- Failf (t , "%sShould be pointers\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
121- } else if valid {
122- return Failf (t , "%sShould not be same\n actual: %s" , message (messages ), print (actual ))
145+ if actual <= expected {
146+ return Failf (t , "%sShould be greater\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
147+ }
148+
149+ return true
150+ }
151+
152+ // GreaterOrEqual asserts that actual is greater than or equal to expected.
153+ func GreaterOrEqual [T Numeric ](t Testing , actual , expected T , messages ... string ) bool {
154+ t .Helper ()
155+
156+ if actual < expected {
157+ return Failf (t , "%sShould be greater or equal\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
158+ }
159+
160+ return true
161+ }
162+
163+ // Less asserts that actual is less than expected.
164+ func Less [T Numeric ](t Testing , actual , expected T , messages ... string ) bool {
165+ t .Helper ()
166+
167+ if actual >= expected {
168+ return Failf (t , "%sShould be less\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
169+ }
170+
171+ return true
172+ }
173+
174+ // LessOrEqual asserts that actual is less than or equal to expected.
175+ func LessOrEqual [T Numeric ](t Testing , actual , expected T , messages ... string ) bool {
176+ t .Helper ()
177+
178+ if actual > expected {
179+ return Failf (t , "%sShould be less or equal\n actual: %s\n expected: %s" , message (messages ), print (actual ), print (expected ))
123180 }
124181
125182 return true
@@ -136,6 +193,28 @@ func Length[S Iterable[any]](t Testing, object S, expected int, messages ...stri
136193 return true
137194}
138195
196+ // Empty asserts that object has zero length.
197+ func Empty [S Iterable [any ]](t Testing , object S , messages ... string ) bool {
198+ t .Helper ()
199+
200+ if length (object ) != 0 {
201+ return Failf (t , "%sShould be empty\n object: %#v" , message (messages ), object )
202+ }
203+
204+ return true
205+ }
206+
207+ // NotEmpty asserts that object has non-zero length.
208+ func NotEmpty [S Iterable [any ]](t Testing , object S , messages ... string ) bool {
209+ t .Helper ()
210+
211+ if length (object ) == 0 {
212+ return Failf (t , "%sShould not be empty\n object: %#v" , message (messages ), object )
213+ }
214+
215+ return true
216+ }
217+
139218// Contains asserts that object contains given element.
140219//
141220// Works with strings, arrays, slices, maps values and channels.
@@ -167,21 +246,25 @@ func NotContains[S Iterable[E], E Comparable](t Testing, object S, element E, me
167246}
168247
169248// Error asserts that error is NOT nil.
249+ //
250+ // A typed nil error (e.g. (*MyError)(nil)) is treated as nil.
170251func Error (t Testing , err error , messages ... string ) bool {
171252 t .Helper ()
172253
173- if err == nil {
254+ if isNilError ( err ) {
174255 return Failf (t , "%sShould be error" , message (messages ))
175256 }
176257
177258 return true
178259}
179260
180261// NoError asserts that error is nil.
262+ //
263+ // A typed nil error (e.g. (*MyError)(nil)) is treated as nil.
181264func NoError (t Testing , err error , messages ... string ) bool {
182265 t .Helper ()
183266
184- if err != nil {
267+ if ! isNilError ( err ) {
185268 return Failf (t , "%sShould not be error\n error: %#v" , message (messages ), err )
186269 }
187270
@@ -210,23 +293,6 @@ func NotErrorIs(t Testing, err error, target error, messages ...string) bool {
210293 return true
211294}
212295
213- // EqualJSON asserts that JSON strings are equal.
214- func EqualJSON (t Testing , actual , expected string , messages ... string ) bool {
215- t .Helper ()
216-
217- var actualJSON , expectedJSON any
218-
219- if err := json .Unmarshal ([]byte (actual ), & actualJSON ); err != nil {
220- return Failf (t , "%sShould be valid JSON\n actual: %s\n err: %v" , message (messages ), actual , err )
221- }
222-
223- if err := json .Unmarshal ([]byte (expected ), & expectedJSON ); err != nil {
224- return Failf (t , "%sShould be valid JSON\n expected: %s\n err: %v" , message (messages ), expected , err )
225- }
226-
227- return Equal (t , actualJSON , expectedJSON )
228- }
229-
230296// Matches asserts that a string matches the given regular expression.
231297func Matches (t Testing , actual , pattern string , messages ... string ) bool {
232298 t .Helper ()
@@ -259,6 +325,23 @@ func NotMatches(t Testing, actual, pattern string, messages ...string) bool {
259325 return true
260326}
261327
328+ // EqualJSON asserts that JSON strings are equal.
329+ func EqualJSON (t Testing , actual , expected string , messages ... string ) bool {
330+ t .Helper ()
331+
332+ var actualJSON , expectedJSON any
333+
334+ if err := json .Unmarshal ([]byte (actual ), & actualJSON ); err != nil {
335+ return Failf (t , "%sShould be valid JSON\n actual: %s\n err: %v" , message (messages ), actual , err )
336+ }
337+
338+ if err := json .Unmarshal ([]byte (expected ), & expectedJSON ); err != nil {
339+ return Failf (t , "%sShould be valid JSON\n expected: %s\n err: %v" , message (messages ), expected , err )
340+ }
341+
342+ return Equal (t , actualJSON , expectedJSON )
343+ }
344+
262345// JSON asserts that object can be marshaled to expected JSON string.
263346func JSON (t Testing , actual any , expected string , messages ... string ) bool {
264347 t .Helper ()
0 commit comments