This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotEmptyValidator_test.go
95 lines (91 loc) · 3.35 KB
/
NotEmptyValidator_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
package ecms_validator
import "testing"
type NotEmptyValidatorTestCase struct {
Name string
Value interface {}
ExpectedResultBln bool
ExpectedMessagesLen int
}
func TestNotEmptyValidator(t *testing.T) {
expectedErrorMessage := "Empty values are not allowed. Received and empty value."
validatorOptions := NotEmptyValidatorOptions{
MessageFuncs: &MessageFuncs{
EmptyNotAllowed: func(options ValidatorOptions, x interface{}) string {
return expectedErrorMessage
},
},
}
validator := NotEmptyValidator(validatorOptions)
for _, testCase := range []NotEmptyValidatorTestCase{
{"validate_(nil)", nil, false, 1},
{"validate_(0)", 0, false, 1},
{"validate_(false)", false, false, 1},
{"validate_([]string)", make([]string, 0), false, 1},
{"validate_([]string{\"hello\"})", []string{"hello"}, true, 0},
{"validate_(map[string]string{})", make(map[string]string), false, 1},
{"validate_(map[string]string{\"hello\": \"world\"})", map[string]string{"hello": "world"}, true, 0},
{"validate_(struct{}{})", struct{}{}, false, 1},
{"validate_(struct{Name string}{\"hello\"})", struct{Name string}{"hello"}, true, 0},
}{
t.Run(testCase.Name, func(t *testing.T) {
result, messages := validator(testCase.Value)
messagesLen := len(messages)
if result != testCase.ExpectedResultBln {
t.Errorf("Expected %v for `result.Result` but got %v",
testCase.ExpectedResultBln, result)
}
if messagesLen != testCase.ExpectedMessagesLen {
t.Errorf("Expected %d messages. Got %d",
testCase.ExpectedMessagesLen, messagesLen)
}
for _, message := range messages {
if len(message) == 0 {
t.Error("Expected non-empty message strings. " +
"Received an empty message string.")
}
if message != expectedErrorMessage {
t.Errorf("Received unknown error message %v; " +
"\nExpected: %v", message, expectedErrorMessage)
}
}
})
}
}
func TestNotEmptyValidator1(t *testing.T) {
expectedErrorMessage := DefaultEmptyNotAllowedMsg
validator := NotEmptyValidator1()
for _, testCase := range []NotEmptyValidatorTestCase{
{"validate_(nil)", nil, false, 1},
{"validate_(0)", 0, false, 1},
{"validate_(false)", false, false, 1},
{"validate_([]string)", make([]string, 0), false, 1},
{"validate_([]string{\"hello\"})", []string{"hello"}, true, 0},
{"validate_(map[string]string{})", make(map[string]string), false, 1},
{"validate_(map[string]string{\"hello\": \"world\"})", map[string]string{"hello": "world"}, true, 0},
{"validate_(struct{}{})", struct{}{}, false, 1},
{"validate_(struct{Name string}{\"hello\"})", struct{Name string}{"hello"}, true, 0},
}{
t.Run(testCase.Name, func(t *testing.T) {
result, messages := validator(testCase.Value)
messagesLen := len(messages)
if result != testCase.ExpectedResultBln {
t.Errorf("Expected %v for `result.Result` but got %v",
testCase.ExpectedResultBln, result)
}
if messagesLen != testCase.ExpectedMessagesLen {
t.Errorf("Expected %d messages. Got %d",
testCase.ExpectedMessagesLen, messagesLen)
}
for _, message := range messages {
if len(message) == 0 {
t.Error("Expected non-empty message strings. " +
"Received an empty message string.")
}
if message != expectedErrorMessage {
t.Errorf("Received unknown error message %v; " +
"\nExpected: %v", message, expectedErrorMessage)
}
}
})
}
}