CustomNodeAppender.spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import * as chai from 'chai';
  2. import * as ESTree from 'estree';
  3. import { TStatement } from '../../../src/types/TStatement';
  4. import { IStackTraceData } from '../../../src/interfaces/stack-trace-analyzer/IStackTraceData';
  5. import { CustomNodeAppender } from '../../../src/custom-nodes/CustomNodeAppender';
  6. import { NodeMocks } from '../../mocks/NodeMocks';
  7. import { NodeUtils } from '../../../src/NodeUtils';
  8. import { StackTraceAnalyzer } from '../../../src/stack-trace-analyzer/StackTraceAnalyzer';
  9. const assert: any = chai.assert;
  10. describe('CustomNodeAppender', () => {
  11. describe('appendNode (blockScopeStackTraceData: IStackTraceData[], blockScopeNode: TNodeWithBlockStatement, nodeBodyStatements: TStatement[], index: number = 0)', () => {
  12. let astTree: ESTree.Program,
  13. expectedAstTree: ESTree.Program,
  14. node: TStatement[],
  15. stackTraceData: IStackTraceData[];
  16. beforeEach(() => {
  17. node = NodeUtils.convertCodeToStructure(`
  18. var test = 1;
  19. `);
  20. });
  21. it('should append node into first and deepest function call in calls trace - variant #1', () => {
  22. astTree = NodeMocks.getProgramNode(
  23. <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
  24. function foo () {
  25. }
  26. function bar () {
  27. function inner1 () {
  28. }
  29. function inner2 () {
  30. var inner3 = function () {
  31. }
  32. inner3();
  33. }
  34. inner2();
  35. inner1();
  36. }
  37. function baz () {
  38. }
  39. baz();
  40. foo();
  41. bar();
  42. `)
  43. );
  44. expectedAstTree = NodeMocks.getProgramNode(
  45. <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
  46. function foo () {
  47. }
  48. function bar () {
  49. function inner1 () {
  50. }
  51. function inner2 () {
  52. var inner3 = function () {
  53. }
  54. inner3();
  55. }
  56. inner2();
  57. inner1();
  58. }
  59. function baz () {
  60. var test = 1;
  61. }
  62. baz();
  63. foo();
  64. bar();
  65. `)
  66. );
  67. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  68. CustomNodeAppender.appendNode(stackTraceData, astTree, node);
  69. assert.deepEqual(astTree, expectedAstTree);
  70. });
  71. it('should append node into first and deepest function call in calls trace - variant #2', () => {
  72. astTree = NodeMocks.getProgramNode(
  73. <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
  74. function foo () {
  75. }
  76. function bar () {
  77. function inner1 () {
  78. }
  79. function inner2 () {
  80. var inner3 = function () {
  81. }
  82. inner3();
  83. }
  84. inner2();
  85. inner1();
  86. }
  87. function baz () {
  88. }
  89. bar();
  90. baz();
  91. foo();
  92. `)
  93. );
  94. expectedAstTree = NodeMocks.getProgramNode(
  95. <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
  96. function foo () {
  97. }
  98. function bar () {
  99. function inner1 () {
  100. }
  101. function inner2 () {
  102. var inner3 = function () {
  103. var test = 1;
  104. }
  105. inner3();
  106. }
  107. inner2();
  108. inner1();
  109. }
  110. function baz () {
  111. }
  112. bar();
  113. baz();
  114. foo();
  115. `)
  116. );
  117. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  118. CustomNodeAppender.appendNode(stackTraceData, astTree, node);
  119. assert.deepEqual(astTree, expectedAstTree);
  120. });
  121. describe('append by specific index', () => {
  122. let astTree: ESTree.Program;
  123. beforeEach(() => {
  124. astTree = NodeMocks.getProgramNode(
  125. <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
  126. function foo () {
  127. }
  128. function bar () {
  129. function inner1 () {
  130. }
  131. function inner2 () {
  132. var inner3 = function () {
  133. }
  134. inner3();
  135. }
  136. inner2();
  137. inner1();
  138. }
  139. function baz () {
  140. }
  141. bar();
  142. baz();
  143. foo();
  144. `)
  145. );
  146. });
  147. it('should append node into deepest function call by specified index in calls trace - variant #1', () => {
  148. expectedAstTree = NodeMocks.getProgramNode(
  149. <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
  150. function foo () {
  151. var test = 1;
  152. }
  153. function bar () {
  154. function inner1 () {
  155. }
  156. function inner2 () {
  157. var inner3 = function () {
  158. }
  159. inner3();
  160. }
  161. inner2();
  162. inner1();
  163. }
  164. function baz () {
  165. }
  166. bar();
  167. baz();
  168. foo();
  169. `)
  170. );
  171. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  172. CustomNodeAppender.appendNode(stackTraceData, astTree, node, 2);
  173. assert.deepEqual(astTree, expectedAstTree);
  174. });
  175. it('should append node into deepest function call by specified index in calls trace - variant #2', () => {
  176. expectedAstTree = NodeMocks.getProgramNode(
  177. <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
  178. function foo () {
  179. }
  180. function bar () {
  181. function inner1 () {
  182. }
  183. function inner2 () {
  184. var inner3 = function () {
  185. }
  186. inner3();
  187. }
  188. inner2();
  189. inner1();
  190. }
  191. function baz () {
  192. var test = 1;
  193. }
  194. bar();
  195. baz();
  196. foo();
  197. `)
  198. );
  199. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  200. CustomNodeAppender.appendNode(stackTraceData, astTree, node, 1);
  201. assert.deepEqual(astTree, expectedAstTree);
  202. });
  203. it('should append node into deepest function call by specified index in calls trace - variant #3', () => {
  204. astTree = NodeMocks.getProgramNode(
  205. <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
  206. var start = new Date();
  207. var log = console.log;
  208. console.log = function () {};
  209. (function () {
  210. function bar () {
  211. function inner1 () {
  212. }
  213. function inner2 () {
  214. var inner3 = function () {
  215. }
  216. inner3();
  217. }
  218. inner2();
  219. inner1();
  220. }
  221. bar();
  222. })();
  223. console.log = log;
  224. console.log(new Date() - start);
  225. `)
  226. );
  227. expectedAstTree = NodeMocks.getProgramNode(
  228. <ESTree.Statement[]>NodeUtils.convertCodeToStructure(`
  229. var start = new Date();
  230. var log = console.log;
  231. console.log = function () {};
  232. (function () {
  233. function bar () {
  234. function inner1 () {
  235. }
  236. function inner2 () {
  237. var inner3 = function () {
  238. var test = 1;
  239. }
  240. inner3();
  241. }
  242. inner2();
  243. inner1();
  244. }
  245. bar();
  246. })();
  247. console.log = log;
  248. console.log(new Date() - start);
  249. `)
  250. );
  251. stackTraceData = new StackTraceAnalyzer(astTree.body).analyze();
  252. CustomNodeAppender.appendNode(
  253. stackTraceData,
  254. astTree,
  255. node,
  256. CustomNodeAppender.getRandomStackTraceIndex(stackTraceData.length)
  257. );
  258. assert.deepEqual(astTree, expectedAstTree);
  259. });
  260. });
  261. });
  262. describe('getRandomStackTraceIndex (stackTraceRootLength: number): number', () => {
  263. it('should returns random index between 0 and stack trace data root length', () => {
  264. let index: number;
  265. for (let i: number = 0; i < 100; i++) {
  266. index = CustomNodeAppender.getRandomStackTraceIndex(100);
  267. assert.isAtLeast(index, 0);
  268. assert.isAtMost(index, 100);
  269. }
  270. });
  271. });
  272. });