equal_test.go 938 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package validate
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. func TestEqualInt(t *testing.T) {
  7. type TestCase struct {
  8. I int
  9. C int
  10. Err error
  11. }
  12. testCases := []TestCase{
  13. {I: 1, C: 1},
  14. {I: 5 ^ 3, C: 5 ^ 3},
  15. {I: 10, C: 15, Err: ErrValueNotAllowed},
  16. }
  17. for i, tc := range testCases {
  18. t.Logf("(%d) Testing %d against %d", i, tc.I, tc.C)
  19. f := Equal(tc.C)
  20. err := f(tc.I)
  21. if !errors.Is(err, tc.Err) {
  22. t.Errorf("Expected error %v, got %v", tc.Err, err)
  23. }
  24. }
  25. }
  26. func TestEqualStr(t *testing.T) {
  27. type TestCase struct {
  28. I string
  29. C string
  30. Err error
  31. }
  32. testCases := []TestCase{
  33. {I: "abc", C: "abc"},
  34. {I: "def ghi 123", C: "def ghi 123"},
  35. {I: "jkl", C: "mno", Err: ErrValueNotAllowed},
  36. }
  37. for i, tc := range testCases {
  38. t.Logf("(%d) Testing %s against %s", i, tc.I, tc.C)
  39. f := Equal(tc.C)
  40. err := f(tc.I)
  41. if !errors.Is(err, tc.Err) {
  42. t.Errorf("Expected error %v, got %v", tc.Err, err)
  43. }
  44. }
  45. }