Add type validation for parameters and fix types
This commit introduces type validation for parameter values within the parser/compiler, aligning with the YAML schema. It aims to eliminate dependencies on side effects in the collection files. This update changes the treatment of data types in the Windows collection, moving away from unintended type casting by the compiler. Previously, numeric and boolean values were used even though only string types were supported. This behavior was unstable and untested, and has now been adjusted to use strings exclusively. Changes ensure that parameter values are correctly validated as strings, enhancing stability and maintainability.
This commit is contained in:
@@ -1,50 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { FunctionParameter } from '@/application/Parser/Executable/Script/Compiler/Function/Parameter/FunctionParameter';
|
||||
import { testParameterName } from '../../ParameterNameTestRunner';
|
||||
|
||||
describe('FunctionParameter', () => {
|
||||
describe('name', () => {
|
||||
testParameterName(
|
||||
(parameterName) => new FunctionParameterBuilder()
|
||||
.withName(parameterName)
|
||||
.build()
|
||||
.name,
|
||||
);
|
||||
});
|
||||
describe('isOptional', () => {
|
||||
describe('sets as expected', () => {
|
||||
// arrange
|
||||
const expectedValues = [true, false];
|
||||
for (const expected of expectedValues) {
|
||||
it(expected.toString(), () => {
|
||||
// act
|
||||
const sut = new FunctionParameterBuilder()
|
||||
.withIsOptional(expected)
|
||||
.build();
|
||||
// expect
|
||||
expect(sut.isOptional).to.equal(expected);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class FunctionParameterBuilder {
|
||||
private name = 'parameterFromParameterBuilder';
|
||||
|
||||
private isOptional = false;
|
||||
|
||||
public withName(name: string) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withIsOptional(isOptional: boolean) {
|
||||
this.isOptional = isOptional;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build() {
|
||||
return new FunctionParameter(this.name, this.isOptional);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import type { ParameterDefinitionData } from '@/application/collections/';
|
||||
import type { ParameterNameValidator } from '@/application/Parser/Executable/Script/Compiler/Function/Shared/ParameterNameValidator';
|
||||
import { createParameterNameValidatorStub } from '@tests/unit/shared/Stubs/ParameterNameValidatorStub';
|
||||
import { parseFunctionParameter } from '@/application/Parser/Executable/Script/Compiler/Function/Parameter/FunctionParameterParser';
|
||||
import { ParameterDefinitionDataStub } from '@tests/unit/shared/Stubs/ParameterDefinitionDataStub';
|
||||
|
||||
describe('FunctionParameterParser', () => {
|
||||
describe('parseFunctionParameter', () => {
|
||||
describe('name', () => {
|
||||
it('assigns correctly', () => {
|
||||
// arrange
|
||||
const expectedName = 'expected-function-name';
|
||||
const data = new ParameterDefinitionDataStub()
|
||||
.withName(expectedName);
|
||||
// act
|
||||
const actual = new TestContext()
|
||||
.withData(data)
|
||||
.parse();
|
||||
// expect
|
||||
const actualName = actual.name;
|
||||
expect(actualName).to.equal(expectedName);
|
||||
});
|
||||
it('validates correctly', () => {
|
||||
// arrange
|
||||
const expectedName = 'expected-function-name';
|
||||
const { validator, validatedNames } = createParameterNameValidatorStub();
|
||||
const data = new ParameterDefinitionDataStub()
|
||||
.withName(expectedName);
|
||||
// act
|
||||
new TestContext()
|
||||
.withData(data)
|
||||
.withValidator(validator)
|
||||
.parse();
|
||||
// expect
|
||||
expect(validatedNames).to.have.lengthOf(1);
|
||||
expect(validatedNames).to.contain(expectedName);
|
||||
});
|
||||
});
|
||||
describe('isOptional', () => {
|
||||
describe('assigns correctly', () => {
|
||||
// arrange
|
||||
const expectedValues = [true, false];
|
||||
for (const expected of expectedValues) {
|
||||
it(expected.toString(), () => {
|
||||
const data = new ParameterDefinitionDataStub()
|
||||
.withOptionality(expected);
|
||||
// act
|
||||
const actual = new TestContext()
|
||||
.withData(data)
|
||||
.parse();
|
||||
// expect
|
||||
expect(actual.isOptional).to.equal(expected);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class TestContext {
|
||||
private data: ParameterDefinitionData = new ParameterDefinitionDataStub()
|
||||
.withName(`[${TestContext.name}]function-name`);
|
||||
|
||||
private validator: ParameterNameValidator = createParameterNameValidatorStub().validator;
|
||||
|
||||
public withData(data: ParameterDefinitionData) {
|
||||
this.data = data;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withValidator(parameterNameValidator: ParameterNameValidator): this {
|
||||
this.validator = parameterNameValidator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public parse() {
|
||||
return parseFunctionParameter(
|
||||
this.data,
|
||||
this.validator,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user