Skip to content

Commit 3917ef5

Browse files
committed
fix(test): compare suggestions
1 parent 0be9199 commit 3917ef5

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,21 @@ func getDbs() ([]string, error) {
194194
if err != nil {
195195
return nil, err
196196
}
197-
var out []string
197+
var dbList []string
198198
for _, e := range entries {
199199
if e.IsDir() {
200-
out = append(out, "@"+e.Name())
200+
dbList = append(dbList, e.Name())
201201
}
202202
}
203-
return out, nil
203+
return formatDbs(dbList), nil
204+
}
205+
206+
func formatDbs(dbs []string) []string {
207+
var out []string
208+
for _, db := range dbs {
209+
out = append(out, "@"+db)
210+
}
211+
return out
204212
}
205213

206214
// getFilePath: get the file path to the skate databases.

main_test.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"errors"
55
"os"
6+
"reflect"
7+
"sort"
68
"testing"
79

810
"github.com/charmbracelet/charm/testserver"
@@ -33,7 +35,7 @@ func TestFindDbs(t *testing.T) {
3335
},
3436
{
3537
tname: "name > db",
36-
name: "pcharm.sh.kv.user.defaultcharm.sh.kv.user.default",
38+
name: "pcharm.sh.kv.user.defaultii",
3739
dbs: defaultDbs,
3840
err: errDBNotFound{
3941
suggestions: nil,
@@ -45,7 +47,7 @@ func TestFindDbs(t *testing.T) {
4547
name: "",
4648
dbs: defaultDbs,
4749
err: errDBNotFound{
48-
suggestions: defaultDbs,
50+
suggestions: formatDbs(defaultDbs),
4951
},
5052
},
5153
{
@@ -111,12 +113,17 @@ func TestFindDbs(t *testing.T) {
111113
if err == nil {
112114
t.Fatalf("expected an error, got: %v", err)
113115
}
116+
// check we got the right type of error
114117
var perr errDBNotFound
115118
if !errors.As(err, &perr) {
116119
t.Fatalf("something went wrong! got: %v", err)
117120
}
118-
if len(err.(errDBNotFound).suggestions) !=
119-
len(tc.err.(errDBNotFound).suggestions) {
121+
// check suggestions match
122+
gotSuggestions := err.(errDBNotFound).suggestions
123+
wantSuggestions := tc.err.(errDBNotFound).suggestions
124+
sort.Strings(gotSuggestions)
125+
sort.Strings(wantSuggestions)
126+
if !reflect.DeepEqual(gotSuggestions, wantSuggestions) {
120127
t.Fatalf("got != want. got: %v, want: %v", err, tc.err)
121128
}
122129
}

0 commit comments

Comments
 (0)