Skip to content

Commit

Permalink
Add package description and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Sep 16, 2022
1 parent f0e24b3 commit 370efec
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/slice/slice.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Package slice defines functions to manipulate the slice
package slice

import "reflect"

// Contains return whether list has elem.
// Contains returns whether the list has the elements specified in the argument.
func Contains(list interface{}, elem interface{}) bool {
rvList := reflect.ValueOf(list)

Expand Down
71 changes: 71 additions & 0 deletions internal/slice/slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package slice

import "testing"

func TestContains(t *testing.T) {
type args struct {
list interface{}
elem interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "[Success] string slice has 'metallica'",
args: args{
list: []string{"a", "bb", "metallica", "abc"},
elem: "metallica",
},
want: true,
},
{
name: "[Success] string slice does not have 'metallica'",
args: args{
list: []string{"a", "bbb", "abc"},
elem: "metallica",
},
want: false,
},
{
name: "[Success] Integer slice has 100",
args: args{
list: []int64{1, 3, 100, 21},
elem: 100,
},
want: true,
},
{
name: "[Success] Integer slice does not have 100",
args: args{
list: []int64{1, 3, 21},
elem: 100,
},
want: false,
},
{
name: "[Error] If the slice and element types are different",
args: args{
list: []string{"a", "bb", "metallica", "abc"},
elem: -100,
},
want: false,
},
{
name: "[Error] If both arguments are slices",
args: args{
list: []string{"a", "bb", "metallica", "abc"},
elem: []int64{1, 3, 21},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Contains(tt.args.list, tt.args.elem); got != tt.want {
t.Errorf("Contains() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 370efec

Please sign in to comment.