123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import 'reflect-metadata';
- import { assert } from 'chai';
- import { ServiceIdentifiers } from '../../../../../src/container/ServiceIdentifiers';
- import { TInputOptions } from '../../../../../src/types/options/TInputOptions';
- import { TNodeWithLexicalScopeAndStatements } from '../../../../../src/types/node/TNodeWithLexicalScopeAndStatements';
- import { IInversifyContainerFacade } from '../../../../../src/interfaces/container/IInversifyContainerFacade';
- import { IVisitedLexicalScopeNodesStackStorage } from '../../../../../src/interfaces/storages/string-array-transformers/IVisitedLexicalScopeNodesStackStorage';
- import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
- import { InversifyContainerFacade } from '../../../../../src/container/InversifyContainerFacade';
- import { NodeFactory } from '../../../../../src/node/NodeFactory';
- /**
- * @returns {IMapStorage<string, V>}
- */
- const getStorageInstance = (options: TInputOptions = {}): IVisitedLexicalScopeNodesStackStorage => {
- const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
- inversifyContainerFacade.load('', '', {
- ...NO_ADDITIONAL_NODES_PRESET,
- ...options
- });
- const storage: IVisitedLexicalScopeNodesStackStorage = inversifyContainerFacade.get(ServiceIdentifiers.IVisitedLexicalScopeNodesStackStorage);
- storage.initialize();
- return storage;
- };
- describe('VisitedLexicalScopeNodesStackStorage', () => {
- describe('getLastElement', () => {
- const firstElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
- 'first',
- [],
- NodeFactory.blockStatementNode([])
- );
- const secondElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
- 'second',
- [],
- NodeFactory.blockStatementNode([])
- );
- const expectedLastElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
- 'last',
- [],
- NodeFactory.blockStatementNode([])
- );
- let lastElement: TNodeWithLexicalScopeAndStatements | undefined;
- before(() => {
- const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
- visitedLexicalScopeNodesStackStorage.push(firstElement);
- visitedLexicalScopeNodesStackStorage.push(secondElement);
- visitedLexicalScopeNodesStackStorage.push(expectedLastElement);
- lastElement = visitedLexicalScopeNodesStackStorage.getLastElement();
- });
- it('should return a last element from the stack', () => {
- assert.equal(lastElement, expectedLastElement);
- });
- });
- describe('push', () => {
- const firstElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
- 'first',
- [],
- NodeFactory.blockStatementNode([])
- );
- const secondElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
- 'second',
- [],
- NodeFactory.blockStatementNode([])
- );
- const expectedStorage: TNodeWithLexicalScopeAndStatements[] = [
- firstElement,
- secondElement
- ];
- let storage: TNodeWithLexicalScopeAndStatements[];
- before(() => {
- const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
- visitedLexicalScopeNodesStackStorage.push(firstElement);
- visitedLexicalScopeNodesStackStorage.push(secondElement);
- storage = visitedLexicalScopeNodesStackStorage.getStorage();
- });
- it('should push a new element into the storage', () => {
- assert.deepEqual(storage, expectedStorage);
- });
- });
- describe('pop', () => {
- const firstElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
- 'first',
- [],
- NodeFactory.blockStatementNode([])
- );
- const secondElement: TNodeWithLexicalScopeAndStatements = NodeFactory.functionDeclarationNode(
- 'second',
- [],
- NodeFactory.blockStatementNode([])
- );
- const expectedStorage: TNodeWithLexicalScopeAndStatements[] = [
- firstElement
- ];
- const expectedPoppedElement: TNodeWithLexicalScopeAndStatements = secondElement;
- let storage: TNodeWithLexicalScopeAndStatements[];
- let poppedElement: TNodeWithLexicalScopeAndStatements | undefined;
- before(() => {
- const visitedLexicalScopeNodesStackStorage: IVisitedLexicalScopeNodesStackStorage = getStorageInstance();
- visitedLexicalScopeNodesStackStorage.push(firstElement);
- visitedLexicalScopeNodesStackStorage.push(secondElement);
- poppedElement = visitedLexicalScopeNodesStackStorage.pop();
- storage = visitedLexicalScopeNodesStackStorage.getStorage();
- });
- it('should pop a last element from the storage', () => {
- assert.deepEqual(storage, expectedStorage);
- });
- it('should return a popped element from the storage', () => {
- assert.equal(poppedElement, expectedPoppedElement);
- });
- });
- });
|