VisitedLexicalScopeNodesStackStorage.spec.ts 5.4 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 { 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('push', () => {
  54. const firstElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  55. NodeFactory.expressionStatementNode(
  56. NodeFactory.literalNode('first')
  57. )
  58. ]);
  59. const secondElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  60. NodeFactory.expressionStatementNode(
  61. NodeFactory.literalNode('second')
  62. )
  63. ]);
  64. const expectedStorage: TNodeWithLexicalScopeStatements[] = [
  65. firstElement,
  66. secondElement
  67. ];
  68. let storage: TNodeWithLexicalScopeStatements[];
  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: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  81. NodeFactory.expressionStatementNode(
  82. NodeFactory.literalNode('first')
  83. )
  84. ]);
  85. const secondElement: TNodeWithLexicalScopeStatements = NodeFactory.blockStatementNode([
  86. NodeFactory.expressionStatementNode(
  87. NodeFactory.literalNode('second')
  88. )
  89. ]);
  90. const expectedStorage: TNodeWithLexicalScopeStatements[] = [
  91. firstElement
  92. ];
  93. const expectedPoppedElement: TNodeWithLexicalScopeStatements = secondElement;
  94. let storage: TNodeWithLexicalScopeStatements[];
  95. let poppedElement: TNodeWithLexicalScopeStatements | 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. });