whilst.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Tests the "whilst" function
  3. */
  4. var mod_util = require('util');
  5. var mod_tap = require('tap');
  6. var mod_vasync = require('..');
  7. mod_tap.test('basic whilst', function (test) {
  8. var n = 0;
  9. mod_vasync.whilst(
  10. function condition() {
  11. return (n < 5);
  12. },
  13. function body(cb) {
  14. n++;
  15. cb(null, n);
  16. },
  17. function done(err, arg) {
  18. test.ok(!err, 'error unset');
  19. test.equal(n, 5, 'n == 5');
  20. test.equal(n, arg, 'n == arg');
  21. test.end();
  22. });
  23. });
  24. mod_tap.test('whilst return object', function (test) {
  25. var n = 0;
  26. var w = mod_vasync.whilst(
  27. function condition() {
  28. return (n < 5);
  29. },
  30. function body(cb) {
  31. n++;
  32. test.equal(n, w.iterations, 'n == w.iterations: ' + n);
  33. cb(null, n, 'foo');
  34. },
  35. function done(err, arg1, arg2, arg3) {
  36. test.ok(!err, 'error unset');
  37. test.equal(w.iterations, 5, 'whilst had 5 iterations');
  38. test.equal(w.finished, true, 'whilst has finished');
  39. test.equal(arg1, n, 'whilst arg1 == n');
  40. test.equal(arg2, 'foo', 'whilst arg2 == "foo"');
  41. test.equal(arg3, undefined, 'whilst arg3 == undefined');
  42. test.end();
  43. });
  44. test.equal(typeof (w), 'object', 'whilst returns an object');
  45. test.equal(w.finished, false, 'whilst is not finished');
  46. test.equal(w.iterations, 0, 'whilst has not started yet');
  47. });
  48. mod_tap.test('whilst false condition', function (test) {
  49. mod_vasync.whilst(
  50. function condition() {
  51. return (false);
  52. },
  53. function body(cb) {
  54. cb();
  55. },
  56. function done(err, arg) {
  57. test.ok(!err, 'error is unset');
  58. test.ok(!arg, 'arg is unset');
  59. test.end();
  60. });
  61. });
  62. mod_tap.test('whilst error', function (test) {
  63. var n = 0;
  64. var w = mod_vasync.whilst(
  65. function condition() {
  66. return (true);
  67. },
  68. function body(cb) {
  69. n++;
  70. if (n > 5) {
  71. cb(new Error('n > 5'), 'bar');
  72. } else {
  73. cb(null, 'foo');
  74. }
  75. },
  76. function done(err, arg) {
  77. test.ok(err, 'error is set');
  78. test.equal(err.message, 'n > 5');
  79. test.equal(arg, 'bar');
  80. test.equal(w.finished, true, 'whilst is finished');
  81. /*
  82. * Iterations is bumped after the test condition is run and
  83. * before the iteration function is run. Because the condition
  84. * in this example is inside the iteration function (the test
  85. * condition always returns true), the iteration count will be
  86. * 1 higher than expected, since it will fail when (n > 5), or
  87. * when iterations is 6.
  88. */
  89. test.equal(w.iterations, 6, 'whilst had 6 iterations');
  90. test.end();
  91. });
  92. });