Browse Source

standardise all validate tests

Aneurin Barker Snook 1 year ago
parent
commit
7cb1900ec1
6 changed files with 65 additions and 113 deletions
  1. 4 12
      all_test.go
  2. 7 13
      chars_test.go
  3. 7 10
      email_test.go
  4. 18 36
      in_test.go
  5. 19 29
      length_test.go
  6. 10 13
      uuid_test.go

+ 4 - 12
all_test.go

@@ -27,20 +27,12 @@ func TestAll(t *testing.T) {
 	}
 
 	for _, tc := range testCases {
-		t.Logf("%q", tc.Input)
+		t.Logf("Testing %q", 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 {
-				t.Errorf("Expected %s; got %s", tc.Err, err)
-			}
-		} 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)
 		}
 	}
 }

+ 7 - 13
chars_test.go

@@ -4,32 +4,26 @@ import "testing"
 
 func TestChars(t *testing.T) {
 	type TestCase struct {
-		C     string
 		Input string
-		Err   bool
+		C     string
+		Err   error
 	}
 
 	hexRange := "0123456789abcdef"
 
 	testCases := []TestCase{
-		{C: hexRange, Input: "abcd1234"},
-		{C: hexRange, Input: "abcd 1234", Err: true},
+		{Input: "abcd1234", C: hexRange},
+		{Input: "abcd 1234", C: hexRange, Err: ErrDisallowedChars},
 	}
 
 	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)
 		err := f(tc.Input)
 
-		if tc.Err {
-			if err == nil {
-				t.Error("Expected 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)
 		}
 	}
 }

+ 7 - 10
email_test.go

@@ -5,24 +5,21 @@ import "testing"
 func TestEmail(t *testing.T) {
 	type TestCase struct {
 		Input string
-		Err   bool
+		Err   error
 	}
 
 	testCases := []TestCase{
 		{Input: "test@example.com"},
-		{Input: "testexample.com", Err: true},
+		{Input: "testexample.com", Err: ErrInvalidEmail},
 	}
 
 	for _, tc := range testCases {
+		t.Logf("Testing %q", tc.Input)
+
 		err := Email(tc.Input)
-		if tc.Err {
-			if err == nil {
-				t.Errorf("Expected %q to be an invalid email; got nil", tc.Input)
-			}
-		} else {
-			if err != nil {
-				t.Errorf("Expected %q to be a valid email; got %s", tc.Input, err)
-			}
+
+		if err != tc.Err {
+			t.Errorf("Expected error %v, got %v", tc.Err, err)
 		}
 	}
 }

+ 18 - 36
in_test.go

@@ -1,69 +1,51 @@
 package validate
 
 import (
-	"fmt"
-	"strings"
 	"testing"
 )
 
 func TestIn(t *testing.T) {
 	type TestCase[T comparable] struct {
-		S     []T
 		Input T
-		Err   bool
+		A     []T
+		Err   error
 	}
 
 	strIn := []string{"abcd", "ef", "1234"}
 	strTestCases := []TestCase[string]{
-		{S: strIn, Input: "abcd"},
-		{S: strIn, Input: "ef"},
-		{S: strIn, Input: "1234"},
-		{S: strIn, Input: "5678", Err: true},
+		{Input: "abcd", A: strIn},
+		{Input: "ef", A: strIn},
+		{Input: "1234", A: strIn},
+		{Input: "5678", A: strIn, Err: ErrValueNotAllowed},
 	}
 
 	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)
 
-		if tc.Err {
-			if err == nil {
-				t.Error("Expected 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)
 		}
 	}
 
 	intIn := []int{1, 23, 456}
 	intTestCases := []TestCase[int]{
-		{S: intIn, Input: 1},
-		{S: intIn, Input: 23},
-		{S: intIn, Input: 456},
-		{S: intIn, Input: 789, Err: true},
+		{Input: 1, A: intIn},
+		{Input: 23, A: intIn},
+		{Input: 456, A: intIn},
+		{Input: 789, A: intIn, Err: ErrValueNotAllowed},
 	}
 
 	for _, tc := range intTestCases {
-		intf := []string{}
-		for _, v := range tc.S {
-			intf = append(intf, fmt.Sprint(v))
-		}
-		t.Logf("%d in %s", tc.Input, strings.Join(intf, ", "))
+		t.Logf("Testing %d against %v", tc.Input, tc.A)
 
-		f := In(tc.S...)
+		f := In(tc.A...)
 		err := f(tc.Input)
 
-		if tc.Err {
-			if err == nil {
-				t.Error("Expected 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)
 		}
 	}
 }

+ 19 - 29
length_test.go

@@ -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)
 		}
 	}
 }

+ 10 - 13
uuid_test.go

@@ -5,29 +5,26 @@ import "testing"
 func TestUUID(t *testing.T) {
 	type TestCase struct {
 		Input string
-		Err   bool
+		Err   error
 	}
 
 	testCases := []TestCase{
 		{Input: "00000000-0000-0000-0000-000000000000"},
 		{Input: "01234567-89ab-cdef-0123-456789abcdef"},
 		{Input: "abcdef01-2345-6789-abcd-ef0123456789"},
-		{Input: "Not a UUID", Err: true},
-		{Input: "00000000-00-0000-0000-00000000000000", Err: true},
-		{Input: "00000000000000000000000000000000", Err: true},
-		{Input: "01234567-89ab-cdef-ghij-klmnopqrstuv", Err: true},
+		{Input: "Not a UUID", Err: ErrInvalidUUID},
+		{Input: "00000000-00-0000-0000-00000000000000", Err: ErrInvalidUUID},
+		{Input: "00000000000000000000000000000000", Err: ErrInvalidUUID},
+		{Input: "01234567-89ab-cdef-ghij-klmnopqrstuv", Err: ErrInvalidUUID},
 	}
 
 	for _, tc := range testCases {
+		t.Logf("Testing %q", tc.Input)
+
 		err := UUID(tc.Input)
-		if tc.Err {
-			if err == nil {
-				t.Errorf("Expected %q to be an invalid UUID; got nil", tc.Input)
-			}
-		} else {
-			if err != nil {
-				t.Errorf("Expected %q to be a valid UUID; got %s", tc.Input, err)
-			}
+
+		if err != tc.Err {
+			t.Errorf("Expected error %v, got %v", tc.Err, err)
 		}
 	}
 }