compat_tryEach.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. var async = require('../lib/vasync');
  2. /*
  3. * tryEach tests, transliterated from mocha to tap.
  4. *
  5. * They are nearly identical except for some details related to vasync. For
  6. * example, we don't support calling the callback more than once from any of
  7. * the given functions.
  8. */
  9. exports['tryEach no callback'] = function (test) {
  10. async.tryEach([]);
  11. test.done();
  12. };
  13. exports['tryEach empty'] = function (test) {
  14. async.tryEach([], function (err, results) {
  15. test.equals(err, null);
  16. test.same(results, undefined);
  17. test.done();
  18. });
  19. };
  20. exports['tryEach one task, multiple results'] = function (test) {
  21. var RESULTS = ['something', 'something2'];
  22. async.tryEach([
  23. function (callback) {
  24. callback(null, RESULTS[0], RESULTS[1]);
  25. }
  26. ], function (err, results) {
  27. test.equals(err, null);
  28. test.same(results, RESULTS);
  29. test.done();
  30. });
  31. };
  32. exports['tryEach one task'] = function (test) {
  33. var RESULT = 'something';
  34. async.tryEach([
  35. function (callback) {
  36. callback(null, RESULT);
  37. }
  38. ], function (err, results) {
  39. test.equals(err, null);
  40. test.same(results, RESULT);
  41. test.done();
  42. });
  43. };
  44. exports['tryEach two tasks, one failing'] = function (test) {
  45. var RESULT = 'something';
  46. async.tryEach([
  47. function (callback) {
  48. callback(new Error('Failure'), {});
  49. },
  50. function (callback) {
  51. callback(null, RESULT);
  52. }
  53. ], function (err, results) {
  54. test.equals(err, null);
  55. test.same(results, RESULT);
  56. test.done();
  57. });
  58. };
  59. exports['tryEach two tasks, both failing'] = function (test) {
  60. var ERROR_RESULT = new Error('Failure2');
  61. async.tryEach([
  62. function (callback) {
  63. callback(new Error('Should not stop here'));
  64. },
  65. function (callback) {
  66. callback(ERROR_RESULT);
  67. }
  68. ], function (err, results) {
  69. test.equals(err, ERROR_RESULT);
  70. test.same(results, undefined);
  71. test.done();
  72. });
  73. };
  74. exports['tryEach two tasks, non failing'] = function (test) {
  75. var RESULT = 'something';
  76. async.tryEach([
  77. function (callback) {
  78. callback(null, RESULT);
  79. },
  80. function () {
  81. test.fail('Should not been called');
  82. }
  83. ], function (err, results) {
  84. test.equals(err, null);
  85. test.same(results, RESULT);
  86. test.done();
  87. });
  88. };