stringify.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. 'use strict';
  2. var test = require('tap').test;
  3. var CronParser = require('../lib/parser');
  4. test('stringify cron expression all stars no seconds', function (t) {
  5. try {
  6. var expected = '0 * * * * *';
  7. var interval = CronParser.parseExpression('* * * * *', {});
  8. var str = interval.stringify(true);
  9. t.equal(str, expected);
  10. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  11. t.equal(str, expected);
  12. } catch (err) {
  13. t.error(err, 'Parse read error');
  14. }
  15. t.end();
  16. });
  17. test('stringify cron expression all stars no seconds (discard seconds)', function (t) {
  18. try {
  19. var expected = '* * * * *';
  20. var interval = CronParser.parseExpression('* * * * *', {});
  21. var str = interval.stringify();
  22. t.equal(str, expected);
  23. str = CronParser.fieldsToExpression(interval.fields).stringify();
  24. t.equal(str, expected);
  25. } catch (err) {
  26. t.error(err, 'Parse read error');
  27. }
  28. t.end();
  29. });
  30. test('stringify cron expression all stars with seconds', function (t) {
  31. try {
  32. var expected = '* * * * * *';
  33. var interval = CronParser.parseExpression('* * * * * *', {});
  34. var str = interval.stringify(true);
  35. t.equal(str, expected);
  36. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  37. t.equal(str, expected);
  38. } catch (err) {
  39. t.error(err, 'Parse read error');
  40. }
  41. t.end();
  42. });
  43. test('stringify cron expression all stars with seconds (discard seconds)', function (t) {
  44. try {
  45. var expected = '* * * * *';
  46. var interval = CronParser.parseExpression('* * * * * *', {});
  47. var str = interval.stringify();
  48. t.equal(str, expected);
  49. str = CronParser.fieldsToExpression(interval.fields).stringify();
  50. t.equal(str, expected);
  51. } catch (err) {
  52. t.error(err, 'Parse read error');
  53. }
  54. t.end();
  55. });
  56. test('stringify cron expression', function (t) {
  57. try {
  58. var expected = '0 1,2,4-10,20-35/5,57 * * * *';
  59. var interval = CronParser.parseExpression('1,2,4-10,20-35/5,57 * * * *', {});
  60. var str = interval.stringify(true);
  61. t.equal(str, expected);
  62. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  63. t.equal(str, expected);
  64. } catch (err) {
  65. t.error(err, 'Parse read error');
  66. }
  67. t.end();
  68. });
  69. test('stringify cron expression (discard seconds)', function (t) {
  70. try {
  71. var expected = '1,2,4-10,20-35/5,57 * * * *';
  72. var interval = CronParser.parseExpression('1,2,4-10,20-35/5,57 * * * *', {});
  73. var str = interval.stringify();
  74. t.equal(str, expected);
  75. str = CronParser.fieldsToExpression(interval.fields).stringify();
  76. t.equal(str, expected);
  77. } catch (err) {
  78. t.error(err, 'Parse read error');
  79. }
  80. t.end();
  81. });
  82. test('stringify cron expression with star range step', function (t) {
  83. try {
  84. var expected = '0 */5 */2 * * *';
  85. var interval = CronParser.parseExpression('*/5 */2 */1 * *', {});
  86. var str = interval.stringify(true);
  87. t.equal(str, expected);
  88. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  89. t.equal(str, expected);
  90. } catch (err) {
  91. t.error(err, 'Parse read error');
  92. }
  93. t.end();
  94. });
  95. test('stringify cron expression with multiple values and retain original value', function (t) {
  96. try {
  97. var expected = '0 * * * * 1,3,5';
  98. var interval = CronParser.parseExpression('* * * * 1,3,5', {});
  99. var str = interval.stringify(true);
  100. t.equal(str, expected);
  101. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  102. t.equal(str, expected);
  103. } catch (err) {
  104. t.error(err, 'Parse read error');
  105. }
  106. t.end();
  107. });
  108. test('stringify cron expression with multiple values and convert value to range step', function (t) {
  109. try {
  110. var expected = '0 * * * * 0-6/2';
  111. var interval = CronParser.parseExpression('* * * * 0,2,4,6', {});
  112. var str = interval.stringify(true);
  113. t.equal(str, expected);
  114. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  115. t.equal(str, expected);
  116. } catch (err) {
  117. t.error(err, 'Parse read error');
  118. }
  119. t.end();
  120. });
  121. test('stringify cron expression with star range step (discard seconds)', function (t) {
  122. try {
  123. var expected = '*/5 */2 * * *';
  124. var interval = CronParser.parseExpression('*/5 */2 */1 * *', {});
  125. var str = interval.stringify();
  126. t.equal(str, expected);
  127. str = CronParser.fieldsToExpression(interval.fields).stringify();
  128. t.equal(str, expected);
  129. } catch (err) {
  130. t.error(err, 'Parse read error');
  131. }
  132. t.end();
  133. });
  134. test('stringify cron expression with semi range step', function (t) {
  135. try {
  136. var expected = '0 5/5 * * * *';
  137. var interval = CronParser.parseExpression('5/5 * * * *', {});
  138. var str = interval.stringify(true);
  139. t.equal(str, expected);
  140. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  141. t.equal(str, expected);
  142. } catch (err) {
  143. t.error(err, 'Parse read error');
  144. }
  145. t.end();
  146. });
  147. test('stringify cron expression with semi range step (discard seconds)', function (t) {
  148. try {
  149. var expected = '5/5 * * * *';
  150. var interval = CronParser.parseExpression('5/5 * * * *', {});
  151. var str = interval.stringify();
  152. t.equal(str, expected);
  153. str = CronParser.fieldsToExpression(interval.fields).stringify();
  154. t.equal(str, expected);
  155. } catch (err) {
  156. t.error(err, 'Parse read error');
  157. }
  158. t.end();
  159. });
  160. test('stringify cron expression with L', function (t) {
  161. try {
  162. var expected = '0 * * 1,4-10,L * *';
  163. var interval = CronParser.parseExpression('* * 1,4-10,L * *', {});
  164. var str = interval.stringify(true);
  165. t.equal(str, expected);
  166. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  167. t.equal(str, expected);
  168. } catch (err) {
  169. t.error(err, 'Parse read error');
  170. }
  171. t.end();
  172. });
  173. test('stringify cron expression with L (discard seconds)', function (t) {
  174. try {
  175. var expected = '* * 1,4-10,L * *';
  176. var interval = CronParser.parseExpression('* * 1,4-10,L * *', {});
  177. var str = interval.stringify();
  178. t.equal(str, expected);
  179. str = CronParser.fieldsToExpression(interval.fields).stringify();
  180. t.equal(str, expected);
  181. } catch (err) {
  182. t.error(err, 'Parse read error');
  183. }
  184. t.end();
  185. });
  186. test('stringify cron expression with weekday L', function (t) {
  187. try {
  188. var expected = '0 0 0 * * 1L';
  189. var interval = CronParser.parseExpression(expected, {});
  190. var str = interval.stringify(true);
  191. t.equal(str, expected);
  192. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  193. t.equal(str, expected);
  194. } catch (err) {
  195. t.error(err, 'Parse read error');
  196. }
  197. t.end();
  198. });
  199. test('stringify cron expression with multiple weekday, one of them with an L', function (t) {
  200. try {
  201. var expected = '0 0 0 * * 4,6L';
  202. var interval = CronParser.parseExpression(expected, {});
  203. var str = interval.stringify(true);
  204. t.equal(str, expected);
  205. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  206. t.equal(str, expected);
  207. } catch (err) {
  208. t.error(err, 'Parse read error');
  209. }
  210. t.end();
  211. });
  212. test('stringify cron expression with multiple weekday, two of them with an L', function (t) {
  213. try {
  214. var expected = '0 0 0 * * 1L,5L';
  215. var interval = CronParser.parseExpression(expected, {});
  216. var str = interval.stringify(true);
  217. t.equal(str, expected);
  218. str = CronParser.fieldsToExpression(interval.fields).stringify(true);
  219. t.equal(str, expected);
  220. } catch (err) {
  221. t.error(err, 'Parse read error');
  222. }
  223. t.end();
  224. });
  225. test('stringify cron expression with wildcard day of month and single month value', function (t) {
  226. try {
  227. var expected = '* * * 4 *';
  228. var interval = CronParser.parseExpression(expected, {});
  229. var str = interval.stringify();
  230. t.equal(str, expected);
  231. } catch (err) {
  232. t.error(err, 'Parse read error');
  233. }
  234. t.end();
  235. });
  236. test('stringify cron expression with wildcard day of month and month range', function (t) {
  237. try {
  238. var expected = '* * * 4-6 *';
  239. var interval = CronParser.parseExpression(expected, {});
  240. var str = interval.stringify();
  241. t.equal(str, expected);
  242. } catch (err) {
  243. t.error(err, 'Parse read error');
  244. }
  245. t.end();
  246. });
  247. test('stringify cron expression with day of month range and single month value', function (t) {
  248. try {
  249. var expected = '* * 1-25 4 *';
  250. var interval = CronParser.parseExpression(expected, {});
  251. var str = interval.stringify();
  252. t.equal(str, expected);
  253. } catch (err) {
  254. t.error(err, 'Parse read error');
  255. }
  256. t.end();
  257. });
  258. test('stringify from fields out of order', function (t) {
  259. try {
  260. var expected = '1-5 1 1 1 1 1';
  261. var str = CronParser.fieldsToExpression({
  262. second: [5,2,1,4,3],
  263. minute: [1],
  264. hour: [1],
  265. month: [1],
  266. dayOfMonth: [1],
  267. dayOfWeek: [1],
  268. }).stringify(true);
  269. t.equal(str, expected);
  270. } catch (err) {
  271. t.error(err, 'Parse read error');
  272. }
  273. t.end();
  274. });
  275. test('stringify from fields out of order (discard seconds)', function (t) {
  276. try {
  277. var expected = '1 1 1 1 1';
  278. var str = CronParser.fieldsToExpression({
  279. second: [5,2,1,4,3],
  280. minute: [1],
  281. hour: [1],
  282. month: [1],
  283. dayOfMonth: [1],
  284. dayOfWeek: [1],
  285. }).stringify();
  286. t.equal(str, expected);
  287. } catch (err) {
  288. t.error(err, 'Parse read error');
  289. }
  290. t.end();
  291. });
  292. test('stringify cron expression with extended day of week range (0,7)', function (t) {
  293. try {
  294. var expected = '* * * * *';
  295. var interval = CronParser.parseExpression('* * * * *');
  296. var str = CronParser.fieldsToExpression({
  297. second: interval.fields.second,
  298. minute: interval.fields.minute,
  299. hour: interval.fields.hour,
  300. month: interval.fields.month,
  301. dayOfMonth: interval.fields.dayOfMonth,
  302. dayOfWeek: [0, 1, 2, 3, 4, 5, 6],
  303. }).stringify();
  304. t.equal(str, expected);
  305. str = CronParser.fieldsToExpression({
  306. second: interval.fields.second,
  307. minute: interval.fields.minute,
  308. hour: interval.fields.hour,
  309. month: interval.fields.month,
  310. dayOfMonth: interval.fields.dayOfMonth,
  311. dayOfWeek: [0, 1, 2, 3, 4, 5, 6, 7],
  312. }).stringify();
  313. t.equal(str, expected);
  314. } catch (err) {
  315. t.error(err, 'Parse read error');
  316. }
  317. t.end();
  318. });
  319. test('validation error - missing seconds', function (t) {
  320. t.throws(function () {
  321. CronParser.fieldsToExpression({
  322. minute: [1],
  323. hour: [1],
  324. dayOfMonth: [1],
  325. month: [1],
  326. dayOfWeek: [1],
  327. });
  328. }, new Error('Validation error, Field second is missing'));
  329. t.end();
  330. });
  331. test('validation error - empty seconds', function (t) {
  332. t.throws(function () {
  333. CronParser.fieldsToExpression({
  334. second: [],
  335. minute: [1],
  336. hour: [1],
  337. dayOfMonth: [1],
  338. month: [1],
  339. dayOfWeek: [1],
  340. });
  341. }, new Error('Validation error, Field second contains no values'));
  342. t.end();
  343. });
  344. test('validation error - missing values - empty array', function (t) {
  345. t.throws(function () {
  346. CronParser.fieldsToExpression({
  347. second: [1],
  348. minute: [],
  349. hour: [1],
  350. dayOfMonth: [1],
  351. month: [1],
  352. dayOfWeek: [1],
  353. });
  354. }, new Error('Validation error, Field minute contains no values'));
  355. t.end();
  356. });
  357. test('validation error - missing values', function (t) {
  358. t.throws(function () {
  359. CronParser.fieldsToExpression({
  360. second: [1],
  361. hour: [1],
  362. dayOfMonth: [1],
  363. month: [1],
  364. dayOfWeek: [1],
  365. });
  366. }, new Error('Validation error, Field minute is missing'));
  367. t.end();
  368. });
  369. test('validation error - range error', function (t) {
  370. t.throws(function () {
  371. CronParser.fieldsToExpression({
  372. second: [-1, 1, 0],
  373. minute: [1],
  374. hour: [1],
  375. dayOfMonth: [1],
  376. month: [1],
  377. dayOfWeek: [1],
  378. });
  379. }, new Error('Constraint error, got value -1 expected range 0-59'));
  380. t.end();
  381. });
  382. test('validation error - bad chars error', function (t) {
  383. t.throws(function () {
  384. CronParser.fieldsToExpression({
  385. second: [0, 'R'],
  386. minute: [1],
  387. hour: [1],
  388. dayOfMonth: [1],
  389. month: [1],
  390. dayOfWeek: [1],
  391. });
  392. }, new Error('Constraint error, got value R expected range 0-59'));
  393. t.end();
  394. });
  395. test('validation error - duplicates', function (t) {
  396. t.throws(function () {
  397. CronParser.fieldsToExpression({
  398. second: [1, 1],
  399. minute: [1],
  400. hour: [1],
  401. dayOfMonth: [1],
  402. month: [1],
  403. dayOfWeek: [1],
  404. });
  405. }, new Error('Validation error, Field second contains duplicate values'));
  406. t.end();
  407. });