add min/max length validation, improve tests

This commit is contained in:
Aneurin Barker Snook
2023-10-07 10:43:21 +01:00
parent f41d6f248a
commit 923734fe25
4 changed files with 140 additions and 41 deletions
+65
View File
@@ -0,0 +1,65 @@
package validate
import "testing"
func TestMaxLength(t *testing.T) {
type TestCase struct {
L int
Input string
Err bool
}
testCases := []TestCase{
{L: 8, Input: "abcd"},
{L: 8, Input: "abcdefgh"},
{L: 8, Input: "abcd efg"},
{L: 8, Input: "abcdefghi", Err: true},
}
for _, testCase := range testCases {
t.Logf("Max length %d for %q", testCase.L, testCase.Input)
f := MaxLength(testCase.L)
err := f(testCase.Input)
if testCase.Err {
if err == nil {
t.Error("Expected an error; got nil")
}
} else {
if err != nil {
t.Errorf("Expected nil; got %s", err)
}
}
}
}
func TestMinLength(t *testing.T) {
type TestCase struct {
L int
Input string
Err bool
}
testCases := []TestCase{
{L: 8, Input: "abcd", Err: true},
{L: 8, Input: "abcdefgh"},
{L: 8, Input: "abcd efg"},
{L: 8, Input: "abcdefghi"},
}
for _, testCase := range testCases {
t.Logf("Min length %d for %q", testCase.L, testCase.Input)
f := MinLength(testCase.L)
err := f(testCase.Input)
if testCase.Err {
if err == nil {
t.Error("Expected an error; got nil")
}
} else {
if err != nil {
t.Errorf("Expected nil; got %s", err)
}
}
}
}