rule_not_nil_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package valid
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. type MyInterface interface {
  7. Hello()
  8. }
  9. func TestNotNil(t *testing.T) {
  10. var v1 []int
  11. var v2 map[string]int
  12. var v3 *int
  13. var v4 interface{}
  14. var v5 MyInterface
  15. tests := []struct {
  16. tag string
  17. value interface{}
  18. err string
  19. }{
  20. {"t1", v1, "is required"},
  21. {"t2", v2, "is required"},
  22. {"t3", v3, "is required"},
  23. {"t4", v4, "is required"},
  24. {"t5", v5, "is required"},
  25. {"t6", "", ""},
  26. {"t7", 0, ""},
  27. }
  28. for _, test := range tests {
  29. r := NotNil
  30. err := r.Validate(test.value)
  31. assertError(t, test.err, err, test.tag)
  32. }
  33. }
  34. func Test_notNilRule_Error(t *testing.T) {
  35. r := NotNil
  36. assert.Equal(t, "is required", r.Validate(nil).Error())
  37. r2 := r.Error("123")
  38. assert.Equal(t, "is required", r.Validate(nil).Error())
  39. assert.Equal(t, "123", r2.err.Message())
  40. }
  41. func TestNotNilRule_ErrorObject(t *testing.T) {
  42. r := NotNil
  43. err := NewError("code", "abc")
  44. r = r.ErrorObject(err)
  45. assert.Equal(t, err, r.err)
  46. assert.Equal(t, err.Code(), r.err.Code())
  47. assert.Equal(t, err.Message(), r.err.Message())
  48. assert.NotEqual(t, err, NotNil.err)
  49. }