Skip to content

Commit

Permalink
test: type/stringsutil: add TestSliceOrderExplicit()
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Feb 4, 2024
1 parent cc81c2c commit e6a31b7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion type/stringsutil/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ func SliceJoinFunc(s []string, sep string, f func(string) string) string {
if f == nil {
return strings.Join(s, sep)
}
n := []string{}
var n []string
for _, el := range s {
n = append(n, f(el))
}
Expand Down
68 changes: 68 additions & 0 deletions type/stringsutil/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
"testing"

"github.com/grokify/mogo/strconv/strconvutil"
"golang.org/x/exp/slices"
)

Expand All @@ -25,3 +26,70 @@ func TestSlices(t *testing.T) {
}
}
}

var sliceOrderExplicitTests = []struct {
v []string
ord []string
inclUnordered bool
want []string
wantIdx []int
}{
{
[]string{
"Done",
"In Progress",
"In Review",
"Open",
"Second Review",
"To Do",
},
[]string{
"Open",
"To Do",
"In Progress",
"In Review",
"Second Review",
"Done",
},
true,
[]string{
"Open",
"To Do",
"In Progress",
"In Review",
"Second Review",
"Done",
},
[]int{3, 5, 1, 2, 4, 0},
},
{[]string{"foo", "bar"}, []string{"bar"}, false, []string{"bar"}, []int{1}},
{[]string{"foo", "bar"}, []string{"bar"}, true, []string{"bar", "foo"}, []int{1, 0}},
{[]string{"foo", "bar"}, []string{"bar", "foo"}, false, []string{"bar", "foo"}, []int{1, 0}},
{[]string{"foo", "bar"}, []string{"bar", "foo"}, true, []string{"bar", "foo"}, []int{1, 0}},
{[]string{"foo", "bar", "baz", "qux"}, []string{"qux", "foo", "bar"}, false, []string{"qux", "foo", "bar"}, []int{3, 0, 1}},
{[]string{"foo", "bar", "baz", "qux"}, []string{"qux", "foo", "bar"}, true, []string{"qux", "foo", "bar", "baz"}, []int{3, 0, 1, 2}},
{[]string{"foo", "bar", "baz", "qux"}, []string{"qux", "foo", "bar", "baz"}, true, []string{"qux", "foo", "bar", "baz"}, []int{3, 0, 1, 2}},
}

func TestSliceOrderExplicit(t *testing.T) {
for i, tt := range sliceOrderExplicitTests {
gotStr, gotIdx := SliceOrderExplicit(tt.v, tt.ord, tt.inclUnordered)
if !slices.Equal(tt.want, gotStr) {
t.Errorf("stringsutil.SliceTrim(\"%s\", \"%s\", %v) strings want (%s) got (%s) test (%d)",
strings.Join(tt.v, ","),
strings.Join(tt.ord, ","),
tt.inclUnordered,
strings.Join(tt.want, ","),
strings.Join(gotStr, ","), i)
}
if !slices.Equal(tt.wantIdx, gotIdx) {
t.Errorf("stringsutil.SliceTrim(\"%s\", \"%s\", %v) indexes want (%s) got (%s) test (%d)",
strings.Join(tt.v, ","),
strings.Join(tt.ord, ","),
tt.inclUnordered,
strings.Join(strconvutil.SliceItoa(tt.wantIdx), ","),
strings.Join(strconvutil.SliceItoa(gotIdx), ","), i,
)
}
}
}

0 comments on commit e6a31b7

Please sign in to comment.