Skip to content

Commit

Permalink
internal/assert: add >=,>,<=,< assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Sep 1, 2024
1 parent 739703c commit 28bd316
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestReceive(t *testing.T) {

// We must capture the event
assert.NoError(t, err)
assert.True(t, len(data) >= 0)
assert.GreaterOrEqual(t, len(data), 1)
for _, d := range data {
assert.NotEqual(t, string(d.Data), "")
assert.NotEqual(t, string(d.Type), "")
Expand Down Expand Up @@ -80,7 +80,7 @@ func TestSubscribe(t *testing.T) {

assert.Error(t, err)
assert.True(t, errors.Is(err, context.Canceled))
assert.True(t, elapsed >= 100*time.Millisecond)
assert.GreaterOrEqual(t, elapsed, 100*time.Millisecond)
}

func TestProcessEvent(t *testing.T) {
Expand Down
29 changes: 29 additions & 0 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package assert

import (
"cmp"

Check failure on line 4 in internal/assert/assert.go

View workflow job for this annotation

GitHub Actions / build

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/cmp)
"reflect"
"testing"
)
Expand Down Expand Up @@ -71,3 +72,31 @@ func True(t *testing.T, got bool) {
t.Errorf("got: %#v, want: true", got)
}
}

func GreaterOrEqual[T cmp.Ordered](t *testing.T, got, want T) {
t.Helper()
if got < want {
t.Errorf("got: %#v, want: >=%#v", got, want)
}
}

func Greater[T cmp.Ordered](t *testing.T, got, want T) {
t.Helper()
if got <= want {
t.Errorf("got: %#v, want: >%#v", got, want)
}
}

func LesserOrEqual[T cmp.Ordered](t *testing.T, got, want T) {
t.Helper()
if got > want {
t.Errorf("got: %#v, want: <=%#v", got, want)
}
}

func Lesser[T cmp.Ordered](t *testing.T, got, want T) {
t.Helper()
if got >= want {
t.Errorf("got: %#v, want: <%#v", got, want)
}
}

0 comments on commit 28bd316

Please sign in to comment.