CustomNodeAppender.spec.ts 9.4 KB

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