| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- 'use strict';
- var test = require('tap').test;
- var CronParser = require('../lib/parser');
- test('stringify cron expression all stars no seconds', function (t) {
- try {
- var expected = '0 * * * * *';
- var interval = CronParser.parseExpression('* * * * *', {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression all stars no seconds (discard seconds)', function (t) {
- try {
- var expected = '* * * * *';
- var interval = CronParser.parseExpression('* * * * *', {});
- var str = interval.stringify();
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression all stars with seconds', function (t) {
- try {
- var expected = '* * * * * *';
- var interval = CronParser.parseExpression('* * * * * *', {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression all stars with seconds (discard seconds)', function (t) {
- try {
- var expected = '* * * * *';
- var interval = CronParser.parseExpression('* * * * * *', {});
- var str = interval.stringify();
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression', function (t) {
- try {
- var expected = '0 1,2,4-10,20-35/5,57 * * * *';
- var interval = CronParser.parseExpression('1,2,4-10,20-35/5,57 * * * *', {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression (discard seconds)', function (t) {
- try {
- var expected = '1,2,4-10,20-35/5,57 * * * *';
- var interval = CronParser.parseExpression('1,2,4-10,20-35/5,57 * * * *', {});
- var str = interval.stringify();
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with star range step', function (t) {
- try {
- var expected = '0 */5 */2 * * *';
- var interval = CronParser.parseExpression('*/5 */2 */1 * *', {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with multiple values and retain original value', function (t) {
- try {
- var expected = '0 * * * * 1,3,5';
- var interval = CronParser.parseExpression('* * * * 1,3,5', {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with multiple values and convert value to range step', function (t) {
- try {
- var expected = '0 * * * * 0-6/2';
- var interval = CronParser.parseExpression('* * * * 0,2,4,6', {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with star range step (discard seconds)', function (t) {
- try {
- var expected = '*/5 */2 * * *';
- var interval = CronParser.parseExpression('*/5 */2 */1 * *', {});
- var str = interval.stringify();
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with semi range step', function (t) {
- try {
- var expected = '0 5/5 * * * *';
- var interval = CronParser.parseExpression('5/5 * * * *', {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with semi range step (discard seconds)', function (t) {
- try {
- var expected = '5/5 * * * *';
- var interval = CronParser.parseExpression('5/5 * * * *', {});
- var str = interval.stringify();
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with L', function (t) {
- try {
- var expected = '0 * * 1,4-10,L * *';
- var interval = CronParser.parseExpression('* * 1,4-10,L * *', {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with L (discard seconds)', function (t) {
- try {
- var expected = '* * 1,4-10,L * *';
- var interval = CronParser.parseExpression('* * 1,4-10,L * *', {});
- var str = interval.stringify();
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with weekday L', function (t) {
- try {
- var expected = '0 0 0 * * 1L';
- var interval = CronParser.parseExpression(expected, {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with multiple weekday, one of them with an L', function (t) {
- try {
- var expected = '0 0 0 * * 4,6L';
- var interval = CronParser.parseExpression(expected, {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with multiple weekday, two of them with an L', function (t) {
- try {
- var expected = '0 0 0 * * 1L,5L';
- var interval = CronParser.parseExpression(expected, {});
- var str = interval.stringify(true);
- t.equal(str, expected);
- str = CronParser.fieldsToExpression(interval.fields).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with wildcard day of month and single month value', function (t) {
- try {
- var expected = '* * * 4 *';
- var interval = CronParser.parseExpression(expected, {});
- var str = interval.stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with wildcard day of month and month range', function (t) {
- try {
- var expected = '* * * 4-6 *';
- var interval = CronParser.parseExpression(expected, {});
- var str = interval.stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with day of month range and single month value', function (t) {
- try {
- var expected = '* * 1-25 4 *';
- var interval = CronParser.parseExpression(expected, {});
- var str = interval.stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify from fields out of order', function (t) {
- try {
- var expected = '1-5 1 1 1 1 1';
- var str = CronParser.fieldsToExpression({
- second: [5,2,1,4,3],
- minute: [1],
- hour: [1],
- month: [1],
- dayOfMonth: [1],
- dayOfWeek: [1],
- }).stringify(true);
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify from fields out of order (discard seconds)', function (t) {
- try {
- var expected = '1 1 1 1 1';
- var str = CronParser.fieldsToExpression({
- second: [5,2,1,4,3],
- minute: [1],
- hour: [1],
- month: [1],
- dayOfMonth: [1],
- dayOfWeek: [1],
- }).stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('stringify cron expression with extended day of week range (0,7)', function (t) {
- try {
- var expected = '* * * * *';
- var interval = CronParser.parseExpression('* * * * *');
- var str = CronParser.fieldsToExpression({
- second: interval.fields.second,
- minute: interval.fields.minute,
- hour: interval.fields.hour,
- month: interval.fields.month,
- dayOfMonth: interval.fields.dayOfMonth,
- dayOfWeek: [0, 1, 2, 3, 4, 5, 6],
- }).stringify();
- t.equal(str, expected);
- str = CronParser.fieldsToExpression({
- second: interval.fields.second,
- minute: interval.fields.minute,
- hour: interval.fields.hour,
- month: interval.fields.month,
- dayOfMonth: interval.fields.dayOfMonth,
- dayOfWeek: [0, 1, 2, 3, 4, 5, 6, 7],
- }).stringify();
- t.equal(str, expected);
- } catch (err) {
- t.error(err, 'Parse read error');
- }
- t.end();
- });
- test('validation error - missing seconds', function (t) {
- t.throws(function () {
- CronParser.fieldsToExpression({
- minute: [1],
- hour: [1],
- dayOfMonth: [1],
- month: [1],
- dayOfWeek: [1],
- });
- }, new Error('Validation error, Field second is missing'));
- t.end();
- });
- test('validation error - empty seconds', function (t) {
- t.throws(function () {
- CronParser.fieldsToExpression({
- second: [],
- minute: [1],
- hour: [1],
- dayOfMonth: [1],
- month: [1],
- dayOfWeek: [1],
- });
- }, new Error('Validation error, Field second contains no values'));
- t.end();
- });
- test('validation error - missing values - empty array', function (t) {
- t.throws(function () {
- CronParser.fieldsToExpression({
- second: [1],
- minute: [],
- hour: [1],
- dayOfMonth: [1],
- month: [1],
- dayOfWeek: [1],
- });
- }, new Error('Validation error, Field minute contains no values'));
- t.end();
- });
- test('validation error - missing values', function (t) {
- t.throws(function () {
- CronParser.fieldsToExpression({
- second: [1],
- hour: [1],
- dayOfMonth: [1],
- month: [1],
- dayOfWeek: [1],
- });
- }, new Error('Validation error, Field minute is missing'));
- t.end();
- });
- test('validation error - range error', function (t) {
- t.throws(function () {
- CronParser.fieldsToExpression({
- second: [-1, 1, 0],
- minute: [1],
- hour: [1],
- dayOfMonth: [1],
- month: [1],
- dayOfWeek: [1],
- });
- }, new Error('Constraint error, got value -1 expected range 0-59'));
- t.end();
- });
- test('validation error - bad chars error', function (t) {
- t.throws(function () {
- CronParser.fieldsToExpression({
- second: [0, 'R'],
- minute: [1],
- hour: [1],
- dayOfMonth: [1],
- month: [1],
- dayOfWeek: [1],
- });
- }, new Error('Constraint error, got value R expected range 0-59'));
- t.end();
- });
- test('validation error - duplicates', function (t) {
- t.throws(function () {
- CronParser.fieldsToExpression({
- second: [1, 1],
- minute: [1],
- hour: [1],
- dayOfMonth: [1],
- month: [1],
- dayOfWeek: [1],
- });
- }, new Error('Validation error, Field second contains duplicate values'));
- t.end();
- });
|