Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: pkg-utils-slice #473

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/utils/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ func ContainsString(slice []string, str string) bool {
}

func RemoveString(slice []string, str string) []string {
if len(slice) == 0 {
return slice
}
var newSlice []string
for _, item := range slice {
if item != str {
Expand Down
77 changes: 77 additions & 0 deletions pkg/utils/slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package utils

import (
"reflect"
"testing"
)

func TestContainsString(t *testing.T) {
tests := []struct {
name string
slice []string
str string
want bool
}{
{
name: "slice is empty",
slice: []string{},
str: "a",
want: false,
},
{
name: "searching object exists the slice",
slice: []string{"a", "b"},
str: "a",
want: true,
},
{
name: "searching object doesn't exist the slice",
slice: []string{"a", "b"},
str: "c",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ContainsString(tt.slice, tt.str); got != tt.want {
t.Errorf("ContainsString() = %v, want %v", got, tt.want)
}
})
}
}

func TestRemoveString(t *testing.T) {
tests := []struct {
name string
slice []string
str string
want []string
}{
{
name: "removing object exists the slice",
slice: []string{"a", "b", "c"},
str: "b",
want: []string{"a", "c"},
},
{
name: "remove object doesn't exist in the slice",
slice: []string{"a", "b", "c"},
str: "d",
want: []string{"a", "b", "c"},
},
{
name: "the slice is empty",
slice: []string{},
str: "d",
want: []string{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RemoveString(tt.slice, tt.str); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RemoveString() = %v, want %v", got, tt.want)
}
})
}
}