From e6a31b7352129dc4ae32624a8fcb32ed640a31a6 Mon Sep 17 00:00:00 2001 From: John Wang Date: Sun, 4 Feb 2024 07:53:06 -0800 Subject: [PATCH] test: `type/stringsutil`: add `TestSliceOrderExplicit()` --- type/stringsutil/slice.go | 2 +- type/stringsutil/slice_test.go | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/type/stringsutil/slice.go b/type/stringsutil/slice.go index 6f597adb..77c20b30 100644 --- a/type/stringsutil/slice.go +++ b/type/stringsutil/slice.go @@ -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)) } diff --git a/type/stringsutil/slice_test.go b/type/stringsutil/slice_test.go index 7a55130e..2d30d9ea 100644 --- a/type/stringsutil/slice_test.go +++ b/type/stringsutil/slice_test.go @@ -4,6 +4,7 @@ import ( "strings" "testing" + "github.com/grokify/mogo/strconv/strconvutil" "golang.org/x/exp/slices" ) @@ -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, + ) + } + } +}