2023-10-07 11:24:34 +01:00
|
|
|
package validate
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestIn(t *testing.T) {
|
|
|
|
|
type TestCase[T comparable] struct {
|
|
|
|
|
Input T
|
2023-10-08 15:21:02 +01:00
|
|
|
A []T
|
|
|
|
|
Err error
|
2023-10-07 11:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
strIn := []string{"abcd", "ef", "1234"}
|
|
|
|
|
strTestCases := []TestCase[string]{
|
2023-10-08 15:21:02 +01:00
|
|
|
{Input: "abcd", A: strIn},
|
|
|
|
|
{Input: "ef", A: strIn},
|
|
|
|
|
{Input: "1234", A: strIn},
|
|
|
|
|
{Input: "5678", A: strIn, Err: ErrValueNotAllowed},
|
2023-10-07 11:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tc := range strTestCases {
|
2023-10-08 15:21:02 +01:00
|
|
|
t.Logf("Testing %q against %v", tc.Input, tc.A)
|
2023-10-07 11:24:34 +01:00
|
|
|
|
2023-10-08 15:21:02 +01:00
|
|
|
f := In(tc.A...)
|
2023-10-07 11:24:34 +01:00
|
|
|
err := f(tc.Input)
|
|
|
|
|
|
2023-10-08 15:21:02 +01:00
|
|
|
if err != tc.Err {
|
|
|
|
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
2023-10-07 11:24:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intIn := []int{1, 23, 456}
|
|
|
|
|
intTestCases := []TestCase[int]{
|
2023-10-08 15:21:02 +01:00
|
|
|
{Input: 1, A: intIn},
|
|
|
|
|
{Input: 23, A: intIn},
|
|
|
|
|
{Input: 456, A: intIn},
|
|
|
|
|
{Input: 789, A: intIn, Err: ErrValueNotAllowed},
|
2023-10-07 11:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tc := range intTestCases {
|
2023-10-08 15:21:02 +01:00
|
|
|
t.Logf("Testing %d against %v", tc.Input, tc.A)
|
2023-10-07 11:24:34 +01:00
|
|
|
|
2023-10-08 15:21:02 +01:00
|
|
|
f := In(tc.A...)
|
2023-10-07 11:24:34 +01:00
|
|
|
err := f(tc.Input)
|
|
|
|
|
|
2023-10-08 15:21:02 +01:00
|
|
|
if err != tc.Err {
|
|
|
|
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
2023-10-07 11:24:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|