From efd9772a0d21b379d35b6e8be2d7d21f9c20b687 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Wed, 4 Sep 2024 13:44:06 +0100 Subject: [PATCH] internal/assert: add >=,>,<,<= assertion helpers --- event/event_test.go | 4 ++-- internal/assert/assert.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/event/event_test.go b/event/event_test.go index b6d4df4..8d8f786 100644 --- a/event/event_test.go +++ b/event/event_test.go @@ -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), "") @@ -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) { diff --git a/internal/assert/assert.go b/internal/assert/assert.go index b6768d6..753ccf0 100644 --- a/internal/assert/assert.go +++ b/internal/assert/assert.go @@ -1,6 +1,7 @@ package assert import ( + "cmp" "reflect" "testing" ) @@ -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) + } +}