diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5321365..ffa1a18 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -18,7 +18,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: '=1.21' - name: Format run: test -z $(gofmt -l .) 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/go.mod b/go.mod index 7b06d4d..3be2d34 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/thiagokokada/hyprland-go retract v0.0.3 // Published too soon without proper event API consideration. -go 1.20 +go 1.21 diff --git a/internal/assert/assert.go b/internal/assert/assert.go index b6768d6..d629410 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) + } +} diff --git a/request.go b/request.go index b0d51f4..932ef88 100644 --- a/request.go +++ b/request.go @@ -138,14 +138,12 @@ func parseResponse(raw RawResponse) (response []Response, err error) { func validateResponse(params []string, response []Response) ([]Response, error) { // Empty response, something went terrible wrong if len(response) == 0 { - return []Response{""}, ValidationError("empty response") + return []Response{}, ValidationError("empty response") } - want := len(params) - if want == 0 { - // commands without parameters will have at least one return - want = 1 - } + // commands without parameters will have at least one return + want := max(len(params), 1) + // we have a different number of requests and responses if want != len(response) { return response, ValidationError(fmt.Sprintf( diff --git a/request_test.go b/request_test.go index 410adb4..288fdf5 100644 --- a/request_test.go +++ b/request_test.go @@ -200,7 +200,7 @@ func TestValidateResponse(t *testing.T) { wantErr bool }{ // empty response should error - {genParams("param", 1), []Response{}, []Response{""}, true}, + {genParams("param", 1), []Response{}, []Response{}, true}, // happy path, nil param {nil, []Response{"ok"}, []Response{"ok"}, false}, // happy path, 1 param