2023-10-07 10:43:21 +01:00
|
|
|
package validate
|
|
|
|
|
|
2023-11-19 20:27:33 +00:00
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
2023-10-07 10:43:21 +01:00
|
|
|
|
|
|
|
|
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},
|
2023-11-19 20:42:48 +00:00
|
|
|
{Input: "abcdefghi", L: 8, Err: ErrMustBeShorter},
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 16:20:21 +01:00
|
|
|
for n, tc := range testCases {
|
|
|
|
|
t.Logf("(%d) Testing %q against maximum length of %d", n, 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
|
|
|
|
2023-11-19 20:27:33 +00:00
|
|
|
if !errors.Is(err, tc.Err) {
|
2023-10-08 15:21:02 +01:00
|
|
|
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 {
|
|
|
|
|
Input string
|
2023-11-19 20:27:33 +00:00
|
|
|
L int
|
2023-10-08 15:21:02 +01:00
|
|
|
Err error
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
testCases := []TestCase{
|
2023-11-19 20:42:48 +00:00
|
|
|
{Input: "abcd", L: 8, Err: ErrMustBeLonger},
|
2023-10-08 15:21:02 +01:00
|
|
|
{Input: "abcdefgh", L: 8},
|
|
|
|
|
{Input: "abcd efg", L: 8},
|
|
|
|
|
{Input: "abcdefghi", L: 8},
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 16:20:21 +01:00
|
|
|
for n, tc := range testCases {
|
|
|
|
|
t.Logf("(%d) Testing %q against minimum length of %d", n, 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
|
|
|
|
2023-11-19 20:27:33 +00:00
|
|
|
if !errors.Is(err, tc.Err) {
|
2023-10-08 15:21:02 +01:00
|
|
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
2023-10-07 10:43:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|