in_test.go 1.0 KB

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