NodeAppender.spec.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
  2. import * as ESTree from 'estree';
  3. import { assert } from 'chai';
  4. import { TStatement } from '../../../../src/types/node/TStatement';
  5. import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { IStackTraceAnalyzer } from '../../../../src/interfaces/stack-trace-analyzer/IStackTraceAnalyzer';
  7. import { IStackTraceData } from '../../../../src/interfaces/stack-trace-analyzer/IStackTraceData';
  8. import { readFileAsString } from '../../../helpers/readFileAsString';
  9. import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
  10. import { NodeAppender } from '../../../../src/node/NodeAppender';
  11. import { Nodes } from '../../../../src/node/Nodes';
  12. import { NodeUtils } from '../../../../src/node/NodeUtils';
  13. /**
  14. * @param fixturePath
  15. * @return {TStatement[]}
  16. */
  17. const convertCodeToStructure: (fixturePath: string) => TStatement[] = (fixturePath) => {
  18. return NodeUtils.convertCodeToStructure(
  19. readFileAsString(`${__dirname}${fixturePath}`)
  20. );
  21. };
  22. /**
  23. * @param fixturePath
  24. * @return {ESTree.Program}
  25. */
  26. const convertCodeToAst: (fixturePath: string) => ESTree.Program = (fixturePath) => {
  27. return Nodes.getProgramNode(convertCodeToStructure(fixturePath));
  28. };
  29. describe('NodeAppender', () => {
  30. describe('appendNode (blockScopeNode: TNodeWithBlockStatement[], nodeBodyStatements: TStatement[]): void', () => {
  31. let astTree: ESTree.Program,
  32. expectedAstTree: ESTree.Program,
  33. node: TStatement[];
  34. before(() => {
  35. node = convertCodeToStructure('/fixtures/simple-input.js');
  36. astTree = convertCodeToAst('/fixtures/append-node.js');
  37. expectedAstTree = convertCodeToAst('/fixtures/append-node-expected.js');
  38. astTree = NodeUtils.parentize(astTree);
  39. expectedAstTree = NodeUtils.parentize(expectedAstTree);
  40. NodeAppender.appendNode(astTree, node);
  41. });
  42. it('should append given node to a `BlockStatement` node body', () => {
  43. assert.deepEqual(astTree, expectedAstTree);
  44. });
  45. });
  46. describe('appendNodeToOptimalBlockScope (blockScopeStackTraceData: IStackTraceData[], blockScopeNode: TNodeWithBlockStatement, nodeBodyStatements: TStatement[], index: number = 0): void', () => {
  47. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade({});
  48. const stackTraceAnalyzer: IStackTraceAnalyzer = inversifyContainerFacade
  49. .get<IStackTraceAnalyzer>(ServiceIdentifiers.IStackTraceAnalyzer);
  50. let astTree: ESTree.Program,
  51. expectedAstTree: ESTree.Program,
  52. node: TStatement[],
  53. stackTraceData: IStackTraceData[];
  54. beforeEach(() => {
  55. node = convertCodeToStructure('/fixtures/simple-input.js');
  56. });
  57. describe('variant #1: nested function calls', () => {
  58. beforeEach(() => {
  59. astTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/variant-1.js');
  60. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/variant-1-expected.js');
  61. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  62. NodeAppender.appendNodeToOptimalBlockScope(stackTraceData, astTree, node);
  63. });
  64. it('should append node into first and deepest function call in nested function calls', () => {
  65. assert.deepEqual(astTree, expectedAstTree);
  66. });
  67. });
  68. describe('variant #2: nested function calls', () => {
  69. beforeEach(() => {
  70. astTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/variant-2.js');
  71. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/variant-2-expected.js');
  72. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  73. NodeAppender.appendNodeToOptimalBlockScope(stackTraceData, astTree, node);
  74. });
  75. it('should append node into first and deepest function call in nested function calls', () => {
  76. assert.deepEqual(astTree, expectedAstTree);
  77. });
  78. });
  79. describe('append by specific index', () => {
  80. let astTree: ESTree.Program;
  81. beforeEach(() => {
  82. astTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index.js');
  83. });
  84. describe('variant #1: append by specific index in nested function calls', () => {
  85. beforeEach(() => {
  86. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index-variant-1-expected.js');
  87. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  88. NodeAppender.appendNodeToOptimalBlockScope(stackTraceData, astTree, node, 2);
  89. });
  90. it('should append node into deepest function call by specified index in nested function calls', () => {
  91. assert.deepEqual(astTree, expectedAstTree);
  92. });
  93. });
  94. describe('variant #2: append by specific index in nested function calls', () => {
  95. beforeEach(() => {
  96. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index-variant-2-expected.js');
  97. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  98. NodeAppender.appendNodeToOptimalBlockScope(stackTraceData, astTree, node, 1);
  99. });
  100. it('should append node into deepest function call by specified index in nested function calls', () => {
  101. assert.deepEqual(astTree, expectedAstTree);
  102. });
  103. });
  104. describe('variant #3: append by specific index in nested function calls', () => {
  105. beforeEach(() => {
  106. astTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index-variant-3.js');
  107. expectedAstTree = convertCodeToAst('/fixtures/append-node-to-optimal-block-scope/by-index-variant-3-expected.js');
  108. stackTraceData = stackTraceAnalyzer.analyze(astTree.body);
  109. NodeAppender.appendNodeToOptimalBlockScope(
  110. stackTraceData,
  111. astTree,
  112. node,
  113. NodeAppender.getRandomStackTraceIndex(stackTraceData.length)
  114. );
  115. });
  116. it('should append node into deepest function call by specified index in nested function calls', () => {
  117. assert.deepEqual(astTree, expectedAstTree);
  118. });
  119. });
  120. });
  121. });
  122. describe('getRandomStackTraceIndex (stackTraceRootLength: number): number', () => {
  123. it('should return random index between 0 and stack trace data root length', () => {
  124. let index: number;
  125. for (let i: number = 0; i < 100; i++) {
  126. index = NodeAppender.getRandomStackTraceIndex(100);
  127. assert.isAtLeast(index, 0);
  128. assert.isAtMost(index, 100);
  129. }
  130. });
  131. });
  132. describe('insertNodeAtIndex (blockScopeNode: TNodeWithBlockStatement[], nodeBodyStatements: TStatement[], index: number): void', () => {
  133. let astTree: ESTree.Program,
  134. expectedAstTree: ESTree.Program,
  135. node: TStatement[];
  136. before(() => {
  137. node = convertCodeToStructure('/fixtures/simple-input.js');
  138. astTree = convertCodeToAst('/fixtures/insert-node-at-index.js');
  139. expectedAstTree = convertCodeToAst('/fixtures/insert-node-at-index-expected.js');
  140. astTree = NodeUtils.parentize(astTree);
  141. expectedAstTree = NodeUtils.parentize(expectedAstTree);
  142. NodeAppender.insertNodeAtIndex(astTree, node, 2);
  143. });
  144. it('should insert given node in `BlockStatement` node body at index', () => {
  145. assert.deepEqual(astTree, expectedAstTree);
  146. });
  147. });
  148. describe('prependNode (blockScopeNode: TNodeWithBlockStatement[], nodeBodyStatements: TStatement[]): void', () => {
  149. let astTree: ESTree.Program,
  150. expectedAstTree: ESTree.Program,
  151. node: TStatement[];
  152. before(() => {
  153. node = convertCodeToStructure('/fixtures/simple-input.js');
  154. astTree = convertCodeToAst('/fixtures/prepend-node.js');
  155. expectedAstTree = convertCodeToAst('/fixtures/prepend-node-expected.js');
  156. astTree = NodeUtils.parentize(astTree);
  157. expectedAstTree = NodeUtils.parentize(expectedAstTree);
  158. NodeAppender.prependNode(astTree, node);
  159. });
  160. it('should prepend given node to a `BlockStatement` node body', () => {
  161. assert.deepEqual(astTree, expectedAstTree);
  162. });
  163. });
  164. });