Skip to content

Commit

Permalink
Merge pull request #37 from rezakhademix/add-in-validation-function
Browse files Browse the repository at this point in the history
added: in validation generic function
  • Loading branch information
rezakhademix authored Mar 7, 2024
2 parents 7a60738 + 6627a6b commit 44da097
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
12 changes: 12 additions & 0 deletions in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package validator

// In checks value under validation must be included in the given list of values
func In[T comparable](value T, acceptableValues ...T) bool {
for i := range acceptableValues {
if value == acceptableValues[i] {
return true
}
}

return false
}
65 changes: 65 additions & 0 deletions in_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package validator

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_In(t *testing.T) {
tests := []struct {
name string
value any
acceptableValues []any
expectedResult bool
}{
{
name: "test integer value does not exist in acceptableValues",
value: 4,
acceptableValues: []any{1, 2, 3},
expectedResult: false,
},
{
name: "test string value does not exist in acceptable values",
value: "redis",
acceptableValues: []any{"mysql", "mariadb", "postgres"},
expectedResult: false,
},
{
name: "test empty string value does not exist in acceptable values",
value: "",
acceptableValues: []any{"pen", "pencil", "pipe"},
expectedResult: false,
},
{
name: "test empty space string value does not exist in acceptable values",
value: " ",
acceptableValues: []any{"joe", "jane", "john"},
expectedResult: false,
},
{
name: "test integer value exists in acceptable values",
value: 20,
acceptableValues: []any{10, 20, 30},
expectedResult: true,
},
{
name: "test string value exists in acceptable values",
value: "go",
acceptableValues: []any{"go", "php", "java"},
expectedResult: true,
},
}

for _, test := range tests {
result := In(test.value, test.acceptableValues...)

assert.Equalf(
t,
test.expectedResult,
result,
"test case %q failed: expected %v, got %v",
test.name, test.expectedResult, result,
)
}
}

0 comments on commit 44da097

Please sign in to comment.