Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joserafaelSH committed Oct 3, 2023
1 parent 782a516 commit a3909c5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 56 deletions.
91 changes: 44 additions & 47 deletions src/lib/isCron.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const validMonths = {
12: 'DEC',
};


const verifySecondsAndMinutes = (field) => {
if (field === '*') {
return true;
Expand Down Expand Up @@ -83,10 +82,11 @@ const verifyHours = (hours) => {
};

const verifyDaysOfMonth = (dayOfMonth) => {
if (dayOfMonth === '*') {
if (dayOfMonth === '*' || dayOfMonth === '?') {
return true;
}


if (dayOfMonth.includes(',')) {
const dayOfMonthList = dayOfMonth.split(',');
return dayOfMonthList.every(day => verifyDaysOfMonth(day));
Expand Down Expand Up @@ -115,39 +115,28 @@ const verifyDaysOfMonth = (dayOfMonth) => {
}

if (dayOfMonth.startsWith('L')) {
if (dayOfMonth.startsWith('LW')) {
if (dayOfMonth.length > 2) {
return false;
}
return true;
}
if (dayOfMonth.length > 1) {
return false;
}
return true;
}

if (dayOfMonth.startsWith('L-')) {
const daysBefore = dayOfMonth.split('L-')[1];
return parseInt(daysBefore, 10) < 32 && parseInt(daysBefore, 10) > 0;
}

if (dayOfMonth.startsWith('LW')) {
if (dayOfMonth.length > 2) {
if (dayOfMonth.includes('W')) {
const day = dayOfMonth.split('W')[0];
const dayNumber = parseInt(day, 10);
if (dayNumber > 31 || dayNumber < 1) {
return false;
}
return true;
}

if (dayOfMonth.startsWith('LW-')) {
const daysBefore = dayOfMonth.split('LW-')[1];
return parseInt(daysBefore, 10) < 32 && parseInt(daysBefore, 10) > 0;
}

if (dayOfMonth.startsWith('W')) {
const daysBefore = dayOfMonth.split('W')[1];
return parseInt(daysBefore, 10) < 32 && parseInt(daysBefore, 10) > 0;
}

if (dayOfMonth.startsWith('W-')) {
const daysBefore = dayOfMonth.split('W-')[1];
return parseInt(daysBefore, 10) < 32 && parseInt(daysBefore, 10) > 0;
}


return parseInt(dayOfMonth, 10) < 32 && parseInt(dayOfMonth, 10) > 0;
};
Expand Down Expand Up @@ -198,25 +187,8 @@ const verifyMonths = (month) => {
};


const validDaysOfWeek = {
1: 'SUN',
2: 'MON',
3: 'TUE',
4: 'WED',
5: 'THU',
6: 'FRI',
7: 'SAT',
};

const dayToNumber = (day) => {
const dayAbbreviation = day.toUpperCase();
const dayNumber = Object.keys(validDaysOfWeek)
.find(key => validDaysOfWeek[key] === dayAbbreviation);
return dayNumber ? parseInt(dayNumber, 10) : null;
};

const verifyDaysOfWeek = (day) => {
if (day === '*') {
if (day === '*' || day === '?') {
return true;
}

Expand All @@ -227,19 +199,45 @@ const verifyDaysOfWeek = (day) => {

if (day.includes('-')) {
const [start, end] = day.split('-');
const startDay = dayToNumber(start);
const endDay = dayToNumber(end);
const startDay = parseInt(start, 10);
const endDay = parseInt(end, 10);

if (!startDay || !endDay || startDay > 7 || endDay > 7 || startDay < 1 || endDay < 1) {
return false;
}
return startDay < endDay;
}

const numericDay = parseInt(day, 10);
return validDaysOfWeek[numericDay] !== undefined || dayToNumber(day) !== null;
};
if (day.includes('/')) {
if (day.startsWith('*/')) {
const interval = day.split('/')[1];
return parseInt(interval, 10) < 8;
}
const [start, interval] = day.split('/');

if (parseInt(start, 10) > 7 || parseInt(interval, 10) > 7 ||
parseInt(start, 10) < 1 || parseInt(interval, 10) < 1) {
return false;
}
}

if (day.startsWith('L')) {
if (day.length > 1) {
return false;
}
return true;
}

if (day.startsWith('#')) {
const [dayOfWeek, weekOfMonth] = day.split('#');
if (parseInt(dayOfWeek, 10) > 7 || parseInt(dayOfWeek, 10) < 1) {
return false;
}
return parseInt(weekOfMonth, 10) < 6 && parseInt(weekOfMonth, 10) > 0;
}

return parseInt(day, 10) < 8 && parseInt(day, 10) > 0;
};

export default function isCron(cron) {
assertString(cron);
Expand All @@ -248,7 +246,6 @@ export default function isCron(cron) {
cron = cron.replace(/[)]/g, '');
}


const cronParts = cron.split(' ');

if (cronParts.length !== 6) {
Expand Down
36 changes: 27 additions & 9 deletions test/validators/isCron.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,33 @@ import test from '../testFunctions';

describe('isCron', () => {
const correctsCronExpressions = [
'0 * * * * *',
'0 15 * * * *',
'*/30 * * * * *',
'0 9 * * 1 *',
'0 18 * * 5 *',
'0 2 1 * * *',
'0 */2 * * 1-5 *',
'*/15 8-12 * * * *',
'0 4 */3 * * *',
'* * * * * *',
'*/10 * * * * *',
'* */10 * * * *',
'* * */10 * * *',
'* * * */10 * *',
'* * * * */10 *',
'* * * * * */6',
'10,12 * * * * *',
'* 10,12 * * * *',
'* * 10,12 * * *',
'* * * 10,12 * *',
'* * * * 10,12 *',
'* * * * * 2,6',
'10-12 * * * * *',
'* 10-12 * * * *',
'* * 10-12 * * *',
'* * * 10-12 * *',
'* * * * 10-12 *',
'* * * * * 2-6',
'* * * ? * *',
'* * * * * ?',
'* * * L * *',
'* * * LW * *',
'* * * W * *',
'* * * 4W * *',
'* * * * * L',
'* * * * * 3#4',
];
const incorrectsCronExpressions = [
'60 * * * *',
Expand Down

0 comments on commit a3909c5

Please sign in to comment.