Add optionality for parameters

This commit allows for parameters that does not require any arguments to
be provided in function calls. It changes collection syntax where
parameters are list of objects instead of primitive strings. A
parameter has now 'name' and 'optional' properties. 'name' is required
and used in same way as older strings as parameter definitions.
'Optional' property is optional, 'false' is the default behavior if
undefined. It also adds additional validation to restrict parameter
names to alphanumeric strings to have a clear syntax in expressions.
This commit is contained in:
undergroundwires
2021-09-02 18:59:25 +01:00
parent dcccb61781
commit 6a89c6224b
51 changed files with 1311 additions and 354 deletions

View File

@@ -1,6 +1,8 @@
import 'mocha';
import { expect } from 'chai';
import { SharedFunction } from '@/application/Parser/Script/Compiler/Function/SharedFunction';
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
import { FunctionParameterCollectionStub } from '@tests/unit/stubs/FunctionParameterCollectionStub';
describe('SharedFunction', () => {
describe('name', () => {
@@ -31,25 +33,25 @@ describe('SharedFunction', () => {
describe('parameters', () => {
it('sets as expected', () => {
// arrange
const expected = [ 'expected-parameter' ];
const expected = new FunctionParameterCollectionStub()
.withParameterName('test-parameter');
// act
const sut = new SharedFunctionBuilder()
.withParameters(expected)
.build();
// assert
expect(sut.parameters).to.deep.equal(expected);
expect(sut.parameters).to.equal(expected);
});
it('returns empty array if undefined', () => {
it('throws if undefined', () => {
// arrange
const expected = [ ];
const value = undefined;
const expectedError = 'undefined parameters';
const parameters = undefined;
// act
const sut = new SharedFunctionBuilder()
.withParameters(value)
const act = () => new SharedFunctionBuilder()
.withParameters(parameters)
.build();
// assert
expect(sut.parameters).to.not.equal(undefined);
expect(sut.parameters).to.deep.equal(expected);
expect(act).to.throw(expectedError);
});
});
describe('code', () => {
@@ -97,7 +99,7 @@ describe('SharedFunction', () => {
class SharedFunctionBuilder {
private name = 'name';
private parameters: readonly string[] = [ 'parameter' ];
private parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub();
private code = 'code';
private revertCode = 'revert-code';
@@ -113,7 +115,7 @@ class SharedFunctionBuilder {
this.name = name;
return this;
}
public withParameters(parameters: readonly string[]) {
public withParameters(parameters: IReadOnlyFunctionParameterCollection) {
this.parameters = parameters;
return this;
}