diff --git a/equal.go b/equal.go new file mode 100644 index 0000000..69c3be1 --- /dev/null +++ b/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 + } +} diff --git a/equal_test.go b/equal_test.go new file mode 100644 index 0000000..6da691a --- /dev/null +++ b/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) + } + } +} diff --git a/in_test.go b/in_test.go index 1a9c883..c8bde32 100644 --- a/in_test.go +++ b/in_test.go @@ -4,33 +4,6 @@ import ( "testing" ) -func TestInString(t *testing.T) { - type TestCase struct { - Input string - A []string - Err error - } - - allow := []string{"abcd", "ef", "1234"} - testCases := []TestCase{ - {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 %q against %v", n, tc.Input, tc.A) - - f := In(tc.A...) - err := f(tc.Input) - - if err != tc.Err { - t.Errorf("Expected error %v, got %v", tc.Err, err) - } - } -} - func TestInInt(t *testing.T) { type TestCase struct { Input int @@ -57,3 +30,30 @@ func TestInInt(t *testing.T) { } } } + +func TestInString(t *testing.T) { + type TestCase struct { + Input string + A []string + Err error + } + + allow := []string{"abcd", "ef", "1234"} + testCases := []TestCase{ + {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 %q against %v", n, tc.Input, tc.A) + + f := In(tc.A...) + err := f(tc.Input) + + if err != tc.Err { + t.Errorf("Expected error %v, got %v", tc.Err, err) + } + } +}