test-crontime.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. var testCase = require('nodeunit').testCase,
  2. cron = require('../lib/cron');
  3. module.exports = testCase({
  4. 'test stars (* * * * * *)': function(assert) {
  5. assert.expect(1);
  6. assert.doesNotThrow(function() {
  7. new cron.CronTime('* * * * * *');
  8. });
  9. assert.done();
  10. },
  11. 'test digit (0 * * * * *)': function(assert) {
  12. assert.expect(1);
  13. assert.doesNotThrow(function() {
  14. new cron.CronTime('0 * * * * *');
  15. });
  16. assert.done();
  17. },
  18. 'test multi digits (08 * * * * *)': function(assert) {
  19. assert.expect(1);
  20. assert.doesNotThrow(function() {
  21. new cron.CronTime('08 * * * * *');
  22. });
  23. assert.done();
  24. },
  25. 'test all digits (08 8 8 8 8 5)': function(assert) {
  26. assert.expect(1);
  27. assert.doesNotThrow(function() {
  28. new cron.CronTime('08 * * * * *');
  29. });
  30. assert.done();
  31. },
  32. 'test too many digits (08 8 8 8 8 5)': function(assert) {
  33. assert.expect(1);
  34. assert.doesNotThrow(function() {
  35. new cron.CronTime('08 * * * * *');
  36. });
  37. assert.done();
  38. },
  39. 'test no second digit doesnt throw, i.e. standard cron format (8 8 8 8 5)': function(assert) {
  40. assert.expect(1);
  41. assert.doesNotThrow(function() {
  42. new cron.CronTime('* * * * *');
  43. });
  44. assert.done();
  45. },
  46. 'test no second digit defaults to 0, i.e. standard cron format (8 8 8 8 5)': function(assert) {
  47. assert.expect(1);
  48. var now = new Date();
  49. var standard = new cron.CronTime('8 8 8 8 5');
  50. var extended = new cron.CronTime('0 8 8 8 8 5');
  51. assert.ok(standard._getNextDateFrom(now).getTime() === extended._getNextDateFrom(now).getTime());
  52. assert.done();
  53. },
  54. 'test hyphen (0-10 * * * * *)': function(assert) {
  55. assert.expect(1);
  56. assert.doesNotThrow(function() {
  57. new cron.CronTime('0-10 * * * * *');
  58. });
  59. assert.done();
  60. },
  61. 'test multi hyphens (0-10 0-10 * * * *)': function(assert) {
  62. assert.expect(1);
  63. assert.doesNotThrow(function() {
  64. new cron.CronTime('0-10 0-10 * * * *');
  65. });
  66. assert.done();
  67. },
  68. 'test all hyphens (0-10 0-10 0-10 0-10 0-10 0-1)': function(assert) {
  69. assert.expect(1);
  70. assert.doesNotThrow(function() {
  71. new cron.CronTime('0-10 0-10 0-10 0-10 0-10 0-1');
  72. });
  73. assert.done();
  74. },
  75. 'test comma (0,10 * * * * *)': function(assert) {
  76. assert.expect(1);
  77. assert.doesNotThrow(function() {
  78. new cron.CronTime('0,10 * * * * *');
  79. });
  80. assert.done();
  81. },
  82. 'test multi commas (0,10 0,10 * * * *)': function(assert) {
  83. assert.expect(1);
  84. assert.doesNotThrow(function() {
  85. new cron.CronTime('0,10 0,10 * * * *');
  86. });
  87. assert.done();
  88. },
  89. 'test all commas (0,10 0,10 0,10 0,10 0,10 0,1)': function(assert) {
  90. assert.expect(1);
  91. assert.doesNotThrow(function() {
  92. new cron.CronTime('0,10 0,10 0,10 0,10 0,10 0,1');
  93. });
  94. assert.done();
  95. },
  96. 'test alias (* * * * jan *)': function(assert) {
  97. assert.expect(1);
  98. assert.doesNotThrow(function() {
  99. new cron.CronTime('* * * * jan *');
  100. });
  101. assert.done();
  102. },
  103. 'test multi aliases (* * * * jan,feb *)': function(assert) {
  104. assert.expect(1);
  105. assert.doesNotThrow(function() {
  106. new cron.CronTime('* * * * jan,feb *');
  107. });
  108. assert.done();
  109. },
  110. 'test all aliases (* * * * jan,feb mon,tue)': function(assert) {
  111. assert.expect(1);
  112. assert.doesNotThrow(function() {
  113. new cron.CronTime('* * * * jan,feb mon,tue');
  114. });
  115. assert.done();
  116. },
  117. 'test unknown alias (* * * * jar *)': function(assert) {
  118. assert.expect(1);
  119. assert.throws(function() {
  120. new cron.CronTime('* * * * jar *');
  121. });
  122. assert.done();
  123. },
  124. 'test unknown alias - short (* * * * j *)': function(assert) {
  125. assert.expect(1);
  126. assert.throws(function() {
  127. new cron.CronTime('* * * * j *');
  128. });
  129. assert.done();
  130. },
  131. 'test Date': function(assert) {
  132. assert.expect(1);
  133. var d = new Date();
  134. var ct = new cron.CronTime(d);
  135. assert.equals(ct.source.getTime(), d.getTime());
  136. assert.done();
  137. },
  138. 'test day roll-over': function(assert) {
  139. var numHours = 24;
  140. assert.expect(numHours * 2);
  141. var ct = new cron.CronTime('0 0 17 * * *');
  142. for (var hr = 0; hr < numHours; hr++) {
  143. var start = new Date(2012, 3, 16, hr, 30, 30);
  144. var next = ct._getNextDateFrom(start);
  145. assert.ok(next - start < 24*60*60*1000);
  146. assert.ok(next > start);
  147. }
  148. assert.done();
  149. }
  150. });