add validation error type, fix tests, add number tests

This commit is contained in:
Aneurin Barker Snook
2023-11-19 20:27:33 +00:00
parent 196e1faeeb
commit db902e3c1c
13 changed files with 340 additions and 40 deletions
+49
View File
@@ -0,0 +1,49 @@
package validate
import (
"errors"
"testing"
)
func TestErrorIs(t *testing.T) {
type TestCase struct {
Err error
Target error
Is bool
}
testCases := []TestCase{
// Is any validation error
{Err: Err, Target: Err, Is: true},
{Err: ErrDisallowedChars, Target: Err, Is: true},
{Err: ErrMustBeGreater, Target: Err, Is: true},
// Is specific validation error
{Err: ErrDisallowedChars, Target: ErrDisallowedChars, Is: true},
{Err: ErrMustBeGreater, Target: ErrMustBeGreater, Is: true},
// Is not specific validation error
{Err: Err, Target: ErrDisallowedChars},
{Err: Err, Target: ErrMustBeGreater},
{Err: ErrMustBeGreater, Target: ErrDisallowedChars},
{Err: ErrDisallowedChars, Target: ErrMustBeGreater},
// Is not any other error
{Err: ErrDisallowedChars, Target: errors.New("contains disallowed characters")},
{Err: ErrMustBeGreater, Target: errors.New("must be greater than %v")},
}
for i, tc := range testCases {
t.Logf("(%d) Testing %v against %v", i, tc.Err, tc.Target)
if errors.Is(tc.Err, tc.Target) {
if !tc.Is {
t.Errorf("%v should not equal %v", tc.Err, tc.Target)
}
} else {
if tc.Is {
t.Errorf("%v should equal %v", tc.Err, tc.Target)
}
}
}
}