VisitedLexicalScopeNodesStackStorage.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import 'reflect-metadata';
  2. import { assert } from 'chai';
  3. import { ServiceIdentifiers } from '../../../../../src/container/ServiceIdentifiers';
  4. import { TInputOptions } from '../../../../../src/types/options/TInputOptions';
  5. import { TNodeWithLexicalScopeStatements } from '../../../../../src/types/node/TNodeWithLexicalScopeStatements';
  6. import { IInversifyContainerFacade } from '../../../../../src/interfaces/container/IInversifyContainerFacade';
  7. import { IVisitedLexicalScopeNodesStackStorage } from '../../../../../src/interfaces/storages/string-array-transformers/IVisitedLexicalScopeNodesStackStorage';
  8. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
  9. import { InversifyContainerFacade } from '../../../../../src/container/InversifyContainerFacade';
  10. import { NodeFactory } from '../../../../../src/node/NodeFactory';
  11. /**
  12. * @returns {IMapStorage<string, V>}
  13. */
  14. const getStorageInstance = (options: TInputOptions = {}): IVisitedLexicalScopeNodesStackStorage => {
  15. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  16. inversifyContainerFacade.load('', '', {
  17. ...NO_ADDITIONAL_NODES_PRESET,
  18. ...options
  19. });
  20. const storage: IVisitedLexicalScopeNodesStackStorage = inversifyContainerFacade.get(ServiceIdentifiers.IVisitedLexicalScopeNodesStackStorage);
  21. storage.initialize();
  22. return storage;
  23. };
  24. describe('VisitedLexicalScopeNodesStackStorage', () => {
  25. describe('getLastElement', () => {
  26. const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  27. NodeFactory.expressionStatementNode(
  28. NodeFactory.literalNode('first')
  29. )
  30. ]);
  31. const secondElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  32. NodeFactory.expressionStatementNode(
  33. NodeFactory.literalNode('second')
  34. )
  35. ]);
  36. const expectedLastElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  37. NodeFactory.expressionStatementNode(
  38. NodeFactory.literalNode('last')
  39. )
  40. ]);
  41. let lastElement: TNodeWithLexicalScopeStatements | undefined;
  42. before(() => {
  43. const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
  44. visitedLexicalScopeNodesStackStorage.push(firstElement);
  45. visitedLexicalScopeNodesStackStorage.push(secondElement);
  46. visitedLexicalScopeNodesStackStorage.push(expectedLastElement);
  47. lastElement = visitedLexicalScopeNodesStackStorage.getLastElement();
  48. });
  49. it('should return a last element from the stack', () => {
  50. assert.equal(lastElement, expectedLastElement);
  51. });
  52. });
  53. describe('getPenultimateElement', () => {
  54. describe('Variant #1: three array elements', () => {
  55. const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  56. NodeFactory.expressionStatementNode(
  57. NodeFactory.literalNode('first')
  58. )
  59. ]);
  60. const expectedSecondElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  61. NodeFactory.expressionStatementNode(
  62. NodeFactory.literalNode('second')
  63. )
  64. ]);
  65. const lastElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  66. NodeFactory.expressionStatementNode(
  67. NodeFactory.literalNode('last')
  68. )
  69. ]);
  70. let penultimateElement: TNodeWithLexicalScopeStatements | undefined;
  71. before(() => {
  72. const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
  73. visitedLexicalScopeNodesStackStorage.push(firstElement);
  74. visitedLexicalScopeNodesStackStorage.push(expectedSecondElement);
  75. visitedLexicalScopeNodesStackStorage.push(lastElement);
  76. penultimateElement = visitedLexicalScopeNodesStackStorage.getPenultimateElement();
  77. });
  78. it('should return a penultimate element from the stack', () => {
  79. assert.equal(penultimateElement, expectedSecondElement);
  80. });
  81. });
  82. describe('Variant #2: one array element', () => {
  83. const expectedPenultimateElement: undefined = undefined;
  84. const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  85. NodeFactory.expressionStatementNode(
  86. NodeFactory.literalNode('first')
  87. )
  88. ]);
  89. let penultimateElement: TNodeWithLexicalScopeStatements | undefined;
  90. before(() => {
  91. const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
  92. visitedLexicalScopeNodesStackStorage.push(firstElement);
  93. penultimateElement = visitedLexicalScopeNodesStackStorage.getPenultimateElement();
  94. });
  95. it('should return a penultimate element from the stack', () => {
  96. assert.equal(penultimateElement, expectedPenultimateElement);
  97. });
  98. });
  99. describe('Variant #3: empty array', () => {
  100. const expectedPenultimateElement: undefined = undefined;
  101. let penultimateElement: TNodeWithLexicalScopeStatements | undefined;
  102. before(() => {
  103. const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
  104. penultimateElement = visitedLexicalScopeNodesStackStorage.getPenultimateElement();
  105. });
  106. it('should return a penultimate element from the stack', () => {
  107. assert.equal(penultimateElement, expectedPenultimateElement);
  108. });
  109. });
  110. });
  111. describe('push', () => {
  112. const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  113. NodeFactory.expressionStatementNode(
  114. NodeFactory.literalNode('first')
  115. )
  116. ]);
  117. const secondElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  118. NodeFactory.expressionStatementNode(
  119. NodeFactory.literalNode('second')
  120. )
  121. ]);
  122. const expectedStorage: TNodeWithLexicalScopeStatements[] = [
  123. firstElement,
  124. secondElement
  125. ];
  126. let storage: TNodeWithLexicalScopeStatements[];
  127. before(() => {
  128. const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
  129. visitedLexicalScopeNodesStackStorage.push(firstElement);
  130. visitedLexicalScopeNodesStackStorage.push(secondElement);
  131. storage = visitedLexicalScopeNodesStackStorage.getStorage();
  132. });
  133. it('should push a new element into the storage', () => {
  134. assert.deepEqual(storage, expectedStorage);
  135. });
  136. });
  137. describe('pop', () => {
  138. describe('Variant #1: few elements', () => {
  139. const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  140. NodeFactory.expressionStatementNode(
  141. NodeFactory.literalNode('first')
  142. )
  143. ]);
  144. const secondElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  145. NodeFactory.expressionStatementNode(
  146. NodeFactory.literalNode('second')
  147. )
  148. ]);
  149. const expectedStorage: TNodeWithLexicalScopeStatements[] = [
  150. firstElement
  151. ];
  152. const expectedPoppedElement: TNodeWithLexicalScopeStatements = secondElement;
  153. let storage: TNodeWithLexicalScopeStatements[];
  154. let poppedElement: TNodeWithLexicalScopeStatements | undefined;
  155. before(() => {
  156. const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
  157. visitedLexicalScopeNodesStackStorage.push(firstElement);
  158. visitedLexicalScopeNodesStackStorage.push(secondElement);
  159. poppedElement = visitedLexicalScopeNodesStackStorage.pop();
  160. storage = visitedLexicalScopeNodesStackStorage.getStorage();
  161. });
  162. it('should pop a last element from the storage', () => {
  163. assert.deepEqual(storage, expectedStorage);
  164. });
  165. it('should return a popped element from the storage', () => {
  166. assert.equal(poppedElement, expectedPoppedElement);
  167. });
  168. });
  169. describe('Variant #2: single element', () => {
  170. const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  171. NodeFactory.expressionStatementNode(
  172. NodeFactory.literalNode('first')
  173. )
  174. ]);
  175. const expectedStorage: TNodeWithLexicalScopeStatements[] = [];
  176. const expectedPoppedElement: TNodeWithLexicalScopeStatements = firstElement;
  177. let storage: TNodeWithLexicalScopeStatements[];
  178. let poppedElement: TNodeWithLexicalScopeStatements | undefined;
  179. before(() => {
  180. const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
  181. visitedLexicalScopeNodesStackStorage.push(firstElement);
  182. poppedElement = visitedLexicalScopeNodesStackStorage.pop();
  183. storage = visitedLexicalScopeNodesStackStorage.getStorage();
  184. });
  185. it('should pop a last element from the storage', () => {
  186. assert.deepEqual(storage, expectedStorage);
  187. });
  188. it('should return a popped element from the storage', () => {
  189. assert.equal(poppedElement, expectedPoppedElement);
  190. });
  191. });
  192. });
  193. });