-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoptions_test.go
118 lines (105 loc) · 2.69 KB
/
options_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
package valigo
import (
"fmt"
"testing"
"github.com/insei/fmap/v3"
"github.com/insei/valigo/shared"
"github.com/insei/valigo/translator"
)
const (
customRegexpLocaleMsg = "Only numbers and words is allowed"
customRegexpLocaleKey = "validation:string:" + customRegexpLocaleMsg
)
func TestOptionApply(t *testing.T) {
tStorage := translator.NewInMemStorage()
tStorage.Add("en", map[string]string{
customRegexpLocaleKey: customRegexpLocaleMsg,
})
tr := translator.New(translator.WithStorage(tStorage), translator.WithDefaultLang("en"))
opt := WithTranslator(tr)
v := New(opt)
opt.apply(v)
if v.helper.t == nil {
t.Errorf("expected translator to be set")
}
}
func TestOptionApplyWithNilTranslator(t *testing.T) {
opt := WithTranslator(nil)
v := New(opt)
opt.apply(v)
if v.helper.t == nil {
t.Errorf("expected translator to be nil")
}
}
func TestOptionApplyWithFieldLocationNamingFn(t *testing.T) {
fn := func(field fmap.Field) string {
return "test"
}
opt := WithFieldLocationNamingFn(fn)
v := New(opt)
v.helper.getFieldLocation = nil
opt.apply(v)
if v.helper.getFieldLocation == nil {
t.Errorf("expected getFieldLocation to be set")
}
}
func TestOptionApplyWithNilFieldLocationNamingFn(t *testing.T) {
opt := WithFieldLocationNamingFn(nil)
v := New(opt)
opt.apply(v)
if v.helper.getFieldLocation == nil {
t.Errorf("expected getFieldLocation to be nil")
}
}
func TestOptionApplyWithMultipleOptions(t *testing.T) {
tStorage := translator.NewInMemStorage()
tStorage.Add("en", map[string]string{
customRegexpLocaleKey: customRegexpLocaleMsg,
})
tr := translator.New(translator.WithStorage(tStorage), translator.WithDefaultLang("en"))
opt1 := WithTranslator(tr)
opt2 := WithFieldLocationNamingFn(func(field fmap.Field) string {
return "test"
})
v := New(opt1, opt2)
opt1.apply(v)
opt2.apply(v)
if v.helper.t == nil {
t.Errorf("expected translator to be set")
}
if v.helper.getFieldLocation == nil {
t.Errorf("expected getFieldLocation to be set")
}
}
func TestWithMultipleOptionsWithErrorsTransformer(t *testing.T) {
transformer := func(errs []shared.Error) []error {
var newErrs []error
for _, err := range errs {
newErrs = append(newErrs, fmt.Errorf("test: %v", err))
}
return newErrs
}
testCases := []struct {
name string
transformer func([]shared.Error) []error
}{
{
name: "Transformer set",
transformer: transformer,
},
{
name: "Transformer not set",
transformer: nil,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
opt := WithErrorsTransformer(tc.transformer)
v := New(opt)
opt.apply(v)
if v.transformError == nil {
t.Errorf("expected function, but got nil")
}
})
}
}