Files
privacy.sexy/tests/unit/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.spec.ts
undergroundwires 6a89c6224b 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.
2021-09-02 18:59:25 +01:00

72 lines
2.9 KiB
TypeScript

import 'mocha';
import { expect } from 'chai';
import { ParameterSubstitutionParser } from '@/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser';
import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition';
import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub';
describe('ParameterSubstitutionParser', () => {
describe('finds at expected positions', () => {
// arrange
const testCases = [
{
name: 'matches single parameter',
code: '{{ $parameter }}!',
expected: [new ExpressionPosition(0, 16)],
},
{
name: 'matches different parameters',
code: 'He{{ $firstParameter }} {{ $secondParameter }}!!',
expected: [new ExpressionPosition(2, 23), new ExpressionPosition(24, 46)],
},
{
name: 'tolerates spaces around brackets',
code: 'He{{$firstParameter}}!!',
expected: [new ExpressionPosition(2, 21) ],
},
{
name: 'does not tolerate space after dollar sign',
code: 'He{{ $ firstParameter }}!!',
expected: [ ],
},
];
for (const testCase of testCases) {
it(testCase.name, () => {
const sut = new ParameterSubstitutionParser();
// act
const expressions = sut.findExpressions(testCase.code);
// assert
const actual = expressions.map((e) => e.position);
expect(actual).to.deep.equal(testCase.expected);
});
}
});
describe('evaluates as expected', () => {
const testCases = [ {
name: 'single parameter',
code: '{{ $parameter }}',
args: new FunctionCallArgumentCollectionStub()
.withArgument('parameter', 'Hello world'),
expected: [ 'Hello world' ],
},
{
name: 'different parameters',
code: '{{ $firstParameter }} {{ $secondParameter }}!',
args: new FunctionCallArgumentCollectionStub()
.withArgument('firstParameter', 'Hello')
.withArgument('secondParameter', 'World'),
expected: [ 'Hello', 'World' ],
}];
for (const testCase of testCases) {
it(testCase.name, () => {
const sut = new ParameterSubstitutionParser();
// act
const expressions = sut.findExpressions(testCase.code);
// assert
const actual = expressions.map((e) => e.evaluate(testCase.args));
expect(actual).to.deep.equal(testCase.expected);
});
}
});
});