CustomNodeAppender.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. it('should append node into deepest function call by specified index in calls trace - variant #3', () => {
  192. astTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  193. var start = new Date();
  194. var log = console.log;
  195. console.log = function () {};
  196. (function () {
  197. function bar () {
  198. function inner1 () {
  199. }
  200. function inner2 () {
  201. var inner3 = function () {
  202. }
  203. inner3();
  204. }
  205. inner2();
  206. inner1();
  207. }
  208. bar();
  209. })();
  210. console.log = log;
  211. console.log(new Date() - start);
  212. `, false);
  213. expectedAstTree = <ESTree.Program>NodeUtils.convertCodeToStructure(`
  214. var start = new Date();
  215. var log = console.log;
  216. console.log = function () {};
  217. (function () {
  218. function bar () {
  219. function inner1 () {
  220. }
  221. function inner2 () {
  222. var inner3 = function () {
  223. var test = 1;
  224. }
  225. inner3();
  226. }
  227. inner2();
  228. inner1();
  229. }
  230. bar();
  231. })();
  232. console.log = log;
  233. console.log(new Date() - start);
  234. `, false);
  235. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  236. CustomNodeAppender.appendNode(
  237. stackTraceData,
  238. astTree.body,
  239. node,
  240. CustomNodeAppender.getRandomStackTraceIndex(stackTraceData.length)
  241. );
  242. NodeUtils.parentize(astTree);
  243. assert.deepEqual(astTree, expectedAstTree);
  244. });
  245. });
  246. });
  247. describe('getRandomStackTraceIndex (stackTraceRootLength: number): number', () => {
  248. it('should returns random index between 0 and stack trace deata root length', () => {
  249. let index: number;
  250. for (let i: number = 0; i < 100; i++) {
  251. index = CustomNodeAppender.getRandomStackTraceIndex(100);
  252. assert.isAtLeast(index, 0);
  253. assert.isAtMost(index, 100);
  254. }
  255. });
  256. });
  257. });