2023-10-12 19:55:08 +01:00
|
|
|
package validate
|
|
|
|
|
|
2023-11-19 20:27:33 +00:00
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
2023-10-12 19:55:08 +01:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2023-11-19 20:27:33 +00:00
|
|
|
if !errors.Is(err, tc.Err) {
|
2023-10-12 19:55:08 +01:00
|
|
|
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)
|
|
|
|
|
|
2023-11-19 20:27:33 +00:00
|
|
|
if !errors.Is(err, tc.Err) {
|
2023-10-12 19:55:08 +01:00
|
|
|
t.Errorf("Expected error %v, got %v", tc.Err, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|