Add support for more depth in function calls
It allow pipes to be used in nested functions. Before, pipes were added
to a variable before variable content was evaluated/compiled by
another function. This commit ensures that the commits are evaluted in
expected order.
The issue is solved by stopping precompiling functions. It makes code
less complex. It adds to compile time of the script file but nothing
noticable and something optimizable.
The problem was that the call trees we're not executed in expected
order. E.g. let's say we have functionA that outputs something like
"Hello {{ $name| pipe }}", and we have function B calling with "name:
dear {{ $firstName}}", and at last we have a script that's calling
function B with "firstName: undergroundwires". Before, expressions were
evaluated directly, meaning that function A would become:
"Hello Dear {{ $firstName}}", as you see the pipe in function A
is lost here after being applied to function B and not reaching
$firstTime input value. Parsing expressions in the end allows for pipes
etc. to not get lost.
The commit also does necessary name refactorings and folder refactorings
to reflect logical changes. `FunctionCompiler` is renamed to
`SharedFunctionsParser` as precompiling is removed and it just simply
parses now. `/FunctionCall/` is moved to `/Function/Call`.
Finally, it improves documentation and adds more tests.
This commit is contained in:
@@ -2,6 +2,8 @@ import 'mocha';
|
||||
import { expect } from 'chai';
|
||||
import { SharedFunctionCollection } from '@/application/Parser/Script/Compiler/Function/SharedFunctionCollection';
|
||||
import { SharedFunctionStub } from '@tests/unit/stubs/SharedFunctionStub';
|
||||
import { FunctionBodyType } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
|
||||
import { FunctionCallStub } from '@tests/unit/stubs/FunctionCallStub';
|
||||
|
||||
describe('SharedFunctionCollection', () => {
|
||||
describe('addFunction', () => {
|
||||
@@ -19,7 +21,7 @@ describe('SharedFunctionCollection', () => {
|
||||
// arrange
|
||||
const functionName = 'duplicate-function';
|
||||
const expectedError = `function with name ${functionName} already exists`;
|
||||
const func = new SharedFunctionStub()
|
||||
const func = new SharedFunctionStub(FunctionBodyType.Code)
|
||||
.withName('duplicate-function');
|
||||
const sut = new SharedFunctionCollection();
|
||||
sut.addFunction(func);
|
||||
@@ -27,7 +29,6 @@ describe('SharedFunctionCollection', () => {
|
||||
const act = () => sut.addFunction(func);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
|
||||
});
|
||||
});
|
||||
describe('getFunctionByName', () => {
|
||||
@@ -48,7 +49,7 @@ describe('SharedFunctionCollection', () => {
|
||||
// arrange
|
||||
const name = 'unique-name';
|
||||
const expectedError = `called function is not defined "${name}"`;
|
||||
const func = new SharedFunctionStub()
|
||||
const func = new SharedFunctionStub(FunctionBodyType.Code)
|
||||
.withName('unexpected-name');
|
||||
const sut = new SharedFunctionCollection();
|
||||
sut.addFunction(func);
|
||||
@@ -57,18 +58,33 @@ describe('SharedFunctionCollection', () => {
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
it('returns existing function', () => {
|
||||
// arrange
|
||||
const name = 'expected-function-name';
|
||||
const expected = new SharedFunctionStub()
|
||||
.withName(name);
|
||||
const sut = new SharedFunctionCollection();
|
||||
sut.addFunction(new SharedFunctionStub().withName('another-function-name'));
|
||||
sut.addFunction(expected);
|
||||
// act
|
||||
const actual = sut.getFunctionByName(name);
|
||||
// assert
|
||||
expect(actual).to.equal(expected);
|
||||
describe('returns existing function', () => {
|
||||
it('when function with inline code is added', () => {
|
||||
// arrange
|
||||
const expected = new SharedFunctionStub(FunctionBodyType.Code)
|
||||
.withName('expected-function-name');
|
||||
const sut = new SharedFunctionCollection();
|
||||
// act
|
||||
sut.addFunction(expected);
|
||||
const actual = sut.getFunctionByName(expected.name);
|
||||
// assert
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
it('when calling function is added', () => {
|
||||
// arrange
|
||||
const callee = new SharedFunctionStub(FunctionBodyType.Code)
|
||||
.withName('calleeFunction');
|
||||
const caller = new SharedFunctionStub(FunctionBodyType.Calls)
|
||||
.withName('callerFunction')
|
||||
.withCalls(new FunctionCallStub().withFunctionName(callee.name));
|
||||
const sut = new SharedFunctionCollection();
|
||||
// act
|
||||
sut.addFunction(callee);
|
||||
sut.addFunction(caller);
|
||||
const actual = sut.getFunctionByName(caller.name);
|
||||
// assert
|
||||
expect(actual).to.equal(caller);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user