mocha_Class.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. var assert = require("assert");
  2. var promise = Promise;//require('easy-promise');
  3. var utils = require('../src/utils');
  4. var EP = require("../");
  5. var Q = EP.extendClass(promise,{})
  6. var typeTest = require('./tutils.js');
  7. var succ = function(k,done,xc){
  8. return function(data){
  9. xc && clearTimeout(xc)
  10. if(data !== k) return done("返回参数错误");
  11. done();
  12. }
  13. }
  14. ,err = function(done,err,xc){
  15. return function(err){
  16. xc && clearTimeout(xc)
  17. done(err || "调用错误");
  18. }
  19. };
  20. describe('Promise 对像扩展测试', function(){
  21. describe('.defer() 测试',function(){
  22. it('同步',function(done){
  23. var defer = Q.defer();
  24. defer.resolve(1);
  25. defer.promise.then(function(v){
  26. assert.equal(1,v);
  27. done()
  28. })
  29. })
  30. it('异步',function(done){
  31. var defer = Q.defer();
  32. setTimeout(function(){
  33. defer.resolve(1);
  34. },0);
  35. defer.promise.then(function(v){
  36. assert.equal(1,v);
  37. done()
  38. })
  39. })
  40. typeTest(function(typename,promise,value){
  41. it('resolve(Type) Type 为 ' + typename , function(done){
  42. var defer = Q.defer();
  43. defer.resolve(promise);
  44. defer.promise.then(succ(value,done))
  45. })
  46. })
  47. })
  48. describe('resolve 转promise对像',function(){
  49. typeTest(function(typename,promise,value){
  50. it('resolve(Type) Type 为 ' + typename , function(done){
  51. Q.resolve(promise).then(succ(value,done))
  52. })
  53. })
  54. })
  55. describe('delay 延迟返回',function(){
  56. // typeTest(function(typename,promise,value){
  57. // it('delay(time,value) Type 为 ' + typename , function(done){
  58. // Q.delay(0,promise).then(succ(value,done))
  59. // })
  60. // })
  61. it('#delay 延迟测试', function(done){
  62. var data1 = new Date(),cp = 200;
  63. Q.delay(cp).then(function(){
  64. if((Math.abs(new Date - data1) - cp) / cp > 0.1) return done("延迟误差过大!")
  65. done();
  66. })
  67. });
  68. it('#delay 延迟测试 值测试', function(done){
  69. var data1 = new Date(),cp = 200;
  70. Q.delay(cp,3).then(function(v){
  71. assert.equal(v,3);
  72. if((Math.abs(new Date - data1) - cp) / cp > 0.1) return done("延迟误差过大!")
  73. done();
  74. })
  75. });
  76. })
  77. describe('all promise队列解决',function(){
  78. it('#all([])', function(done){
  79. Q.all([]).then(function(v){
  80. assert.ok(utils.isArray(v) && v.length == 0);
  81. done()
  82. })
  83. })
  84. typeTest(function(typename,promise,value){
  85. it('#all([Type]) Type 为 ' + typename , function(done){
  86. Q.all([promise]).then(function(v){
  87. assert.ok(utils.isArray(v));
  88. assert.equal(v[0],value);
  89. done();
  90. })
  91. })
  92. })
  93. })
  94. describe('allMap promise Map对像解决',function(){
  95. it('#allMap({})', function(done){
  96. Q.allMap({}).then(function(v){
  97. assert.deepEqual(v,{},err);
  98. done()
  99. },err(done,"错误调用"))
  100. })
  101. typeTest(function(typename,promise,value){
  102. it('#allMap({a:Type}) Type 为 ' + typename , function(done){
  103. Q.allMap({a:promise}).then(function(v){
  104. assert.deepEqual(v,{a:value});
  105. done();
  106. })
  107. })
  108. })
  109. });
  110. describe('some 最快解决',function(){
  111. function getParr(){
  112. var arr = [];
  113. arr.push(Q.delay(30,3))
  114. arr.push(Q.delay(20,2))
  115. arr.push(Q.delay(40,4))
  116. arr.push(Q.delay(10,1))
  117. arr.push(Q.delay(0,0))
  118. return arr;
  119. }
  120. it('无err', function(done){
  121. var arr = getParr();
  122. Q.some(arr,3).then(function(v){
  123. assert.ok(utils.isArray(v));
  124. assert.equal(v.join(","),"0,1,2","errr");
  125. done();
  126. }).then(null,err(done))
  127. })
  128. it('有err', function(done){
  129. var arr = getParr();
  130. arr.push(Q.reject("err"));
  131. arr.push(Q.delay(10,Q.reject("err")));
  132. Q.some(arr,3).then(function(v){
  133. assert.ok(utils.isArray(v));
  134. assert.equal(v.join(","),"0,1,2","errr");
  135. done();
  136. }).then(null,err(done))
  137. })
  138. it('无第二个参数', function(done){
  139. var arr = getParr();
  140. Q.some(arr).then(function(v){
  141. assert.ok(utils.isArray(v))
  142. assert.equal(v.join(","),"0,1,2,3,4");
  143. done();
  144. },err(done))
  145. })
  146. it('第二个参数大于数组长度', function(done){
  147. var arr = getParr();
  148. Q.some(arr).then(function(v){
  149. assert.ok(utils.isArray(v))
  150. assert.equal(v.join(","),"0,1,2,3,4");
  151. done();
  152. },err(done))
  153. })
  154. })
  155. describe('map 数组映射处理',function(){
  156. it('#同步', function(done){
  157. Q.map([0,1,2],function(v){
  158. return v
  159. }).then(function(v){
  160. assert.ok(utils.isArray(v));
  161. assert.equal(v.join(","),"0,1,2","errr");
  162. done();
  163. }).then(null,err(done))
  164. })
  165. it('#异步', function(done){
  166. Q.map([0,1,2],function(v){
  167. return Q.delay(Math.random()*100,v);
  168. }).then(function(v){
  169. assert.ok(utils.isArray(v));
  170. assert.equal(v.join(","),"0,1,2","errr");
  171. done();
  172. }).then(null,err(done))
  173. })
  174. it('#定义并发', function(done){
  175. var temp = [];
  176. Q.map([0,1,2],function(v){
  177. return Q.delay(Math.random()*100,v).then(function(v){
  178. temp.push(v)
  179. return v;
  180. })
  181. },{concurrency:1}).then(function(v){
  182. assert.ok(utils.isArray(v));
  183. assert.equal(v.join(","),"0,1,2");
  184. assert.equal(temp.join(","),"0,1,2","并发错误");
  185. done();
  186. }).then(null,err(done))
  187. })
  188. })
  189. describe('race ',function(){
  190. //it('')race
  191. })
  192. describe('CPS 转 Promise',function(){
  193. var FS = require("fs");
  194. it('#nfcall (promise风格化CPS)', function(done){
  195. Q.nfcall(FS.readFile, __dirname + "/1.txt", "utf-8").then(succ('1.txt',done),err(done,"错误调用"));
  196. });
  197. it('#nfapply (promise风格化CPS)', function(done){
  198. Q.nfapply(FS.readFile, [__dirname + "/1.txt", "utf-8"]).then(succ('1.txt',done),err(done,"错误调用"));
  199. });
  200. it('#denodeify 封装CPS函数', function(done){
  201. var readFile = Q.denodeify(FS.readFile);
  202. readFile(__dirname + "/1.txt", "utf-8").then(succ('1.txt',done),err(done,"错误调用"));
  203. });
  204. })
  205. })