Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Commit

Permalink
Add id.Slice.Contains with tests (#2252)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwk authored Aug 21, 2018
1 parent 69ce1ac commit b0ee723
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions id/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ func (s Slice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// Contains returns true if the slice contains the given ID; otherwise false is
// returned.
func (s Slice) Contains(v uuid.UUID) bool {
for _, i := range s {
if i == v {
return true
}
}
return false
}

// Ensure Slice implements the sort.Interface
var _ sort.Interface = Slice{}
var _ sort.Interface = (*Slice)(nil)
8 changes: 8 additions & 0 deletions id/slice_blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,11 @@ func TestToStringSlice(t *testing.T) {
s := id.Slice{a, b, c}
require.Equal(t, []string{a.String(), b.String(), c.String()}, s.ToStringSlice())
}

func TestSlice_Contains(t *testing.T) {
a := uuid.FromStringOrNil("cb4364bf-893c-4ac2-a35f-4ef74c212e88")
b := uuid.FromStringOrNil("d085a858-6b98-44ce-8777-b36d927b07dc")
require.True(t, id.Slice{a, b}.Contains(a))
require.False(t, id.Slice{}.Contains(a))
require.False(t, id.Slice{a}.Contains(b))
}

0 comments on commit b0ee723

Please sign in to comment.