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
/
InRangeValidator_test.go
87 lines (82 loc) · 2.58 KB
/
InRangeValidator_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
package ecms_validator
import (
"testing"
)
type IntRangeValidatorTestCase struct {
Name string
TestValue interface {}
ExpectedResultBln bool
ExpectedMessagesLen int
Min int64
Max int64
}
type FloatRangeValidatorTestCase struct {
Name string
TestValue interface {}
ExpectedResultBln bool
ExpectedMessagesLen int
Min float64
Max float64
}
func TestIntRangeValidator(t *testing.T) {
for _, testCase := range []IntRangeValidatorTestCase{
{"validate_(nil)", nil, false, 1, 0, 0},
{"validate_(0, 0, 0)", 0, true, 0, 0, 0},
{"validate_(-10, 10, 0)", 0, true, 0, -10, 10},
{"validate_(10, -10, 0)", 0, true, 0, 10, -10},
} {
t.Run(testCase.Name, func(t *testing.T) {
validatorOptions := NewIntRangeValidatorOptions()
validatorOptions.Min = testCase.Min
validatorOptions.Max = testCase.Max
validator := IntRangeValidator(validatorOptions)
resultBln, messages := validator(testCase.TestValue)
messagesLen := len(messages)
if resultBln != testCase.ExpectedResultBln {
t.Errorf("Expected %v for `resultBln.Result` but got %v",
testCase.ExpectedResultBln, resultBln)
}
if messagesLen != testCase.ExpectedMessagesLen {
t.Errorf("Expected %d messages. Got %d; Messages: %v",
testCase.ExpectedMessagesLen, messagesLen, messages)
}
for _, message := range messages {
if len(message) == 0 {
t.Error("Expected non-empty message strings. " +
"Received an empty message string.")
}
}
})
}
}
func TestFloatRangeValidator(t *testing.T) {
for _, testCase := range []FloatRangeValidatorTestCase{
{"float_validate_(nil)", nil, false, 1, 0, 0},
{"float_validate_(0, 0, 0)", 0, true, 0, 0, 0},
{"float_validate_(-10, 10, 0)", 0, true, 0, -10, 10},
{"float_validate_(10, -10, 0)", 0, true, 0, 10, -10},
} {
t.Run(testCase.Name, func(t *testing.T) {
validatorOptions := NewFloatRangeValidatorOptions()
validatorOptions.Min = testCase.Min
validatorOptions.Max = testCase.Max
validator := FloatRangeValidator(validatorOptions)
resultBln, messages := validator(testCase.TestValue)
messagesLen := len(messages)
if resultBln != testCase.ExpectedResultBln {
t.Errorf("Expected %v for `resultBln.Result` but got %v",
testCase.ExpectedResultBln, resultBln)
}
if messagesLen != testCase.ExpectedMessagesLen {
t.Errorf("Expected %d messages. Got %d; Messages: %v",
testCase.ExpectedMessagesLen, messagesLen, messages)
}
for _, message := range messages {
if len(message) == 0 {
t.Error("Expected non-empty message strings. " +
"Received an empty message string.")
}
}
})
}
}