2023-10-07 10:43:21 +01:00
|
|
|
package validate
|
|
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
|
|
func TestMaxLength(t *testing.T) {
|
|
|
|
|
type TestCase struct {
|
|
|
|
|
Input string
|
2023-10-08 15:21:02 +01:00
|
|
|
L int
|
|
|
|
|
Err error
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testCases := []TestCase{
|
2023-10-08 15:21:02 +01:00
|
|
|
{Input: "abcd", L: 8},
|
|
|
|
|
{Input: "abcdefgh", L: 8},
|
|
|
|
|
{Input: "abcd efg", L: 8},
|
|
|
|
|
{Input: "abcdefghi", L: 8, Err: ErrTooManyChars},
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-07 10:44:47 +01:00
|
|
|
for _, tc := range testCases {
|
2023-10-08 15:21:02 +01:00
|
|
|
t.Logf("Testing %q against maximum length of %d", tc.Input, tc.L)
|
2023-10-07 10:43:21 +01:00
|
|
|
|
2023-10-07 10:44:47 +01:00
|
|
|
f := MaxLength(tc.L)
|
|
|
|
|
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 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMinLength(t *testing.T) {
|
|
|
|
|
type TestCase struct {
|
|
|
|
|
L int
|
|
|
|
|
Input string
|
2023-10-08 15:21:02 +01:00
|
|
|
Err error
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testCases := []TestCase{
|
2023-10-08 15:21:02 +01:00
|
|
|
{Input: "abcd", L: 8, Err: ErrTooFewChars},
|
|
|
|
|
{Input: "abcdefgh", L: 8},
|
|
|
|
|
{Input: "abcd efg", L: 8},
|
|
|
|
|
{Input: "abcdefghi", L: 8},
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-07 10:44:47 +01:00
|
|
|
for _, tc := range testCases {
|
2023-10-08 15:21:02 +01:00
|
|
|
t.Logf("Testing %q against minimum length of %d", tc.Input, tc.L)
|
2023-10-07 10:43:21 +01:00
|
|
|
|
2023-10-07 10:44:47 +01:00
|
|
|
f := MinLength(tc.L)
|
|
|
|
|
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 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|