rule_not_in_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package valid
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestNotIn(t *testing.T) {
  7. v := 1
  8. var v2 *int
  9. var tests = []struct {
  10. tag string
  11. values []interface{}
  12. value interface{}
  13. err string
  14. }{
  15. {"t0", []interface{}{1, 2}, 0, ""},
  16. {"t1", []interface{}{1, 2}, 1, "must not be in list"},
  17. {"t2", []interface{}{1, 2}, 2, "must not be in list"},
  18. {"t3", []interface{}{1, 2}, 3, ""},
  19. {"t4", []interface{}{}, 3, ""},
  20. {"t5", []interface{}{1, 2}, "1", ""},
  21. {"t6", []interface{}{1, 2}, &v, "must not be in list"},
  22. {"t7", []interface{}{1, 2}, v2, ""},
  23. }
  24. for _, test := range tests {
  25. r := NotIn(test.values...)
  26. err := r.Validate(test.value)
  27. assertError(t, test.err, err, test.tag)
  28. }
  29. }
  30. func Test_NotInRule_Error(t *testing.T) {
  31. r := NotIn(1, 2, 3)
  32. assert.Equal(t, "must not be in list", r.Validate(1).Error())
  33. r = r.Error("123")
  34. assert.Equal(t, "123", r.err.Message())
  35. }
  36. func TestNotInRule_ErrorObject(t *testing.T) {
  37. r := NotIn(1, 2, 3)
  38. err := NewError("code", "abc")
  39. r = r.ErrorObject(err)
  40. assert.Equal(t, err, r.err)
  41. assert.Equal(t, err.Code(), r.err.Code())
  42. assert.Equal(t, err.Message(), r.err.Message())
  43. }