in_test.go 981 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package validate
  2. import (
  3. "testing"
  4. )
  5. func TestIn(t *testing.T) {
  6. type TestCase[T comparable] struct {
  7. Input T
  8. A []T
  9. Err error
  10. }
  11. strIn := []string{"abcd", "ef", "1234"}
  12. strTestCases := []TestCase[string]{
  13. {Input: "abcd", A: strIn},
  14. {Input: "ef", A: strIn},
  15. {Input: "1234", A: strIn},
  16. {Input: "5678", A: strIn, Err: ErrValueNotAllowed},
  17. }
  18. for _, tc := range strTestCases {
  19. t.Logf("Testing %q against %v", tc.Input, tc.A)
  20. f := In(tc.A...)
  21. err := f(tc.Input)
  22. if err != tc.Err {
  23. t.Errorf("Expected error %v, got %v", tc.Err, err)
  24. }
  25. }
  26. intIn := []int{1, 23, 456}
  27. intTestCases := []TestCase[int]{
  28. {Input: 1, A: intIn},
  29. {Input: 23, A: intIn},
  30. {Input: 456, A: intIn},
  31. {Input: 789, A: intIn, Err: ErrValueNotAllowed},
  32. }
  33. for _, tc := range intTestCases {
  34. t.Logf("Testing %d against %v", tc.Input, tc.A)
  35. f := In(tc.A...)
  36. err := f(tc.Input)
  37. if err != tc.Err {
  38. t.Errorf("Expected error %v, got %v", tc.Err, err)
  39. }
  40. }
  41. }