-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
127 lines (102 loc) · 2.82 KB
/
parser_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
125
126
127
package gocron
import (
"errors"
"testing"
)
func TestParser_Parse_refusesTwiceNotSpecified(t *testing.T) {
_, err := defaultParser.Parse("* * * ? * ?")
requireErrorIs(t, err, ErrMultipleNotSpecified)
}
func TestParser_Parse_abortsOnValuesOutsideRange(t *testing.T) {
vectors := []string{
"0-60 * * * * *",
"60 * * * * *",
"* 0-60 * * * *",
"* 60 * * * *",
"* * 0-24 * * *",
"* * 24 * * *",
"* * * 0-31 * *",
"* * * 0 * *",
"* * * 32 * *",
"* * * * 0-12 *",
"* * * * 13 *",
"* * * * * 0-7",
"* * * * * 7",
}
parser := defaultParser
for _, v := range vectors {
t.Run(v, func(t *testing.T) {
_, err := parser.Parse(v)
requireErrorIs(t, err, ErrValueOutsideRange)
})
}
}
func TestParser_Parse_abortsOnMalformedSeconds(t *testing.T) {
_, err := defaultParser.Parse("a * * * * *")
var e TimeUnitError
requireErrorAs(t, err, &e)
requireSameKind(t, e.kind, Seconds)
}
func TestParser_Parse_abortsOnMalformedMinutes(t *testing.T) {
_, err := defaultParser.Parse("* a * * * *")
var e TimeUnitError
requireErrorAs(t, err, &e)
requireSameKind(t, e.kind, Minutes)
}
func TestParser_Parse_abortsOnMalformedHours(t *testing.T) {
_, err := defaultParser.Parse("* * a * * *")
var e TimeUnitError
requireErrorAs(t, err, &e)
requireSameKind(t, e.kind, Hours)
}
func TestParser_Parse_abortsOnMalformedDays(t *testing.T) {
_, err := defaultParser.Parse("* * * a * *")
var e TimeUnitError
requireErrorAs(t, err, &e)
requireSameKind(t, e.kind, Days)
}
func TestParser_Parse_abortsOnMalformedMonths(t *testing.T) {
_, err := defaultParser.Parse("* * * * a *")
var e TimeUnitError
requireErrorAs(t, err, &e)
requireSameKind(t, e.kind, Months)
}
func TestParser_Parse_abortsOnMalformedWeekDays(t *testing.T) {
_, err := defaultParser.Parse("* * * * * a")
var e TimeUnitError
requireErrorAs(t, err, &e)
requireSameKind(t, e.kind, WeekDays)
}
func TestParser_Parse_abortsOnMalformedYears(t *testing.T) {
_, err := defaultParser.Parse("* * * * * * a")
var e TimeUnitError
requireErrorAs(t, err, &e)
requireSameKind(t, e.kind, Years)
}
func TestParser_Parse_abortsOnTooBigLastValue(t *testing.T) {
_, err := defaultParser.Parse("* * * L-32 * *")
requireErrorIs(t, err, ErrValueOutsideRange)
}
func TestParser_Parse_abortsOnTooSmallLastValue(t *testing.T) {
_, err := defaultParser.Parse("* * * L-0 * *")
requireErrorIs(t, err, ErrValueOutsideRange)
}
// --- Utilities
func requireErrorIs(t testing.TB, err, target error) {
t.Helper()
if !errors.Is(err, target) {
t.Fatalf("expected error: %#v, found %#v", target, err)
}
}
func requireErrorAs(t testing.TB, err error, target any) {
t.Helper()
if !errors.As(err, target) {
t.Fatalf("expected error of kind: %T, found %#v", target, err)
}
}
func requireSameKind(t testing.TB, a, b TimeUnitKind) {
t.Helper()
if a != b {
t.Fatalf("%s != %s", a, b)
}
}