2023-10-07 11:24:34 +01:00
|
|
|
package validate
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-12 19:55:08 +01:00
|
|
|
func TestInInt(t *testing.T) {
|
2023-10-08 16:20:21 +01:00
|
|
|
type TestCase struct {
|
2023-10-12 19:55:08 +01:00
|
|
|
Input int
|
|
|
|
|
A []int
|
2023-10-08 15:21:02 +01:00
|
|
|
Err error
|
2023-10-07 11:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-12 19:55:08 +01:00
|
|
|
allow := []int{1, 23, 456}
|
2023-10-08 16:20:21 +01:00
|
|
|
testCases := []TestCase{
|
2023-10-12 19:55:08 +01:00
|
|
|
{Input: 1, A: allow},
|
|
|
|
|
{Input: 23, A: allow},
|
|
|
|
|
{Input: 456, A: allow},
|
|
|
|
|
{Input: 789, A: allow, Err: ErrValueNotAllowed},
|
2023-10-07 11:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 16:20:21 +01:00
|
|
|
for n, tc := range testCases {
|
2023-10-12 19:55:08 +01:00
|
|
|
t.Logf("(%d) Testing %d against %v", n, 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
|
|
|
}
|
|
|
|
|
}
|
2023-10-08 16:20:21 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-12 19:55:08 +01:00
|
|
|
func TestInString(t *testing.T) {
|
2023-10-08 16:20:21 +01:00
|
|
|
type TestCase struct {
|
2023-10-12 19:55:08 +01:00
|
|
|
Input string
|
|
|
|
|
A []string
|
2023-10-08 16:20:21 +01:00
|
|
|
Err error
|
|
|
|
|
}
|
2023-10-07 11:24:34 +01:00
|
|
|
|
2023-10-12 19:55:08 +01:00
|
|
|
allow := []string{"abcd", "ef", "1234"}
|
2023-10-08 16:20:21 +01:00
|
|
|
testCases := []TestCase{
|
2023-10-12 19:55:08 +01:00
|
|
|
{Input: "abcd", A: allow},
|
|
|
|
|
{Input: "ef", A: allow},
|
|
|
|
|
{Input: "1234", A: allow},
|
|
|
|
|
{Input: "5678", A: allow, Err: ErrValueNotAllowed},
|
2023-10-07 11:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 16:20:21 +01:00
|
|
|
for n, tc := range testCases {
|
2023-10-12 19:55:08 +01:00
|
|
|
t.Logf("(%d) Testing %q against %v", n, 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|