standardise all validate tests
This commit is contained in:
+3
-11
@@ -27,20 +27,12 @@ func TestAll(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Logf("%q", tc.Input)
|
t.Logf("Testing %q", tc.Input)
|
||||||
|
|
||||||
err := tc.F(tc.Input)
|
err := tc.F(tc.Input)
|
||||||
if tc.Err != nil {
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("Expected %s; got nil", tc.Err)
|
|
||||||
}
|
|
||||||
if err != tc.Err {
|
if err != tc.Err {
|
||||||
t.Errorf("Expected %s; got %s", tc.Err, err)
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Expected nil; got %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-13
@@ -4,32 +4,26 @@ import "testing"
|
|||||||
|
|
||||||
func TestChars(t *testing.T) {
|
func TestChars(t *testing.T) {
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
C string
|
|
||||||
Input string
|
Input string
|
||||||
Err bool
|
C string
|
||||||
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
hexRange := "0123456789abcdef"
|
hexRange := "0123456789abcdef"
|
||||||
|
|
||||||
testCases := []TestCase{
|
testCases := []TestCase{
|
||||||
{C: hexRange, Input: "abcd1234"},
|
{Input: "abcd1234", C: hexRange},
|
||||||
{C: hexRange, Input: "abcd 1234", Err: true},
|
{Input: "abcd 1234", C: hexRange, Err: ErrDisallowedChars},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Logf("%q contains only allowed characters %q", tc.Input, tc.C)
|
t.Logf("Testing %q against %q", tc.Input, tc.C)
|
||||||
|
|
||||||
f := Chars(tc.C)
|
f := Chars(tc.C)
|
||||||
err := f(tc.Input)
|
err := f(tc.Input)
|
||||||
|
|
||||||
if tc.Err {
|
if err != tc.Err {
|
||||||
if err == nil {
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
t.Error("Expected error; got nil")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Expected nil; got %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-10
@@ -5,24 +5,21 @@ import "testing"
|
|||||||
func TestEmail(t *testing.T) {
|
func TestEmail(t *testing.T) {
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
Input string
|
Input string
|
||||||
Err bool
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
testCases := []TestCase{
|
testCases := []TestCase{
|
||||||
{Input: "test@example.com"},
|
{Input: "test@example.com"},
|
||||||
{Input: "testexample.com", Err: true},
|
{Input: "testexample.com", Err: ErrInvalidEmail},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
t.Logf("Testing %q", tc.Input)
|
||||||
|
|
||||||
err := Email(tc.Input)
|
err := Email(tc.Input)
|
||||||
if tc.Err {
|
|
||||||
if err == nil {
|
if err != tc.Err {
|
||||||
t.Errorf("Expected %q to be an invalid email; got nil", tc.Input)
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Expected %q to be a valid email; got %s", tc.Input, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+18
-36
@@ -1,69 +1,51 @@
|
|||||||
package validate
|
package validate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIn(t *testing.T) {
|
func TestIn(t *testing.T) {
|
||||||
type TestCase[T comparable] struct {
|
type TestCase[T comparable] struct {
|
||||||
S []T
|
|
||||||
Input T
|
Input T
|
||||||
Err bool
|
A []T
|
||||||
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
strIn := []string{"abcd", "ef", "1234"}
|
strIn := []string{"abcd", "ef", "1234"}
|
||||||
strTestCases := []TestCase[string]{
|
strTestCases := []TestCase[string]{
|
||||||
{S: strIn, Input: "abcd"},
|
{Input: "abcd", A: strIn},
|
||||||
{S: strIn, Input: "ef"},
|
{Input: "ef", A: strIn},
|
||||||
{S: strIn, Input: "1234"},
|
{Input: "1234", A: strIn},
|
||||||
{S: strIn, Input: "5678", Err: true},
|
{Input: "5678", A: strIn, Err: ErrValueNotAllowed},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range strTestCases {
|
for _, tc := range strTestCases {
|
||||||
t.Logf("%q in %s", tc.Input, strings.Join(tc.S, ", "))
|
t.Logf("Testing %q against %v", tc.Input, tc.A)
|
||||||
|
|
||||||
f := In(tc.S...)
|
f := In(tc.A...)
|
||||||
err := f(tc.Input)
|
err := f(tc.Input)
|
||||||
|
|
||||||
if tc.Err {
|
if err != tc.Err {
|
||||||
if err == nil {
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
t.Error("Expected error; got nil")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Expected nil; got %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
intIn := []int{1, 23, 456}
|
intIn := []int{1, 23, 456}
|
||||||
intTestCases := []TestCase[int]{
|
intTestCases := []TestCase[int]{
|
||||||
{S: intIn, Input: 1},
|
{Input: 1, A: intIn},
|
||||||
{S: intIn, Input: 23},
|
{Input: 23, A: intIn},
|
||||||
{S: intIn, Input: 456},
|
{Input: 456, A: intIn},
|
||||||
{S: intIn, Input: 789, Err: true},
|
{Input: 789, A: intIn, Err: ErrValueNotAllowed},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range intTestCases {
|
for _, tc := range intTestCases {
|
||||||
intf := []string{}
|
t.Logf("Testing %d against %v", tc.Input, tc.A)
|
||||||
for _, v := range tc.S {
|
|
||||||
intf = append(intf, fmt.Sprint(v))
|
|
||||||
}
|
|
||||||
t.Logf("%d in %s", tc.Input, strings.Join(intf, ", "))
|
|
||||||
|
|
||||||
f := In(tc.S...)
|
f := In(tc.A...)
|
||||||
err := f(tc.Input)
|
err := f(tc.Input)
|
||||||
|
|
||||||
if tc.Err {
|
if err != tc.Err {
|
||||||
if err == nil {
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
t.Error("Expected error; got nil")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Expected nil; got %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-29
@@ -4,31 +4,26 @@ import "testing"
|
|||||||
|
|
||||||
func TestMaxLength(t *testing.T) {
|
func TestMaxLength(t *testing.T) {
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
L int
|
|
||||||
Input string
|
Input string
|
||||||
Err bool
|
L int
|
||||||
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
testCases := []TestCase{
|
testCases := []TestCase{
|
||||||
{L: 8, Input: "abcd"},
|
{Input: "abcd", L: 8},
|
||||||
{L: 8, Input: "abcdefgh"},
|
{Input: "abcdefgh", L: 8},
|
||||||
{L: 8, Input: "abcd efg"},
|
{Input: "abcd efg", L: 8},
|
||||||
{L: 8, Input: "abcdefghi", Err: true},
|
{Input: "abcdefghi", L: 8, Err: ErrTooManyChars},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
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)
|
f := MaxLength(tc.L)
|
||||||
err := f(tc.Input)
|
err := f(tc.Input)
|
||||||
if tc.Err {
|
|
||||||
if err == nil {
|
if err != tc.Err {
|
||||||
t.Error("Expected an error; got nil")
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Expected nil; got %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,29 +32,24 @@ func TestMinLength(t *testing.T) {
|
|||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
L int
|
L int
|
||||||
Input string
|
Input string
|
||||||
Err bool
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
testCases := []TestCase{
|
testCases := []TestCase{
|
||||||
{L: 8, Input: "abcd", Err: true},
|
{Input: "abcd", L: 8, Err: ErrTooFewChars},
|
||||||
{L: 8, Input: "abcdefgh"},
|
{Input: "abcdefgh", L: 8},
|
||||||
{L: 8, Input: "abcd efg"},
|
{Input: "abcd efg", L: 8},
|
||||||
{L: 8, Input: "abcdefghi"},
|
{Input: "abcdefghi", L: 8},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
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)
|
f := MinLength(tc.L)
|
||||||
err := f(tc.Input)
|
err := f(tc.Input)
|
||||||
if tc.Err {
|
|
||||||
if err == nil {
|
if err != tc.Err {
|
||||||
t.Error("Expected an error; got nil")
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Expected nil; got %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-13
@@ -5,29 +5,26 @@ import "testing"
|
|||||||
func TestUUID(t *testing.T) {
|
func TestUUID(t *testing.T) {
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
Input string
|
Input string
|
||||||
Err bool
|
Err error
|
||||||
}
|
}
|
||||||
|
|
||||||
testCases := []TestCase{
|
testCases := []TestCase{
|
||||||
{Input: "00000000-0000-0000-0000-000000000000"},
|
{Input: "00000000-0000-0000-0000-000000000000"},
|
||||||
{Input: "01234567-89ab-cdef-0123-456789abcdef"},
|
{Input: "01234567-89ab-cdef-0123-456789abcdef"},
|
||||||
{Input: "abcdef01-2345-6789-abcd-ef0123456789"},
|
{Input: "abcdef01-2345-6789-abcd-ef0123456789"},
|
||||||
{Input: "Not a UUID", Err: true},
|
{Input: "Not a UUID", Err: ErrInvalidUUID},
|
||||||
{Input: "00000000-00-0000-0000-00000000000000", Err: true},
|
{Input: "00000000-00-0000-0000-00000000000000", Err: ErrInvalidUUID},
|
||||||
{Input: "00000000000000000000000000000000", Err: true},
|
{Input: "00000000000000000000000000000000", Err: ErrInvalidUUID},
|
||||||
{Input: "01234567-89ab-cdef-ghij-klmnopqrstuv", Err: true},
|
{Input: "01234567-89ab-cdef-ghij-klmnopqrstuv", Err: ErrInvalidUUID},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
t.Logf("Testing %q", tc.Input)
|
||||||
|
|
||||||
err := UUID(tc.Input)
|
err := UUID(tc.Input)
|
||||||
if tc.Err {
|
|
||||||
if err == nil {
|
if err != tc.Err {
|
||||||
t.Errorf("Expected %q to be an invalid UUID; got nil", tc.Input)
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Expected %q to be a valid UUID; got %s", tc.Input, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user