Skip to content

Commit

Permalink
fix(test): compare suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed Mar 4, 2024
1 parent 0be9199 commit 3917ef5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,21 @@ func getDbs() ([]string, error) {
if err != nil {
return nil, err
}
var out []string
var dbList []string
for _, e := range entries {
if e.IsDir() {
out = append(out, "@"+e.Name())
dbList = append(dbList, e.Name())
}
}
return out, nil
return formatDbs(dbList), nil
}

func formatDbs(dbs []string) []string {
var out []string

Check failure on line 207 in main.go

View workflow job for this annotation

GitHub Actions / lint

Consider pre-allocating `out` (prealloc)

Check failure on line 207 in main.go

View workflow job for this annotation

GitHub Actions / lint

Consider pre-allocating `out` (prealloc)
for _, db := range dbs {
out = append(out, "@"+db)
}
return out
}

// getFilePath: get the file path to the skate databases.
Expand Down
15 changes: 11 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"errors"
"os"
"reflect"
"sort"
"testing"

"github.com/charmbracelet/charm/testserver"
Expand Down Expand Up @@ -33,7 +35,7 @@ func TestFindDbs(t *testing.T) {
},
{
tname: "name > db",
name: "pcharm.sh.kv.user.defaultcharm.sh.kv.user.default",
name: "pcharm.sh.kv.user.defaultii",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: nil,
Expand All @@ -45,7 +47,7 @@ func TestFindDbs(t *testing.T) {
name: "",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: defaultDbs,
suggestions: formatDbs(defaultDbs),
},
},
{
Expand Down Expand Up @@ -111,12 +113,17 @@ func TestFindDbs(t *testing.T) {
if err == nil {
t.Fatalf("expected an error, got: %v", err)
}
// check we got the right type of error
var perr errDBNotFound
if !errors.As(err, &perr) {
t.Fatalf("something went wrong! got: %v", err)
}
if len(err.(errDBNotFound).suggestions) !=
len(tc.err.(errDBNotFound).suggestions) {
// check suggestions match
gotSuggestions := err.(errDBNotFound).suggestions
wantSuggestions := tc.err.(errDBNotFound).suggestions
sort.Strings(gotSuggestions)
sort.Strings(wantSuggestions)
if !reflect.DeepEqual(gotSuggestions, wantSuggestions) {
t.Fatalf("got != want. got: %v, want: %v", err, tc.err)
}
}
Expand Down

0 comments on commit 3917ef5

Please sign in to comment.