standardise all validate tests

This commit is contained in:
Aneurin Barker Snook
2023-10-08 15:21:02 +01:00
parent c74486d122
commit 7cb1900ec1
6 changed files with 65 additions and 113 deletions
+19 -29
View File
@@ -4,31 +4,26 @@ import "testing"
func TestMaxLength(t *testing.T) {
type TestCase struct {
L int
Input string
Err bool
L int
Err error
}
testCases := []TestCase{
{L: 8, Input: "abcd"},
{L: 8, Input: "abcdefgh"},
{L: 8, Input: "abcd efg"},
{L: 8, Input: "abcdefghi", Err: true},
{Input: "abcd", L: 8},
{Input: "abcdefgh", L: 8},
{Input: "abcd efg", L: 8},
{Input: "abcdefghi", L: 8, Err: ErrTooManyChars},
}
for _, tc := range testCases {
t.Logf("Max length %d for %q", tc.L, tc.Input)
t.Logf("Testing %q against maximum length of %d", tc.Input, tc.L)
f := MaxLength(tc.L)
err := f(tc.Input)
if tc.Err {
if err == nil {
t.Error("Expected an error; got nil")
}
} else {
if err != nil {
t.Errorf("Expected nil; got %s", err)
}
if err != tc.Err {
t.Errorf("Expected error %v, got %v", tc.Err, err)
}
}
}
@@ -37,29 +32,24 @@ func TestMinLength(t *testing.T) {
type TestCase struct {
L int
Input string
Err bool
Err error
}
testCases := []TestCase{
{L: 8, Input: "abcd", Err: true},
{L: 8, Input: "abcdefgh"},
{L: 8, Input: "abcd efg"},
{L: 8, Input: "abcdefghi"},
{Input: "abcd", L: 8, Err: ErrTooFewChars},
{Input: "abcdefgh", L: 8},
{Input: "abcd efg", L: 8},
{Input: "abcdefghi", L: 8},
}
for _, tc := range testCases {
t.Logf("Min length %d for %q", tc.L, tc.Input)
t.Logf("Testing %q against minimum length of %d", tc.Input, tc.L)
f := MinLength(tc.L)
err := f(tc.Input)
if tc.Err {
if err == nil {
t.Error("Expected an error; got nil")
}
} else {
if err != nil {
t.Errorf("Expected nil; got %s", err)
}
if err != tc.Err {
t.Errorf("Expected error %v, got %v", tc.Err, err)
}
}
}