VisitedLexicalScopeNodesStackStorage.spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 { TNodeWithLexicalScopeAndStatements } from '../../../../../src/types/node/TNodeWithLexicalScopeAndStatements';
  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: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
  27. 'first',
  28. [],
  29. NodeFactory.blockStatementNode([])
  30. );
  31. const secondElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
  32. 'second',
  33. [],
  34. NodeFactory.blockStatementNode([])
  35. );
  36. const expectedLastElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
  37. 'last',
  38. [],
  39. NodeFactory.blockStatementNode([])
  40. );
  41. let lastElement: TNodeWithLexicalScopeAndStatements | 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('push', () => {
  54. const firstElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
  55. 'first',
  56. [],
  57. NodeFactory.blockStatementNode([])
  58. );
  59. const secondElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
  60. 'second',
  61. [],
  62. NodeFactory.blockStatementNode([])
  63. );
  64. const expectedStorage: TNodeWithLexicalScopeAndStatements[] = [
  65. firstElement,
  66. secondElement
  67. ];
  68. let storage: TNodeWithLexicalScopeAndStatements[];
  69. before(() => {
  70. const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
  71. visitedLexicalScopeNodesStackStorage.push(firstElement);
  72. visitedLexicalScopeNodesStackStorage.push(secondElement);
  73. storage = visitedLexicalScopeNodesStackStorage.getStorage();
  74. });
  75. it('should push a new element into the storage', () => {
  76. assert.deepEqual(storage, expectedStorage);
  77. });
  78. });
  79. describe('pop', () => {
  80. const firstElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
  81. 'first',
  82. [],
  83. NodeFactory.blockStatementNode([])
  84. );
  85. const secondElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
  86. 'second',
  87. [],
  88. NodeFactory.blockStatementNode([])
  89. );
  90. const expectedStorage: TNodeWithLexicalScopeAndStatements[] = [
  91. firstElement
  92. ];
  93. const expectedPoppedElement: TNodeWithLexicalScopeAndStatements = secondElement;
  94. let storage: TNodeWithLexicalScopeAndStatements[];
  95. let poppedElement: TNodeWithLexicalScopeAndStatements | undefined;
  96. before(() => {
  97. const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
  98. visitedLexicalScopeNodesStackStorage.push(firstElement);
  99. visitedLexicalScopeNodesStackStorage.push(secondElement);
  100. poppedElement = visitedLexicalScopeNodesStackStorage.pop();
  101. storage = visitedLexicalScopeNodesStackStorage.getStorage();
  102. });
  103. it('should pop a last element from the storage', () => {
  104. assert.deepEqual(storage, expectedStorage);
  105. });
  106. it('should return a popped element from the storage', () => {
  107. assert.equal(poppedElement, expectedPoppedElement);
  108. });
  109. });
  110. });