-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_test.go
206 lines (163 loc) · 5.1 KB
/
string_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package validator_test
import (
"errors"
"github.com/gungun974/validator"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type FakeTrueStringValidator struct{}
func (v FakeTrueStringValidator) Validate(_ string) error {
return nil
}
type FakeErrorStringValidator struct{}
func (v FakeErrorStringValidator) Validate(_ string) error {
return errors.New("this String validator always fail")
}
func stringValidatorTests() {
Describe("ValidateString", func() {
It("should return an String", func() {
// arrange
value := "5"
// act
result, err := validator.ValidateString(value, validator.StringValidators{})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal("5"))
})
It("should not return an String when input is garbage", func() {
// arrange
value := struct {
garbage bool
}{
garbage: true,
}
// act
_, err := validator.ValidateString(value, validator.StringValidators{})
// assert
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(Equal("value is not a string"))
})
It("should convert int into String", func() {
// arrange
value := 42
// act
result, err := validator.ValidateString(value, validator.StringValidators{})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal("42"))
})
It("should convert float into String", func() {
// arrange
value := 42.69
// act
result, err := validator.ValidateString(value, validator.StringValidators{})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal("42.69"))
})
It("should return an String when all rules are satisfied", func() {
// arrange
value := "result"
// act
result, err := validator.ValidateString(value, validator.StringValidators{
FakeTrueStringValidator{},
FakeTrueStringValidator{},
FakeTrueStringValidator{},
})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal("result"))
})
It("should not return an String when one rule is not satisfied", func() {
// arrange
value := "invalid"
// act
_, err := validator.ValidateString(value, validator.StringValidators{
FakeTrueStringValidator{},
FakeErrorStringValidator{},
FakeTrueStringValidator{},
})
// assert
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(Equal("this String validator always fail"))
})
Describe("StringEmailValidator", func() {
It("should not return a String when input is not a supported golang email", func() {
// arrange
value := "hoi"
// act
_, err := validator.ValidateString(value, validator.StringValidators{
validator.StringEmailValidator{},
})
// assert
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(Equal("value is not an email"))
})
It("should return a String when input is a supported golang email", func() {
// arrange
value := "john@doe.com"
// act
result, err := validator.ValidateString(value, validator.StringValidators{
validator.StringEmailValidator{},
})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal("john@doe.com"))
})
DescribeTable("should return a String when input is a weird but valid email account",
func(email string) {
// arrange
value := email
// act
result, err := validator.ValidateString(value, validator.StringValidators{
validator.StringEmailValidator{},
})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal(email))
},
Entry("t'challa@ is a valid email", "t'challa@me.com"),
Entry("rocket+groot@ is a valid email", "rocket+groot@gmail.com"),
Entry("\"Bruce Banner\"@ is a valid email", "\"Bruce Banner\"@batman.com"),
Entry("'@ is a valid email", "'@hoi.com"),
Entry("-@ is a valid email", "-@github.com"),
Entry("_@ is a valid email", "_@golang.com"),
)
})
Describe("StringPhoneValidator", func() {
It("should not return a String when input is not a phone number", func() {
// arrange
value := "hoi"
// act
_, err := validator.ValidateString(value, validator.StringValidators{
validator.StringPhoneValidator{},
})
// assert
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(Equal("value is not an international phone number"))
})
It("should return a String when input is an international phone number", func() {
// arrange
value := "+33 1 09 75 83 51"
// act
result, err := validator.ValidateString(value, validator.StringValidators{
validator.StringPhoneValidator{},
})
// assert
Expect(err).ShouldNot(HaveOccurred())
Expect(result).To(Equal("+33 1 09 75 83 51"))
})
It("should not return a String when input is not an international phone number", func() {
// arrange
value := "01 09 75 83 51"
// act
_, err := validator.ValidateString(value, validator.StringValidators{
validator.StringPhoneValidator{},
})
// assert
Expect(err).Should(HaveOccurred())
Expect(err.Error()).To(Equal("value is not an international phone number"))
})
})
})
}