-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdefault_test.go
124 lines (110 loc) · 2.43 KB
/
default_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
package govalidator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_DefaultInt(t *testing.T) {
v := New()
tests := []struct {
name string
actual int
expected int
defaultValue int
setDefault func(*int, int) Validator
}{
{
name: "Test default int of '3' wont be used if value is greater than 0 or nil",
actual: 1,
expected: 1,
defaultValue: 3,
setDefault: v.DefaultInt,
},
{
name: "Test default int of '3' will be used because value is empty",
expected: 3,
defaultValue: 3,
setDefault: v.DefaultInt,
},
}
for _, test := range tests {
test.setDefault(&test.actual, test.defaultValue)
assert.Equalf(
t,
test.actual,
test.expected,
"test case %q failed, expected: %s, got: %s",
test.expected,
test.actual,
)
}
}
func Test_DefaultFloat(t *testing.T) {
v := New()
tests := []struct {
name string
actual float64
expected float64
defaultValue float64
setDefault func(*float64, float64) Validator
}{
{
name: "Test default float of '5.0' won't be used if value is greater than 0.0 or nil",
actual: 1,
expected: 1,
defaultValue: 5.0,
setDefault: v.DefaultFloat,
},
{
name: "Test default float of '1' will be used",
expected: 1,
defaultValue: 1,
setDefault: v.DefaultFloat,
},
}
for _, test := range tests {
test.setDefault(&test.actual, test.defaultValue)
assert.Equalf(
t,
test.actual,
test.expected,
"test case %q failed, expected: %s, got: %s",
test.expected,
test.actual,
)
}
}
func Test_DefaultString(t *testing.T) {
v := New()
tests := []struct {
name string
actual string
expected string
defaultValue string
setDefault func(*string, string) Validator
}{
{
name: "Test default string of 'something' won't be used if a value is already valid",
actual: "hi",
expected: "hi",
defaultValue: "something",
setDefault: v.DefaultString,
},
{
name: "Test default string of 'hello' will be used",
expected: "hello",
defaultValue: "hello",
setDefault: v.DefaultString,
},
}
for _, test := range tests {
test.setDefault(&test.actual, test.defaultValue)
assert.Equalf(
t,
test.actual,
test.expected,
"test case %q failed, expected: %s, got: %s",
test.expected,
test.actual,
)
}
}