ScopeAnalyzer tests

This commit is contained in:
sanex3339
2020-01-18 10:27:47 +03:00
parent fd0736ca0e
commit b776df8f90
7 changed files with 184 additions and 6 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -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);
@@ -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
View File
@@ -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';
@@ -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);
});
});
});
});