-
Notifications
You must be signed in to change notification settings - Fork 4
/
date_test.go
84 lines (77 loc) · 1.85 KB
/
date_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
package govalidator
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_Date(t *testing.T) {
tests := []struct {
name string
field string
value string
layout string
isPassed bool
msg string
expectedMsg string
}{
{
name: "test `1995-02-12` will pass under `2006-01-02` layout",
field: "birth_date",
value: "1995-02-12",
layout: "2006-01-02",
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test `1995/02/12` will pass under 2006/01/02 layout",
field: "created_at",
value: "1995/02/12",
layout: "2006/01/02",
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test `15:04:05` will pass under TimeOnly layout",
field: "arrived_at",
value: "15:04:05",
layout: time.TimeOnly,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test `20240207` won't pass because it didn't comply defined date layout",
field: "due_at",
value: "20240207",
layout: "2006-01-02",
isPassed: false,
msg: "",
expectedMsg: "due_at has wrong date format",
},
{
name: "test `1995-02-12` won't pass because it didn't comply defined date layout",
field: "expired_at",
value: "1995-02-12",
layout: "2006/01/02",
isPassed: false,
msg: "expired_at has wrong date format",
expectedMsg: "expired_at has wrong date format",
},
}
for _, test := range tests {
v := New()
v.Date(test.layout, test.value, test.field, test.msg)
if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed, expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}