-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
116 lines (99 loc) · 2.37 KB
/
config_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
package cronfab
import (
"testing"
"time"
)
// DayUnit units in days
type testDayUnit struct{}
func (testDayUnit) String() string {
return "day"
}
func (testDayUnit) Add(t time.Time, n int) time.Time {
return t.AddDate(0, 0, n)
}
func (testDayUnit) Less(u Unit) bool {
switch u.(type) {
case testDayUnit:
return false
}
return true
}
func (testDayUnit) Trunc(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
type testMonthUnit struct{}
func (testMonthUnit) String() string {
return "month"
}
func (testMonthUnit) Add(t time.Time, n int) time.Time {
return t.AddDate(0, n, 0)
}
func (testMonthUnit) Less(u Unit) bool {
switch u.(type) {
case testDayUnit, testMonthUnit:
return false
}
return true
}
func (testMonthUnit) Trunc(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location())
}
type testYearUnit struct{}
func (testYearUnit) String() string {
return "year"
}
func (testYearUnit) Add(t time.Time, n int) time.Time {
return t.AddDate(n, 0, 0)
}
func (testYearUnit) Less(u Unit) bool {
switch u.(type) {
case testDayUnit, testMonthUnit, testYearUnit:
return false
}
return true
}
func (testYearUnit) Trunc(t time.Time) time.Time {
return time.Date(t.Year(), 0, 0, 0, 0, 0, 0, t.Location())
}
func TestNewCrontabConfig(t *testing.T) {
var testContabConfig = NewCrontabConfig([]FieldConfig{
{
unit: testDayUnit{},
name: "day of month",
min: 1,
max: 31,
getIndex: func(t time.Time) int {
return t.Day()
},
},
{
unit: testMonthUnit{},
name: "month",
rangeNames: []string{"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"},
min: 1,
max: 12,
getIndex: func(t time.Time) int {
return int(t.Month())
},
},
{
unit: testDayUnit{},
name: "day of week",
rangeNames: []string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"},
min: 0,
max: 6,
getIndex: func(t time.Time) int {
return int(t.Weekday())
},
},
})
if len(testContabConfig.Units) != 2 {
t.Fatalf("unexpected value.")
}
if testContabConfig.Units[0] != Unit(testDayUnit{}) {
t.Fatalf("unexpected value.")
}
if testContabConfig.Units[1] != Unit(testMonthUnit{}) {
t.Fatalf("unexpected value.")
}
}