Skip to content

Commit

Permalink
internal/assert: add >=,>,<,<= assertion helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokokada committed Sep 4, 2024
1 parent 6d59f50 commit 0f8aa7a
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 @@ -43,7 +43,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 @@ -85,7 +85,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"
"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 LessOrEqual[T cmp.Ordered](t *testing.T, got, want T) {
t.Helper()
if !(got <= want) {
t.Errorf("got: %#v, want: <=%#v", got, want)
}
}

func Less[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 0f8aa7a

Please sign in to comment.