You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. const { assert } = require('chai');
  2. const TimeMatcher = require('../src/time-matcher');
  3. const moment = require('moment-timezone');
  4. describe('TimeMatcher', () => {
  5. describe('wildcard', () => {
  6. it('should accept wildcard for second', () => {
  7. let matcher = new TimeMatcher('* * * * * *');
  8. assert.isTrue(matcher.match(new Date()));
  9. });
  10. it('should accept wildcard for minute', () => {
  11. let matcher = new TimeMatcher('0 * * * * *');
  12. assert.isTrue(matcher.match(new Date(2018, 0, 1, 10, 20, 0)));
  13. assert.isFalse(matcher.match(new Date(2018, 0, 1, 10, 20, 1)));
  14. });
  15. it('should accept wildcard for hour', () => {
  16. let matcher = new TimeMatcher('0 0 * * * *');
  17. assert.isTrue(matcher.match(new Date(2018, 0, 1, 10, 0, 0)));
  18. assert.isFalse(matcher.match(new Date(2018, 0, 1, 10, 1, 0)));
  19. });
  20. it('should accept wildcard for day', () => {
  21. let matcher = new TimeMatcher('0 0 0 * * *');
  22. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 0)));
  23. assert.isFalse(matcher.match(new Date(2018, 0, 1, 1, 0, 0)));
  24. });
  25. it('should accept wildcard for month', () => {
  26. let matcher = new TimeMatcher('0 0 0 1 * *');
  27. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 0)));
  28. assert.isFalse(matcher.match(new Date(2018, 0, 2, 0, 0, 0)));
  29. });
  30. it('should accept wildcard for week day', () => {
  31. let matcher = new TimeMatcher('0 0 0 1 4 *');
  32. assert.isTrue(matcher.match(new Date(2018, 3, 1, 0, 0, 0)));
  33. assert.isFalse(matcher.match(new Date(2018, 3, 2, 0, 0, 0)));
  34. });
  35. });
  36. describe('single value', () => {
  37. it('should accept single value for second', () => {
  38. let matcher = new TimeMatcher('5 * * * * *');
  39. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 5)));
  40. assert.isFalse(matcher.match(new Date(2018, 0, 1, 0, 0, 6)));
  41. });
  42. it('should accept single value for minute', () => {
  43. let matcher = new TimeMatcher('0 5 * * * *');
  44. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 5, 0)));
  45. assert.isFalse(matcher.match(new Date(2018, 0, 1, 0, 6, 0)));
  46. });
  47. it('should accept single value for hour', () => {
  48. let matcher = new TimeMatcher('0 0 5 * * *');
  49. assert.isTrue(matcher.match(new Date(2018, 0, 1, 5, 0, 0)));
  50. assert.isFalse(matcher.match(new Date(2018, 0, 1, 6, 0, 0)));
  51. });
  52. it('should accept single value for day', () => {
  53. let matcher = new TimeMatcher('0 0 0 5 * *');
  54. assert.isTrue(matcher.match(new Date(2018, 0, 5, 0, 0, 0)));
  55. assert.isFalse(matcher.match(new Date(2018, 0, 6, 0, 0, 0)));
  56. });
  57. it('should accept single value for month', () => {
  58. let matcher = new TimeMatcher('0 0 0 1 5 *');
  59. assert.isTrue(matcher.match(new Date(2018, 4, 1, 0, 0, 0)));
  60. assert.isFalse(matcher.match(new Date(2018, 5, 1, 0, 0, 0)));
  61. });
  62. it('should accept single value for week day', () => {
  63. let matcher = new TimeMatcher('0 0 0 * * monday');
  64. assert.isTrue(matcher.match(new Date(2018, 4, 7, 0, 0, 0)));
  65. assert.isFalse(matcher.match(new Date(2018, 4, 8, 0, 0, 0)));
  66. });
  67. });
  68. describe('multiple values', () => {
  69. it('should accept multiple values for second', () => {
  70. let matcher = new TimeMatcher('5,6 * * * * *');
  71. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 5)));
  72. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 6)));
  73. assert.isFalse(matcher.match(new Date(2018, 0, 1, 0, 0, 7)));
  74. });
  75. it('should accept multiple values for minute', () => {
  76. let matcher = new TimeMatcher('0 5,6 * * * *');
  77. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 5, 0)));
  78. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 6, 0)));
  79. assert.isFalse(matcher.match(new Date(2018, 0, 1, 0, 7, 0)));
  80. });
  81. it('should accept multiple values for hour', () => {
  82. let matcher = new TimeMatcher('0 0 5,6 * * *');
  83. assert.isTrue(matcher.match(new Date(2018, 0, 1, 5, 0, 0)));
  84. assert.isTrue(matcher.match(new Date(2018, 0, 1, 6, 0, 0)));
  85. assert.isFalse(matcher.match(new Date(2018, 0, 1, 7, 0, 0)));
  86. });
  87. it('should accept multiple values for day', () => {
  88. let matcher = new TimeMatcher('0 0 0 5,6 * *');
  89. assert.isTrue(matcher.match(new Date(2018, 0, 5, 0, 0, 0)));
  90. assert.isTrue(matcher.match(new Date(2018, 0, 6, 0, 0, 0)));
  91. assert.isFalse(matcher.match(new Date(2018, 0, 7, 0, 0, 0)));
  92. });
  93. it('should accept multiple values for month', () => {
  94. let matcher = new TimeMatcher('0 0 0 1 may,june *');
  95. assert.isTrue(matcher.match(new Date(2018, 4, 1, 0, 0, 0)));
  96. assert.isTrue(matcher.match(new Date(2018, 5, 1, 0, 0, 0)));
  97. assert.isFalse(matcher.match(new Date(2018, 6, 1, 0, 0, 0)));
  98. });
  99. it('should accept multiple values for week day', () => {
  100. let matcher = new TimeMatcher('0 0 0 * * monday,tue');
  101. assert.isTrue(matcher.match(new Date(2018, 4, 7, 0, 0, 0)));
  102. assert.isTrue(matcher.match(new Date(2018, 4, 8, 0, 0, 0)));
  103. assert.isFalse(matcher.match(new Date(2018, 4, 9, 0, 0, 0)));
  104. });
  105. });
  106. describe('range', () => {
  107. it('should accept range for second', () => {
  108. let matcher = new TimeMatcher('5-7 * * * * *');
  109. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 5)));
  110. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 6)));
  111. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 7)));
  112. assert.isFalse(matcher.match(new Date(2018, 0, 1, 0, 0, 8)));
  113. });
  114. it('should accept range for minute', () => {
  115. let matcher = new TimeMatcher('0 5-7 * * * *');
  116. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 5, 0)));
  117. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 6, 0)));
  118. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 7, 0)));
  119. assert.isFalse(matcher.match(new Date(2018, 0, 1, 0, 8, 0)));
  120. });
  121. it('should accept range for hour', () => {
  122. let matcher = new TimeMatcher('0 0 5-7 * * *');
  123. assert.isTrue(matcher.match(new Date(2018, 0, 1, 5, 0, 0)));
  124. assert.isTrue(matcher.match(new Date(2018, 0, 1, 6, 0, 0)));
  125. assert.isTrue(matcher.match(new Date(2018, 0, 1, 7, 0, 0)));
  126. assert.isFalse(matcher.match(new Date(2018, 0, 1, 8, 0, 0)));
  127. });
  128. it('should accept range for day', () => {
  129. let matcher = new TimeMatcher('0 0 0 5-7 * *');
  130. assert.isTrue(matcher.match(new Date(2018, 0, 5, 0, 0, 0)));
  131. assert.isTrue(matcher.match(new Date(2018, 0, 6, 0, 0, 0)));
  132. assert.isTrue(matcher.match(new Date(2018, 0, 7, 0, 0, 0)));
  133. assert.isFalse(matcher.match(new Date(2018, 0, 8, 0, 0, 0)));
  134. });
  135. it('should accept range for month', () => {
  136. let matcher = new TimeMatcher('0 0 0 1 may-july *');
  137. assert.isTrue(matcher.match(new Date(2018, 4, 1, 0, 0, 0)));
  138. assert.isTrue(matcher.match(new Date(2018, 5, 1, 0, 0, 0)));
  139. assert.isTrue(matcher.match(new Date(2018, 6, 1, 0, 0, 0)));
  140. assert.isFalse(matcher.match(new Date(2018, 7, 1, 0, 0, 0)));
  141. });
  142. it('should accept range for week day', () => {
  143. let matcher = new TimeMatcher('0 0 0 * * monday-wed');
  144. assert.isTrue(matcher.match(new Date(2018, 4, 7, 0, 0, 0)));
  145. assert.isTrue(matcher.match(new Date(2018, 4, 8, 0, 0, 0)));
  146. assert.isTrue(matcher.match(new Date(2018, 4, 9, 0, 0, 0)));
  147. assert.isFalse(matcher.match(new Date(2018, 4, 10, 0, 0, 0)));
  148. });
  149. });
  150. describe('step values', () => {
  151. it('should accept step values for second', () => {
  152. let matcher = new TimeMatcher('*/2 * * * * *');
  153. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 2)));
  154. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 0, 6)));
  155. assert.isFalse(matcher.match(new Date(2018, 0, 1, 0, 0, 7)));
  156. });
  157. it('should accept step values for minute', () => {
  158. let matcher = new TimeMatcher('0 */2 * * * *');
  159. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 2, 0)));
  160. assert.isTrue(matcher.match(new Date(2018, 0, 1, 0, 6, 0)));
  161. assert.isFalse(matcher.match(new Date(2018, 0, 1, 0, 7, 0)));
  162. });
  163. it('should accept step values for hour', () => {
  164. let matcher = new TimeMatcher('0 0 */2 * * *');
  165. assert.isTrue(matcher.match(new Date(2018, 0, 1, 2, 0, 0)));
  166. assert.isTrue(matcher.match(new Date(2018, 0, 1, 6, 0, 0)));
  167. assert.isFalse(matcher.match(new Date(2018, 0, 1, 7, 0, 0)));
  168. });
  169. it('should accept step values for day', () => {
  170. let matcher = new TimeMatcher('0 0 0 */2 * *');
  171. assert.isTrue(matcher.match(new Date(2018, 0, 2, 0, 0, 0)));
  172. assert.isTrue(matcher.match(new Date(2018, 0, 6, 0, 0, 0)));
  173. assert.isFalse(matcher.match(new Date(2018, 0, 7, 0, 0, 0)));
  174. });
  175. it('should accept step values for month', () => {
  176. let matcher = new TimeMatcher('0 0 0 1 */2 *');
  177. assert.isTrue(matcher.match(new Date(2018, 1, 1, 0, 0, 0)));
  178. assert.isTrue(matcher.match(new Date(2018, 5, 1, 0, 0, 0)));
  179. assert.isFalse(matcher.match(new Date(2018, 6, 1, 0, 0, 0)));
  180. });
  181. it('should accept step values for week day', () => {
  182. let matcher = new TimeMatcher('0 0 0 * * */2');
  183. assert.isTrue(matcher.match(new Date(2018, 4, 6, 0, 0, 0)));
  184. assert.isTrue(matcher.match(new Date(2018, 4, 8, 0, 0, 0)));
  185. assert.isFalse(matcher.match(new Date(2018, 4, 9, 0, 0, 0)));
  186. });
  187. });
  188. describe('timezone', ()=>{
  189. it('should match with timezone America/Sao_Paulo', () => {
  190. let matcher = new TimeMatcher('0 0 0 * * *', 'America/Sao_Paulo');
  191. let utcTime = new Date('Thu Oct 11 2018 03:00:00Z');
  192. assert.isTrue(matcher.match(utcTime));
  193. });
  194. it('should match with timezone Europe/Rome', () => {
  195. let matcher = new TimeMatcher('0 0 0 * * *', 'Europe/Rome');
  196. let utcTime = new Date('Thu Oct 11 2018 22:00:00Z');
  197. assert.isTrue(matcher.match(utcTime));
  198. });
  199. it('should match with all available timezone of moment-timezone', () => {
  200. const allTimeZone = moment.tz.names();
  201. for (let zone in allTimeZone) {
  202. const tmp = moment();
  203. const expected = moment.tz(tmp,allTimeZone[zone]);
  204. const pattern = expected.second() + ' ' + expected.minute() + ' ' + expected.hour() + ' ' + expected.date() + ' ' + (expected.month()+1) + ' ' + expected.day();
  205. const matcher = new TimeMatcher(pattern, allTimeZone[zone]);
  206. const utcTime = new Date(tmp.year(), tmp.month(), tmp.date(), tmp.hour(), tmp.minute(), tmp.second(), tmp.millisecond());
  207. assert.isTrue(matcher.match(utcTime));
  208. }
  209. });
  210. });
  211. });