Rework code validation to be bound to a context and not context-independent. It means that the generated code is validated based on different phases during the compilation. This is done by moving validation from `ScriptCode` constructor to a different callable function. It removes duplicate detection for function calls once a call is fully compiled, but still checks for duplicates inside each function body that has inline code. This allows for having duplicates in final scripts (thus relaxing the duplicate detection), e.g., when multiple calls to the same function is made. It fixes non-duplicates (when using common syntax) being misrepresented as duplicate lines. It improves the output of errors, such as printing valid lines, to give more context. This improvement also fixes empty line validation not showing the right empty lines in the error output. Empty line validation shows tabs and whitespaces more clearly. Finally, it adds more tests including tests for existing logic, such as singleton factories.
86 lines
3.3 KiB
TypeScript
86 lines
3.3 KiB
TypeScript
import 'mocha';
|
|
import { expect } from 'chai';
|
|
import { ISyntaxFactory } from '@/application/Parser/Script/Validation/Syntax/ISyntaxFactory';
|
|
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
|
import { CategoryCollectionParseContext } from '@/application/Parser/Script/CategoryCollectionParseContext';
|
|
import { ScriptCompiler } from '@/application/Parser/Script/Compiler/ScriptCompiler';
|
|
import { LanguageSyntaxStub } from '@tests/unit/shared/Stubs/LanguageSyntaxStub';
|
|
import { ScriptingDefinitionStub } from '@tests/unit/shared/Stubs/ScriptingDefinitionStub';
|
|
import { FunctionDataStub } from '@tests/unit/shared/Stubs/FunctionDataStub';
|
|
import { itEachAbsentCollectionValue, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
|
|
import { ILanguageSyntax } from '@/application/Parser/Script/Validation/Syntax/ILanguageSyntax';
|
|
|
|
describe('CategoryCollectionParseContext', () => {
|
|
describe('ctor', () => {
|
|
describe('functionsData', () => {
|
|
describe('can create with absent data', () => {
|
|
itEachAbsentCollectionValue((absentValue) => {
|
|
// arrange
|
|
const scripting = new ScriptingDefinitionStub();
|
|
// act
|
|
const act = () => new CategoryCollectionParseContext(absentValue, scripting);
|
|
// assert
|
|
expect(act).to.not.throw();
|
|
});
|
|
});
|
|
});
|
|
describe('scripting', () => {
|
|
describe('throws when missing', () => {
|
|
itEachAbsentObjectValue((absentValue) => {
|
|
// arrange
|
|
const expectedError = 'missing scripting';
|
|
const scripting = absentValue;
|
|
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;
|
|
},
|
|
};
|
|
}
|