浏览代码

Added tests for VisitedLexicalScopeNodesStackStorage class

sanex 4 年之前
父节点
当前提交
47263a4c1c

文件差异内容过多而无法显示
+ 0 - 0
dist/index.browser.js


文件差异内容过多而无法显示
+ 0 - 0
dist/index.cli.js


文件差异内容过多而无法显示
+ 0 - 0
dist/index.js


+ 4 - 4
src/node-transformers/string-array-transformers/StringArrayScopeCallsWrapperTransformer.ts

@@ -79,6 +79,10 @@ export class StringArrayScopeCallsWrapperTransformer extends AbstractNodeTransfo
      * @returns {IVisitor | null}
      */
     public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null {
+        if (!this.options.stringArrayWrappersCount) {
+            return null;
+        }
+
         switch (nodeTransformationStage) {
             case NodeTransformationStage.StringArray:
                 return {
@@ -106,10 +110,6 @@ export class StringArrayScopeCallsWrapperTransformer extends AbstractNodeTransfo
      * @returns {TNodeWithLexicalScopeAndStatements}
      */
     public transformNode (lexicalScopeNode: TNodeWithLexicalScopeAndStatements): TNodeWithLexicalScopeAndStatements {
-        if (!this.options.stringArrayWrappersCount) {
-            return lexicalScopeNode;
-        }
-
         const lexicalScopeBodyNode: ESTree.Program | ESTree.BlockStatement =
             NodeGuards.isProgramNode(lexicalScopeNode)
                 ? lexicalScopeNode

+ 1 - 0
test/index.spec.ts

@@ -38,6 +38,7 @@ import './unit-tests/storages/ArrayStorage.spec';
 import './unit-tests/storages/MapStorage.spec';
 import './unit-tests/storages/string-array-transformers/literal-nodes-cache/LiteralNodesCacheStorage.spec';
 import './unit-tests/storages/string-array-transformers/string-array/StringArrayStorage.spec';
+import './unit-tests/storages/string-array-transformers/visited-lexical-scope-nodes-stack/VisitedLexicalScopeNodesStackStorage.spec';
 import './unit-tests/utils/ArrayUtils.spec';
 import './unit-tests/utils/CryptUtils.spec';
 import './unit-tests/utils/CryptUtilsSwappedAlphabet.spec';

+ 138 - 0
test/unit-tests/storages/string-array-transformers/visited-lexical-scope-nodes-stack/VisitedLexicalScopeNodesStackStorage.spec.ts

@@ -0,0 +1,138 @@
+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);
+        });
+    });
+});

部分文件因为文件数量过多而无法显示