Add test reasons

This commit is contained in:
Jacob Wallraff
2023-01-10 13:08:29 -08:00
parent 78e2e32518
commit e6a7271ba5
@@ -2,59 +2,57 @@ import { isValidCron } from "./cron"
describe("isValidCron", () => { describe("isValidCron", () => {
const valid = [ const valid = [
"0 0 * * *", ["0 0 * * *", "every day at midnight"],
"0 000 001 * *", ["0 000 001 * *", "accepts leading zeros"],
"15 * * * *", ["15 * * * *", "accepts numbers in range"],
"2,10 4,5 * * *", ["2,10 4,5 * * *", "accepts comma separated values"],
"30 4-6 * * *", ["30 4-6 * * *", "accepts range"],
"20/15 * * * *", ["0 4-4 * * *", "accepts range with two equal values"],
"30 5,17 * * *", ["20/15 * * * *", "accepts step with numerical values"],
"30 5 * * 1,3", ["30 5,17 * * *", "accepts numbers and ranges"],
"30 5 * * 2,4", ["28 */4 * * *", "accepts step with * and numerical value"],
"28 */4 * * *", ["28 5,*/4 * * *", "accepts comma separated value with step"],
"28 5,*/4 * * *", ["28 5,*/4,6-8 * * *", "accepts comma separated value with step and range"],
"28 5,*/4,6 * * *", ["0 0 * * SUN", "accepts day of week short name"],
"28 5,*/4,6-8 * * *", ["0 0 * * SUN-TUE", "accepts day of week short name range"],
"0 0 * * SUN", ["0 0 * * SUN-2", "accepts day of week range combined with number"],
"0 0 * * SUN-TUE", ["0 2-4/5 * * *", "accepts range with step"],
"0 0 * * SUN-2", ["0 0 * * *", "accepts multiple spaces"],
"0 2-4/5 * * *",
"0 0 * * *",
] ]
for (const cron of valid) { for (const [cron, reason] of valid) {
it(`${cron} should be valid`, () => { it(`${cron} should be valid: ${reason}`, () => {
expect(isValidCron(cron)).toBe(true) expect(isValidCron(cron)).toBe(true)
}) })
} }
const invalid = [ const invalid = [
"0 0 * *", ["0 0 * *", "too few parts"],
"0 0 * * * * *", ["0 0 * * * * *", "too many parts"],
"0 -1 * * *", ["0 -1 * * *", "should not accept negative numbers"],
"0 1- * * *", ["0 1- * * *", "should not accept trailing -"],
"0 /1 * * *", ["0 /1 * * *", "should not accept leading / (empty value)"],
"0 1/ * * *", ["0 1/ * * *", "should not accept trailing / (empty value)"],
"0 ,1 * * *", ["0 ,1 * * *", "should not accept leading , (empty value)"],
"0 1, * * *", ["0 1, * * *", "should not accept trailing , (empty value)"],
"0 5--5 * * *", ["0 5--5 * * *", "should not accept multiple -"],
"0 *//5 * * *", ["0 *//5 * * *", "should not accept multiple /"],
"0 ,, * * *", ["0 ,, * * *", "should not accept multiple ,"],
"0 , , , ,", [", , , , ,", "comma is not a valid part"],
"0 ** * * *", ["0 ** * * *", "should not accept multiple *"],
"0 0 * * BUN", ["0 0 * * BUN", "invalid short name"],
"0 0 * SUN JAN", ["0 0 * SUN JAN", "short name in incorrect position"],
"0 0 * * FRI-TUE", ["0 0 * * FRI-TUE", "should not accept short name range with start > end"],
"0 12-4 * * *", ["0 12-4 * * *", "should not accept nuerical range with start > end"],
"0 */0 * * *", ["0 */0 * * *", "step size may not be 0"],
"0 2/4-5 * * *", ["0 2/4-5 * * *", "step size may not be a range"],
"0 2-4-6 * * *", ["0 2-4-6 * * *", "range may not contain multiple -"],
"0 2/4/6 * * *", ["0 2/4/6 * * *", "step size may not contain multiple /"],
"0 * * */FEB */TUE", ["0 * * */FEB */TUE", "step size may not be a short name"],
] ]
for (const cron of invalid) { for (const [cron, reason] of invalid) {
it(`${cron} should be invalid`, () => { it(`${cron} should be invalid: ${reason}`, () => {
expect(isValidCron(cron)).toBe(false) expect(isValidCron(cron)).toBe(false)
}) })
} }