-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / Run Go Linter (ubuntu-latest, 1.21)
|
||
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) | ||
}) | ||
} | ||
} |