Skip to content

Commit

Permalink
feat: Add IsOneOf routine
Browse files Browse the repository at this point in the history
  • Loading branch information
maestre3d committed May 25, 2024
1 parent 6ba2dfc commit b473cb2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
11 changes: 11 additions & 0 deletions values/one_of.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package values

// IsOneOf returns if src is equals to any of the items contained in cmpValues variadic slice.
func IsOneOf[T comparable](src T, cmpValues ...T) bool {
for _, comparison := range cmpValues {
if src == comparison {
return true
}
}
return false
}
74 changes: 74 additions & 0 deletions values/one_of_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package values_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/neutrinocorp/nolan/values"
)

func TestIsOneOf(t *testing.T) {

Check failure on line 11 in values/one_of_test.go

View workflow job for this annotation

GitHub Actions / Run Go Linter (ubuntu-latest, 1.21)

Function 'TestIsOneOf' is too long (62 > 60) (funlen)

Check failure on line 11 in values/one_of_test.go

View workflow job for this annotation

GitHub Actions / Run Go Linter (ubuntu-latest, 1.22)

Function 'TestIsOneOf' is too long (62 > 60) (funlen)
tests := []struct {
name string
inBaseCase string
inOneOf []string
exp bool
}{
{
name: "both empty",
inBaseCase: "",
inOneOf: nil,
exp: false,
},
{
name: "one of nil",
inBaseCase: "foo",
inOneOf: nil,
exp: false,
},
{
name: "base empty",
inBaseCase: "",
inOneOf: []string{"foo"},
exp: false,
},
{
name: "single not equal",
inBaseCase: "foo",
inOneOf: []string{"bar"},
exp: false,
},
{
name: "multi not equal",
inBaseCase: "foo",
inOneOf: []string{"bar", "baz", "foobar"},
exp: false,
},
{
name: "single equal",
inBaseCase: "foo",
inOneOf: []string{"foo"},
exp: true,
},
{
name: "multi one equal",
inBaseCase: "foo",
inOneOf: []string{"bar", "baz", "foo"},
exp: true,
},
{
name: "multi all equal",
inBaseCase: "foo",
inOneOf: []string{"foo", "foo", "foo"},
exp: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := values.IsOneOf(tt.inBaseCase, tt.inOneOf...)
assert.Equal(t, tt.exp, out)
})
}
}

0 comments on commit b473cb2

Please sign in to comment.