Skip to content

Commit

Permalink
test: Simplify asserts by using ElementsMatch (#1442)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Starr-Bochicchio <[email protected]>
  • Loading branch information
alexandear and andrewsomething authored Oct 17, 2023
1 parent 5d2bcd0 commit 86fc331
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 41 deletions.
5 changes: 1 addition & 4 deletions commands/activations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package commands
import (
"bytes"
"errors"
"sort"
"strconv"
"strings"
"testing"
Expand All @@ -37,9 +36,7 @@ func TestActivationsCommand(t *testing.T) {
names = append(names, c.Name())
}

sort.Strings(expected)
sort.Strings(names)
assert.Equal(t, expected, names)
assert.ElementsMatch(t, expected, names)
}

var hello1Result = whisk.Result(map[string]interface{}{
Expand Down
5 changes: 1 addition & 4 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package commands

import (
"io"
"sort"
"testing"

"github.com/digitalocean/doctl"
Expand Down Expand Up @@ -140,9 +139,7 @@ func assertCommandNames(t *testing.T, cmd *Command, expected ...string) {
}
}

sort.Strings(expected)
sort.Strings(names)
assert.Equal(t, expected, names)
assert.ElementsMatch(t, expected, names)
}

type testFn func(c *CmdConfig, tm *tcMocks)
Expand Down
5 changes: 1 addition & 4 deletions commands/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package commands
import (
"bytes"
"os"
"sort"
"strings"
"testing"
"time"
Expand All @@ -37,9 +36,7 @@ func TestFunctionsCommand(t *testing.T) {
names = append(names, c.Name())
}

sort.Strings(expected)
sort.Strings(names)
assert.Equal(t, expected, names)
assert.ElementsMatch(t, expected, names)
}

func TestFunctionsGet(t *testing.T) {
Expand Down
5 changes: 1 addition & 4 deletions commands/namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"bytes"
"context"
"errors"
"sort"
"testing"

"github.com/digitalocean/doctl/do"
Expand All @@ -35,9 +34,7 @@ func TestNamespacesCommand(t *testing.T) {
names = append(names, c.Name())
}

sort.Strings(expected)
sort.Strings(names)
assert.Equal(t, expected, names)
assert.ElementsMatch(t, expected, names)
}

func TestNamespacesCreate(t *testing.T) {
Expand Down
5 changes: 1 addition & 4 deletions commands/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package commands
import (
"bytes"
"context"
"sort"
"testing"
"time"

Expand All @@ -35,9 +34,7 @@ func TestTriggersCommand(t *testing.T) {
names = append(names, c.Name())
}

sort.Strings(expected)
sort.Strings(names)
assert.Equal(t, expected, names)
assert.ElementsMatch(t, expected, names)
}

func TestTriggersGet(t *testing.T) {
Expand Down
28 changes: 7 additions & 21 deletions pkg/listen/listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ var (
func wsHandler(t *testing.T) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)
defer c.Close()
i := 0
finish := 5
Expand All @@ -43,9 +41,7 @@ func wsHandler(t *testing.T) http.HandlerFunc {
json.NewEncoder(buf).Encode(data)

err = c.WriteMessage(websocket.TextMessage, buf.Bytes())
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)

if i == finish {
break
Expand All @@ -60,17 +56,13 @@ func TestListener(t *testing.T) {

u := "ws" + strings.TrimPrefix(server.URL, "http")
url, err := url.Parse(u)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)

buffer := &bytes.Buffer{}

listener := NewListener(url, "", nil, buffer)
err = listener.Start()
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)

want := `{"message":"1\n"}
{"message":"2\n"}
Expand All @@ -87,9 +79,7 @@ func TestListenerWithSchemaFunc(t *testing.T) {

u := "ws" + strings.TrimPrefix(server.URL, "http")
url, err := url.Parse(u)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)

buffer := &bytes.Buffer{}

Expand All @@ -108,9 +98,7 @@ func TestListenerWithSchemaFunc(t *testing.T) {

listener := NewListener(url, "", schemaFunc, buffer)
err = listener.Start()
if err != nil {
t.Fatalf("%v", err)
}
require.NoError(t, err)

want := `1
2
Expand All @@ -127,9 +115,7 @@ func TestListenerStop(t *testing.T) {

u := "ws" + strings.TrimPrefix(server.URL, "http")
url, err := url.Parse(u)
if err != nil {
require.NoError(t, err)
}
require.NoError(t, err)

buffer := &bytes.Buffer{}

Expand Down

0 comments on commit 86fc331

Please sign in to comment.