Skip to content

Commit 4a8efcb

Browse files
committed
Less and Greater for numbers + example tests
1 parent af3ea4b commit 4a8efcb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

assert/assert.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,32 @@ func Error(err error, a ...any) {
865865
}
866866
}
867867

868+
// Greater asserts that the value is greater than want. If it is not it panics
869+
// and builds a violation message. Thanks to inlining, the performance penalty
870+
// is equal to a single 'if-statement' that is almost nothing.
871+
//
872+
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
873+
// are used to override the auto-generated assert violation message.
874+
func Greater[T Number](val, want T, a ...any) {
875+
if val <= want {
876+
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want <= '%v'", val, want)
877+
current().reportAssertionFault(defMsg, a)
878+
}
879+
}
880+
881+
// Less asserts that the value is less than want. If it is not it panics and
882+
// builds a violation message. Thanks to inlining, the performance penalty is
883+
// equal to a single 'if-statement' that is almost nothing.
884+
//
885+
// Note that when [Plain] asserter is used ([SetDefault]), optional arguments
886+
// are used to override the auto-generated assert violation message.
887+
func Less[T Number](val, want T, a ...any) {
888+
if val >= want {
889+
defMsg := fmt.Sprintf(assertionMsg+": got '%v', want >= '%v'", val, want)
890+
current().reportAssertionFault(defMsg, a)
891+
}
892+
}
893+
868894
// Zero asserts that the value is 0. If it is not it panics and builds a
869895
// violation message. Thanks to inlining, the performance penalty is equal to a
870896
// single 'if-statement' that is almost nothing.

assert/assert_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,36 @@ func ExampleSShorter() {
191191
// Output: sample: assert_test.go:186: ExampleSShorter.func1(): assertion violation: got '1', should be shorter than '0': optional message (test_str)
192192
}
193193

194+
func ExampleLess() {
195+
sample := func(b int8) (err error) {
196+
defer err2.Handle(&err, "sample")
197+
198+
assert.Equal(b, 1) // ok
199+
assert.Less(b, 2) // ok
200+
assert.Less(b, 1) // not ok
201+
return err
202+
}
203+
var b int8 = 1
204+
err := sample(b)
205+
fmt.Printf("%v", err)
206+
// Output: sample: assert_test.go:200: ExampleLess.func1(): assertion violation: got '1', want >= '1'
207+
}
208+
209+
func ExampleGreater() {
210+
sample := func(b int8) (err error) {
211+
defer err2.Handle(&err, "sample")
212+
213+
assert.Equal(b, 2) // ok
214+
assert.Greater(b, 1) // ok
215+
assert.Greater(b, 2) // not ok
216+
return err
217+
}
218+
var b int8 = 2
219+
err := sample(b)
220+
fmt.Printf("%v", err)
221+
// Output: sample: assert_test.go:215: ExampleGreater.func1(): assertion violation: got '2', want <= '2'
222+
}
223+
194224
func assertZero(i int) {
195225
assert.Zero(i)
196226
}

0 commit comments

Comments
 (0)