-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvalidator_test.go
66 lines (58 loc) · 1.39 KB
/
validator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package gody
import (
"errors"
"testing"
"github.com/guiferpa/gody/v2/rule/ruletest"
)
func TestValidator(t *testing.T) {
payload := struct {
A int `validate:"test"`
}{10}
validator := NewValidator()
rule := ruletest.NewRule("test", true, nil)
if err := validator.AddRules(rule); err != nil {
t.Error("Unexpected error")
return
}
validated, err := validator.Validate(payload)
if !validated {
t.Error("Validated result is not expected")
return
}
if err != nil {
t.Error("Error result from validate is not expected")
return
}
if !rule.ValidateCalled {
t.Error("The rule validate wasn't call")
return
}
}
func TestDuplicatedRule(t *testing.T) {
validator := NewValidator()
rule := ruletest.NewRule("a", true, nil)
rules := []Rule{
rule,
ruletest.NewRule("b", true, nil),
ruletest.NewRule("c", true, nil),
rule,
}
err := validator.AddRules(rules...)
if err == nil {
t.Error("Unexpected nil value for duplicated rule error")
return
}
if _, ok := err.(*ErrDuplicatedRule); !ok {
t.Errorf("Unexpected error type: got: %v", err)
return
}
}
func TestDuplicatedRuleError(t *testing.T) {
r := ruletest.NewRule("mock", true, errors.New("mock error"))
err := &ErrDuplicatedRule{RuleDuplicated: r}
got := err.Error()
want := "rule mock is duplicated"
if got != want {
t.Errorf(`&ErrDuplicatedRule{RuleDuplicated: r}.Error(), got: %v, want: %v`, got, want)
}
}