rule_when_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package valid
  2. import (
  3. "context"
  4. "errors"
  5. "strings"
  6. "testing"
  7. )
  8. func abcValidation(val string) bool {
  9. return val == "abc"
  10. }
  11. func TestWhen(t *testing.T) {
  12. abcRule := NewStringRule(true, abcValidation, "wrong_abc")
  13. validateMeRule := NewStringRule(true, validateMe, "wrong_me")
  14. tests := []struct {
  15. tag string
  16. condition bool
  17. value interface{}
  18. rules []Rule
  19. elseRules []Rule
  20. err string
  21. }{
  22. // True condition
  23. {"t1.1", true, nil, []Rule{}, []Rule{}, ""},
  24. {"t1.2", true, "", []Rule{}, []Rule{}, ""},
  25. {"t1.3", true, "", []Rule{abcRule}, []Rule{}, ""},
  26. {"t1.4", true, 12, []Rule{Required}, []Rule{}, ""},
  27. {"t1.5", true, nil, []Rule{Required}, []Rule{}, "cannot be blank"},
  28. {"t1.6", true, "123", []Rule{abcRule}, []Rule{}, "wrong_abc"},
  29. {"t1.7", true, "abc", []Rule{abcRule}, []Rule{}, ""},
  30. {"t1.8", true, "abc", []Rule{abcRule, abcRule}, []Rule{}, ""},
  31. {"t1.9", true, "abc", []Rule{abcRule, validateMeRule}, []Rule{}, "wrong_me"},
  32. {"t1.10", true, "me", []Rule{abcRule, validateMeRule}, []Rule{}, "wrong_abc"},
  33. {"t1.11", true, "me", []Rule{}, []Rule{abcRule}, ""},
  34. // False condition
  35. {"t2.1", false, "", []Rule{}, []Rule{}, ""},
  36. {"t2.2", false, "", []Rule{abcRule}, []Rule{}, ""},
  37. {"t2.3", false, "abc", []Rule{abcRule}, []Rule{}, ""},
  38. {"t2.4", false, "abc", []Rule{abcRule, abcRule}, []Rule{}, ""},
  39. {"t2.5", false, "abc", []Rule{abcRule, validateMeRule}, []Rule{}, ""},
  40. {"t2.6", false, "me", []Rule{abcRule, validateMeRule}, []Rule{}, ""},
  41. {"t2.7", false, "", []Rule{abcRule, validateMeRule}, []Rule{}, ""},
  42. {"t2.8", false, "me", []Rule{}, []Rule{abcRule, validateMeRule}, "wrong_abc"},
  43. }
  44. for _, test := range tests {
  45. err := Validate(test.value, When(test.condition, test.rules...).Else(test.elseRules...))
  46. assertError(t, test.err, err, test.tag)
  47. }
  48. }
  49. func TestWhenWithContext(t *testing.T) {
  50. rule := WithContext(func(ctx context.Context, value interface{}) error {
  51. if !strings.Contains(value.(string), ctx.Value("contains").(string)) {
  52. return errors.New("unexpected value")
  53. }
  54. return nil
  55. })
  56. ctx1 := context.WithValue(context.Background(), "contains", "abc")
  57. ctx2 := context.WithValue(context.Background(), "contains", "xyz")
  58. tests := []struct {
  59. tag string
  60. condition bool
  61. value interface{}
  62. ctx context.Context
  63. err string
  64. }{
  65. // True condition
  66. {"t1.1", true, "abc", ctx1, ""},
  67. {"t1.2", true, "abc", ctx2, "unexpected value"},
  68. {"t1.3", true, "xyz", ctx1, "unexpected value"},
  69. {"t1.4", true, "xyz", ctx2, ""},
  70. // False condition
  71. {"t2.1", false, "abc", ctx1, ""},
  72. {"t2.2", false, "abc", ctx2, "unexpected value"},
  73. {"t2.3", false, "xyz", ctx1, "unexpected value"},
  74. {"t2.4", false, "xyz", ctx2, ""},
  75. }
  76. for _, test := range tests {
  77. err := ValidateWithContext(test.ctx, test.value, When(test.condition, rule).Else(rule))
  78. assertError(t, test.err, err, test.tag)
  79. }
  80. }