CustomNodeAppender-spec.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import * as chai from 'chai';
  2. import * as ESTree from 'estree';
  3. import { CustomNodeAppender } from '../../../src/custom-nodes/CustomNodeAppender';
  4. import { NodeUtils } from '../../../src/NodeUtils';
  5. const assert: any = chai.assert;
  6. describe('CustomNodeAppender', () => {
  7. describe('appendNode (blockScopeNode: TNodeWithBlockStatement, node: ESTree.Node): void', () => {
  8. let astTree: ESTree.Program,
  9. expectedAstTree: ESTree.Program,
  10. node: ESTree.Node;
  11. beforeEach(() => {
  12. node = NodeUtils.convertCodeToStructure(`
  13. var test = 1;
  14. `);
  15. });
  16. it('should append node into first and deepest function call in calls trace - variant #1', () => {
  17. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  18. function foo () {
  19. }
  20. function bar () {
  21. function inner1 () {
  22. }
  23. function inner2 () {
  24. var inner3 = function () {
  25. }
  26. inner3();
  27. }
  28. inner2();
  29. inner1();
  30. }
  31. function baz () {
  32. }
  33. baz();
  34. foo();
  35. bar();
  36. `, false);
  37. expectedAstTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  38. function foo () {
  39. }
  40. function bar () {
  41. function inner1 () {
  42. }
  43. function inner2 () {
  44. var inner3 = function () {
  45. }
  46. inner3();
  47. }
  48. inner2();
  49. inner1();
  50. }
  51. function baz () {
  52. var test = 1;
  53. }
  54. baz();
  55. foo();
  56. bar();
  57. `, false);
  58. CustomNodeAppender.appendNode(astTree.body, node);
  59. NodeUtils.parentize(astTree);
  60. assert.deepEqual(astTree, expectedAstTree);
  61. });
  62. it('should append node into first and deepest function call in calls trace - variant #2', () => {
  63. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  64. function foo () {
  65. }
  66. function bar () {
  67. function inner1 () {
  68. }
  69. function inner2 () {
  70. var inner3 = function () {
  71. }
  72. inner3();
  73. }
  74. inner2();
  75. inner1();
  76. }
  77. function baz () {
  78. }
  79. bar();
  80. baz();
  81. foo();
  82. `, false);
  83. expectedAstTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  84. function foo () {
  85. }
  86. function bar () {
  87. function inner1 () {
  88. }
  89. function inner2 () {
  90. var inner3 = function () {
  91. var test = 1;
  92. }
  93. inner3();
  94. }
  95. inner2();
  96. inner1();
  97. }
  98. function baz () {
  99. }
  100. bar();
  101. baz();
  102. foo();
  103. `, false);
  104. CustomNodeAppender.appendNode(astTree.body, node);
  105. NodeUtils.parentize(astTree);
  106. assert.deepEqual(astTree, expectedAstTree);
  107. });
  108. describe('append by specific index', () => {
  109. let astTree: ESTree.Program;
  110. beforeEach(() => {
  111. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  112. function foo () {
  113. }
  114. function bar () {
  115. function inner1 () {
  116. }
  117. function inner2 () {
  118. var inner3 = function () {
  119. }
  120. inner3();
  121. }
  122. inner2();
  123. inner1();
  124. }
  125. function baz () {
  126. }
  127. bar();
  128. baz();
  129. foo();
  130. `, false);
  131. });
  132. it('should append node into deepest function call by specified index in calls trace - variant #1', () => {
  133. expectedAstTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  134. function foo () {
  135. var test = 1;
  136. }
  137. function bar () {
  138. function inner1 () {
  139. }
  140. function inner2 () {
  141. var inner3 = function () {
  142. }
  143. inner3();
  144. }
  145. inner2();
  146. inner1();
  147. }
  148. function baz () {
  149. }
  150. bar();
  151. baz();
  152. foo();
  153. `, false);
  154. CustomNodeAppender.appendNode(astTree.body, node, 2);
  155. NodeUtils.parentize(astTree);
  156. assert.deepEqual(astTree, expectedAstTree);
  157. });
  158. it('should append node into deepest function call by specified index in calls trace - variant #2', () => {
  159. expectedAstTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  160. function foo () {
  161. }
  162. function bar () {
  163. function inner1 () {
  164. }
  165. function inner2 () {
  166. var inner3 = function () {
  167. }
  168. inner3();
  169. }
  170. inner2();
  171. inner1();
  172. }
  173. function baz () {
  174. var test = 1;
  175. }
  176. bar();
  177. baz();
  178. foo();
  179. `, false);
  180. CustomNodeAppender.appendNode(astTree.body, node, 1);
  181. NodeUtils.parentize(astTree);
  182. assert.deepEqual(astTree, expectedAstTree);
  183. });
  184. });
  185. });
  186. describe('getIndexByThreshold (blockStatementBodyLength: number, threshold: number = 0.1): number', () => {
  187. it('should returns random index between 0 and index based on threshold value', () => {
  188. let index: number;
  189. for (let i = 0; i < 10; i++) {
  190. index = CustomNodeAppender.getIndexByThreshold(100, 0.1);
  191. assert.isAtLeast(index, 0);
  192. assert.isAtMost(index, 10);
  193. }
  194. for (let i = 0; i < 10; i++) {
  195. index = CustomNodeAppender.getIndexByThreshold(10, 0.5);
  196. assert.isAtLeast(index, 0);
  197. assert.isAtMost(index, 5);
  198. }
  199. for (let i = 0; i < 10; i++) {
  200. index = CustomNodeAppender.getIndexByThreshold(1, 1);
  201. assert.isAtLeast(index, 0);
  202. assert.isAtMost(index, 1);
  203. }
  204. });
  205. });
  206. });