Files
privacy.sexy/tests/unit/application/Parser/Script/CategoryCollectionParseContext.spec.ts
undergroundwires 5b1fbe1e2f Refactor code to comply with ESLint rules
Major refactoring using ESLint with rules from AirBnb and Vue.

Enable most of the ESLint rules and do necessary linting in the code.
Also add more information for rules that are disabled to describe what
they are and why they are disabled.

Allow logging (`console.log`) in test files, and in development mode
(e.g. when working with `npm run serve`), but disable it when
environment is production (as pre-configured by Vue). Also add flag
(`--mode production`) in `lint:eslint` command so production linting is
executed earlier in lifecycle.

Disable rules that requires a separate work. Such as ESLint rules that
are broken in TypeScript: no-useless-constructor (eslint/eslint#14118)
and no-shadow (eslint/eslint#13014).
2022-01-02 18:20:14 +01:00

83 lines
3.1 KiB
TypeScript

import 'mocha';
import { expect } from 'chai';
import { FunctionData } from 'js-yaml-loader!@/*';
import { ISyntaxFactory } from '@/application/Parser/Script/Syntax/ISyntaxFactory';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { CategoryCollectionParseContext } from '@/application/Parser/Script/CategoryCollectionParseContext';
import { ILanguageSyntax } from '@/domain/ScriptCode';
import { ScriptCompiler } from '@/application/Parser/Script/Compiler/ScriptCompiler';
import { LanguageSyntaxStub } from '@tests/unit/stubs/LanguageSyntaxStub';
import { ScriptingDefinitionStub } from '@tests/unit/stubs/ScriptingDefinitionStub';
import { FunctionDataStub } from '@tests/unit/stubs/FunctionDataStub';
describe('CategoryCollectionParseContext', () => {
describe('ctor', () => {
describe('functionsData', () => {
it('can create with empty values', () => {
// arrange
const testData: FunctionData[][] = [undefined, []];
const scripting = new ScriptingDefinitionStub();
for (const functionsData of testData) {
// act
const act = () => new CategoryCollectionParseContext(functionsData, scripting);
// assert
expect(act).to.not.throw();
}
});
});
it('scripting', () => {
// arrange
const expectedError = 'undefined scripting';
const scripting = undefined;
const functionsData = [FunctionDataStub.createWithCode()];
// act
const act = () => new CategoryCollectionParseContext(functionsData, scripting);
// assert
expect(act).to.throw(expectedError);
});
});
describe('compiler', () => {
it('constructed as expected', () => {
// arrange
const functionsData = [FunctionDataStub.createWithCode()];
const syntax = new LanguageSyntaxStub();
const expected = new ScriptCompiler(functionsData, syntax);
const language = ScriptingLanguage.shellscript;
const factoryMock = mockFactory(language, syntax);
const definition = new ScriptingDefinitionStub()
.withLanguage(language);
// act
const sut = new CategoryCollectionParseContext(functionsData, definition, factoryMock);
const actual = sut.compiler;
// assert
expect(actual).to.deep.equal(expected);
});
});
describe('syntax', () => {
it('set from syntax factory', () => {
// arrange
const language = ScriptingLanguage.shellscript;
const expected = new LanguageSyntaxStub();
const factoryMock = mockFactory(language, expected);
const definition = new ScriptingDefinitionStub()
.withLanguage(language);
// act
const sut = new CategoryCollectionParseContext([], definition, factoryMock);
const actual = sut.syntax;
// assert
expect(actual).to.equal(expected);
});
});
});
function mockFactory(expectedLanguage: ScriptingLanguage, result: ILanguageSyntax): ISyntaxFactory {
return {
create: (language: ScriptingLanguage) => {
if (language !== expectedLanguage) {
throw new Error('unexpected language');
}
return result;
},
};
}