Aneurin Barker Snook 1 жил өмнө
parent
commit
9537961e29
3 өөрчлөгдсөн 82 нэмэгдсэн , 18 устгасан
  1. 11 0
      equal.go
  2. 53 0
      equal_test.go
  3. 18 18
      in_test.go

+ 11 - 0
equal.go

@@ -0,0 +1,11 @@
+package validate
+
+// Equal validates whether an input value is equal to a comparison value.
+func Equal[T comparable](cmp T) func(T) error {
+	return func(value T) error {
+		if value != cmp {
+			return ErrValueNotAllowed
+		}
+		return nil
+	}
+}

+ 53 - 0
equal_test.go

@@ -0,0 +1,53 @@
+package validate
+
+import "testing"
+
+func TestEqualInt(t *testing.T) {
+	type TestCase struct {
+		I   int
+		C   int
+		Err error
+	}
+
+	testCases := []TestCase{
+		{I: 1, C: 1},
+		{I: 5 ^ 3, C: 5 ^ 3},
+		{I: 10, C: 15, Err: ErrValueNotAllowed},
+	}
+
+	for i, tc := range testCases {
+		t.Logf("(%d) Testing %d against %d", i, tc.I, tc.C)
+
+		f := Equal(tc.C)
+		err := f(tc.I)
+
+		if err != tc.Err {
+			t.Errorf("Expected error %v, got %v", tc.Err, err)
+		}
+	}
+}
+
+func TestEqualStr(t *testing.T) {
+	type TestCase struct {
+		I   string
+		C   string
+		Err error
+	}
+
+	testCases := []TestCase{
+		{I: "abc", C: "abc"},
+		{I: "def ghi 123", C: "def ghi 123"},
+		{I: "jkl", C: "mno", Err: ErrValueNotAllowed},
+	}
+
+	for i, tc := range testCases {
+		t.Logf("(%d) Testing %s against %s", i, tc.I, tc.C)
+
+		f := Equal(tc.C)
+		err := f(tc.I)
+
+		if err != tc.Err {
+			t.Errorf("Expected error %v, got %v", tc.Err, err)
+		}
+	}
+}

+ 18 - 18
in_test.go

@@ -4,23 +4,23 @@ import (
 	"testing"
 )
 
-func TestInString(t *testing.T) {
+func TestInInt(t *testing.T) {
 	type TestCase struct {
-		Input string
-		A     []string
+		Input int
+		A     []int
 		Err   error
 	}
 
-	allow := []string{"abcd", "ef", "1234"}
+	allow := []int{1, 23, 456}
 	testCases := []TestCase{
-		{Input: "abcd", A: allow},
-		{Input: "ef", A: allow},
-		{Input: "1234", A: allow},
-		{Input: "5678", A: allow, Err: ErrValueNotAllowed},
+		{Input: 1, A: allow},
+		{Input: 23, A: allow},
+		{Input: 456, A: allow},
+		{Input: 789, A: allow, Err: ErrValueNotAllowed},
 	}
 
 	for n, tc := range testCases {
-		t.Logf("(%d) Testing %q against %v", n, tc.Input, tc.A)
+		t.Logf("(%d) Testing %d against %v", n, tc.Input, tc.A)
 
 		f := In(tc.A...)
 		err := f(tc.Input)
@@ -31,23 +31,23 @@ func TestInString(t *testing.T) {
 	}
 }
 
-func TestInInt(t *testing.T) {
+func TestInString(t *testing.T) {
 	type TestCase struct {
-		Input int
-		A     []int
+		Input string
+		A     []string
 		Err   error
 	}
 
-	allow := []int{1, 23, 456}
+	allow := []string{"abcd", "ef", "1234"}
 	testCases := []TestCase{
-		{Input: 1, A: allow},
-		{Input: 23, A: allow},
-		{Input: 456, A: allow},
-		{Input: 789, A: allow, Err: ErrValueNotAllowed},
+		{Input: "abcd", A: allow},
+		{Input: "ef", A: allow},
+		{Input: "1234", A: allow},
+		{Input: "5678", A: allow, Err: ErrValueNotAllowed},
 	}
 
 	for n, tc := range testCases {
-		t.Logf("(%d) Testing %d against %v", n, tc.Input, tc.A)
+		t.Logf("(%d) Testing %q against %v", n, tc.Input, tc.A)
 
 		f := In(tc.A...)
 		err := f(tc.Input)