in_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package validate
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. func TestInInt(t *testing.T) {
  7. type TestCase struct {
  8. Input int
  9. A []int
  10. Err error
  11. }
  12. allow := []int{1, 23, 456}
  13. testCases := []TestCase{
  14. {Input: 1, A: allow},
  15. {Input: 23, A: allow},
  16. {Input: 456, A: allow},
  17. {Input: 789, A: allow, Err: ErrValueNotAllowed},
  18. }
  19. for n, tc := range testCases {
  20. t.Logf("(%d) Testing %d against %v", n, tc.Input, tc.A)
  21. f := In(tc.A...)
  22. err := f(tc.Input)
  23. if !errors.Is(err, tc.Err) {
  24. t.Errorf("Expected error %v, got %v", tc.Err, err)
  25. }
  26. }
  27. }
  28. func TestInString(t *testing.T) {
  29. type TestCase struct {
  30. Input string
  31. A []string
  32. Err error
  33. }
  34. allow := []string{"abcd", "ef", "1234"}
  35. testCases := []TestCase{
  36. {Input: "abcd", A: allow},
  37. {Input: "ef", A: allow},
  38. {Input: "1234", A: allow},
  39. {Input: "5678", A: allow, Err: ErrValueNotAllowed},
  40. }
  41. for n, tc := range testCases {
  42. t.Logf("(%d) Testing %q against %v", n, tc.Input, tc.A)
  43. f := In(tc.A...)
  44. err := f(tc.Input)
  45. if !errors.Is(err, tc.Err) {
  46. t.Errorf("Expected error %v, got %v", tc.Err, err)
  47. }
  48. }
  49. }