-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_test.go
125 lines (89 loc) · 2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package formulate
import "testing"
func TestMemoryValidationStore_AddValidationError(t *testing.T) {
v := NewMemoryValidationStore()
err := v.AddValidationError("AddressLine1", ValidationError{Error: "Invalid address", Value: "1 Fake Street"})
if err != nil {
t.Error(err)
return
}
err = v.AddValidationError("AddressLine2", ValidationError{Error: "Value must not be empty", Value: ""})
if err != nil {
t.Error(err)
return
}
err = v.AddValidationError("AddressLine1", ValidationError{Error: "Fake Street not found", Value: "1 Fake Street"})
if err != nil {
t.Error(err)
return
}
validationErrors, err := v.GetValidationErrors("AddressLine1")
if err != nil {
t.Error(err)
return
}
if len(validationErrors) != 2 {
t.Fail()
}
assertEquals(t, validationErrors[0].Error, "Invalid address")
assertEquals(t, validationErrors[1].Error, "Fake Street not found")
validationErrors, err = v.GetValidationErrors("AddressLine2")
if err != nil {
t.Error(err)
return
}
if len(validationErrors) != 1 {
t.Fail()
}
assertEquals(t, validationErrors[0].Error, "Value must not be empty")
validationErrors, err = v.GetValidationErrors("NotFound")
assertEquals(t, len(validationErrors), 0)
}
func TestMemoryValidationStore_GetFormValue(t *testing.T) {
t.Run("Saved value is pointer", func(t *testing.T) {
v := NewMemoryValidationStore()
value := &YourDetails{Name: "Test"}
if err := v.SetFormValue(value); err != nil {
t.Error(err)
return
}
var value2 YourDetails
if err := v.GetFormValue(&value2); err != nil {
t.Error(err)
return
}
assertEquals(t, value2.Name, "Test")
})
t.Run("Saved value is not pointer", func(t *testing.T) {
v := NewMemoryValidationStore()
value := YourDetails{Name: "Test2"}
if err := v.SetFormValue(value); err != nil {
t.Error(err)
return
}
var value2 YourDetails
if err := v.GetFormValue(&value2); err != nil {
t.Error(err)
return
}
assertEquals(t, value2.Name, "Test2")
})
t.Run("Get value is not pointer", func(t *testing.T) {
v := NewMemoryValidationStore()
value := YourDetails{Name: "Test2"}
if err := v.SetFormValue(value); err != nil {
t.Error(err)
return
}
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic() on non-ptr type.")
}
}()
var value2 YourDetails
if err := v.GetFormValue(value2); err != nil {
t.Error(err)
return
}
})
}