Files
privacy.sexy/tests/unit/application/Parser/CategoryCollectionParser.spec.ts
undergroundwires 20b7d283b0 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.
2021-10-04 18:13:25 +01:00

136 lines
6.7 KiB
TypeScript

import 'mocha';
import { expect } from 'chai';
import { IEntity } from '@/infrastructure/Entity/IEntity';
import { parseCategoryCollection } from '@/application/Parser/CategoryCollectionParser';
import { parseCategory } from '@/application/Parser/CategoryParser';
import { parseProjectInformation } from '@/application/Parser/ProjectInformationParser';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { ScriptingDefinitionParser } from '@/application/Parser/ScriptingDefinition/ScriptingDefinitionParser';
import { EnumParserStub } from '@tests/unit/stubs/EnumParserStub';
import { ProjectInformationStub } from '@tests/unit/stubs/ProjectInformationStub';
import { getCategoryStub, CollectionDataStub } from '@tests/unit/stubs/CollectionDataStub';
import { CategoryCollectionParseContextStub } from '@tests/unit/stubs/CategoryCollectionParseContextStub';
import { CategoryDataStub } from '@tests/unit/stubs/CategoryDataStub';
import { ScriptDataStub } from '@tests/unit/stubs/ScriptDataStub';
import { FunctionDataStub } from '@tests/unit/stubs/FunctionDataStub';
import { FunctionCallDataStub } from '@tests/unit/stubs/FunctionCallDataStub';
describe('CategoryCollectionParser', () => {
describe('parseCategoryCollection', () => {
it('throws when undefined', () => {
// arrange
const expectedError = 'content is null or undefined';
const info = new ProjectInformationStub();
// act
const act = () => parseCategoryCollection(undefined, info);
// assert
expect(act).to.throw(expectedError);
});
describe('actions', () => {
it('throws when undefined actions', () => {
// arrange
const expectedError = 'content does not define any action';
const collection = new CollectionDataStub()
.withActions(undefined);
const info = new ProjectInformationStub();
// act
const act = () => parseCategoryCollection(collection, info);
// assert
expect(act).to.throw(expectedError);
});
it('throws when has no actions', () => {
// arrange
const expectedError = 'content does not define any action';
const collection = new CollectionDataStub()
.withActions([]);
const info = new ProjectInformationStub();
// act
const act = () => parseCategoryCollection(collection, info);
// assert
expect(act).to.throw(expectedError);
});
it('parses actions', () => {
// arrange
const actions = [ getCategoryStub('test1'), getCategoryStub('test2') ];
const context = new CategoryCollectionParseContextStub();
const expected = [ parseCategory(actions[0], context), parseCategory(actions[1], context) ];
const collection = new CollectionDataStub()
.withActions(actions);
const info = new ProjectInformationStub();
// act
const actual = parseCategoryCollection(collection, info).actions;
// assert
expect(excludingId(actual)).to.be.deep.equal(excludingId(expected));
function excludingId<TId>(array: ReadonlyArray<IEntity<TId>>) {
return array.map((obj) => {
const { ['id']: omitted, ...rest } = obj;
return rest;
});
}
});
});
describe('scripting definition', () => {
it('parses scripting definition as expected', () => {
// arrange
const collection = new CollectionDataStub();
const information = parseProjectInformation(process.env);
const expected = new ScriptingDefinitionParser()
.parse(collection.scripting, information);
// act
const actual = parseCategoryCollection(collection, information).scripting;
// assert
expect(expected).to.deep.equal(actual);
});
});
describe('os', () => {
it('parses as expected', () => {
// arrange
const expectedOs = OperatingSystem.macOS;
const osText = 'macos';
const expectedName = 'os';
const collection = new CollectionDataStub()
.withOs(osText);
const parserMock = new EnumParserStub<OperatingSystem>()
.setup(expectedName, osText, expectedOs);
const info = new ProjectInformationStub();
// act
const actual = parseCategoryCollection(collection, info, parserMock);
// assert
expect(actual.os).to.equal(expectedOs);
});
});
describe('functions', () => {
it('compiles script call with given function', () => {
// arrange
const expectedCode = 'code-from-the-function';
const functionName = 'function-name';
const scriptName = 'script-name';
const script = ScriptDataStub.createWithCall()
.withCall(new FunctionCallDataStub().withName(functionName).withParameters({}))
.withName(scriptName);
const func = FunctionDataStub.createWithCode().withParametersObject([])
.withName(functionName)
.withCode(expectedCode);
const category = new CategoryDataStub()
.withChildren([ script,
ScriptDataStub.createWithCode().withName('2')
.withRecommendationLevel(RecommendationLevel.Standard),
ScriptDataStub.createWithCode()
.withName('3').withRecommendationLevel(RecommendationLevel.Strict),
]);
const collection = new CollectionDataStub()
.withActions([ category ])
.withFunctions([ func ]);
const info = new ProjectInformationStub();
// act
const actual = parseCategoryCollection(collection, info);
// assert
const actualScript = actual.findScript(scriptName);
const actualCode = actualScript.code.execute;
expect(actualCode).to.equal(expectedCode);
});
});
});
});