-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_test.go
179 lines (170 loc) · 4.45 KB
/
validation_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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package csv
import (
"testing"
"github.com/motemen/go-testutil/dataloc"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"gopkg.in/yaml.v2"
)
// helperLocalizer is a helper function that returns a new localizer.
func helperLocalizer(t *testing.T) *i18n.Localizer {
t.Helper()
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
bundle.LoadMessageFileFS(LocaleFS, "i18n/en.yaml")
bundle.LoadMessageFileFS(LocaleFS, "i18n/ja.yaml")
return i18n.NewLocalizer(bundle, "en")
}
func Test_booleanValidator_Do(t *testing.T) {
t.Parallel()
type args struct {
target any
}
tests := []struct {
name string
b *booleanValidator
args args
wantErr bool
}{
{
name: "should return nil if target is a boolean: true",
b: newBooleanValidator(),
args: args{target: "true"},
wantErr: false,
},
{
name: "should return nil if target is a boolean: false",
b: newBooleanValidator(),
args: args{target: "false"},
wantErr: false,
},
{
name: "should return nil if target is an int and is 0",
b: newBooleanValidator(),
args: args{target: "0"},
wantErr: false,
},
{
name: "should return nil if target is an int and is 1",
b: newBooleanValidator(),
args: args{target: "1"},
wantErr: false,
},
{
name: "should return an error if target is an int and is not 0 or 1",
b: newBooleanValidator(),
args: args{target: "2"},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
b := &booleanValidator{}
if err := b.Do(helperLocalizer(t), tt.args.target); (err != nil) != tt.wantErr {
t.Errorf("booleanValidator.Do() error = %v, wantErr %v, test case at %s", err, tt.wantErr, dataloc.L(tt.name))
}
})
}
}
func Test_alphaValidator_Do(t *testing.T) {
t.Parallel()
type args struct {
target any
}
tests := []struct {
name string
a *alphabetValidator
args args
wantErr bool
}{
{
name: "should return nil if target is a string and is a multiple alphabetic characters",
a: newAlphaValidator(),
args: args{target: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"},
wantErr: false,
},
{
name: "should return nil if target is empty string",
a: newAlphaValidator(),
args: args{target: ""},
},
{
name: "should return an error if target is not a string",
a: newAlphaValidator(),
args: args{target: 1},
wantErr: true,
},
{
name: "should return an error if target contains number",
a: newAlphaValidator(),
args: args{target: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1"},
wantErr: true,
},
{
name: "should return an error if target contains special character",
a: newAlphaValidator(),
args: args{target: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &alphabetValidator{}
if err := a.Do(helperLocalizer(t), tt.args.target); (err != nil) != tt.wantErr {
t.Errorf("alphaValidator.Do() error = %v, wantErr %v, test case at %s", err, tt.wantErr, dataloc.L(tt.name))
}
})
}
}
func Test_numericValidator_Do(t *testing.T) {
t.Parallel()
tests := []struct {
name string
n *numericValidator
arg any
wantErr bool
}{
{
name: "should return nil if target is a string and is a numeric character",
n: newNumericValidator(),
arg: "1234567890",
wantErr: false,
},
{
name: "should return an error if target is not a string",
n: newNumericValidator(),
arg: 1,
wantErr: true,
},
{
name: "should return an error if target is not a numeric character",
n: newNumericValidator(),
arg: "1234567890a",
wantErr: true,
},
{
name: "should return an error if target is an empty string",
n: newNumericValidator(),
arg: "",
wantErr: false,
},
{
name: "should return error if target is a string and is a float",
n: newNumericValidator(),
arg: "0.0",
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
n := &numericValidator{}
if err := n.Do(helperLocalizer(t), tt.arg); (err != nil) != tt.wantErr {
t.Errorf("numericValidator.Do() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}