rule_strings_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package valid
  2. import (
  3. "database/sql"
  4. "reflect"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func validateMe(s string) bool {
  9. return s == "me"
  10. }
  11. func TestNewStringRule(t *testing.T) {
  12. v := NewStringRule(true, validateMe, "abc")
  13. assert.NotNil(t, v.validate)
  14. assert.Equal(t, "", v.err.Code())
  15. assert.Equal(t, "abc", v.err.Message())
  16. }
  17. func TestNewStringRuleWithError(t *testing.T) {
  18. err := NewError("C", "abc")
  19. v := NewStringRuleWithError(validateMe, err)
  20. assert.NotNil(t, v.validate)
  21. assert.Equal(t, err, v.err)
  22. assert.Equal(t, "C", v.err.Code())
  23. assert.Equal(t, "abc", v.err.Message())
  24. }
  25. func TestStringRule_Error(t *testing.T) {
  26. err := NewError("code", "abc")
  27. v := NewStringRuleWithError(validateMe, err).Error("abc")
  28. assert.Equal(t, "code", v.err.Code())
  29. assert.Equal(t, "abc", v.err.Message())
  30. v2 := v.Error("correct")
  31. assert.Equal(t, "code", v.err.Code())
  32. assert.Equal(t, "correct", v2.err.Message())
  33. assert.Equal(t, "abc", v.err.Message())
  34. }
  35. func TestStringValidator_Validate(t *testing.T) {
  36. t.Run("allow blank", func(t *testing.T) {
  37. v := NewStringRule(true, validateMe, "wrong_rule").Error("wrong")
  38. value := "me"
  39. err := v.Validate(value)
  40. assert.Nil(t, err)
  41. err = v.Validate(&value)
  42. assert.Nil(t, err)
  43. value = ""
  44. err = v.Validate(value)
  45. assert.Nil(t, err)
  46. err = v.Validate(&value)
  47. assert.Nil(t, err)
  48. nullValue := sql.NullString{String: "me", Valid: true}
  49. err = v.Validate(nullValue)
  50. assert.Nil(t, err)
  51. nullValue = sql.NullString{String: "", Valid: true}
  52. err = v.Validate(nullValue)
  53. assert.Nil(t, err)
  54. var s *string
  55. err = v.Validate(s)
  56. assert.Nil(t, err)
  57. err = v.Validate("not me")
  58. if assert.NotNil(t, err) {
  59. assert.Equal(t, "wrong", err.Error())
  60. }
  61. err = v.Validate(100)
  62. if assert.NotNil(t, err) {
  63. assert.NotEqual(t, "wrong", err.Error())
  64. }
  65. v2 := v.Error("Wrong!")
  66. err = v2.Validate("not me")
  67. if assert.NotNil(t, err) {
  68. assert.Equal(t, "Wrong!", err.Error())
  69. }
  70. })
  71. t.Run("deny blank", func(t *testing.T) {
  72. v := NewStringRule(false, validateMe, "abc")
  73. err := v.Validate(" ")
  74. if assert.NotNil(t, err) {
  75. assert.Equal(t, "cannot be blank", err.Error())
  76. }
  77. })
  78. }
  79. func TestGetErrorFieldName(t *testing.T) {
  80. type A struct {
  81. T0 string
  82. T1 string `json:"t1"`
  83. T2 string `json:"t2,omitempty"`
  84. T3 string `json:",omitempty"`
  85. T4 string `json:"t4,x1,omitempty"`
  86. }
  87. tests := []struct {
  88. tag string
  89. field string
  90. name string
  91. }{
  92. {"t1", "T0", "T0"},
  93. {"t2", "T1", "t1"},
  94. {"t3", "T2", "t2"},
  95. {"t4", "T3", "T3"},
  96. {"t5", "T4", "t4"},
  97. }
  98. a := reflect.TypeOf(A{})
  99. for _, test := range tests {
  100. field, _ := a.FieldByName(test.field)
  101. assert.Equal(t, test.name, getErrorFieldName(&field), test.tag)
  102. }
  103. }
  104. func TestStringRule_ErrorObject(t *testing.T) {
  105. r := NewStringRule(true, validateMe, "wrong_rule")
  106. err := NewError("code", "abc")
  107. r = r.ErrorObject(err)
  108. assert.Equal(t, err, r.err)
  109. assert.Equal(t, "code", r.err.Code())
  110. assert.Equal(t, "abc", r.err.Message())
  111. }