Add validation for max line length in compiler
This commit adds validation logic in compiler to check for max allowed characters per line for scripts. This allows preventing bugs caused by limitation of terminal emulators. Other supporting changes: - Rename/refactor related code for clarity and better maintainability. - Drop `I` prefix from interfaces to align with latest convention. - Refactor CodeValidator to be functional rather than object-oriented for simplicity. - Refactor syntax definition construction to be functional and be part of rule for better separation of concerns. - Refactored validation logic to use an enum-based factory pattern for improved maintainability and scalability.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import { describe } from 'vitest';
|
||||
import { BatchFileSyntax } from '@/application/Parser/Executable/Script/Validation/Analyzers/Syntax/BatchFileSyntax';
|
||||
import { runLanguageSyntaxTests } from './LanguageSyntaxTestRunner';
|
||||
|
||||
describe('BatchFileSyntax', () => {
|
||||
runLanguageSyntaxTests(
|
||||
() => new BatchFileSyntax(),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import type { LanguageSyntax } from '@/application/Parser/Executable/Script/Validation/Analyzers/Syntax/LanguageSyntax';
|
||||
|
||||
export function runLanguageSyntaxTests(createSyntax: () => LanguageSyntax) {
|
||||
describe('commentDelimiters', () => {
|
||||
it('returns defined value', () => {
|
||||
// arrange
|
||||
const sut = createSyntax();
|
||||
// act
|
||||
const value = sut.commentDelimiters;
|
||||
// assert
|
||||
expect(value);
|
||||
});
|
||||
});
|
||||
describe('commonCodeParts', () => {
|
||||
it('returns defined value', () => {
|
||||
// arrange
|
||||
const sut = createSyntax();
|
||||
// act
|
||||
const value = sut.commonCodeParts;
|
||||
// assert
|
||||
expect(value);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { describe } from 'vitest';
|
||||
import { ShellScriptSyntax } from '@/application/Parser/Executable/Script/Validation/Analyzers/Syntax/ShellScriptSyntax';
|
||||
import { runLanguageSyntaxTests } from './LanguageSyntaxTestRunner';
|
||||
|
||||
describe('ShellScriptSyntax', () => {
|
||||
runLanguageSyntaxTests(
|
||||
() => new ShellScriptSyntax(),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe } from 'vitest';
|
||||
import { createSyntax } from '@/application/Parser/Executable/Script/Validation/Analyzers/Syntax/SyntaxFactory';
|
||||
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
|
||||
import { ShellScriptSyntax } from '@/application/Parser/Executable/Script/Validation/Analyzers/Syntax/ShellScriptSyntax';
|
||||
import { BatchFileSyntax } from '@/application/Parser/Executable/Script/Validation/Analyzers/Syntax/BatchFileSyntax';
|
||||
import type { LanguageSyntax } from '@/application/Parser/Executable/Script/Validation/Analyzers/Syntax/LanguageSyntax';
|
||||
import type { Constructible } from '@/TypeHelpers';
|
||||
|
||||
describe('SyntaxFactory', () => {
|
||||
describe('createSyntax', () => {
|
||||
it('throws given invalid language', () => {
|
||||
// arrange
|
||||
const invalidLanguage = 5 as ScriptingLanguage;
|
||||
const expectedErrorMessage = `Invalid language: "${ScriptingLanguage[invalidLanguage]}"`;
|
||||
// act
|
||||
const act = () => createSyntax(invalidLanguage);
|
||||
// assert
|
||||
expect(act).to.throw(expectedErrorMessage);
|
||||
});
|
||||
describe('creates syntax for supported languages', () => {
|
||||
const languageTestScenarios: Record<ScriptingLanguage, Constructible<LanguageSyntax>> = {
|
||||
[ScriptingLanguage.batchfile]: BatchFileSyntax,
|
||||
[ScriptingLanguage.shellscript]: ShellScriptSyntax,
|
||||
};
|
||||
Object.entries(languageTestScenarios).forEach(([key, value]) => {
|
||||
// arrange
|
||||
const scriptingLanguage = Number(key) as ScriptingLanguage;
|
||||
const expectedType = value;
|
||||
it(`gets correct type for "${ScriptingLanguage[scriptingLanguage]}" language`, () => {
|
||||
// act
|
||||
const syntax = createSyntax(scriptingLanguage);
|
||||
// assert
|
||||
expect(syntax).to.be.instanceOf(expectedType);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user