Skip to content

Commit f8abf7c

Browse files
committed
feat: add ast parse to cron unix syntax
1 parent deb68bd commit f8abf7c

File tree

2 files changed

+523
-0
lines changed

2 files changed

+523
-0
lines changed

lib/crontab-parse.spec.ts

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
import {
2+
assertArrayIncludes,
3+
assertEquals,
4+
assertInstanceOf,
5+
assertObjectMatch,
6+
} from "https://deno.land/std@0.204.0/assert/mod.ts";
7+
import { astCrontabParse } from "./crontab-parse.ts";
8+
9+
Deno.test("Parse AST", () => {
10+
assertEquals(
11+
astCrontabParse("* * * * *"),
12+
{
13+
type: "Statement",
14+
start: 0,
15+
end: 9,
16+
body: [
17+
{
18+
type: 'AnyValue',
19+
start: 0,
20+
end: 1,
21+
},
22+
{
23+
end: 3,
24+
start: 2,
25+
type: "AnyValue",
26+
},
27+
{
28+
end: 5,
29+
start: 4,
30+
type: "AnyValue",
31+
},
32+
{
33+
end: 7,
34+
start: 6,
35+
type: "AnyValue",
36+
},
37+
{
38+
end: 9,
39+
start: 8,
40+
type: "AnyValue",
41+
},
42+
]
43+
}
44+
)
45+
})
46+
47+
Deno.test("Parse AST", () => {
48+
assertEquals(
49+
astCrontabParse("1 * * * *").body.at(0),
50+
{
51+
type: 'NumberValue',
52+
start: 0,
53+
end: 1,
54+
value: 1
55+
}
56+
);
57+
assertObjectMatch(
58+
astCrontabParse("1,2,3,4 * * * *"),
59+
{
60+
body: [{
61+
values: [
62+
{ type: 'NumberValue', value: 1 },
63+
{ type: 'NumberValue', value: 2 },
64+
{ type: 'NumberValue', value: 3 },
65+
{ type: 'NumberValue', value: 4 },
66+
]
67+
}]
68+
}
69+
);
70+
assertObjectMatch(
71+
astCrontabParse("1,2,3,4 * * * *"),
72+
{
73+
body: [{
74+
values: [
75+
{ type: 'NumberValue', value: 1 },
76+
{ type: 'NumberValue', value: 2 },
77+
{ type: 'NumberValue', value: 3 },
78+
{ type: 'NumberValue', value: 4 },
79+
]
80+
}]
81+
}
82+
);
83+
assertObjectMatch(
84+
astCrontabParse("1,2,3,4 * * * *"),
85+
{
86+
body: [{
87+
type: 'ListValue',
88+
values: [
89+
{ type: 'NumberValue', value: 1 },
90+
{ type: 'NumberValue', value: 2 },
91+
{ type: 'NumberValue', value: 3 },
92+
{ type: 'NumberValue', value: 4 },
93+
]
94+
}]
95+
}
96+
);
97+
assertObjectMatch(
98+
astCrontabParse("1 * * * *"),
99+
{
100+
body: [{
101+
type: 'NumberValue',
102+
value: 1
103+
}]
104+
}
105+
);
106+
assertObjectMatch(
107+
astCrontabParse("1/2 * * * *"),
108+
{
109+
body: [{
110+
type: 'StepValue',
111+
left: { type: "NumberValue", value: 1 },
112+
right: { type: "NumberValue", value: 2 },
113+
}]
114+
}
115+
);
116+
assertObjectMatch(
117+
astCrontabParse("1-2 * * * *"),
118+
{
119+
body: [{
120+
type: 'RangeValue',
121+
left: {
122+
type: 'NumberValue',
123+
value: 1
124+
},
125+
right: {
126+
type: 'NumberValue',
127+
value: 2
128+
}
129+
}]
130+
}
131+
);
132+
assertObjectMatch(
133+
astCrontabParse("1-2/3 * * * *"),
134+
{
135+
body: [{
136+
type: 'StepValue',
137+
left: {
138+
type: 'RangeValue',
139+
left: {
140+
type: 'NumberValue',
141+
value: 1
142+
},
143+
right: {
144+
type: 'NumberValue',
145+
value: 2
146+
}
147+
},
148+
right: {
149+
type: 'NumberValue',
150+
value: 3
151+
}
152+
}]
153+
}
154+
);
155+
assertObjectMatch(
156+
astCrontabParse("4,1-2/3 * * * *"),
157+
{
158+
body: [{
159+
type: "ListValue",
160+
values: [
161+
{
162+
type: "NumberValue",
163+
value: 4
164+
},
165+
{
166+
type: 'StepValue',
167+
left: {
168+
type: 'RangeValue',
169+
left: {
170+
type: 'NumberValue',
171+
value: 1
172+
},
173+
right: {
174+
type: 'NumberValue',
175+
value: 2
176+
}
177+
},
178+
right: {
179+
type: 'NumberValue',
180+
value: 3
181+
}
182+
}
183+
]
184+
}]
185+
}
186+
);
187+
assertObjectMatch(
188+
astCrontabParse("* * * jan *"),
189+
{ body: [{}, {}, {}, { value: 1 }] },
190+
);
191+
})

0 commit comments

Comments
 (0)