index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. var assert = require("assert");
  2. var q = require('../lib/promise');
  3. var maxtime = 50
  4. //同步函数
  5. function fun1(i,err){
  6. var deferred = q.defer();
  7. if(err){
  8. deferred.reject(err)
  9. }else{
  10. deferred.resolve(i)
  11. }
  12. return deferred.promise;
  13. }
  14. //异步函数
  15. function fun2(i,err){
  16. var deferred = q.defer();
  17. setTimeout(function(){
  18. if(err){
  19. deferred.reject(err)
  20. }else{
  21. deferred.resolve(i)
  22. }
  23. },maxtime)
  24. //},(Math.random() * maxtime)>>0)
  25. return deferred.promise;
  26. }
  27. var succ = function(k,done,xc){
  28. return function(data){
  29. xc && clearTimeout(xc)
  30. if(data !== k){
  31. return done("返回参数错误");
  32. }
  33. done();
  34. }
  35. }
  36. ,err = function(done,xc){
  37. return function(err){
  38. xc && clearTimeout(xc)
  39. done("回调错误");
  40. }
  41. }
  42. ,timeout_succ = function(done,c){
  43. c = c ? c : 1;
  44. return setTimeout(function(){
  45. done();
  46. },(maxtime+100)*c)
  47. }
  48. ,timeout_err = function(done,errmsg,c){
  49. c = c ? c : 1;
  50. return setTimeout(function(){
  51. done(errmsg);
  52. },(maxtime+100)*c)
  53. }
  54. //普通测试
  55. describe("原型扩展测试", function(){
  56. describe('#then/when', function(){
  57. it('.then(succ) 同步函数执行 成功', function(done){
  58. var xc = timeout_err(done,"未成功调用succ");
  59. fun1(1).then(succ(1,done,xc))
  60. })
  61. it('.then(succ) 同步函数执行 失败', function(done){
  62. var xc = timeout_succ(done);
  63. fun1(1,2).then(err(done,xc))
  64. })
  65. it('.then(succ) 异步函数执行 成功', function(done){
  66. var xc = timeout_err(done,"未成功调用succ");
  67. fun2(1).then(succ(1,done,xc))
  68. })
  69. it('.then(succ) 异步函数执行 失败', function(done){
  70. var xc = timeout_succ(done);
  71. fun2(1,2).then(err(done,xc))
  72. })
  73. it('.then(succ,err) 同步函数执行 成功', function(done){
  74. var xc = timeout_err(done,"未成功调用succ");
  75. fun1(1).then(succ(1,done,xc),err(done,xc))
  76. })
  77. it('.then(succ,err) 同步函数执行 失败', function(done){
  78. var xc = timeout_err(done,'未成功调用err');
  79. fun1(1,2).then(err(done,xc),succ(2,done,xc))
  80. })
  81. it('.then(succ,err) 异步函数执行 成功', function(done){
  82. var xc = timeout_err(done,"未成功调用succ");
  83. fun2(1).then(succ(1,done,xc),err(done,xc))
  84. })
  85. it('.then(succ,err) 异步函数执行 失败', function(done){
  86. var xc = timeout_err(done,'未成功调用err');
  87. fun2(1,2).then(err(done,xc),succ(2,done,xc))
  88. })
  89. })
  90. describe('#done', function(){
  91. it('.done(succ) 成功', function(done){
  92. var xc = timeout_err(done,"未成功调用succ");
  93. fun2(1).done(succ(1,done,xc))
  94. })
  95. it('.done(succ,err) 成功', function(done){
  96. var xc = timeout_err(done,'未成功调用err');
  97. fun2(1).done(succ(1,done,xc),err(done,xc))
  98. })
  99. it('.done(succ,err) 失败', function(done){
  100. var xc = timeout_succ(done);
  101. fun2(1,2).done(err(done,xc),succ(2,done,xc))
  102. })
  103. it('.done(null,err) 成功', function(done){
  104. var xc = timeout_succ(done);
  105. fun2(1).done(null,err(done,xc))
  106. })
  107. it('.done(null,err) 失败', function(done){
  108. var xc = timeout_err(done,'未成功走完流程');
  109. fun2(1,2).done(null,succ(2,done,xc))
  110. })
  111. })
  112. describe('#fail/catch', function(){
  113. it('.fail(err) 成功', function(done){
  114. var xc = timeout_succ(done);
  115. fun2(1).fail(err(done,xc))
  116. })
  117. it('.fail(err) 失败', function(done){
  118. var xc = timeout_err(done,"未成功调用err");
  119. fun2(1,2).fail(succ(2,done,xc))
  120. })
  121. })
  122. describe('#fin/finally', function(){
  123. it('.finally() 成功后执行', function(done){
  124. var xc = timeout_err(done,"执行错误");
  125. var fin = 0;
  126. fun2(1).finally(function(){
  127. return fin = 1;
  128. }).then(function(data){
  129. if(fin !== 1) throw "执行错误";
  130. succ(1,done,xc)(data)
  131. },err(done,xc))
  132. })
  133. it('.finally() 失败后执行', function(done){
  134. var xc = timeout_err(done,"未成功调用err");
  135. var fin = 0;
  136. fun2(1,2).finally(function(){
  137. return fin = 1;
  138. }).then(err(done,xc),succ(2,done,xc))
  139. })
  140. })
  141. });