rule_length_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package valid
  2. import (
  3. "database/sql"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestLength(t *testing.T) {
  8. var v *string
  9. tests := []struct {
  10. tag string
  11. min, max int
  12. value interface{}
  13. err string
  14. }{
  15. {"t1", 2, 4, "abc", ""},
  16. {"t2", 2, 4, "", ""},
  17. {"t3", 2, 4, "abcdf", "the length must be between 2 and 4"},
  18. {"t4", 0, 4, "ab", ""},
  19. {"t5", 0, 4, "abcde", "the length must be no more than 4"},
  20. {"t6", 2, 0, "ab", ""},
  21. {"t7", 2, 0, "a", "the length must be no less than 2"},
  22. {"t8", 2, 0, v, ""},
  23. {"t9", 2, 0, 123, "cannot get the length of int"},
  24. {"t10", 2, 4, sql.NullString{String: "abc", Valid: true}, ""},
  25. {"t11", 2, 4, sql.NullString{String: "", Valid: true}, ""},
  26. {"t12", 2, 4, &sql.NullString{String: "abc", Valid: true}, ""},
  27. {"t13", 2, 2, "abcdf", "the length must be exactly 2"},
  28. {"t14", 2, 2, "ab", ""},
  29. {"t15", 0, 0, "", ""},
  30. {"t16", 0, 0, "ab", "the value must be empty"},
  31. }
  32. for _, test := range tests {
  33. r := Length(test.min, test.max)
  34. err := r.Validate(test.value)
  35. assertError(t, test.err, err, test.tag)
  36. }
  37. }
  38. func TestRuneLength(t *testing.T) {
  39. var v *string
  40. tests := []struct {
  41. tag string
  42. min, max int
  43. value interface{}
  44. err string
  45. }{
  46. {"t1", 2, 4, "abc", ""},
  47. {"t1.1", 2, 3, "💥💥", ""},
  48. {"t1.2", 2, 3, "💥💥💥", ""},
  49. {"t1.3", 2, 3, "💥", "the length must be between 2 and 3"},
  50. {"t1.4", 2, 3, "💥💥💥💥", "the length must be between 2 and 3"},
  51. {"t2", 2, 4, "", ""},
  52. {"t3", 2, 4, "abcdf", "the length must be between 2 and 4"},
  53. {"t4", 0, 4, "ab", ""},
  54. {"t5", 0, 4, "abcde", "the length must be no more than 4"},
  55. {"t6", 2, 0, "ab", ""},
  56. {"t7", 2, 0, "a", "the length must be no less than 2"},
  57. {"t8", 2, 0, v, ""},
  58. {"t9", 2, 0, 123, "cannot get the length of int"},
  59. {"t10", 2, 4, sql.NullString{String: "abc", Valid: true}, ""},
  60. {"t11", 2, 4, sql.NullString{String: "", Valid: true}, ""},
  61. {"t12", 2, 4, &sql.NullString{String: "abc", Valid: true}, ""},
  62. {"t13", 2, 3, &sql.NullString{String: "💥💥", Valid: true}, ""},
  63. {"t14", 2, 3, &sql.NullString{String: "💥", Valid: true}, "the length must be between 2 and 3"},
  64. }
  65. for _, test := range tests {
  66. r := RuneLength(test.min, test.max)
  67. err := r.Validate(test.value)
  68. assertError(t, test.err, err, test.tag)
  69. }
  70. }
  71. func Test_LengthRule_Error(t *testing.T) {
  72. r := Length(10, 20)
  73. assert.Equal(t, "the length must be between 10 and 20", r.Validate("abc").Error())
  74. r = Length(0, 20)
  75. assert.Equal(t, "the length must be no more than 20", r.Validate(make([]string, 21)).Error())
  76. r = Length(10, 0)
  77. assert.Equal(t, "the length must be no less than 10", r.Validate([9]string{}).Error())
  78. r = Length(0, 0)
  79. assert.Equal(t, "validation_length_empty_required", r.err.Code())
  80. r = r.Error("123")
  81. assert.Equal(t, "123", r.err.Message())
  82. }
  83. func TestLengthRule_ErrorObject(t *testing.T) {
  84. r := Length(10, 20)
  85. err := NewError("code", "abc")
  86. r = r.ErrorObject(err)
  87. assert.Equal(t, err, r.err)
  88. assert.Equal(t, err.Code(), r.err.Code())
  89. assert.Equal(t, err.Message(), r.err.Message())
  90. }