Prechádzať zdrojové kódy

ScopeAnalyzer tests

sanex3339 5 rokov pred
rodič
commit
b776df8f90

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/index.browser.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/index.cli.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/index.js


+ 1 - 1
src/analyzers/scope-analyzer/ScopeAnalyzer.ts

@@ -78,7 +78,7 @@ export class ScopeAnalyzer implements IScopeAnalyzer {
     /**
      * @param {Program} astTree
      */
-    public analyze (astTree: ESTree.Program): void {
+    public analyze (astTree: ESTree.Node): void {
         const sourceTypeLength: number = ScopeAnalyzer.sourceTypes.length;
 
         ScopeAnalyzer.attachMissingRanges(astTree);

+ 1 - 1
src/interfaces/analyzers/scope-analyzer/IScopeAnalyzer.d.ts

@@ -7,7 +7,7 @@ export interface IScopeAnalyzer extends IAnalyzer<void> {
     /**
      * @param {Program} astTree
      */
-    analyze (astTree: ESTree.Program): void;
+    analyze (astTree: ESTree.Node): void;
 
     /**
      * @param {Node} node

+ 1 - 0
test/index.spec.ts

@@ -7,6 +7,7 @@ require('source-map-support').install();
  */
 import './unit-tests/analyzers/calls-graph-analyzer/CallsGraphAnalyzer.spec';
 import './unit-tests/analyzers/prevailing-kind-of-variables-analyzer/PrevailingKindOfVariablesAnalyzer.spec';
+import './unit-tests/analyzers/scope-analyzer/ScopeAnalyzer.spec';
 import './unit-tests/analyzers/string-array-storage-analyzer/StringArrayStorageAnalyzer.spec';
 import './unit-tests/cli/sanitizers/ArraySanitizer.spec';
 import './unit-tests/cli/sanitizers/BooleanSanitizer.spec';

+ 177 - 0
test/unit-tests/analyzers/scope-analyzer/ScopeAnalyzer.spec.ts

@@ -0,0 +1,177 @@
+import 'reflect-metadata';
+
+import { assert } from 'chai';
+import * as eslintScope from 'eslint-scope';
+import * as ESTree from 'estree';
+
+import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';
+
+import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
+import { IScopeAnalyzer } from '../../../../src/interfaces/analyzers/scope-analyzer/IScopeAnalyzer';
+
+import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
+import { NodeFactory } from '../../../../src/node/NodeFactory';
+
+describe('ScopeAnalyzer', () => {
+    let scopeAnalyzer: IScopeAnalyzer;
+
+    beforeEach(() => {
+        const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
+
+        inversifyContainerFacade.load('', '', {});
+        scopeAnalyzer = inversifyContainerFacade
+            .get<IScopeAnalyzer>(ServiceIdentifiers.IScopeAnalyzer);
+    });
+
+    describe('analyze', () => {
+        const expectedScopeVariablesLength: number = 1;
+        const expectedScopeVariablesName: string = 'foo';
+
+        let scopeVariablesLength: number;
+        let scopeVariableName: string;
+
+        describe('Variant #1: base analyze of the scope for ProgramNode', () => {
+            beforeEach(() => {
+                const programNode: ESTree.Program = NodeFactory.programNode([
+                    NodeFactory.variableDeclarationNode([
+                        NodeFactory.variableDeclaratorNode(
+                            NodeFactory.identifierNode('foo'),
+                            NodeFactory.literalNode(1)
+                        )
+                    ])
+                ]);
+
+                scopeAnalyzer.analyze(programNode);
+
+                const scope: eslintScope.Scope = scopeAnalyzer.acquireScope(programNode);
+
+                scopeVariablesLength = scope.variables.length;
+                scopeVariableName = scope.variables[0].name;
+            });
+
+            it('should return scope data with variables', () => {
+                assert.equal(scopeVariablesLength, expectedScopeVariablesLength);
+            });
+
+            it('should return scope data with correct variable name', () => {
+                assert.equal(scopeVariableName, expectedScopeVariablesName);
+            });
+        });
+
+        describe('Variant #2: Acquire of the scope for VariableDeclarationNode', () => {
+            const expectedError: string = 'Cannot acquire scope for node';
+
+            let testFunc: () => eslintScope.Scope;
+
+            beforeEach(() => {
+                const variableDeclarationNode: ESTree.VariableDeclaration = NodeFactory.variableDeclarationNode([
+                    NodeFactory.variableDeclaratorNode(
+                        NodeFactory.identifierNode('foo'),
+                        NodeFactory.literalNode(1)
+                    )
+                ]);
+                const programNode: ESTree.Program = NodeFactory.programNode([variableDeclarationNode]);
+
+                scopeAnalyzer.analyze(programNode);
+
+                testFunc = () => scopeAnalyzer.acquireScope(variableDeclarationNode);
+            });
+
+            it('should throw error', () => {
+                assert.throws(testFunc, expectedError);
+            });
+        });
+
+        describe('Variant #3: acquire scope without analyzing', () => {
+            const expectedError: string = 'Scope manager is not defined';
+
+            let testFunc: () => eslintScope.Scope;
+
+            beforeEach(() => {
+                const programNode: ESTree.Program = NodeFactory.programNode([
+                    NodeFactory.variableDeclarationNode([
+                        NodeFactory.variableDeclaratorNode(
+                            NodeFactory.identifierNode('foo'),
+                            NodeFactory.literalNode(1)
+                        )
+                    ])
+                ]);
+
+                testFunc = () => scopeAnalyzer.acquireScope(programNode);
+            });
+
+            it('should throw error', () => {
+                assert.throws(testFunc, expectedError);
+            });
+        });
+
+        describe('Variant #4: analyzing error', () => {
+            const expectedError: ErrorConstructor = Error;
+
+            let testFunc: () => void;
+
+            beforeEach(() => {
+                const variableDeclarationNode: ESTree.VariableDeclaration = NodeFactory.variableDeclarationNode([
+                    NodeFactory.variableDeclaratorNode(
+                        NodeFactory.identifierNode('foo'),
+                        NodeFactory.literalNode(1)
+                    )
+                ]);
+                const programNode: ESTree.Program = NodeFactory.programNode([variableDeclarationNode]);
+
+                testFunc = () => {
+                    scopeAnalyzer.analyze(programNode);
+                    scopeAnalyzer.analyze(variableDeclarationNode);
+                };
+            });
+
+            it('should throw error', () => {
+                assert.throws(testFunc, expectedError);
+            });
+        });
+
+        describe('Variant #5: cannot read property `0` of undefined error', () => {
+            const expectedError: string = 'Cannot read property';
+
+            let testFunc: () => void;
+
+            beforeEach(() => {
+                const variableDeclarationNode: ESTree.VariableDeclaration = NodeFactory.variableDeclarationNode([
+                    NodeFactory.variableDeclaratorNode(
+                        NodeFactory.identifierNode('foo'),
+                        NodeFactory.literalNode(1)
+                    )
+                ]);
+                const programNode: ESTree.Program = NodeFactory.programNode([
+                    NodeFactory.ifStatementNode(
+                        NodeFactory.binaryExpressionNode(
+                            '+',
+                            NodeFactory.literalNode(1),
+                            NodeFactory.literalNode(2)
+                        ),
+                        NodeFactory.blockStatementNode([
+                            variableDeclarationNode
+                        ]),
+                        NodeFactory.blockStatementNode([
+                            NodeFactory.functionDeclarationNode(
+                                'bar',
+                                [],
+                                NodeFactory.blockStatementNode([
+                                    variableDeclarationNode
+                                ])
+                            )
+                        ])
+                    )
+                ]);
+
+                testFunc = () => {
+                    scopeAnalyzer.analyze(programNode);
+                };
+            });
+
+            it('should does not throw error', () => {
+                assert.doesNotThrow(testFunc, expectedError);
+            });
+        });
+    });
+});

Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov