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.

120 lines
2.1 KiB

7 years ago
  1. /*global suite, test*/
  2. var assert = require("assert")
  3. , after = require("../")
  4. test("exists", function () {
  5. assert(typeof after === "function", "after is not a function")
  6. })
  7. test("after when called with 0 invokes", function (done) {
  8. after(0, done)
  9. });
  10. test("after 1", function (done) {
  11. var next = after(1, done)
  12. next()
  13. })
  14. test("after 5", function (done) {
  15. var next = after(5, done)
  16. , i = 5
  17. while (i--) {
  18. next()
  19. }
  20. })
  21. test("manipulate count", function (done) {
  22. var next = after(1, done)
  23. , i = 5
  24. next.count = i
  25. while (i--) {
  26. next()
  27. }
  28. })
  29. test("after terminates on error", function (done) {
  30. var next = after(2, function(err) {
  31. assert.equal(err.message, 'test');
  32. done();
  33. })
  34. next(new Error('test'))
  35. next(new Error('test2'))
  36. })
  37. test('gee', function(done) {
  38. done = after(2, done)
  39. function cb(err) {
  40. assert.equal(err.message, 1);
  41. done()
  42. }
  43. var next = after(3, cb, function(err) {
  44. assert.equal(err.message, 2)
  45. done()
  46. });
  47. next()
  48. next(new Error(1))
  49. next(new Error(2))
  50. })
  51. test('eee', function(done) {
  52. done = after(3, done)
  53. function cb(err) {
  54. assert.equal(err.message, 1);
  55. done()
  56. }
  57. var next = after(3, cb, function(err) {
  58. assert.equal(err.message, 2)
  59. done()
  60. });
  61. next(new Error(1))
  62. next(new Error(2))
  63. next(new Error(2))
  64. })
  65. test('gge', function(done) {
  66. function cb(err) {
  67. assert.equal(err.message, 1);
  68. done()
  69. }
  70. var next = after(3, cb, function(err) {
  71. // should not happen
  72. assert.ok(false);
  73. });
  74. next()
  75. next()
  76. next(new Error(1))
  77. })
  78. test('egg', function(done) {
  79. function cb(err) {
  80. assert.equal(err.message, 1);
  81. done()
  82. }
  83. var next = after(3, cb, function(err) {
  84. // should not happen
  85. assert.ok(false);
  86. });
  87. next(new Error(1))
  88. next()
  89. next()
  90. })
  91. test('throws on too many calls', function(done) {
  92. var next = after(1, done);
  93. next()
  94. assert.throws(next, /after called too many times/);
  95. });