diff --git a/pkg/sliceutil/collections_test.go b/pkg/sliceutil/collections_test.go index 70d34946fce..7fbafd3df2a 100644 --- a/pkg/sliceutil/collections_test.go +++ b/pkg/sliceutil/collections_test.go @@ -1,6 +1,7 @@ package sliceutil import ( + "reflect" "testing" "github.com/stretchr/testify/assert" @@ -66,3 +67,79 @@ func TestSliceSame(t *testing.T) { }) } } + +func TestAppendUniques(t *testing.T) { + type args struct { + vs []int + toAdd []int + } + tests := []struct { + name string + args args + want []int + }{ + { + name: "append to empty slice", + args: args{ + vs: []int{}, + toAdd: []int{1, 2, 3}, + }, + want: []int{1, 2, 3}, + }, + { + name: "append all unique values", + args: args{ + vs: []int{1, 2, 3}, + toAdd: []int{4, 5, 6}, + }, + want: []int{1, 2, 3, 4, 5, 6}, + }, + { + name: "append with some duplicates", + args: args{ + vs: []int{1, 2, 3}, + toAdd: []int{3, 4, 5}, + }, + want: []int{1, 2, 3, 4, 5}, + }, + { + name: "append all duplicates", + args: args{ + vs: []int{1, 2, 3}, + toAdd: []int{1, 2, 3}, + }, + want: []int{1, 2, 3}, + }, + { + name: "append to nil slice", + args: args{ + vs: nil, + toAdd: []int{1, 2, 3}, + }, + want: []int{1, 2, 3}, + }, + { + name: "append empty slice", + args: args{ + vs: []int{1, 2, 3}, + toAdd: []int{}, + }, + want: []int{1, 2, 3}, + }, + { + name: "append nil to slice", + args: args{ + vs: []int{1, 2, 3}, + toAdd: nil, + }, + want: []int{1, 2, 3}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := AppendUniques(tt.args.vs, tt.args.toAdd); !reflect.DeepEqual(got, tt.want) { + t.Errorf("AppendUniques() = %v, want %v", got, tt.want) + } + }) + } +}