parser_day_of_month.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. var test = require('tap').test;
  2. var CronParser = require('../lib/parser');
  3. test('parse cron with last day in a month', function(t) {
  4. var options = {
  5. currentDate: new Date(2014, 0, 1),
  6. endDate: new Date(2014, 10, 1)
  7. };
  8. try {
  9. var interval = CronParser.parseExpression('0 0 L * *', options);
  10. t.equal(interval.hasNext(), true);
  11. for (i = 0; i < 10; ++i) {
  12. var next = interval.next();
  13. t.ok(next, 'has a date');
  14. }
  15. } catch (err) {
  16. t.error(err, 'Parse read error');
  17. }
  18. t.end();
  19. });
  20. test('parse cron with last day in feb', function(t) {
  21. var options = {
  22. currentDate: new Date(2016, 0, 1),
  23. endDate: new Date(2016, 10, 1)
  24. };
  25. try {
  26. var interval = CronParser.parseExpression('0 0 6-20/2,L 2 *', options);
  27. t.equal(interval.hasNext(), true);
  28. var next = null;
  29. var items = 9;
  30. var i = 0;
  31. while(interval.hasNext()) {
  32. next = interval.next();
  33. i += 1;
  34. t.ok(next, 'has a date');
  35. }
  36. //leap year
  37. t.equal(next.getDate(), 29);
  38. t.equal(i, items);
  39. } catch (err) {
  40. t.error(err, 'Parse read error');
  41. }
  42. t.end();
  43. });
  44. test('parse cron with last day in feb', function(t) {
  45. var options = {
  46. currentDate: new Date(2014, 0, 1),
  47. endDate: new Date(2014, 10, 1)
  48. };
  49. try {
  50. var interval = CronParser.parseExpression('0 0 1,3,6-10,L 2 *', options);
  51. t.equal(interval.hasNext(), true);
  52. var next = null;
  53. while(interval.hasNext()) {
  54. next = interval.next();
  55. t.ok(next, 'has a date');
  56. }
  57. //common year
  58. t.equal(next.getDate(), 28);
  59. } catch (err) {
  60. t.error(err, 'Parse read error');
  61. }
  62. t.end();
  63. });
  64. test('parse cron with last weekday of the month', function(t) {
  65. var options = {
  66. currentDate: new Date(2021, 8, 1),
  67. endDate: new Date(2021, 11, 1)
  68. };
  69. var testCases = [
  70. { expression: '0 0 0 * * 1L', expectedDate: 27 },
  71. { expression: '0 0 0 * * 2L', expectedDate: 28 },
  72. { expression: '0 0 0 * * 3L', expectedDate: 29 },
  73. { expression: '0 0 0 * * 4L', expectedDate: 30 },
  74. { expression: '0 0 0 * * 5L', expectedDate: 24 },
  75. { expression: '0 0 0 * * 6L', expectedDate: 25 },
  76. { expression: '0 0 0 * * 0L', expectedDate: 26 },
  77. { expression: '0 0 0 * * 7L', expectedDate: 26 }
  78. ];
  79. testCases.forEach(function({ expression, expectedDate }) {
  80. t.test(expression, function(t) {
  81. try {
  82. var interval = CronParser.parseExpression(expression, options);
  83. t.equal(interval.hasNext(), true);
  84. var next = interval.next();
  85. t.equal(next.getDate(), expectedDate);
  86. } catch (err) {
  87. t.error(err, 'Parse read error');
  88. }
  89. t.end();
  90. });
  91. });
  92. t.end();
  93. });
  94. test('parses expression that runs on both last monday and friday of the month', function(t) {
  95. var options = {
  96. currentDate: new Date(2021, 8, 1),
  97. endDate: new Date(2021, 11, 1)
  98. };
  99. try {
  100. var interval = CronParser.parseExpression('0 0 0 * * 1L,5L', options);
  101. t.equal(interval.next().getDate(), 24);
  102. t.equal(interval.next().getDate(), 27);
  103. } catch (err) {
  104. t.error(err, 'Parse read error');
  105. }
  106. t.end();
  107. });
  108. test('parses expression that runs on both every monday and last friday of mont', function(t) {
  109. var options = {
  110. currentDate: new Date(2021, 8, 1),
  111. endDate: new Date(2021, 8, 30)
  112. };
  113. try {
  114. var interval = CronParser.parseExpression('0 0 0 * * 1,5L', options);
  115. var dates = [];
  116. while(true) {
  117. try {
  118. dates.push(interval.next().getDate());
  119. } catch (e) {
  120. if (e.message !== 'Out of the timespan range') {
  121. throw e;
  122. }
  123. break;
  124. }
  125. }
  126. t.same(dates, [6, 13, 20, 24, 27]);
  127. } catch (err) {
  128. t.error(err, 'Parse read error');
  129. }
  130. t.end();
  131. });
  132. test('fails to parse for invalid last weekday of month expression', function(t) {
  133. t.throws(function() {
  134. var interval = CronParser.parseExpression('0 0 0 * * L');
  135. interval.next();
  136. });
  137. t.end();
  138. });