tools2.test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. var tools = require("../tools2");
  2. var expect = require('expect.js');
  3. describe('Tools API', function() {
  4. // before(setup);
  5. // after(cleanup);
  6. describe('add_action do_action', function() {
  7. it('Can add an `action` function to be called with do_action', function(done) {
  8. var tool = tools();
  9. tool.add_action("test", function() {
  10. expect(true)
  11. done();
  12. }, 10);
  13. tool.do_action("test");
  14. });
  15. it('Can add an `action` function to be called with do_action with extra arguments', function(
  16. done) {
  17. var tool = tools();
  18. tool.add_action("test", function(myarg) {
  19. expect(myarg).to.equal("hello");
  20. done();
  21. }, 10);
  22. tool.do_action("test", "hello");
  23. });
  24. it('Can add an `action` function to be called with do_action sorted', function(done) {
  25. var tool = tools();
  26. var iii = 0;
  27. tool.add_action("test", function() {
  28. expect(iii).to.equal(2)
  29. iii++
  30. }, 30);
  31. tool.add_action("test", function() {
  32. expect(iii).to.equal(1)
  33. iii++
  34. done();
  35. }, 20);
  36. tool.add_action("test", function() {
  37. expect(iii).to.equal(3)
  38. iii++
  39. }, 50);
  40. tool.add_action("test", function() {
  41. expect(iii).to.equal(0)
  42. iii++
  43. }, 10);
  44. tool.do_action("test");
  45. });
  46. it('Can add an `action` function to be called with do_action with extra arguments sorted',
  47. function(done) {
  48. var tool = tools();
  49. var iii = 0;
  50. tool.add_action("test", function(myarg) {
  51. expect(iii).to.equal(2)
  52. expect(myarg).to.equal("hello");
  53. iii++
  54. }, 30);
  55. tool.add_action("test", function(myarg) {
  56. expect(iii).to.equal(1)
  57. expect(myarg).to.equal("hello");
  58. iii++
  59. done();
  60. }, 20);
  61. tool.add_action("test", function(myarg) {
  62. expect(iii).to.equal(3)
  63. expect(myarg).to.equal("hello");
  64. iii++
  65. }, 50);
  66. tool.add_action("test", function(myarg) {
  67. expect(iii).to.equal(0)
  68. expect(myarg).to.equal("hello");
  69. iii++
  70. }, 10);
  71. tool.do_action("test", "hello");
  72. });
  73. })
  74. describe('add_filters apply_filters', function() {
  75. it('Can add an `filter` function to be called with apply_filters ARRAY', function(done) {
  76. var tool = tools();
  77. tool.add_filters("test", function(a) {
  78. a.push("test");
  79. return a;
  80. }, 10, "test");
  81. var t = tool.apply_filters("test", []);
  82. expect(t).to.be.an('array');
  83. expect(t.length).to.equal(1);
  84. expect(t[0]).to.equal("test");
  85. done();
  86. });
  87. it('Can add an `filter` function to be called with apply_filters ARRAY sorted', function(done) {
  88. var tool = tools();
  89. tool.add_filters("test", function(a) {
  90. a.push("test");
  91. return a;
  92. }, 10);
  93. tool.add_filters("test", function(a) {
  94. a.push("test2");
  95. return a;
  96. }, 5);
  97. var t = tool.apply_filters("test", []);
  98. expect(t).to.be.an('array');
  99. expect(t.length).to.equal(2);
  100. expect(t[0]).to.equal("test2");
  101. done();
  102. });
  103. it('Can add an `filter` function to be called with apply_filters ARRAY sorted 2', function(done) {
  104. var tool = tools();
  105. tool.add_filters("test", function(a) {
  106. if(a.length>0){
  107. a[0]=a[0]+":1";
  108. }
  109. return a;
  110. }, 10);
  111. var t = tool.apply_filters("test", ["1","2","3"]);
  112. expect(t).to.be.an('array');
  113. expect(t.length).to.equal(3);
  114. expect(t[0]).to.equal("1:1");
  115. done();
  116. });
  117. it('Can add an `filter` function to be called with apply_filters ARRAY sorted', function(done) {
  118. var tool = tools();
  119. tool.add_filters("test", function(a) {
  120. a.push(function(b, cb) {
  121. console.log("LLL", arguments);
  122. cb(null, b())
  123. })
  124. return a;
  125. }, 10);
  126. tool.add_filters("test", function(a) {
  127. a.push(function(b, cb) {
  128. console.log("oooo", arguments);
  129. cb(null, b())
  130. })
  131. return a;
  132. }, 5);
  133. tool.add_filters("testaa", function(a) {
  134. a.push(function(b, cb) {
  135. console.log("aaa", arguments);
  136. cb(null, b())
  137. })
  138. return a;
  139. }, 5);
  140. var t = tool.apply_filters_map(["test","testaa"],function(ao){
  141. console.log("OOOOOAAA",ao)
  142. }, function(err, result) {
  143. console.log("JJJJJJJJJJJJ", arguments);
  144. });
  145. console.log(t);
  146. expect(t).to.be.an('array');
  147. expect(t.length).to.equal(2);
  148. expect(t[0]).to.equal("test2");
  149. done();
  150. });
  151. })
  152. })