From 7869872d1f6c3ca2bf98cddf8947acb5e4d575c4 Mon Sep 17 00:00:00 2001 From: goreorto Date: Thu, 22 Aug 2024 11:12:48 -0600 Subject: [PATCH] Add v0 compatibility functions (#82) * Add v0 compatibility functions bunch of dependants still use a version before generics this will help migration to be done progressively * lovely lint --- aesutil/aesutil_test.go | 10 +- goutil/goutil.go | 2 +- ioutilx/copy_test.go | 2 +- jsonutil/jsonutil_test.go | 2 +- maputil/maputil_test.go | 2 +- ptrutil/ptr_test.go | 4 +- sliceutil/compatibility.go | 85 ++++++++ sliceutil/compatibility_test.go | 373 ++++++++++++++++++++++++++++++++ sliceutil/sliceutil_test.go | 24 +- syncutil/syncmap_test.go | 4 +- 10 files changed, 483 insertions(+), 25 deletions(-) create mode 100644 sliceutil/compatibility.go create mode 100644 sliceutil/compatibility_test.go diff --git a/aesutil/aesutil_test.go b/aesutil/aesutil_test.go index 6b27408..fbea62f 100644 --- a/aesutil/aesutil_test.go +++ b/aesutil/aesutil_test.go @@ -18,13 +18,13 @@ func TestInvalidKeysAndData(t *testing.T) { data := []byte(testPlaintext) _, err := Encrypt("", data) if err == nil { - t.Errorf("Encrypt succeeded with an empty key") + t.Error("Encrypt succeeded with an empty key") } // Decrypt with empty key _, err = Decrypt("", testCiphertext) if err == nil { - t.Errorf("Decrypt succeeded with an empty key") + t.Error("Decrypt succeeded with an empty key") } // Decrypt with an incorrect key @@ -48,13 +48,13 @@ func TestInvalidKeysAndData(t *testing.T) { // Decrypt an short string (i.e. smaller than block size) _, err = Decrypt(testKeyString, "aaaabbbbcccc") if err == nil { - t.Errorf("Decrypt succeeded with an invalid key size") + t.Error("Decrypt succeeded with an invalid key size") } // Decrypt a non-base64 string _, err = Decrypt(testKeyString, fmt.Sprintf("%s#@?`", testCiphertext)) if err == nil { - t.Errorf("Decrypt succeeded with an invalid base64 string") + t.Error("Decrypt succeeded with an invalid base64 string") } } @@ -87,7 +87,7 @@ func TestEncryptAndDecrypt(t *testing.T) { t.Fatal("Decrypt failed, plaintext result is nil") } if string(plain) != testData { - t.Fatalf(diff.Cmp(testData, string(plain))) + t.Fatal(diff.Cmp(testData, string(plain))) } }) } diff --git a/goutil/goutil.go b/goutil/goutil.go index dc935b1..7d5d142 100644 --- a/goutil/goutil.go +++ b/goutil/goutil.go @@ -96,7 +96,7 @@ func ResolveWildcard(path string, mode build.ImportMode) ([]*build.Package, erro // Gather a list of directories with *.go files. goDirs := make(map[string]struct{}) - err = filepath.Walk(root.Dir, func(path string, info os.FileInfo, err error) error { + err = filepath.Walk(root.Dir, func(path string, info os.FileInfo, _ error) error { if !strings.HasSuffix(path, ".go") || info.IsDir() || strings.Contains(path, "/vendor/") { return nil } diff --git a/ioutilx/copy_test.go b/ioutilx/copy_test.go index 55b412c..c1ce84e 100644 --- a/ioutilx/copy_test.go +++ b/ioutilx/copy_test.go @@ -312,7 +312,7 @@ func TestCopyTree(t *testing.T) { err := CopyTree("test", "test_copytree", &CopyTreeOptions{ Symlinks: false, - Ignore: func(path string, fi []os.FileInfo) []string { + Ignore: func(_ string, _ []os.FileInfo) []string { return []string{"fifo"} }, CopyFunction: Copy, diff --git a/jsonutil/jsonutil_test.go b/jsonutil/jsonutil_test.go index 3e097ec..8bad34d 100644 --- a/jsonutil/jsonutil_test.go +++ b/jsonutil/jsonutil_test.go @@ -76,7 +76,7 @@ func TestMustUnmarshal(t *testing.T) { defer func() { rec := recover() if rec == nil { - t.Errorf("no panic?") + t.Error("no panic?") } }() diff --git a/maputil/maputil_test.go b/maputil/maputil_test.go index 88d3607..d6a2a47 100644 --- a/maputil/maputil_test.go +++ b/maputil/maputil_test.go @@ -21,7 +21,7 @@ func TestSwap(t *testing.T) { t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { got := Swap(tc.in) if !reflect.DeepEqual(got, tc.expected) { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } diff --git a/ptrutil/ptr_test.go b/ptrutil/ptr_test.go index da2640f..a84e276 100644 --- a/ptrutil/ptr_test.go +++ b/ptrutil/ptr_test.go @@ -24,7 +24,7 @@ func TestDereference_Int(t *testing.T) { for i, test := range tests { t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) { if got := Dereference(test.ptr); got != test.expected { - t.Errorf(diff.Cmp(test.expected, got)) + t.Error(diff.Cmp(test.expected, got)) } }) } @@ -47,7 +47,7 @@ func TestDereference_String(t *testing.T) { for i, test := range tests { t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) { if got := Dereference(test.ptr); got != test.expected { - t.Errorf(diff.Cmp(test.expected, got)) + t.Error(diff.Cmp(test.expected, got)) } }) } diff --git a/sliceutil/compatibility.go b/sliceutil/compatibility.go new file mode 100644 index 0000000..683a898 --- /dev/null +++ b/sliceutil/compatibility.go @@ -0,0 +1,85 @@ +package sliceutil + +// JoinInt see Join +// Deprecated: use Join +func JoinInt(ints []int64) string { + return Join(ints) +} + +// UniqInt64 see Unique +// Deprecated: use Unique +func UniqInt64(list []int64) []int64 { + return Unique(list) +} + +// UniqString see Unique +// Deprecated: use Unique +func UniqString(list []string) []string { + return Unique(list) +} + +// UniqueMergeSlices see MergeUnique +// Deprecated: use MergeUnique +func UniqueMergeSlices(s [][]int64) (result []int64) { + return MergeUnique(s) +} + +// InStringSlice see Contains +// Deprecated: use Contains +func InStringSlice(list []string, str string) bool { + return Contains(list, str) +} + +// InIntSlice see Contains +// Deprecated: use Contains +func InIntSlice(list []int, i int) bool { + return Contains(list, i) +} + +// InInt64Slice see Contains +// Deprecated: use Contains +func InInt64Slice(list []int64, i int64) bool { + return Contains(list, i) +} + +// RepeatString see Repeat +// Deprecated: use Repeat +func RepeatString(s string, n int) (r []string) { + return Repeat(s, n) +} + +// ChooseString see Choose +// Deprecated: use Choose +func ChooseString(l []string) string { + return Choose(l) +} + +// FilterString see Filter +// Deprecated: use Filter +func FilterString(list []string, fun func(string) bool) []string { + return Filter(list, fun) +} + +// RemoveString see Remove +// Deprecated: use Remove +func RemoveString(list []string, s string) (out []string) { + return Remove(list, s) +} + +// FilterStringEmpty see FilterEmpty +// Deprecated: use FilterEmpty +func FilterStringEmpty(e string) bool { + return FilterEmpty(e) +} + +// FilterInt see Filter +// Deprecated: use Filter +func FilterInt(list []int64, fun func(int64) bool) []int64 { + return Filter(list, fun) +} + +// StringMap see Map +// Deprecated: use Map +func StringMap(list []string, f func(string) string) []string { + return Map(list, f) +} diff --git a/sliceutil/compatibility_test.go b/sliceutil/compatibility_test.go new file mode 100644 index 0000000..2ce87b3 --- /dev/null +++ b/sliceutil/compatibility_test.go @@ -0,0 +1,373 @@ +package sliceutil + +import ( + "fmt" + "reflect" + "testing" + + "github.com/teamwork/test/diff" +) + +func TestIntsToString(t *testing.T) { + cases := []struct { + in []int64 + expected string + }{ + { + []int64{1, 2, 3, 4, 4, 5, 6, 6, 6, 6, 7, 8, 8, 8}, + "1, 2, 3, 4, 4, 5, 6, 6, 6, 6, 7, 8, 8, 8", + }, + { + []int64{-1, -2, -3, -4, -4, -5, -6, -6, -6, -6, -7, -8, -8, -8}, + "-1, -2, -3, -4, -4, -5, -6, -6, -6, -6, -7, -8, -8, -8", + }, + { + []int64{}, + "", + }, + } + + for i, tc := range cases { + t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { + got := JoinInt(tc.in) + if got != tc.expected { + t.Error(diff.Cmp(tc.expected, got)) + } + }) + } +} + +func TestUniqInt64(t *testing.T) { + cases := []struct { + in []int64 + expected []int64 + }{ + { + []int64{1, 2, 3, 4, 4, 5, 6, 6, 6, 6, 7, 8, 8, 8}, + []int64{1, 2, 3, 4, 5, 6, 7, 8}, + }, + { + []int64{1, 3, 8, 3, 8}, + []int64{1, 3, 8}, + }, + { + []int64{1, 2, 3}, + []int64{1, 2, 3}, + }, + { + []int64{}, + nil, + }, + { + nil, + nil, + }, + } + + for i, tc := range cases { + t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { + got := UniqInt64(tc.in) + if !reflect.DeepEqual(got, tc.expected) { + t.Error(diff.Cmp(tc.expected, got)) + } + }) + } +} + +func TestUniqueMergeSlices(t *testing.T) { + var tests = []struct { + in [][]int64 + expected []int64 + }{ + { + generate2dintslice([]int64{1, 2, 3}), + []int64{1, 2, 3}, + }, + { + generate2dintslice([]int64{0, 1, 2, 3, -1, -10}), + []int64{0, 1, 2, 3, -1, -10}, + }, + } + + for i, tc := range tests { + t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { + got := UniqueMergeSlices(tc.in) + if !int64slicesequal(got, tc.expected) { + t.Error(diff.Cmp(tc.expected, got)) + } + }) + } +} + +func TestUniqString(t *testing.T) { + var tests = []struct { + in []string + expected []string + }{ + { + []string{"a", "b", "c"}, + []string{"a", "b", "c"}, + }, + { + []string{"a", "b", "c", "a", "b", "n", "a", "aaa", "n", "x"}, + []string{"a", "b", "c", "n", "aaa", "x"}, + }, + } + + for i, tc := range tests { + t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { + got := UniqString(tc.in) + if !stringslicesequal(got, tc.expected) { + t.Error(diff.Cmp(tc.expected, got)) + } + }) + } +} + +func TestInStringSlice(t *testing.T) { + tests := []struct { + list []string + find string + expected bool + }{ + {[]string{"hello"}, "hello", true}, + {[]string{"hello"}, "hell", false}, + {[]string{"hello", "world", "test"}, "world", true}, + {[]string{"hello", "world", "test"}, "", false}, + {[]string{}, "", false}, + } + + for i, tc := range tests { + t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { + got := InStringSlice(tc.list, tc.find) + if got != tc.expected { + t.Error(diff.Cmp(tc.expected, got)) + } + }) + } +} + +func TestInIntSlice(t *testing.T) { + tests := []struct { + list []int + find int + expected bool + }{ + {[]int{42}, 42, true}, + {[]int{42}, 4, false}, + {[]int{42, 666, 14159}, 666, true}, + {[]int{42, 666, 14159}, 0, false}, + {[]int{}, 0, false}, + } + + for i, tc := range tests { + t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { + got := InIntSlice(tc.list, tc.find) + if got != tc.expected { + t.Error(diff.Cmp(tc.expected, got)) + } + }) + } +} + +func TestInInt64Slice(t *testing.T) { + tests := []struct { + list []int64 + find int64 + expected bool + }{ + {[]int64{42}, 42, true}, + {[]int64{42}, 4, false}, + {[]int64{42, 666, 14159}, 666, true}, + {[]int64{42, 666, 14159}, 0, false}, + {[]int64{}, 0, false}, + } + + for i, tc := range tests { + t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { + got := InInt64Slice(tc.list, tc.find) + if got != tc.expected { + t.Error(diff.Cmp(tc.expected, got)) + } + }) + } +} + +func TestFilterString(t *testing.T) { + cases := []struct { + fun func(string) bool + in []string + want []string + }{ + { + FilterStringEmpty, + []string(nil), + []string(nil), + }, + { + FilterStringEmpty, + []string{}, + []string(nil), + }, + { + FilterStringEmpty, + []string{"1"}, + []string{"1"}, + }, + { + FilterStringEmpty, + []string{"", "1", ""}, + []string{"1"}, + }, + { + FilterStringEmpty, + []string{"", "1", "", "2", "asd", "", "", "", "zx", "", "a"}, + []string{"1", "2", "asd", "zx", "a"}, + }, + } + + for i, tc := range cases { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + out := FilterString(tc.in, tc.fun) + if !reflect.DeepEqual(tc.want, out) { + t.Errorf("\nout: %#v\nwant: %#v\n", out, tc.want) + } + }) + } +} + +func filterIntEmpty(e int64) bool { + return e != 0 +} +func TestFilterInt(t *testing.T) { + cases := []struct { + fun func(int64) bool + in []int64 + want []int64 + }{ + { + filterIntEmpty, + []int64(nil), + []int64(nil), + }, + { + filterIntEmpty, + []int64{}, + []int64(nil), + }, + { + filterIntEmpty, + []int64{1}, + []int64{1}, + }, + { + filterIntEmpty, + []int64{0, 1, 0}, + []int64{1}, + }, + { + filterIntEmpty, + []int64{0, 1, 0, 2, -1, 0, 0, 0, 42, 666, -666, 0, 0, 0}, + []int64{1, 2, -1, 42, 666, -666}, + }, + } + + for i, tc := range cases { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + out := FilterInt(tc.in, tc.fun) + if !reflect.DeepEqual(tc.want, out) { + t.Errorf("\nout: %#v\nwant: %#v\n", out, tc.want) + } + }) + } +} + +func TestChooseString(t *testing.T) { + tests := []struct { + in []string + want string + }{ + {nil, ""}, + {[]string{}, ""}, + {[]string{"a"}, "a"}, + } + + for i, tt := range tests { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + out := ChooseString(tt.in) + if out != tt.want { + t.Errorf("\nout: %#v\nwant: %#v\n", out, tt.want) + } + }) + } +} + +func TestRemoveString(t *testing.T) { + cases := []struct { + list []string + item string + want []string + }{ + { + list: []string{"1", "2", "3", "4", "5"}, + item: "3", + want: []string{"1", "2", "4", "5"}, + }, + { + list: []string{"1", "2", "3", "4", "5"}, + item: "1", + want: []string{"2", "3", "4", "5"}, + }, + { + list: []string{"1", "2", "3", "4", "5"}, + item: "5", + want: []string{"1", "2", "3", "4"}, + }, + { + list: []string{"1", "2", "3", "4", "5"}, + item: "6", + want: []string{"1", "2", "3", "4", "5"}, + }, + { + list: []string{"2", "1", "2", "2", "2", "5", "2"}, + item: "2", + want: []string{"1", "5"}, + }, + } + + for i, tc := range cases { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + out := RemoveString(tc.list, tc.item) + if !reflect.DeepEqual(tc.want, out) { + t.Errorf("\nout: %#v\nwant: %#v\n", out, tc.want) + } + }) + } +} + +func TestStringMap(t *testing.T) { + cases := []struct { + in []string + want []string + f func(string) string + }{ + { + in: []string{"a", "b", "c"}, + want: []string{"", "", ""}, + f: func(string) string { return "" }, + }, + { + in: []string{"a", "b", "c"}, + want: []string{"aa", "bb", "cc"}, + f: func(c string) string { return c + c }, + }, + } + for _, tc := range cases { + t.Run("", func(t *testing.T) { + out := StringMap(tc.in, tc.f) + if !reflect.DeepEqual(tc.want, out) { + t.Errorf("\nout: %#v\nwant: %#v\n", out, tc.want) + } + }) + } +} diff --git a/sliceutil/sliceutil_test.go b/sliceutil/sliceutil_test.go index 7df4418..caa9162 100644 --- a/sliceutil/sliceutil_test.go +++ b/sliceutil/sliceutil_test.go @@ -34,7 +34,7 @@ func TestJoin(t *testing.T) { t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { got := Join(tc.in) if got != tc.expected { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -77,7 +77,7 @@ func TestJoinWith(t *testing.T) { t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { got := JoinWith(tc.in, tc.delim) if got != tc.expected { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -114,7 +114,7 @@ func TestUniq_Int64(t *testing.T) { t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { got := Unique(tc.in) if !reflect.DeepEqual(got, tc.expected) { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -139,7 +139,7 @@ func TestUniq_String(t *testing.T) { t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { got := Unique(tc.in) if !stringslicesequal(got, tc.expected) { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -165,7 +165,7 @@ func TestMergeUnique_Int64(t *testing.T) { got := MergeUnique(tc.in) if !int64slicesequal(got, tc.expected) { t.Log("IN", tc.in) - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -274,11 +274,11 @@ func TestCSVtoInt64Slice(t *testing.T) { } if err != tc.expectedErr && err.Error() != tc.expectedErr.Error() { - t.Errorf(diff.Cmp(tc.expectedErr.Error(), err.Error())) + t.Error(diff.Cmp(tc.expectedErr.Error(), err.Error())) } if !reflect.DeepEqual(got, tc.expected) { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -301,7 +301,7 @@ func TestItemInSlice_String(t *testing.T) { t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { got := Contains(tc.list, tc.find) if got != tc.expected { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -326,7 +326,7 @@ func TestInFoldedStringSlice(t *testing.T) { t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { got := InFoldedStringSlice(tc.list, tc.find) if got != tc.expected { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -349,7 +349,7 @@ func TestItemInSlice_Int(t *testing.T) { t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { got := Contains(tc.list, tc.find) if got != tc.expected { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -372,7 +372,7 @@ func TestItemInSlice_Int64(t *testing.T) { t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) { got := Contains(tc.list, tc.find) if got != tc.expected { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } @@ -657,7 +657,7 @@ func TestValues(t *testing.T) { return t.Name }) if !reflect.DeepEqual(got, tc.expected) { - t.Errorf(diff.Cmp(tc.expected, got)) + t.Error(diff.Cmp(tc.expected, got)) } }) } diff --git a/syncutil/syncmap_test.go b/syncutil/syncmap_test.go index 871b337..fbaaaf8 100644 --- a/syncutil/syncmap_test.go +++ b/syncutil/syncmap_test.go @@ -65,7 +65,7 @@ func TestMap_Range(t *testing.T) { m.Store(k, v) } - m.Range(func(k string, v int) bool { + m.Range(func(k string, _ int) bool { seen, ok := test[k] if !ok { t.Fatalf("unexpected key '%s'", k) @@ -95,7 +95,7 @@ func TestMap_Range_EarlyReturn(t *testing.T) { var count int - m.Range(func(k string, v int) bool { + m.Range(func(_ string, _ int) bool { count++ return false })