From 6a89c6224bdef5eb96980471f3b3935b9351b197 Mon Sep 17 00:00:00 2001 From: undergroundwires Date: Thu, 2 Sep 2021 18:59:25 +0100 Subject: [PATCH] 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. --- docs/collection-files.md | 35 +++- .../Expressions/Expression/Expression.ts | 54 +++++-- .../Expressions/Expression/IExpression.ts | 11 +- .../Expressions/ExpressionsCompiler.ts | 57 +++++-- .../Expressions/IExpressionsCompiler.ts | 6 +- .../Parser/CompositeExpressionParser.ts | 8 +- .../Expressions/Parser/RegexParser.ts | 17 +- .../ParameterSubstitutionParser.ts | 5 +- .../Compiler/Function/FunctionCompiler.ts | 57 ++++--- .../Compiler/Function/ISharedFunction.ts | 4 +- .../Function/Parameter/FunctionParameter.ts | 10 ++ .../Parameter/FunctionParameterCollection.ts | 26 +++ .../Function/Parameter/IFunctionParameter.ts | 4 + .../Parameter/IFunctionParameterCollection.ts | 9 ++ .../Compiler/Function/SharedFunction.ts | 8 +- .../Argument/FunctionCallArgument.ts | 13 ++ .../FunctionCallArgumentCollection.ts | 34 ++++ .../Argument/IFunctionCallArgument.ts | 4 + .../IFunctionCallArgumentCollection.ts | 11 ++ .../Compiler/FunctionCall/FunctionCall.ts | 15 ++ .../FunctionCall/FunctionCallCompiler.ts | 104 ++++++------ .../Compiler/FunctionCall/IFunctionCall.ts | 6 + .../Script/Compiler/ParameterNameValidator.ts | 8 + .../ScriptingDefinition/CodeSubstituter.ts | 17 +- .../collections/collection.yaml.d.ts | 7 +- src/application/collections/macos.yaml | 5 +- src/application/collections/windows.yaml | 28 ++-- .../Expressions/Expression/Expression.spec.ts | 151 ++++++++++++++---- .../Expressions/ExpressionsCompiler.spec.ts | 132 +++++++-------- .../Expressions/Parser/RegexParser.spec.ts | 8 +- .../ParameterSubstitutionParser.spec.ts | 25 +-- .../Function/FunctionCompiler.spec.ts | 108 ++++++++++--- .../Parameter/FunctionParameter.spec.ts | 47 ++++++ .../FunctionParameterCollection.spec.ts | 47 ++++++ .../Compiler/Function/SharedFunction.spec.ts | 24 +-- .../Argument/FunctionCallArgument.spec.ts | 46 ++++++ .../FunctionCallArgumentCollection.spec.ts | 143 +++++++++++++++++ .../FunctionCall/FunctionCall.spec.ts | 51 ++++++ .../FunctionCall/FunctionCallCompiler.spec.ts | 40 ++--- .../Compiler/ParameterNameTestRunner.ts | 56 +++++++ .../CodeSubstituter.spec.ts | 5 +- tests/unit/stubs/ExpressionStub.ts | 18 ++- tests/unit/stubs/ExpressionsCompilerStub.ts | 52 ++++-- .../FunctionCallArgumentCollectionStub.ts | 39 +++++ tests/unit/stubs/FunctionCallArgumentStub.ts | 15 ++ tests/unit/stubs/FunctionDataStub.ts | 11 +- .../stubs/FunctionParameterCollectionStub.ts | 27 ++++ tests/unit/stubs/FunctionParameterStub.ts | 14 ++ .../unit/stubs/ParameterDefinitionDataStub.ts | 14 ++ .../stubs/SharedFunctionCollectionStub.ts | 11 +- tests/unit/stubs/SharedFunctionStub.ts | 18 ++- 51 files changed, 1311 insertions(+), 354 deletions(-) create mode 100644 src/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter.ts create mode 100644 src/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection.ts create mode 100644 src/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameter.ts create mode 100644 src/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection.ts create mode 100644 src/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument.ts create mode 100644 src/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection.ts create mode 100644 src/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgument.ts create mode 100644 src/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection.ts create mode 100644 src/application/Parser/Script/Compiler/FunctionCall/FunctionCall.ts create mode 100644 src/application/Parser/Script/Compiler/FunctionCall/IFunctionCall.ts create mode 100644 src/application/Parser/Script/Compiler/ParameterNameValidator.ts create mode 100644 tests/unit/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter.spec.ts create mode 100644 tests/unit/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection.spec.ts create mode 100644 tests/unit/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument.spec.ts create mode 100644 tests/unit/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection.spec.ts create mode 100644 tests/unit/application/Parser/Script/Compiler/FunctionCall/FunctionCall.spec.ts create mode 100644 tests/unit/application/Parser/Script/Compiler/ParameterNameTestRunner.ts create mode 100644 tests/unit/stubs/FunctionCallArgumentCollectionStub.ts create mode 100644 tests/unit/stubs/FunctionCallArgumentStub.ts create mode 100644 tests/unit/stubs/FunctionParameterCollectionStub.ts create mode 100644 tests/unit/stubs/FunctionParameterStub.ts create mode 100644 tests/unit/stubs/ParameterDefinitionDataStub.ts diff --git a/docs/collection-files.md b/docs/collection-files.md index d557b61f..9654460c 100644 --- a/docs/collection-files.md +++ b/docs/collection-files.md @@ -115,7 +115,8 @@ A simple function example ```yaml function: EchoArgument - parameters: [ 'argument' ] + parameters: + - name: 'argument' code: Hello {{ $argument }} ! ``` @@ -134,14 +135,16 @@ A function can call other functions such as: ```yaml - function: CallerFunction - parameters: [ 'value' ] + parameters: + - name: 'value' call: function: EchoArgument parameters: argument: {{ $value }} - function: EchoArgument - parameters: [ 'argument' ] + parameters: + - name: 'argument' code: Hello {{ $argument }} ! ``` @@ -152,11 +155,9 @@ A function can call other functions such as: - Convention is to use camelCase, and be verbs. - E.g. `uninstallStoreApp` - ❗ Function names must be unique -- `parameters`: `[` *`string`* `, ... ]` - - Name of the parameters that the function has. - - Parameter values are provided by a [Script](#script) through a [FunctionCall](#FunctionCall) - - Parameter names must be defined to be used in [expressions](#expressions) - - ❗ Parameter names must be unique +- `parameters`: `[` ***[`FunctionParameter`](#FunctionParameter)*** `, ... ]` + - List of parameters that function code refers to. + - ❗ Must be defined to be able use in [`FunctionCall`](#functioncall) or [expressions](#expressions) `code`: *`string`* (**required** if `call` is undefined) - Batch file commands that will be executed - 💡 If defined, best practice to also define `revertCode` @@ -170,6 +171,24 @@ A function can call other functions such as: - The parameter values that are sent can use [expressions](#expressions) - ❗ If not defined `code` must be defined +### `FunctionParameter` + +- Defines a parameter that function requires optionally or mandatory. +- Its arguments are provided by a [Script](#script) through a [FunctionCall](#FunctionCall). + +#### `FunctionParameter` syntax + +- `name`: *`string`* (**required**) + - Name of the parameters that the function has. + - Parameter names must be defined to be used in [expressions](#expressions). + - ❗ Parameter names must be unique and include alphanumeric characters only. +- `optional`: *`boolean`* (default: `false`) + - Specifies whether the caller [Script](#script) must provide any value for the parameter. + - If set to `false` i.e. an argument value is not optional then it expects a non-empty value for the variable; + - Otherwise it throws. + - 💡 Set it to `true` if a parameter is used conditionally; + - Or else set it to `false` for verbosity or do not define it as default value is `false` anyway. + ### `ScriptingDefinition` - Defines global properties for scripting that's used throughout its parent [Collection](#collection). diff --git a/src/application/Parser/Script/Compiler/Expressions/Expression/Expression.ts b/src/application/Parser/Script/Compiler/Expressions/Expression/Expression.ts index d538745c..3c1247bb 100644 --- a/src/application/Parser/Script/Compiler/Expressions/Expression/Expression.ts +++ b/src/application/Parser/Script/Compiler/Expressions/Expression/Expression.ts @@ -1,12 +1,16 @@ import { ExpressionPosition } from './ExpressionPosition'; -import { ExpressionArguments, IExpression } from './IExpression'; +import { IExpression } from './IExpression'; +import { IReadOnlyFunctionCallArgumentCollection } from '../../FunctionCall/Argument/IFunctionCallArgumentCollection'; +import { IReadOnlyFunctionParameterCollection } from '../../Function/Parameter/IFunctionParameterCollection'; +import { FunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection'; +import { FunctionCallArgumentCollection } from '../../FunctionCall/Argument/FunctionCallArgumentCollection'; -export type ExpressionEvaluator = (args?: ExpressionArguments) => string; +export type ExpressionEvaluator = (args: IReadOnlyFunctionCallArgumentCollection) => string; export class Expression implements IExpression { constructor( public readonly position: ExpressionPosition, public readonly evaluator: ExpressionEvaluator, - public readonly parameters: readonly string[] = new Array()) { + public readonly parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollection()) { if (!position) { throw new Error('undefined position'); } @@ -14,22 +18,42 @@ export class Expression implements IExpression { throw new Error('undefined evaluator'); } } - public evaluate(args?: ExpressionArguments): string { + public evaluate(args: IReadOnlyFunctionCallArgumentCollection): string { + if (!args) { + throw new Error('undefined args, send empty collection instead'); + } + validateThatAllRequiredParametersAreSatisfied(this.parameters, args); args = filterUnusedArguments(this.parameters, args); return this.evaluator(args); } } -function filterUnusedArguments( - parameters: readonly string[], args: ExpressionArguments): ExpressionArguments { - let result: ExpressionArguments = {}; - for (const parameter of Object.keys(args)) { - if (parameters.includes(parameter)) { - result = { - ...result, - [parameter]: args[parameter], - }; - } +function validateThatAllRequiredParametersAreSatisfied( + parameters: IReadOnlyFunctionParameterCollection, + args: IReadOnlyFunctionCallArgumentCollection, +) { + const requiredParameterNames = parameters + .all + .filter((parameter) => !parameter.isOptional) + .map((parameter) => parameter.name); + const missingParameterNames = requiredParameterNames + .filter((parameterName) => !args.hasArgument(parameterName)); + if (missingParameterNames.length) { + throw new Error( + `argument values are provided for required parameters: "${missingParameterNames.join('", "')}"`); } - return result; +} + +function filterUnusedArguments( + parameters: IReadOnlyFunctionParameterCollection, + allFunctionArgs: IReadOnlyFunctionCallArgumentCollection): IReadOnlyFunctionCallArgumentCollection { + const specificCallArgs = new FunctionCallArgumentCollection(); + for (const parameter of parameters.all) { + if (parameter.isOptional && !allFunctionArgs.hasArgument(parameter.name)) { + continue; // Optional parameter is not necessarily provided + } + const arg = allFunctionArgs.getArgument(parameter.name); + specificCallArgs.addArgument(arg); + } + return specificCallArgs; } diff --git a/src/application/Parser/Script/Compiler/Expressions/Expression/IExpression.ts b/src/application/Parser/Script/Compiler/Expressions/Expression/IExpression.ts index df5e2848..e10c7369 100644 --- a/src/application/Parser/Script/Compiler/Expressions/Expression/IExpression.ts +++ b/src/application/Parser/Script/Compiler/Expressions/Expression/IExpression.ts @@ -1,12 +1,9 @@ import { ExpressionPosition } from './ExpressionPosition'; +import { IReadOnlyFunctionCallArgumentCollection } from '../../FunctionCall/Argument/IFunctionCallArgumentCollection'; +import { IReadOnlyFunctionParameterCollection } from '../../Function/Parameter/IFunctionParameterCollection'; export interface IExpression { readonly position: ExpressionPosition; - readonly parameters?: readonly string[]; - evaluate(args?: ExpressionArguments): string; + readonly parameters: IReadOnlyFunctionParameterCollection; + evaluate(args: IReadOnlyFunctionCallArgumentCollection): string; } - -export interface ExpressionArguments { - readonly [parameter: string]: string; -} - diff --git a/src/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler.ts b/src/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler.ts index 2102b6ec..71284517 100644 --- a/src/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler.ts +++ b/src/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler.ts @@ -1,30 +1,39 @@ -import { IExpressionsCompiler, ParameterValueDictionary } from './IExpressionsCompiler'; +import { IExpressionsCompiler } from './IExpressionsCompiler'; import { IExpression } from './Expression/IExpression'; import { IExpressionParser } from './Parser/IExpressionParser'; import { CompositeExpressionParser } from './Parser/CompositeExpressionParser'; +import { IReadOnlyFunctionCallArgumentCollection } from '../FunctionCall/Argument/IFunctionCallArgumentCollection'; export class ExpressionsCompiler implements IExpressionsCompiler { - public constructor(private readonly extractor: IExpressionParser = new CompositeExpressionParser()) { } - public compileExpressions(code: string, parameters?: ParameterValueDictionary): string { + public constructor( + private readonly extractor: IExpressionParser = new CompositeExpressionParser()) { } + public compileExpressions( + code: string, + args: IReadOnlyFunctionCallArgumentCollection): string { + if (!args) { + throw new Error('undefined args, send empty collection instead'); + } const expressions = this.extractor.findExpressions(code); - const requiredParameterNames = expressions.map((e) => e.parameters).filter((p) => p).flat(); - const uniqueParameterNames = Array.from(new Set(requiredParameterNames)); - ensureRequiredArgsProvided(uniqueParameterNames, parameters); - return compileExpressions(expressions, code, parameters); + ensureParamsUsedInCodeHasArgsProvided(expressions, args); + const compiledCode = compileExpressions(expressions, code, args); + return compiledCode; } } -function compileExpressions(expressions: IExpression[], code: string, parameters?: ParameterValueDictionary) { +function compileExpressions( + expressions: readonly IExpression[], + code: string, + args: IReadOnlyFunctionCallArgumentCollection): string { let compiledCode = ''; - expressions = expressions + const sortedExpressions = expressions .slice() // copy the array to not mutate the parameter .sort((a, b) => b.position.start - a.position.start); let index = 0; while (index !== code.length) { - const nextExpression = expressions.pop(); + const nextExpression = sortedExpressions.pop(); if (nextExpression) { compiledCode += code.substring(index, nextExpression.position.start); - const expressionCode = nextExpression.evaluate(parameters); + const expressionCode = nextExpression.evaluate(args); compiledCode += expressionCode; index = nextExpression.position.end; } else { @@ -35,15 +44,29 @@ function compileExpressions(expressions: IExpression[], code: string, parameters return compiledCode; } -function ensureRequiredArgsProvided(parameters: readonly string[], args: ParameterValueDictionary) { - parameters = parameters || []; - args = args || {}; - if (!parameters.length) { +function extractRequiredParameterNames( + expressions: readonly IExpression[]): string[] { + const usedParameterNames = expressions + .map((e) => e.parameters.all + .filter((p) => !p.isOptional) + .map((p) => p.name)) + .filter((p) => p) + .flat(); + const uniqueParameterNames = Array.from(new Set(usedParameterNames)); + return uniqueParameterNames; +} + +function ensureParamsUsedInCodeHasArgsProvided( + expressions: readonly IExpression[], + providedArgs: IReadOnlyFunctionCallArgumentCollection): void { + const usedParameterNames = extractRequiredParameterNames(expressions); + if (!usedParameterNames?.length) { return; } - const notProvidedParameters = parameters.filter((parameter) => !Boolean(args[parameter])); + const notProvidedParameters = usedParameterNames + .filter((parameterName) => !providedArgs.hasArgument(parameterName)); if (notProvidedParameters.length) { - throw new Error(`parameter value(s) not provided for: ${printList(notProvidedParameters)}`); + throw new Error(`parameter value(s) not provided for: ${printList(notProvidedParameters)} but used in code`); } } diff --git a/src/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler.ts b/src/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler.ts index ded7da64..c0c4632f 100644 --- a/src/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler.ts +++ b/src/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler.ts @@ -1,5 +1,7 @@ -export interface ParameterValueDictionary { [parameterName: string]: string; } +import { IReadOnlyFunctionCallArgumentCollection } from '../FunctionCall/Argument/IFunctionCallArgumentCollection'; export interface IExpressionsCompiler { - compileExpressions(code: string, parameters?: ParameterValueDictionary): string; + compileExpressions( + code: string, + args: IReadOnlyFunctionCallArgumentCollection): string; } diff --git a/src/application/Parser/Script/Compiler/Expressions/Parser/CompositeExpressionParser.ts b/src/application/Parser/Script/Compiler/Expressions/Parser/CompositeExpressionParser.ts index 928cfeae..6e41d646 100644 --- a/src/application/Parser/Script/Compiler/Expressions/Parser/CompositeExpressionParser.ts +++ b/src/application/Parser/Script/Compiler/Expressions/Parser/CompositeExpressionParser.ts @@ -2,13 +2,15 @@ import { IExpression } from '../Expression/IExpression'; import { IExpressionParser } from './IExpressionParser'; import { ParameterSubstitutionParser } from '../SyntaxParsers/ParameterSubstitutionParser'; -const parsers = [ +const Parsers = [ new ParameterSubstitutionParser(), ]; export class CompositeExpressionParser implements IExpressionParser { - public constructor(private readonly leafs: readonly IExpressionParser[] = parsers) { - if (leafs.some((leaf) => !leaf)) { throw new Error('undefined leaf'); } + public constructor(private readonly leafs: readonly IExpressionParser[] = Parsers) { + if (leafs.some((leaf) => !leaf)) { + throw new Error('undefined leaf'); + } } public findExpressions(code: string): IExpression[] { const expressions = new Array(); diff --git a/src/application/Parser/Script/Compiler/Expressions/Parser/RegexParser.ts b/src/application/Parser/Script/Compiler/Expressions/Parser/RegexParser.ts index 4a1389ca..165e718e 100644 --- a/src/application/Parser/Script/Compiler/Expressions/Parser/RegexParser.ts +++ b/src/application/Parser/Script/Compiler/Expressions/Parser/RegexParser.ts @@ -2,9 +2,12 @@ import { IExpressionParser } from './IExpressionParser'; import { ExpressionPosition } from '../Expression/ExpressionPosition'; import { IExpression } from '../Expression/IExpression'; import { Expression, ExpressionEvaluator } from '../Expression/Expression'; +import { IFunctionParameter } from '../../Function/Parameter/IFunctionParameter'; +import { FunctionParameterCollection } from '../../Function/Parameter/FunctionParameterCollection'; export abstract class RegexParser implements IExpressionParser { protected abstract readonly regex: RegExp; + public findExpressions(code: string): IExpression[] { return Array.from(this.findRegexExpressions(code)); } @@ -23,7 +26,8 @@ export abstract class RegexParser implements IExpressionParser { throw new Error(`[${this.constructor.name}] invalid script position: ${error.message}\nRegex ${this.regex}\nCode: ${code}`); } const primitiveExpression = this.buildExpression(match); - const expression = new Expression(position, primitiveExpression.evaluator, primitiveExpression.parameters); + const parameters = getParameters(primitiveExpression); + const expression = new Expression(position, primitiveExpression.evaluator, parameters); yield expression; } } @@ -31,5 +35,14 @@ export abstract class RegexParser implements IExpressionParser { export interface IPrimitiveExpression { evaluator: ExpressionEvaluator; - parameters?: readonly string[]; + parameters?: readonly IFunctionParameter[]; +} + +function getParameters( + expression: IPrimitiveExpression): FunctionParameterCollection { + const parameters = new FunctionParameterCollection(); + for (const parameter of expression.parameters || []) { + parameters.addParameter(parameter); + } + return parameters; } diff --git a/src/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.ts b/src/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.ts index 79945830..9c50a593 100644 --- a/src/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.ts +++ b/src/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.ts @@ -1,12 +1,13 @@ import { RegexParser, IPrimitiveExpression } from '../Parser/RegexParser'; +import { FunctionParameter } from '@/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter'; export class ParameterSubstitutionParser extends RegexParser { protected readonly regex = /{{\s*\$([^}| ]+)\s*}}/g; protected buildExpression(match: RegExpMatchArray): IPrimitiveExpression { const parameterName = match[1]; return { - parameters: [ parameterName ], - evaluator: (args) => args[parameterName], + parameters: [ new FunctionParameter(parameterName, false) ], + evaluator: (args) => args.getArgument(parameterName).argumentValue, }; } } diff --git a/src/application/Parser/Script/Compiler/Function/FunctionCompiler.ts b/src/application/Parser/Script/Compiler/Function/FunctionCompiler.ts index 96e6841e..fd4f3ef6 100644 --- a/src/application/Parser/Script/Compiler/Function/FunctionCompiler.ts +++ b/src/application/Parser/Script/Compiler/Function/FunctionCompiler.ts @@ -5,6 +5,9 @@ import { ISharedFunctionCollection } from './ISharedFunctionCollection'; import { IFunctionCompiler } from './IFunctionCompiler'; import { IFunctionCallCompiler } from '../FunctionCall/IFunctionCallCompiler'; import { FunctionCallCompiler } from '../FunctionCall/FunctionCallCompiler'; +import { FunctionParameter } from './Parameter/FunctionParameter'; +import { FunctionParameterCollection } from './Parameter/FunctionParameterCollection'; +import { IReadOnlyFunctionParameterCollection } from './Parameter/IFunctionParameterCollection'; export class FunctionCompiler implements IFunctionCompiler { public static readonly instance: IFunctionCompiler = new FunctionCompiler(); @@ -20,20 +23,39 @@ export class FunctionCompiler implements IFunctionCompiler { functions .filter((func) => hasCode(func)) .forEach((func) => { - const shared = new SharedFunction(func.name, func.parameters, func.code, func.revertCode); + const parameters = parseParameters(func); + const shared = new SharedFunction(func.name, parameters, func.code, func.revertCode); collection.addFunction(shared); }); functions .filter((func) => hasCall(func)) .forEach((func) => { + const parameters = parseParameters(func); const code = this.functionCallCompiler.compileCall(func.call, collection); - const shared = new SharedFunction(func.name, func.parameters, code.code, code.revertCode); + const shared = new SharedFunction(func.name, parameters, code.code, code.revertCode); collection.addFunction(shared); }); return collection; } } +function parseParameters(data: FunctionData): IReadOnlyFunctionParameterCollection { + const parameters = new FunctionParameterCollection(); + if (!data.parameters) { + return parameters; + } + for (const parameterData of data.parameters) { + const isOptional = parameterData.optional || false; + try { + const parameter = new FunctionParameter(parameterData.name, isOptional); + parameters.addParameter(parameter); + } catch (err) { + throw new Error(`"${data.name}": ${err.message}`); + } + } + return parameters; +} + function hasCode(data: FunctionData): boolean { return Boolean(data.code); } @@ -46,10 +68,9 @@ function hasCall(data: FunctionData): boolean { function ensureValidFunctions(functions: readonly FunctionData[]) { ensureNoUndefinedItem(functions); ensureNoDuplicatesInFunctionNames(functions); - ensureNoDuplicatesInParameterNames(functions); ensureNoDuplicateCode(functions); ensureEitherCallOrCodeIsDefined(functions); - ensureExpectedParameterNameTypes(functions); + ensureExpectedParametersType(functions); } function printList(list: readonly string[]): string { @@ -69,14 +90,18 @@ function ensureEitherCallOrCodeIsDefined(holders: readonly InstructionHolder[]) } } -function ensureExpectedParameterNameTypes(functions: readonly FunctionData[]) { - const unexpectedFunctions = functions.filter((func) => func.parameters && !isArrayOfStrings(func.parameters)); +function ensureExpectedParametersType(functions: readonly FunctionData[]) { + const unexpectedFunctions = functions + .filter((func) => func.parameters && !isArrayOfObjects(func.parameters)); if (unexpectedFunctions.length) { - throw new Error(`unexpected parameter name type in ${printNames(unexpectedFunctions)}`); + const errorMessage = `parameters must be an array of objects in function(s) ${printNames(unexpectedFunctions)}`; + throw new Error(errorMessage); } - function isArrayOfStrings(value: any): boolean { - return Array.isArray(value) && value.every((item) => typeof item === 'string'); - } +} + +function isArrayOfObjects(value: any): boolean { + return Array.isArray(value) + && value.every((item) => typeof item === 'object'); } function printNames(holders: readonly InstructionHolder[]) { @@ -90,21 +115,13 @@ function ensureNoDuplicatesInFunctionNames(functions: readonly FunctionData[]) { throw new Error(`duplicate function name: ${printList(duplicateFunctionNames)}`); } } + function ensureNoUndefinedItem(functions: readonly FunctionData[]) { if (functions.some((func) => !func)) { throw new Error(`some functions are undefined`); } } -function ensureNoDuplicatesInParameterNames(functions: readonly FunctionData[]) { - const functionsWithParameters = functions - .filter((func) => func.parameters && func.parameters.length > 0); - for (const func of functionsWithParameters) { - const duplicateParameterNames = getDuplicates(func.parameters); - if (duplicateParameterNames.length) { - throw new Error(`"${func.name}": duplicate parameter name: ${printList(duplicateParameterNames)}`); - } - } -} + function ensureNoDuplicateCode(functions: readonly FunctionData[]) { const duplicateCodes = getDuplicates(functions .map((func) => func.code) diff --git a/src/application/Parser/Script/Compiler/Function/ISharedFunction.ts b/src/application/Parser/Script/Compiler/Function/ISharedFunction.ts index 53c4adc0..f23da1f2 100644 --- a/src/application/Parser/Script/Compiler/Function/ISharedFunction.ts +++ b/src/application/Parser/Script/Compiler/Function/ISharedFunction.ts @@ -1,6 +1,8 @@ +import { IReadOnlyFunctionParameterCollection } from './Parameter/IFunctionParameterCollection'; + export interface ISharedFunction { readonly name: string; - readonly parameters?: readonly string[]; + readonly parameters: IReadOnlyFunctionParameterCollection; readonly code: string; readonly revertCode?: string; } diff --git a/src/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter.ts b/src/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter.ts new file mode 100644 index 00000000..f6f49da2 --- /dev/null +++ b/src/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter.ts @@ -0,0 +1,10 @@ +import { IFunctionParameter } from './IFunctionParameter'; +import { ensureValidParameterName } from '../../ParameterNameValidator'; + +export class FunctionParameter implements IFunctionParameter { + constructor( + public readonly name: string, + public readonly isOptional: boolean) { + ensureValidParameterName(name); + } +} diff --git a/src/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection.ts b/src/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection.ts new file mode 100644 index 00000000..7dcedca8 --- /dev/null +++ b/src/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection.ts @@ -0,0 +1,26 @@ +import { IFunctionParameterCollection } from './IFunctionParameterCollection'; +import { IFunctionParameter } from './IFunctionParameter'; + +export class FunctionParameterCollection implements IFunctionParameterCollection { + private parameters = new Array(); + + public get all(): readonly IFunctionParameter[] { + return this.parameters; + } + public addParameter(parameter: IFunctionParameter) { + this.ensureValidParameter(parameter); + this.parameters.push(parameter); + } + + private includesName(name: string) { + return this.parameters.find((existingParameter) => existingParameter.name === name); + } + private ensureValidParameter(parameter: IFunctionParameter) { + if (!parameter) { + throw new Error('undefined parameter'); + } + if (this.includesName(parameter.name)) { + throw new Error(`duplicate parameter name: "${parameter.name}"`); + } + } +} diff --git a/src/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameter.ts b/src/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameter.ts new file mode 100644 index 00000000..2f56a21e --- /dev/null +++ b/src/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameter.ts @@ -0,0 +1,4 @@ +export interface IFunctionParameter { + readonly name: string; + readonly isOptional: boolean; +} diff --git a/src/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection.ts b/src/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection.ts new file mode 100644 index 00000000..79437f18 --- /dev/null +++ b/src/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection.ts @@ -0,0 +1,9 @@ +import { IFunctionParameter } from './IFunctionParameter'; + +export interface IReadOnlyFunctionParameterCollection { + readonly all: readonly IFunctionParameter[]; +} + +export interface IFunctionParameterCollection extends IReadOnlyFunctionParameterCollection { + addParameter(parameter: IFunctionParameter): void; +} diff --git a/src/application/Parser/Script/Compiler/Function/SharedFunction.ts b/src/application/Parser/Script/Compiler/Function/SharedFunction.ts index e1401f73..33678ebb 100644 --- a/src/application/Parser/Script/Compiler/Function/SharedFunction.ts +++ b/src/application/Parser/Script/Compiler/Function/SharedFunction.ts @@ -1,15 +1,15 @@ import { ISharedFunction } from './ISharedFunction'; +import { IReadOnlyFunctionParameterCollection } from './Parameter/IFunctionParameterCollection'; export class SharedFunction implements ISharedFunction { - public readonly parameters: readonly string[]; constructor( public readonly name: string, - parameters: readonly string[], + public readonly parameters: IReadOnlyFunctionParameterCollection, public readonly code: string, - public readonly revertCode: string, + public readonly revertCode?: string, ) { if (!name) { throw new Error('undefined function name'); } if (!code) { throw new Error(`undefined function ("${name}") code`); } - this.parameters = parameters || []; + if (!parameters) { throw new Error(`undefined parameters`); } } } diff --git a/src/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument.ts b/src/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument.ts new file mode 100644 index 00000000..5bed91f8 --- /dev/null +++ b/src/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument.ts @@ -0,0 +1,13 @@ +import { IFunctionCallArgument } from './IFunctionCallArgument'; +import { ensureValidParameterName } from '../../ParameterNameValidator'; + +export class FunctionCallArgument implements IFunctionCallArgument { + constructor( + public readonly parameterName: string, + public readonly argumentValue: string) { + ensureValidParameterName(parameterName); + if (!argumentValue) { + throw new Error(`undefined argument value for "${parameterName}"`); + } + } +} diff --git a/src/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection.ts b/src/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection.ts new file mode 100644 index 00000000..56fbaa73 --- /dev/null +++ b/src/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection.ts @@ -0,0 +1,34 @@ +import { IFunctionCallArgument } from './IFunctionCallArgument'; +import { IFunctionCallArgumentCollection } from './IFunctionCallArgumentCollection'; + +export class FunctionCallArgumentCollection implements IFunctionCallArgumentCollection { + private readonly arguments = new Map(); + public addArgument(argument: IFunctionCallArgument): void { + if (!argument) { + throw new Error('undefined argument'); + } + if (this.hasArgument(argument.parameterName)) { + throw new Error(`argument value for parameter ${argument.parameterName} is already provided`); + } + this.arguments.set(argument.parameterName, argument); + } + public getAllParameterNames(): string[] { + return Array.from(this.arguments.keys()); + } + public hasArgument(parameterName: string): boolean { + if (!parameterName) { + throw new Error('undefined parameter name'); + } + return this.arguments.has(parameterName); + } + public getArgument(parameterName: string): IFunctionCallArgument { + if (!parameterName) { + throw new Error('undefined parameter name'); + } + const arg = this.arguments.get(parameterName); + if (!arg) { + throw new Error(`parameter does not exist: ${parameterName}`); + } + return arg; + } +} diff --git a/src/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgument.ts b/src/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgument.ts new file mode 100644 index 00000000..22d5004c --- /dev/null +++ b/src/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgument.ts @@ -0,0 +1,4 @@ +export interface IFunctionCallArgument { + readonly parameterName: string; + readonly argumentValue: string; +} diff --git a/src/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection.ts b/src/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection.ts new file mode 100644 index 00000000..a5ff8a3f --- /dev/null +++ b/src/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection.ts @@ -0,0 +1,11 @@ +import { IFunctionCallArgument } from './IFunctionCallArgument'; + +export interface IReadOnlyFunctionCallArgumentCollection { + getArgument(parameterName: string): IFunctionCallArgument; + getAllParameterNames(): string[]; + hasArgument(parameterName: string): boolean; +} + +export interface IFunctionCallArgumentCollection extends IReadOnlyFunctionCallArgumentCollection { + addArgument(argument: IFunctionCallArgument): void; +} diff --git a/src/application/Parser/Script/Compiler/FunctionCall/FunctionCall.ts b/src/application/Parser/Script/Compiler/FunctionCall/FunctionCall.ts new file mode 100644 index 00000000..e0d6c677 --- /dev/null +++ b/src/application/Parser/Script/Compiler/FunctionCall/FunctionCall.ts @@ -0,0 +1,15 @@ +import { IReadOnlyFunctionCallArgumentCollection } from './Argument/IFunctionCallArgumentCollection'; +import { IFunctionCall } from './IFunctionCall'; + +export class FunctionCall implements IFunctionCall { + constructor( + public readonly functionName: string, + public readonly args: IReadOnlyFunctionCallArgumentCollection) { + if (!functionName) { + throw new Error('empty function name in function call'); + } + if (!args) { + throw new Error('undefined args'); + } + } +} diff --git a/src/application/Parser/Script/Compiler/FunctionCall/FunctionCallCompiler.ts b/src/application/Parser/Script/Compiler/FunctionCall/FunctionCallCompiler.ts index 715323a4..2fd6a1c3 100644 --- a/src/application/Parser/Script/Compiler/FunctionCall/FunctionCallCompiler.ts +++ b/src/application/Parser/Script/Compiler/FunctionCall/FunctionCallCompiler.ts @@ -1,63 +1,63 @@ -import { FunctionCallData, FunctionCallParametersData, FunctionData, ScriptFunctionCallData } from 'js-yaml-loader!@/*'; +import { FunctionCallData, ScriptFunctionCallData } from 'js-yaml-loader!@/*'; import { ICompiledCode } from './ICompiledCode'; import { ISharedFunctionCollection } from '../Function/ISharedFunctionCollection'; import { IFunctionCallCompiler } from './IFunctionCallCompiler'; import { IExpressionsCompiler } from '../Expressions/IExpressionsCompiler'; import { ExpressionsCompiler } from '../Expressions/ExpressionsCompiler'; +import { ISharedFunction } from '../Function/ISharedFunction'; +import { IFunctionCall } from './IFunctionCall'; +import { FunctionCall } from './FunctionCall'; +import { FunctionCallArgument } from './Argument/FunctionCallArgument'; +import { IReadOnlyFunctionCallArgumentCollection } from './Argument/IFunctionCallArgumentCollection'; +import { FunctionCallArgumentCollection } from './Argument/FunctionCallArgumentCollection'; export class FunctionCallCompiler implements IFunctionCallCompiler { public static readonly instance: IFunctionCallCompiler = new FunctionCallCompiler(); + protected constructor( - private readonly expressionsCompiler: IExpressionsCompiler = new ExpressionsCompiler()) { } + private readonly expressionsCompiler: IExpressionsCompiler = new ExpressionsCompiler()) { + + } + public compileCall( call: ScriptFunctionCallData, functions: ISharedFunctionCollection): ICompiledCode { if (!functions) { throw new Error('undefined functions'); } if (!call) { throw new Error('undefined call'); } - const compiledCodes = new Array(); - const calls = getCallSequence(call); - calls.forEach((currentCall, currentCallIndex) => { - ensureValidCall(currentCall); - const commonFunction = functions.getFunctionByName(currentCall.function); - ensureExpectedParameters(commonFunction, currentCall); - let functionCode = compileCode(commonFunction, currentCall.parameters, this.expressionsCompiler); - if (currentCallIndex !== calls.length - 1) { - functionCode = appendLine(functionCode); - } - compiledCodes.push(functionCode); - }); - const compiledCode = merge(compiledCodes); - return compiledCode; + const compiledFunctions = new Array(); + const callSequence = getCallSequence(call); + for (const currentCall of callSequence) { + const functionCall = parseFunctionCall(currentCall); + const sharedFunction = functions.getFunctionByName(functionCall.functionName); + ensureThatCallArgumentsExistInParameterDefinition(sharedFunction, functionCall.args); + const compiledFunction = compileCode(sharedFunction, functionCall.args, this.expressionsCompiler); + compiledFunctions.push(compiledFunction); + } + return { + code: merge(compiledFunctions.map((f) => f.code)), + revertCode: merge(compiledFunctions.map((f) => f.revertCode)), + }; } } -function ensureExpectedParameters(func: FunctionData, call: FunctionCallData) { - const actual = Object.keys(call.parameters || {}); - const expected = func.parameters || []; - if (!actual.length && !expected.length) { - return; - } - const unexpectedParameters = actual.filter((callParam) => !expected.includes(callParam)); - if (unexpectedParameters.length) { - throw new Error( - `function "${func.name}" has unexpected parameter(s) provided: "${unexpectedParameters.join('", "')}"`); - } +function merge(codeParts: readonly string[]): string { + return codeParts + .filter((part) => part?.length > 0) + .join('\n'); } -function merge(codes: readonly ICompiledCode[]): ICompiledCode { - return { - code: codes.map((code) => code.code).join(''), - revertCode: codes.map((code) => code.revertCode).join(''), - }; +interface ICompiledFunction { + readonly code: string; + readonly revertCode: string; } function compileCode( - func: FunctionData, - parameters: FunctionCallParametersData, - compiler: IExpressionsCompiler): ICompiledCode { + func: ISharedFunction, + args: IReadOnlyFunctionCallArgumentCollection, + compiler: IExpressionsCompiler): ICompiledFunction { return { - code: compiler.compileExpressions(func.code, parameters), - revertCode: compiler.compileExpressions(func.revertCode, parameters), + code: compiler.compileExpressions(func.code, args), + revertCode: compiler.compileExpressions(func.revertCode, args), }; } @@ -71,19 +71,31 @@ function getCallSequence(call: ScriptFunctionCallData): FunctionCallData[] { return [ call as FunctionCallData ]; } -function ensureValidCall(call: FunctionCallData) { +function parseFunctionCall(call: FunctionCallData): IFunctionCall { if (!call) { throw new Error(`undefined function call`); } - if (!call.function) { - throw new Error(`empty function name called`); + const args = new FunctionCallArgumentCollection(); + for (const parameterName of Object.keys(call.parameters || {})) { + const arg = new FunctionCallArgument(parameterName, call.parameters[parameterName]); + args.addArgument(arg); } + return new FunctionCall(call.function, args); } -function appendLine(code: ICompiledCode): ICompiledCode { - const appendLineIfNotEmpty = (str: string) => str ? `${str}\n` : str; - return { - code: appendLineIfNotEmpty(code.code), - revertCode: appendLineIfNotEmpty(code.revertCode), - }; +function ensureThatCallArgumentsExistInParameterDefinition( + func: ISharedFunction, + args: IReadOnlyFunctionCallArgumentCollection): void { + const callArgumentNames = args.getAllParameterNames(); + const functionParameterNames = func.parameters.all.map((param) => param.name) || []; + if (!callArgumentNames.length && !functionParameterNames.length) { + return; + } + const parametersOutsideFunction = callArgumentNames + .filter((callParam) => !functionParameterNames.includes(callParam)); + if (parametersOutsideFunction.length) { + throw new Error( + `function "${func.name}" has unexpected parameter(s) provided:` + + `"${parametersOutsideFunction.join('", "')}"`); + } } diff --git a/src/application/Parser/Script/Compiler/FunctionCall/IFunctionCall.ts b/src/application/Parser/Script/Compiler/FunctionCall/IFunctionCall.ts new file mode 100644 index 00000000..beda83ba --- /dev/null +++ b/src/application/Parser/Script/Compiler/FunctionCall/IFunctionCall.ts @@ -0,0 +1,6 @@ +import { IReadOnlyFunctionCallArgumentCollection } from './Argument/IFunctionCallArgumentCollection'; + +export interface IFunctionCall { + readonly functionName: string; + readonly args: IReadOnlyFunctionCallArgumentCollection; +} diff --git a/src/application/Parser/Script/Compiler/ParameterNameValidator.ts b/src/application/Parser/Script/Compiler/ParameterNameValidator.ts new file mode 100644 index 00000000..7fabdc06 --- /dev/null +++ b/src/application/Parser/Script/Compiler/ParameterNameValidator.ts @@ -0,0 +1,8 @@ +export function ensureValidParameterName(parameterName: string) { + if (!parameterName) { + throw new Error('undefined parameter name'); + } + if (!parameterName.match(/^[0-9a-zA-Z]+$/)) { + throw new Error(`parameter name must be alphanumeric but it was "${parameterName}"`); + } +} diff --git a/src/application/Parser/ScriptingDefinition/CodeSubstituter.ts b/src/application/Parser/ScriptingDefinition/CodeSubstituter.ts index b3665df7..396a9c30 100644 --- a/src/application/Parser/ScriptingDefinition/CodeSubstituter.ts +++ b/src/application/Parser/ScriptingDefinition/CodeSubstituter.ts @@ -1,9 +1,11 @@ -import { IExpressionsCompiler, ParameterValueDictionary } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler'; +import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler'; import { ParameterSubstitutionParser } from '@/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser'; import { CompositeExpressionParser } from '@/application/Parser/Script/Compiler/Expressions/Parser/CompositeExpressionParser'; import { ExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler'; import { IProjectInformation } from '@/domain/IProjectInformation'; import { ICodeSubstituter } from './ICodeSubstituter'; +import { FunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection'; +import { FunctionCallArgument } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument'; export class CodeSubstituter implements ICodeSubstituter { constructor( @@ -15,12 +17,13 @@ export class CodeSubstituter implements ICodeSubstituter { public substitute(code: string, info: IProjectInformation): string { if (!code) { throw new Error('undefined code'); } if (!info) { throw new Error('undefined info'); } - const parameters: ParameterValueDictionary = { - homepage: info.homepage, - version: info.version, - date: this.date.toUTCString(), - }; - const compiledCode = this.compiler.compileExpressions(code, parameters); + const args = new FunctionCallArgumentCollection(); + const substitute = (name: string, value: string) => + args.addArgument(new FunctionCallArgument(name, value)); + substitute('homepage', info.homepage); + substitute('version', info.version); + substitute('date', this.date.toUTCString()); + const compiledCode = this.compiler.compileExpressions(code, args); return compiledCode; } } diff --git a/src/application/collections/collection.yaml.d.ts b/src/application/collections/collection.yaml.d.ts index 020bb876..f505173b 100644 --- a/src/application/collections/collection.yaml.d.ts +++ b/src/application/collections/collection.yaml.d.ts @@ -27,8 +27,13 @@ declare module 'js-yaml-loader!@/*' { readonly call?: ScriptFunctionCallData; } + export interface ParameterDefinitionData { + readonly name: string; + readonly optional?: boolean; + } + export interface FunctionData extends InstructionHolder { - readonly parameters?: readonly string[]; + readonly parameters?: readonly ParameterDefinitionData[]; } export interface FunctionCallParametersData { diff --git a/src/application/collections/macos.yaml b/src/application/collections/macos.yaml index fb136321..083be1c6 100644 --- a/src/application/collections/macos.yaml +++ b/src/application/collections/macos.yaml @@ -1,4 +1,4 @@ -# Structure documented in "docs/collections.md" +# Structure documented in "docs/collection-files.md" os: macos scripting: language: shellscript @@ -532,7 +532,8 @@ actions: functions: - name: PersistUserEnvironmentConfiguration - parameters: [ configuration ] + parameters: + - name: configuration code: |- command='{{ $configuration }}' declare -a profile_files=("$HOME/.bash_profile" "$HOME/.zprofile") diff --git a/src/application/collections/windows.yaml b/src/application/collections/windows.yaml index a289180e..a9d97f3f 100644 --- a/src/application/collections/windows.yaml +++ b/src/application/collections/windows.yaml @@ -1,4 +1,4 @@ -# Structure documented in "docs/collections.md" +# Structure documented in "docs/collection-files.md" os: windows scripting: language: batchfile @@ -4387,18 +4387,21 @@ actions: functions: - name: KillProcessWhenItStarts - parameters: [ processName ] + parameters: + - name: processName # https://docs.microsoft.com/en-us/previous-versions/windows/desktop/xperf/image-file-execution-options code: reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\'{{ $processName }}'" /v "Debugger" /t REG_SZ /d "%windir%\System32\taskkill.exe" /f revertCode: reg delete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\'{{ $processName }}'" /v "Debugger" /f - name: DisableFeature - parameters: [ featureName ] + parameters: + - name: featureName code: dism /Online /Disable-Feature /FeatureName:"{{ $featureName }}" /NoRestart revertCode: dism /Online /Enable-Feature /FeatureName:"{{ $featureName }}" /NoRestart - name: UninstallStoreApp - parameters: [ packageName ] + parameters: + - name: packageName call: function: RunPowerShell parameters: @@ -4412,7 +4415,8 @@ functions: Add-AppxPackage -DisableDevelopmentMode -Register \"$manifest\" - name: UninstallSystemApp - parameters: [ packageName ] + parameters: + - name: packageName # It simply renames files # Because system apps are non removable (check: (Get-AppxPackage -AllUsers 'Windows.CBSPreview').NonRemovable) # Otherwise they throw 0x80070032 when trying to uninstall them @@ -4457,7 +4461,8 @@ functions: } - name: UninstallCapability - parameters: [ capabilityName ] + parameters: + - name: capabilityName call: function: RunPowerShell parameters: @@ -4467,7 +4472,8 @@ functions: Add-WindowsCapability -Name \"$capability.Name\" -Online - name: RenameSystemFile - parameters: [ filePath ] + parameters: + - name: filePath code: |- if exist "{{ $filePath }}" ( takeown /f "{{ $filePath }}" @@ -4488,7 +4494,9 @@ functions: ) - name: SetVsCodeSetting - parameters: [ setting, powerShellValue ] + parameters: + - name: setting + - name: powerShellValue call: function: RunPowerShell parameters: @@ -4511,6 +4519,8 @@ functions: $json | ConvertTo-Json | Set-Content $jsonfile; - name: RunPowerShell - parameters: [ code, revertCode ] + parameters: + - name: code + - name: revertCode code: PowerShell -ExecutionPolicy Unrestricted -Command "{{ $code }}" revertCode: PowerShell -ExecutionPolicy Unrestricted -Command "{{ $revertCode }}" diff --git a/tests/unit/application/Parser/Script/Compiler/Expressions/Expression/Expression.spec.ts b/tests/unit/application/Parser/Script/Compiler/Expressions/Expression/Expression.spec.ts index 2b26bfce..9800566b 100644 --- a/tests/unit/application/Parser/Script/Compiler/Expressions/Expression/Expression.spec.ts +++ b/tests/unit/application/Parser/Script/Compiler/Expressions/Expression/Expression.spec.ts @@ -3,7 +3,11 @@ import { expect } from 'chai'; import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition'; import { ExpressionEvaluator } from '@/application/Parser/Script/Compiler/Expressions/Expression/Expression'; import { Expression } from '@/application/Parser/Script/Compiler/Expressions/Expression/Expression'; -import { ExpressionArguments } from '@/application/Parser/Script/Compiler/Expressions/Expression/IExpression'; +import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection'; +import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection'; +import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub'; +import { FunctionParameterCollectionStub } from '@tests/unit/stubs/FunctionParameterCollectionStub'; +import { FunctionCallArgumentStub } from '@tests/unit/stubs/FunctionCallArgumentStub'; describe('Expression', () => { describe('ctor', () => { @@ -39,11 +43,15 @@ describe('Expression', () => { .withParameters(parameters) .build(); // assert - expect(actual.parameters).to.have.lengthOf(0); + expect(actual.parameters); + expect(actual.parameters.all); + expect(actual.parameters.all.length).to.equal(0); }); it('sets as expected', () => { // arrange - const expected = [ 'firstParameterName', 'secondParameterName' ]; + const expected = new FunctionParameterCollectionStub() + .withParameterName('firstParameterName') + .withParameterName('secondParameterName'); // act const actual = new ExpressionBuilder() .withParameters(expected) @@ -67,52 +75,119 @@ describe('Expression', () => { }); }); describe('evaluate', () => { + describe('throws with invalid arguments', () => { + const testCases = [ + { + name: 'throws if arguments is undefined', + args: undefined, + expectedError: 'undefined args, send empty collection instead', + }, + { + name: 'throws when some of the required args are not provided', + sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b', 'c'], false), + args: new FunctionCallArgumentCollectionStub().withArgument('b', 'provided'), + expectedError: 'argument values are provided for required parameters: "a", "c"', + }, + { + name: 'throws when none of the required args are not provided', + sut: (i: ExpressionBuilder) => i.withParameterNames(['a', 'b'], false), + args: new FunctionCallArgumentCollectionStub().withArgument('c', 'unrelated'), + expectedError: 'argument values are provided for required parameters: "a", "b"', + }, + ]; + for (const testCase of testCases) { + it(testCase.name, () => { + // arrange + const sutBuilder = new ExpressionBuilder(); + if (testCase.sut) { + testCase.sut(sutBuilder); + } + const sut = sutBuilder.build(); + // act + const act = () => sut.evaluate(testCase.args); + // assert + expect(act).to.throw(testCase.expectedError); + }); + } + }); it('returns result from evaluator', () => { // arrange - const evaluatorMock: ExpressionEvaluator = (args) => JSON.stringify(args); - const givenArguments = { parameter1: 'value1', parameter2: 'value2' }; + const evaluatorMock: ExpressionEvaluator = (args) => + `"${args + .getAllParameterNames() + .map((name) => args.getArgument(name)) + .map((arg) => `${arg.parameterName}': '${arg.argumentValue}'`) + .join('", "')}"`; + const givenArguments = new FunctionCallArgumentCollectionStub() + .withArgument('parameter1', 'value1') + .withArgument('parameter2', 'value2'); + const expectedParameterNames = givenArguments.getAllParameterNames(); const expected = evaluatorMock(givenArguments); const sut = new ExpressionBuilder() .withEvaluator(evaluatorMock) - .withParameters(Object.keys(givenArguments)) + .withParameterNames(expectedParameterNames) .build(); // arrange const actual = sut.evaluate(givenArguments); // assert - expect(expected).to.equal(actual); + expect(expected).to.equal(actual, + `\nGiven arguments: ${JSON.stringify(givenArguments)}\n` + + `\nExpected parameter names: ${JSON.stringify(expectedParameterNames)}\n`, + ); }); - it('filters unused arguments', () => { + describe('filters unused parameters', () => { // arrange - let actual: ExpressionArguments = {}; - const evaluatorMock: ExpressionEvaluator = (providedArgs) => { - Object.keys(providedArgs) - .forEach((name) => actual = {...actual, [name]: providedArgs[name] }); - return ''; - }; - const parameterNameToHave = 'parameterToHave'; - const parameterNameToIgnore = 'parameterToIgnore'; - const sut = new ExpressionBuilder() - .withEvaluator(evaluatorMock) - .withParameters([ parameterNameToHave ]) - .build(); - const args: ExpressionArguments = { - [parameterNameToHave]: 'value-to-have', - [parameterNameToIgnore]: 'value-to-ignore', - }; - const expected: ExpressionArguments = { - [parameterNameToHave]: args[parameterNameToHave], - }; - // arrange - sut.evaluate(args); - // assert - expect(expected).to.deep.equal(actual); + const testCases = [ + { + name: 'with a provided argument', + expressionParameters: new FunctionParameterCollectionStub() + .withParameterName('parameterToHave', false), + arguments: new FunctionCallArgumentCollectionStub() + .withArgument('parameterToHave', 'value-to-have') + .withArgument('parameterToIgnore', 'value-to-ignore'), + expectedArguments: [ + new FunctionCallArgumentStub() + .withParameterName('parameterToHave').withArgumentValue('value-to-have'), + ], + }, + { + name: 'without a provided argument', + expressionParameters: new FunctionParameterCollectionStub() + .withParameterName('parameterToHave', false) + .withParameterName('parameterToIgnore', true), + arguments: new FunctionCallArgumentCollectionStub() + .withArgument('parameterToHave', 'value-to-have'), + expectedArguments: [ + new FunctionCallArgumentStub() + .withParameterName('parameterToHave').withArgumentValue('value-to-have'), + ], + }, + ]; + for (const testCase of testCases) { + it(testCase.name, () => { + let actual: IReadOnlyFunctionCallArgumentCollection; + const evaluatorMock: ExpressionEvaluator = (providedArgs) => { + actual = providedArgs; + return ''; + }; + const sut = new ExpressionBuilder() + .withEvaluator(evaluatorMock) + .withParameters(testCase.expressionParameters) + .build(); + // act + sut.evaluate(testCase.arguments); + // assert + const actualArguments = actual.getAllParameterNames().map((name) => actual.getArgument(name)); + expect(actualArguments).to.deep.equal(testCase.expectedArguments); + }); + } }); }); }); class ExpressionBuilder { private position: ExpressionPosition = new ExpressionPosition(0, 5); - private parameters: readonly string[] = new Array(); + private parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub(); public withPosition(position: ExpressionPosition) { this.position = position; @@ -122,10 +197,20 @@ class ExpressionBuilder { this.evaluator = evaluator; return this; } - public withParameters(parameters: string[]) { + public withParameters(parameters: IReadOnlyFunctionParameterCollection) { this.parameters = parameters; return this; } + public withParameterName(parameterName: string, isOptional: boolean = true) { + const collection = new FunctionParameterCollectionStub() + .withParameterName(parameterName, isOptional); + return this.withParameters(collection); + } + public withParameterNames(parameterNames: string[], isOptional: boolean = true) { + const collection = new FunctionParameterCollectionStub() + .withParameterNames(parameterNames, isOptional); + return this.withParameters(collection); + } public build() { return new Expression(this.position, this.evaluator, this.parameters); } diff --git a/tests/unit/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler.spec.ts b/tests/unit/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler.spec.ts index 77410888..cf75c414 100644 --- a/tests/unit/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler.spec.ts +++ b/tests/unit/application/Parser/Script/Compiler/Expressions/ExpressionsCompiler.spec.ts @@ -4,6 +4,7 @@ import { ExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expres import { IExpressionParser } from '@/application/Parser/Script/Compiler/Expressions/Parser/IExpressionParser'; import { ExpressionStub } from '@tests/unit/stubs/ExpressionStub'; import { ExpressionParserStub } from '@tests/unit/stubs/ExpressionParserStub'; +import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub'; describe('ExpressionsCompiler', () => { describe('compileExpressions', () => { @@ -22,8 +23,18 @@ describe('ExpressionsCompiler', () => { { name: 'unordered expressions', expressions: [ - new ExpressionStub().withPosition(6, 13).withEvaluatedResult('a'), new ExpressionStub().withPosition(20, 27).withEvaluatedResult('b'), + new ExpressionStub().withPosition(6, 13).withEvaluatedResult('a'), + ], + expected: 'part1 a part2 b part3', + }, + { + name: 'with an optional expected argument that is not provided', + expressions: [ + new ExpressionStub().withPosition(6, 13).withEvaluatedResult('a') + .withParameterNames(['optionalParameter'], true), + new ExpressionStub().withPosition(20, 27).withEvaluatedResult('b') + .withParameterNames(['optionalParameterTwo'], true), ], expected: 'part1 a part2 b part3', }, @@ -37,97 +48,89 @@ describe('ExpressionsCompiler', () => { it(testCase.name, () => { const expressionParserMock = new ExpressionParserStub() .withResult(testCase.expressions); + const args = new FunctionCallArgumentCollectionStub(); const sut = new MockableExpressionsCompiler(expressionParserMock); // act - const actual = sut.compileExpressions(code); + const actual = sut.compileExpressions(code, args); // assert expect(actual).to.equal(testCase.expected); }); } }); - it('passes arguments to expressions as expected', () => { - // arrange - const expected = { - parameter1: 'value1', - parameter2: 'value2', - }; - const code = 'non-important'; - const expressions = [ - new ExpressionStub(), - new ExpressionStub(), - ]; - const expressionParserMock = new ExpressionParserStub() - .withResult(expressions); - const sut = new MockableExpressionsCompiler(expressionParserMock); - // act - sut.compileExpressions(code, expected); - // assert - expect(expressions[0].callHistory).to.have.lengthOf(1); - expect(expressions[0].callHistory[0]).to.equal(expected); - expect(expressions[1].callHistory).to.have.lengthOf(1); - expect(expressions[1].callHistory[0]).to.equal(expected); + describe('arguments', () => { + it('passes arguments to expressions as expected', () => { + // arrange + const expected = new FunctionCallArgumentCollectionStub() + .withArgument('test-arg', 'test-value'); + const code = 'non-important'; + const expressions = [ + new ExpressionStub(), + new ExpressionStub(), + ]; + const expressionParserMock = new ExpressionParserStub() + .withResult(expressions); + const sut = new MockableExpressionsCompiler(expressionParserMock); + // act + sut.compileExpressions(code, expected); + // assert + expect(expressions[0].callHistory).to.have.lengthOf(1); + expect(expressions[0].callHistory[0]).to.equal(expected); + expect(expressions[1].callHistory).to.have.lengthOf(1); + expect(expressions[1].callHistory[0]).to.equal(expected); + }); + it('throws if arguments is undefined', () => { + // arrange + const expectedError = 'undefined args, send empty collection instead'; + const args = undefined; + const expressionParserMock = new ExpressionParserStub(); + const sut = new MockableExpressionsCompiler(expressionParserMock); + // act + const act = () => sut.compileExpressions('code', args); + // assert + expect(act).to.throw(expectedError); + }); }); - describe('throws when expected argument is not provided', () => { + describe('throws when expected argument is not provided but used in code', () => { // arrange - const noParameterTestCases = [ + const testCases = [ { name: 'empty parameters', expressions: [ - new ExpressionStub().withParameters('parameter'), + new ExpressionStub().withParameterNames(['parameter'], false), ], - args: {}, - expectedError: 'parameter value(s) not provided for: "parameter"', + args: new FunctionCallArgumentCollectionStub(), + expectedError: 'parameter value(s) not provided for: "parameter" but used in code', }, { - name: 'undefined parameters', + name: 'unnecessary parameter is provided', expressions: [ - new ExpressionStub().withParameters('parameter'), + new ExpressionStub().withParameterNames(['parameter'], false), ], - args: undefined, - expectedError: 'parameter value(s) not provided for: "parameter"', - }, - { - name: 'unnecessary parameter provided', - expressions: [ - new ExpressionStub().withParameters('parameter'), - ], - args: { - unnecessaryParameter: 'unnecessaryValue', - }, - expectedError: 'parameter value(s) not provided for: "parameter"', - }, - { - name: 'undefined value', - expressions: [ - new ExpressionStub().withParameters('parameter'), - ], - args: { - parameter: undefined, - }, - expectedError: 'parameter value(s) not provided for: "parameter"', + args: new FunctionCallArgumentCollectionStub() + .withArgument('unnecessaryParameter', 'unnecessaryValue'), + expectedError: 'parameter value(s) not provided for: "parameter" but used in code', }, { name: 'multiple values are not provided', expressions: [ - new ExpressionStub().withParameters('parameter1'), - new ExpressionStub().withParameters('parameter2', 'parameter3'), + new ExpressionStub().withParameterNames(['parameter1'], false), + new ExpressionStub().withParameterNames(['parameter2', 'parameter3'], false), ], - args: {}, - expectedError: 'parameter value(s) not provided for: "parameter1", "parameter2", "parameter3"', + args: new FunctionCallArgumentCollectionStub(), + expectedError: 'parameter value(s) not provided for: "parameter1", "parameter2", "parameter3" but used in code', }, { name: 'some values are provided', expressions: [ - new ExpressionStub().withParameters('parameter1'), - new ExpressionStub().withParameters('parameter2', 'parameter3'), + new ExpressionStub().withParameterNames(['parameter1'], false), + new ExpressionStub().withParameterNames(['parameter2', 'parameter3'], false), ], - args: { - parameter2: 'value', - }, - expectedError: 'parameter value(s) not provided for: "parameter1", "parameter3"', + args: new FunctionCallArgumentCollectionStub() + .withArgument('parameter2', 'value'), + expectedError: 'parameter value(s) not provided for: "parameter1", "parameter3" but used in code', }, ]; - for (const testCase of noParameterTestCases) { + for (const testCase of testCases) { it(testCase.name, () => { const code = 'non-important-code'; const expressionParserMock = new ExpressionParserStub() @@ -145,8 +148,9 @@ describe('ExpressionsCompiler', () => { const expected = 'expected-code'; const expressionParserMock = new ExpressionParserStub(); const sut = new MockableExpressionsCompiler(expressionParserMock); + const args = new FunctionCallArgumentCollectionStub(); // act - sut.compileExpressions(expected); + sut.compileExpressions(expected, args); // assert expect(expressionParserMock.callHistory).to.have.lengthOf(1); expect(expressionParserMock.callHistory[0]).to.equal(expected); diff --git a/tests/unit/application/Parser/Script/Compiler/Expressions/Parser/RegexParser.spec.ts b/tests/unit/application/Parser/Script/Compiler/Expressions/Parser/RegexParser.spec.ts index 1793f7d9..816415f8 100644 --- a/tests/unit/application/Parser/Script/Compiler/Expressions/Parser/RegexParser.spec.ts +++ b/tests/unit/application/Parser/Script/Compiler/Expressions/Parser/RegexParser.spec.ts @@ -3,6 +3,7 @@ import { expect } from 'chai'; import { ExpressionEvaluator } from '@/application/Parser/Script/Compiler/Expressions/Expression/Expression'; import { IPrimitiveExpression, RegexParser } from '@/application/Parser/Script/Compiler/Expressions/Parser/RegexParser'; import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition'; +import { FunctionParameterStub } from '@tests/unit/stubs/FunctionParameterStub'; describe('RegexParser', () => { describe('findExpressions', () => { @@ -59,7 +60,10 @@ describe('RegexParser', () => { }); it('sets parameters as expected', () => { // arrange - const expected = [ 'parameter1', 'parameter2' ]; + const expected = [ + new FunctionParameterStub().withName('parameter1').withOptionality(true), + new FunctionParameterStub().withName('parameter2').withOptionality(false), + ]; const regex = /hello/g; const code = 'hello'; const builder = (): IPrimitiveExpression => ({ @@ -71,7 +75,7 @@ describe('RegexParser', () => { const expressions = sut.findExpressions(code); // assert expect(expressions).to.have.lengthOf(1); - expect(expressions[0].parameters).to.equal(expected); + expect(expressions[0].parameters.all).to.deep.equal(expected); }); it('sets expected position', () => { // arrange diff --git a/tests/unit/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.spec.ts b/tests/unit/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.spec.ts index 70863cf1..faabcd92 100644 --- a/tests/unit/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.spec.ts +++ b/tests/unit/application/Parser/Script/Compiler/Expressions/SyntaxParsers/ParameterSubstitutionParser.spec.ts @@ -2,7 +2,7 @@ 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 { ExpressionArguments } from '@/application/Parser/Script/Compiler/Expressions/Expression/IExpression'; +import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub'; describe('ParameterSubstitutionParser', () => { describe('finds at expected positions', () => { @@ -44,36 +44,25 @@ describe('ParameterSubstitutionParser', () => { const testCases = [ { name: 'single parameter', code: '{{ $parameter }}', - args: [ { - name: 'parameter', - value: 'Hello world', - }], + args: new FunctionCallArgumentCollectionStub() + .withArgument('parameter', 'Hello world'), expected: [ 'Hello world' ], }, { name: 'different parameters', code: '{{ $firstParameter }} {{ $secondParameter }}!', - args: [ { - name: 'firstParameter', - value: 'Hello', - }, - { - name: 'secondParameter', - value: 'World', - }], + args: new FunctionCallArgumentCollectionStub() + .withArgument('firstParameter', 'Hello') + .withArgument('secondParameter', 'World'), expected: [ 'Hello', 'World' ], }]; for (const testCase of testCases) { it(testCase.name, () => { const sut = new ParameterSubstitutionParser(); - let args: ExpressionArguments = {}; - for (const arg of testCase.args) { - args = {...args, [arg.name]: arg.value }; - } // act const expressions = sut.findExpressions(testCase.code); // assert - const actual = expressions.map((e) => e.evaluate(args)); + const actual = expressions.map((e) => e.evaluate(testCase.args)); expect(actual).to.deep.equal(testCase.expected); }); } diff --git a/tests/unit/application/Parser/Script/Compiler/Function/FunctionCompiler.spec.ts b/tests/unit/application/Parser/Script/Compiler/Function/FunctionCompiler.spec.ts index 4aa6170c..05476861 100644 --- a/tests/unit/application/Parser/Script/Compiler/Function/FunctionCompiler.spec.ts +++ b/tests/unit/application/Parser/Script/Compiler/Function/FunctionCompiler.spec.ts @@ -1,11 +1,14 @@ import 'mocha'; import { expect } from 'chai'; import { ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction'; -import { FunctionData } from 'js-yaml-loader!@/*'; +import { FunctionData, ParameterDefinitionData } from 'js-yaml-loader!@/*'; import { IFunctionCallCompiler } from '@/application/Parser/Script/Compiler/FunctionCall/IFunctionCallCompiler'; import { FunctionCompiler } from '@/application/Parser/Script/Compiler/Function/FunctionCompiler'; +import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection'; import { FunctionCallCompilerStub } from '@tests/unit/stubs/FunctionCallCompilerStub'; import { FunctionDataStub } from '@tests/unit/stubs/FunctionDataStub'; +import { ParameterDefinitionDataStub } from '@tests/unit/stubs/ParameterDefinitionDataStub'; +import { FunctionParameter } from '@/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter'; describe('FunctionsCompiler', () => { describe('compileFunctions', () => { @@ -34,29 +37,31 @@ describe('FunctionsCompiler', () => { // assert expect(act).to.throw(expectedError); }); - it('throws when function parameters have same names', () => { - // arrange - const parameterName = 'duplicate-parameter'; - const func = FunctionDataStub.createWithCall() - .withParameters(parameterName, parameterName); - const expectedError = `"${func.name}": duplicate parameter name: "${parameterName}"`; - const sut = new MockableFunctionCompiler(); - // act - const act = () => sut.compileFunctions([ func ]); - // assert - expect(act).to.throw(expectedError); - }); - it('throws when parameters is not an array of strings', () => { - // arrange - const parameterNameWithUnexpectedType = 5; - const func = FunctionDataStub.createWithCall() - .withParameters(parameterNameWithUnexpectedType as any); - const expectedError = `unexpected parameter name type in "${func.name}"`; - const sut = new MockableFunctionCompiler(); - // act - const act = () => sut.compileFunctions([ func ]); - // assert - expect(act).to.throw(expectedError); + describe('throws when parameters type is not as expected', () => { + const testCases = [ + { + state: 'when not an array', + invalidType: 5, + }, + { + state: 'when array but not of objects', + invalidType: [ 'a', { a: 'b'} ], + }, + ]; + for (const testCase of testCases) { + it(testCase.state, () => { + // arrange + const func = FunctionDataStub + .createWithCall() + .withParametersObject(testCase.invalidType as any); + const expectedError = `parameters must be an array of objects in function(s) "${func.name}"`; + const sut = new MockableFunctionCompiler(); + // act + const act = () => sut.compileFunctions([ func ]); + // assert + expect(act).to.throw(expectedError); + }); + } }); describe('throws when when function have duplicate code', () => { it('code', () => { @@ -116,6 +121,37 @@ describe('FunctionsCompiler', () => { // assert expect(act).to.throw(expectedError); }); + it('rethrows including function name when FunctionParameter throws', () => { + // arrange + const functionName = 'invalid-function'; + const expectedError = `neither "code" or "call" is defined in "${functionName}"`; + const invalidFunction = FunctionDataStub.createWithoutCallOrCodes() + .withName(functionName); + const sut = new MockableFunctionCompiler(); + // act + const act = () => sut.compileFunctions([ invalidFunction ]); + // assert + expect(act).to.throw(expectedError); + }); + it('rethrows including function name when FunctionParameter throws', () => { + // arrange + const invalidParameterName = 'invalid function p@r4meter name'; + const functionName = 'functionName'; + let parameterException: Error; + // tslint:disable-next-line:no-unused-expression + try { new FunctionParameter(invalidParameterName, false); } catch (e) { parameterException = e; } + const expectedError = `"${functionName}": ${parameterException.message}`; + const functionData = FunctionDataStub.createWithCode() + .withName(functionName) + .withParameters(new ParameterDefinitionDataStub().withName(invalidParameterName)); + + // act + const sut = new MockableFunctionCompiler(); + const act = () => sut.compileFunctions([ functionData ]); + + // assert + expect(act).to.throw(expectedError); + }); }); it('returns empty with empty functions', () => { // arrange @@ -136,7 +172,10 @@ describe('FunctionsCompiler', () => { .withName(name) .withCode('expected-code') .withRevertCode('expected-revert-code') - .withParameters('expected-parameter-1', 'expected-parameter-2'); + .withParameters( + new ParameterDefinitionDataStub().withName('expectedParameter').withOptionality(true), + new ParameterDefinitionDataStub().withName('expectedParameter2').withOptionality(false), + ); const sut = new MockableFunctionCompiler(); // act const collection = sut.compileFunctions([ expected ]); @@ -188,7 +227,7 @@ describe('FunctionsCompiler', () => { function expectEqualFunctions(expected: FunctionData, actual: ISharedFunction) { expect(actual.name).to.equal(expected.name); - expect(actual.parameters).to.deep.equal(expected.parameters); + expect(areScrambledEqual(actual.parameters, expected.parameters)); expectEqualFunctionCode(expected, actual); } @@ -197,6 +236,23 @@ function expectEqualFunctionCode(expected: FunctionData, actual: ISharedFunction expect(actual.revertCode).to.equal(expected.revertCode); } +function areScrambledEqual( + expected: IReadOnlyFunctionParameterCollection, + actual: readonly ParameterDefinitionData[], +) { + if (expected.all.length !== actual.length) { + return false; + } + for (const expectedParameter of expected.all) { + if (!actual.some( + (a) => a.name === expectedParameter.name + && (a.optional || false) === expectedParameter.isOptional)) { + return false; + } + } + return true; +} + class MockableFunctionCompiler extends FunctionCompiler { constructor(functionCallCompiler: IFunctionCallCompiler = new FunctionCallCompilerStub()) { super(functionCallCompiler); diff --git a/tests/unit/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter.spec.ts b/tests/unit/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter.spec.ts new file mode 100644 index 00000000..0ed0f8a0 --- /dev/null +++ b/tests/unit/application/Parser/Script/Compiler/Function/Parameter/FunctionParameter.spec.ts @@ -0,0 +1,47 @@ +import 'mocha'; +import { expect } from 'chai'; +import { FunctionParameter } from '@/application/Parser/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); + } +} diff --git a/tests/unit/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection.spec.ts b/tests/unit/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection.spec.ts new file mode 100644 index 00000000..9ede92a9 --- /dev/null +++ b/tests/unit/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection.spec.ts @@ -0,0 +1,47 @@ +import 'mocha'; +import { expect } from 'chai'; +import { FunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/FunctionParameterCollection'; +import { FunctionParameterStub } from '@tests/unit/stubs/FunctionParameterStub'; + +describe('FunctionParameterCollection', () => { + it('all returns added parameters as expected', () => { + // arrange + const expected = [ + new FunctionParameterStub().withName('1'), + new FunctionParameterStub().withName('2').withOptionality(true), + new FunctionParameterStub().withName('3').withOptionality(false), + ]; + const sut = new FunctionParameterCollection(); + for (const parameter of expected) { + sut.addParameter(parameter); + } + // act + const actual = sut.all; + // assert + expect(expected).to.deep.equal(actual); + }); + it('throws when function parameters have same names', () => { + // arrange + const parameterName = 'duplicate-parameter'; + const expectedError = `duplicate parameter name: "${parameterName}"`; + const sut = new FunctionParameterCollection(); + sut.addParameter(new FunctionParameterStub().withName(parameterName)); + // act + const act = () => + sut.addParameter(new FunctionParameterStub().withName(parameterName)); + // assert + expect(act).to.throw(expectedError); + }); + describe('addParameter', () => { + it('throws if parameter is undefined', () => { + // arrange + const expectedError = 'undefined parameter'; + const value = undefined; + const sut = new FunctionParameterCollection(); + // act + const act = () => sut.addParameter(value); + // assert + expect(act).to.throw(expectedError); + }); + }); +}); diff --git a/tests/unit/application/Parser/Script/Compiler/Function/SharedFunction.spec.ts b/tests/unit/application/Parser/Script/Compiler/Function/SharedFunction.spec.ts index 6dcb8052..2af8baa8 100644 --- a/tests/unit/application/Parser/Script/Compiler/Function/SharedFunction.spec.ts +++ b/tests/unit/application/Parser/Script/Compiler/Function/SharedFunction.spec.ts @@ -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; } diff --git a/tests/unit/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument.spec.ts b/tests/unit/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument.spec.ts new file mode 100644 index 00000000..0f9c2df3 --- /dev/null +++ b/tests/unit/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument.spec.ts @@ -0,0 +1,46 @@ +import 'mocha'; +import { expect } from 'chai'; +import { FunctionCallArgument } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgument'; +import { testParameterName } from '../../ParameterNameTestRunner'; + +describe('FunctionCallArgument', () => { + describe('ctor', () => { + describe('parameter name', () => { + testParameterName( + (parameterName) => new FunctionCallArgumentBuilder() + .withParameterName(parameterName) + .build() + .parameterName, + ); + }); + it('throws if argument value is undefined', () => { + // arrange + const parameterName = 'paramName'; + const expectedError = `undefined argument value for "${parameterName}"`; + const argumentValue = undefined; + // act + const act = () => new FunctionCallArgumentBuilder() + .withParameterName(parameterName) + .withArgumentValue(argumentValue) + .build(); + // assert + expect(act).to.throw(expectedError); + }); + }); +}); + +class FunctionCallArgumentBuilder { + private parameterName = 'default-parameter-name'; + private argumentValue = 'default-argument-value'; + public withParameterName(parameterName: string) { + this.parameterName = parameterName; + return this; + } + public withArgumentValue(argumentValue: string) { + this.argumentValue = argumentValue; + return this; + } + public build() { + return new FunctionCallArgument(this.parameterName, this.argumentValue); + } +} diff --git a/tests/unit/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection.spec.ts b/tests/unit/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection.spec.ts new file mode 100644 index 00000000..8e58698a --- /dev/null +++ b/tests/unit/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection.spec.ts @@ -0,0 +1,143 @@ +import 'mocha'; +import { expect } from 'chai'; +import { FunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/FunctionCallArgumentCollection'; +import { FunctionCallArgumentStub } from '@tests/unit/stubs/FunctionCallArgumentStub'; + +describe('FunctionCallArgumentCollection', () => { + describe('addArgument', () => { + it('throws if argument is undefined', () => { + // arrange + const errorMessage = 'undefined argument'; + const arg = undefined; + const sut = new FunctionCallArgumentCollection(); + // act + const act = () => sut.addArgument(arg); + // assert + expect(act).to.throw(errorMessage); + }); + it('throws if parameter value is already provided', () => { + // arrange + const duplicateParameterName = 'duplicateParam'; + const errorMessage = `argument value for parameter ${duplicateParameterName} is already provided`; + const arg1 = new FunctionCallArgumentStub().withParameterName(duplicateParameterName); + const arg2 = new FunctionCallArgumentStub().withParameterName(duplicateParameterName); + const sut = new FunctionCallArgumentCollection(); + // act + sut.addArgument(arg1); + const act = () => sut.addArgument(arg2); + // assert + expect(act).to.throw(errorMessage); + }); + }); + describe('getAllParameterNames', () => { + it('returns as expected', () => { + // arrange + const testCases = [ { + name: 'no args', + args: [], + expected: [], + }, { + name: 'with some args', + args: [ + new FunctionCallArgumentStub().withParameterName('a-param-name'), + new FunctionCallArgumentStub().withParameterName('b-param-name')], + expected: [ 'a-param-name', 'b-param-name'], + }]; + for (const testCase of testCases) { + it(testCase.name, () => { + const sut = new FunctionCallArgumentCollection(); + // act + for (const arg of testCase.args) { + sut.addArgument(arg); + } + const actual = sut.getAllParameterNames(); + // assert + expect(actual).to.equal(testCase.expected); + }); + } + }); + }); + describe('getArgument', () => { + it('throws if parameter name is undefined', () => { + // arrange + const expectedError = 'undefined parameter name'; + const undefinedValues = [ '', undefined ]; + for (const undefinedValue of undefinedValues) { + const sut = new FunctionCallArgumentCollection(); + // act + const act = () => sut.getArgument(undefinedValue); + // assert + expect(act).to.throw(expectedError); + } + }); + it('throws if argument does not exist', () => { + // arrange + const parameterName = 'nonExistingParam'; + const expectedError = `parameter does not exist: ${parameterName}`; + const sut = new FunctionCallArgumentCollection(); + // act + const act = () => sut.getArgument(parameterName); + // assert + expect(act).to.throw(expectedError); + }); + it('returns argument as expected', () => { + // arrange + const expected = new FunctionCallArgumentStub() + .withParameterName('expectedName') + .withArgumentValue('expectedValue'); + const sut = new FunctionCallArgumentCollection(); + // act + sut.addArgument(expected); + const actual = sut.getArgument(expected.parameterName); + // assert + expect(actual).to.equal(expected); + }); + }); + describe('hasArgument', () => { + it('throws if parameter name is undefined', () => { + // arrange + const expectedError = 'undefined parameter name'; + const undefinedValues = [ '', undefined ]; + for (const undefinedValue of undefinedValues) { + const sut = new FunctionCallArgumentCollection(); + // act + const act = () => sut.hasArgument(undefinedValue); + // assert + expect(act).to.throw(expectedError); + } + }); + describe('returns as expected', () => { + // arrange + const testCases = [ { + name: 'argument exists', + parameter: 'existing-parameter-name', + args: [ + new FunctionCallArgumentStub().withParameterName('existing-parameter-name'), + new FunctionCallArgumentStub().withParameterName('unrelated-parameter-name'), + ], + expected: true, + }, + { + name: 'argument does not exist', + parameter: 'not-existing-parameter-name', + args: [ + new FunctionCallArgumentStub().withParameterName('unrelated-parameter-name-b'), + new FunctionCallArgumentStub().withParameterName('unrelated-parameter-name-a'), + ], + expected: false, + }]; + for (const testCase of testCases) { + it(`"${testCase.name}" returns "${testCase.expected}"`, () => { + const sut = new FunctionCallArgumentCollection(); + // act + for (const arg of testCase.args) { + sut.addArgument(arg); + } + const actual = sut.hasArgument(testCase.parameter); + // assert + expect(actual).to.equal(testCase.expected); + }); + } + }); + }); +}); diff --git a/tests/unit/application/Parser/Script/Compiler/FunctionCall/FunctionCall.spec.ts b/tests/unit/application/Parser/Script/Compiler/FunctionCall/FunctionCall.spec.ts new file mode 100644 index 00000000..3a614921 --- /dev/null +++ b/tests/unit/application/Parser/Script/Compiler/FunctionCall/FunctionCall.spec.ts @@ -0,0 +1,51 @@ +import 'mocha'; +import { expect } from 'chai'; +import { FunctionCall } from '@/application/Parser/Script/Compiler/FunctionCall/FunctionCall'; +import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection'; +import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub'; + +describe('FunctionCall', () => { + describe('ctor', () => { + it('throws when args is undefined', () => { + // arrange + const expectedError = 'undefined args'; + const args = undefined; + // act + const act = () => new FunctionCallBuilder() + .withArgs(args) + .build(); + // assert + expect(act).to.throw(expectedError); + }); + it('throws when function name is undefined', () => { + // arrange + const expectedError = 'empty function name in function call'; + const functionName = undefined; + // act + const act = () => new FunctionCallBuilder() + .withFunctionName(functionName) + .build(); + // assert + expect(act).to.throw(expectedError); + }); + }); +}); + +class FunctionCallBuilder { + private functionName = 'functionName'; + private args: IReadOnlyFunctionCallArgumentCollection = new FunctionCallArgumentCollectionStub(); + + public withFunctionName(functionName: string) { + this.functionName = functionName; + return this; + } + + public withArgs(args: IReadOnlyFunctionCallArgumentCollection) { + this.args = args; + return this; + } + + public build() { + return new FunctionCall(this.functionName, this.args); + } +} diff --git a/tests/unit/application/Parser/Script/Compiler/FunctionCall/FunctionCallCompiler.spec.ts b/tests/unit/application/Parser/Script/Compiler/FunctionCall/FunctionCallCompiler.spec.ts index 862a64bd..b7641cce 100644 --- a/tests/unit/application/Parser/Script/Compiler/FunctionCall/FunctionCallCompiler.spec.ts +++ b/tests/unit/application/Parser/Script/Compiler/FunctionCall/FunctionCallCompiler.spec.ts @@ -7,6 +7,7 @@ import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expre import { ExpressionsCompilerStub } from '@tests/unit/stubs/ExpressionsCompilerStub'; import { SharedFunctionCollectionStub } from '@tests/unit/stubs/SharedFunctionCollectionStub'; import { SharedFunctionStub } from '@tests/unit/stubs/SharedFunctionStub'; +import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub'; describe('FunctionCallCompiler', () => { describe('compileCall', () => { @@ -52,7 +53,7 @@ describe('FunctionCallCompiler', () => { }); it('throws if call sequence has undefined function name', () => { // arrange - const expectedError = 'empty function name called'; + const expectedError = 'empty function name in function call'; const call: FunctionCallData[] = [ { function: 'function-name' }, { function: undefined }, @@ -91,13 +92,14 @@ describe('FunctionCallCompiler', () => { it(testCase.name, () => { const func = new SharedFunctionStub() .withName('test-function-name') - .withParameters(...testCase.functionParameters); + .withParameterNames(...testCase.functionParameters); let params: FunctionCallParametersData = {}; for (const parameter of testCase.callParameters) { params = {...params, [parameter]: 'defined-parameter-value '}; } const call: FunctionCallData = { function: func.name, parameters: params }; - const functions = new SharedFunctionCollectionStub().withFunction(func); + const functions = new SharedFunctionCollectionStub() + .withFunction(func); const sut = new MockableFunctionCallCompiler(); // act const act = () => sut.compileCall(call, functions); @@ -134,38 +136,33 @@ describe('FunctionCallCompiler', () => { }); }); - }); describe('builds code as expected', () => { describe('builds single call as expected', () => { // arrange const parametersTestCases = [ - { - name: 'undefined parameters', - parameters: undefined, - parameterValues: undefined, - }, { name: 'empty parameters', parameters: [], - parameterValues: { }, + callArgs: { }, }, { name: 'non-empty parameters', parameters: [ 'param1', 'param2' ], - parameterValues: { param1: 'value1', param2: 'value2' }, + callArgs: { param1: 'value1', param2: 'value2' }, }, ]; for (const testCase of parametersTestCases) { it(testCase.name, () => { const expectedExecute = `expected-execute`; const expectedRevert = `expected-revert`; - const func = new SharedFunctionStub().withParameters(...testCase.parameters); + const func = new SharedFunctionStub().withParameterNames(...testCase.parameters); const functions = new SharedFunctionCollectionStub().withFunction(func); - const call: FunctionCallData = { function: func.name, parameters: testCase.parameterValues }; + const call: FunctionCallData = { function: func.name, parameters: testCase.callArgs }; + const args = new FunctionCallArgumentCollectionStub().withArguments(testCase.callArgs); const expressionsCompilerMock = new ExpressionsCompilerStub() - .setup(func.code, testCase.parameterValues, expectedExecute) - .setup(func.revertCode, testCase.parameterValues, expectedRevert); + .setup(func.code, args, expectedExecute) + .setup(func.revertCode, args, expectedRevert); const sut = new MockableFunctionCallCompiler(expressionsCompilerMock); // act const actual = sut.compileCall(call, functions); @@ -183,7 +180,7 @@ describe('FunctionCallCompiler', () => { .withRevertCode('first-function-revert-code'); const secondFunction = new SharedFunctionStub() .withName('second-function-name') - .withParameters('testParameter') + .withParameterNames('testParameter') .withCode('second-function-code') .withRevertCode('second-function-revert-code'); const secondCallArguments = { testParameter: 'testValue' }; @@ -191,11 +188,14 @@ describe('FunctionCallCompiler', () => { { function: firstFunction.name }, { function: secondFunction.name, parameters: secondCallArguments }, ]; + const firstFunctionCallArgs = new FunctionCallArgumentCollectionStub(); + const secondFunctionCallArgs = new FunctionCallArgumentCollectionStub() + .withArguments(secondCallArguments); const expressionsCompilerMock = new ExpressionsCompilerStub() - .setup(firstFunction.code, {}, firstFunction.code) - .setup(firstFunction.revertCode, {}, firstFunction.revertCode) - .setup(secondFunction.code, secondCallArguments, secondFunction.code) - .setup(secondFunction.revertCode, secondCallArguments, secondFunction.revertCode); + .setup(firstFunction.code, firstFunctionCallArgs, firstFunction.code) + .setup(firstFunction.revertCode, firstFunctionCallArgs, firstFunction.revertCode) + .setup(secondFunction.code, secondFunctionCallArgs, secondFunction.code) + .setup(secondFunction.revertCode, secondFunctionCallArgs, secondFunction.revertCode); const expectedExecute = `${firstFunction.code}\n${secondFunction.code}`; const expectedRevert = `${firstFunction.revertCode}\n${secondFunction.revertCode}`; const functions = new SharedFunctionCollectionStub() diff --git a/tests/unit/application/Parser/Script/Compiler/ParameterNameTestRunner.ts b/tests/unit/application/Parser/Script/Compiler/ParameterNameTestRunner.ts new file mode 100644 index 00000000..cff2cec3 --- /dev/null +++ b/tests/unit/application/Parser/Script/Compiler/ParameterNameTestRunner.ts @@ -0,0 +1,56 @@ +import 'mocha'; +import { expect } from 'chai'; + +export function testParameterName(action: (parameterName: string) => string) { + describe('name', () => { + describe('sets as expected', () => { + // arrange + const expectedValues = [ + 'lowercase', + 'onlyLetters', + 'l3tt3rsW1thNumb3rs', + ]; + for (const expected of expectedValues) { + it(expected, () => { + // act + const value = action(expected); + // assert + expect(value).to.equal(expected); + }); + } + }); + describe('throws if invalid', () => { + // arrange + const testCases = [ + { + name: 'undefined', + value: undefined, + expectedError: 'undefined parameter name', + }, + { + name: 'empty', + value: '', + expectedError: 'undefined parameter name', + }, + { + name: 'has @', + value: 'b@d', + expectedError: 'parameter name must be alphanumeric but it was "b@d"', + }, + { + name: 'has {', + value: 'b{a}d', + expectedError: 'parameter name must be alphanumeric but it was "b{a}d"', + }, + ]; + for (const testCase of testCases) { + it(testCase.name, () => { + // act + const act = () => action(testCase.value); + // assert + expect(act).to.throw(testCase.expectedError); + }); + } + }); + }); +} diff --git a/tests/unit/application/Parser/ScriptingDefinition/CodeSubstituter.spec.ts b/tests/unit/application/Parser/ScriptingDefinition/CodeSubstituter.spec.ts index ee9e8dfd..21c88d0e 100644 --- a/tests/unit/application/Parser/ScriptingDefinition/CodeSubstituter.spec.ts +++ b/tests/unit/application/Parser/ScriptingDefinition/CodeSubstituter.spec.ts @@ -60,7 +60,10 @@ describe('CodeSubstituter', () => { sut.substitute('non empty code', info); // assert expect(compilerStub.callHistory).to.have.lengthOf(1); - expect(compilerStub.callHistory[0].parameters[testCase.parameter]).to.equal(testCase.argument); + const parameters = compilerStub.callHistory[0].parameters; + expect(parameters.hasArgument(testCase.parameter)); + const argumentValue = parameters.getArgument(testCase.parameter).argumentValue; + expect(argumentValue).to.equal(testCase.argument); }); } }); diff --git a/tests/unit/stubs/ExpressionStub.ts b/tests/unit/stubs/ExpressionStub.ts index e729e0f6..bd187f40 100644 --- a/tests/unit/stubs/ExpressionStub.ts +++ b/tests/unit/stubs/ExpressionStub.ts @@ -1,15 +1,23 @@ import { ExpressionPosition } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionPosition'; -import { ExpressionArguments, IExpression } from '@/application/Parser/Script/Compiler/Expressions/Expression/IExpression'; +import { IExpression } from '@/application/Parser/Script/Compiler/Expressions/Expression/IExpression'; +import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection'; +import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection'; +import { FunctionParameterCollectionStub } from './FunctionParameterCollectionStub'; export class ExpressionStub implements IExpression { - public callHistory = new Array(); + public callHistory = new Array(); public position = new ExpressionPosition(0, 5); - public parameters = []; + public parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub(); private result: string; - public withParameters(...parameters: string[]) { + public withParameters(parameters: IReadOnlyFunctionParameterCollection) { this.parameters = parameters; return this; } + public withParameterNames(parameterNames: readonly string[], isOptional = false) { + const collection = new FunctionParameterCollectionStub() + .withParameterNames(parameterNames, isOptional); + return this.withParameters(collection); + } public withPosition(start: number, end: number) { this.position = new ExpressionPosition(start, end); return this; @@ -18,7 +26,7 @@ export class ExpressionStub implements IExpression { this.result = result; return this; } - public evaluate(args?: ExpressionArguments): string { + public evaluate(args: IReadOnlyFunctionCallArgumentCollection): string { this.callHistory.push(args); const result = this.result || `[expression-stub] args: ${args ? Object.keys(args).map((key) => `${key}: ${args[key]}`).join('", "') : 'none'}`; return result; diff --git a/tests/unit/stubs/ExpressionsCompilerStub.ts b/tests/unit/stubs/ExpressionsCompilerStub.ts index aa2b4a49..eb11b21c 100644 --- a/tests/unit/stubs/ExpressionsCompilerStub.ts +++ b/tests/unit/stubs/ExpressionsCompilerStub.ts @@ -1,30 +1,56 @@ -import { IExpressionsCompiler, ParameterValueDictionary } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler'; +import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler'; +import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection'; +import { scrambledEqual } from '@/application/Common/Array'; -interface Scenario { code: string; parameters: ParameterValueDictionary; result: string; } export class ExpressionsCompilerStub implements IExpressionsCompiler { - public readonly callHistory = new Array<{code: string, parameters?: ParameterValueDictionary}>(); - private readonly scenarios = new Array(); - public setup(code: string, parameters: ParameterValueDictionary, result: string) { + public readonly callHistory = new Array<{code: string, parameters: IReadOnlyFunctionCallArgumentCollection}>(); + + private readonly scenarios = new Array(); + + public setup( + code: string, + parameters: IReadOnlyFunctionCallArgumentCollection, + result: string): ExpressionsCompilerStub { this.scenarios.push({ code, parameters, result }); return this; } - public compileExpressions(code: string, parameters?: ParameterValueDictionary): string { + public compileExpressions( + code: string, + parameters: IReadOnlyFunctionCallArgumentCollection): string { this.callHistory.push({ code, parameters}); const scenario = this.scenarios.find((s) => s.code === code && deepEqual(s.parameters, parameters)); if (scenario) { return scenario.result; } - return `[ExpressionsCompilerStub] code: "${code}"` + - `| parameters: ${Object.keys(parameters || {}).map((p) => p + '=' + parameters[p]).join(',')}`; + const parametersAndValues = parameters + .getAllParameterNames() + .map((name) => `${name}=${parameters.getArgument(name).argumentValue}`) + .join('", "'); + return `[ExpressionsCompilerStub] code: "${code}" | parameters: "${parametersAndValues}"`; } } -function deepEqual(dict1: ParameterValueDictionary, dict2: ParameterValueDictionary) { - const dict1Keys = Object.keys(dict1 || {}); - const dict2Keys = Object.keys(dict2 || {}); - if (dict1Keys.length !== dict2Keys.length) { +interface ITestScenario { + readonly code: string; + readonly parameters: IReadOnlyFunctionCallArgumentCollection; + readonly result: string; +} + +function deepEqual( + expected: IReadOnlyFunctionCallArgumentCollection, + actual: IReadOnlyFunctionCallArgumentCollection): boolean { + const expectedParameterNames = expected.getAllParameterNames(); + const actualParameterNames = actual.getAllParameterNames(); + if (!scrambledEqual(expectedParameterNames, actualParameterNames)) { return false; } - return dict1Keys.every((key) => dict2.hasOwnProperty(key) && dict2[key] === dict1[key]); + for (const parameterName of expectedParameterNames) { + const expectedValue = expected.getArgument(parameterName); + const actualValue = expected.getArgument(parameterName); + if (expectedValue !== actualValue) { + return false; + } + } + return true; } diff --git a/tests/unit/stubs/FunctionCallArgumentCollectionStub.ts b/tests/unit/stubs/FunctionCallArgumentCollectionStub.ts new file mode 100644 index 00000000..61ba5de6 --- /dev/null +++ b/tests/unit/stubs/FunctionCallArgumentCollectionStub.ts @@ -0,0 +1,39 @@ +// tslint:disable-next-line:max-line-length +import { IFunctionCallArgument } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgument'; +import { IFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection'; +import { FunctionCallArgumentStub } from './FunctionCallArgumentStub'; + +export class FunctionCallArgumentCollectionStub implements IFunctionCallArgumentCollection { + private args = new Array(); + + public withArgument(parameterName: string, argumentValue: string) { + const arg = new FunctionCallArgumentStub() + .withParameterName(parameterName) + .withArgumentValue(argumentValue); + this.addArgument(arg); + return this; + } + public withArguments(args: { readonly [index: string]: string }) { + for (const parameterName of Object.keys(args)) { + const parameterValue = args[parameterName]; + this.withArgument(parameterName, parameterValue); + } + return this; + } + public hasArgument(parameterName: string): boolean { + return this.args.some((a) => a.parameterName === parameterName); + } + public addArgument(argument: IFunctionCallArgument): void { + this.args.push(argument); + } + public getAllParameterNames(): string[] { + return this.args.map((a) => a.parameterName); + } + public getArgument(parameterName: string): IFunctionCallArgument { + const arg = this.args.find((a) => a.parameterName === parameterName); + if (!arg) { + throw new Error(`no argument exists for parameter "${parameterName}"`); + } + return arg; + } +} diff --git a/tests/unit/stubs/FunctionCallArgumentStub.ts b/tests/unit/stubs/FunctionCallArgumentStub.ts new file mode 100644 index 00000000..e18d65a5 --- /dev/null +++ b/tests/unit/stubs/FunctionCallArgumentStub.ts @@ -0,0 +1,15 @@ +// tslint:disable-next-line:max-line-length +import { IFunctionCallArgument } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgument'; + +export class FunctionCallArgumentStub implements IFunctionCallArgument { + public parameterName = 'stub-parameter-name'; + public argumentValue = 'stub-arg-name'; + public withParameterName(parameterName: string) { + this.parameterName = parameterName; + return this; + } + public withArgumentValue(argumentValue: string) { + this.argumentValue = argumentValue; + return this; + } +} diff --git a/tests/unit/stubs/FunctionDataStub.ts b/tests/unit/stubs/FunctionDataStub.ts index 1cbbdbdf..f589127f 100644 --- a/tests/unit/stubs/FunctionDataStub.ts +++ b/tests/unit/stubs/FunctionDataStub.ts @@ -1,4 +1,4 @@ -import { FunctionData, ScriptFunctionCallData } from 'js-yaml-loader!@/*'; +import { FunctionData, ParameterDefinitionData, ScriptFunctionCallData } from 'js-yaml-loader!@/*'; export class FunctionDataStub implements FunctionData { public static createWithCode() { @@ -19,11 +19,11 @@ export class FunctionDataStub implements FunctionData { return new FunctionDataStub(); } - public name = 'function data stub'; + public name = 'functionDataStub'; public code: string; public revertCode: string; - public parameters?: readonly string[]; public call?: ScriptFunctionCallData; + public parameters?: readonly ParameterDefinitionData[]; private constructor() { } @@ -31,7 +31,10 @@ export class FunctionDataStub implements FunctionData { this.name = name; return this; } - public withParameters(...parameters: string[]) { + public withParameters(...parameters: readonly ParameterDefinitionData[]) { + return this.withParametersObject(parameters); + } + public withParametersObject(parameters: readonly ParameterDefinitionData[]) { this.parameters = parameters; return this; } diff --git a/tests/unit/stubs/FunctionParameterCollectionStub.ts b/tests/unit/stubs/FunctionParameterCollectionStub.ts new file mode 100644 index 00000000..d7b79520 --- /dev/null +++ b/tests/unit/stubs/FunctionParameterCollectionStub.ts @@ -0,0 +1,27 @@ +import { IFunctionParameter } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameter'; +import { IFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection'; +import { FunctionParameterStub } from './FunctionParameterStub'; + +export class FunctionParameterCollectionStub implements IFunctionParameterCollection { + private parameters = new Array(); + + public addParameter(parameter: IFunctionParameter): void { + this.parameters.push(parameter); + } + public get all(): readonly IFunctionParameter[] { + return this.parameters; + } + public withParameterName(parameterName: string, isOptional = true) { + const parameter = new FunctionParameterStub() + .withName(parameterName) + .withOptionality(isOptional); + this.addParameter(parameter); + return this; + } + public withParameterNames(parameterNames: readonly string[], isOptional = true) { + for (const parameterName of parameterNames) { + this.withParameterName(parameterName, isOptional); + } + return this; + } +} diff --git a/tests/unit/stubs/FunctionParameterStub.ts b/tests/unit/stubs/FunctionParameterStub.ts new file mode 100644 index 00000000..015ebd05 --- /dev/null +++ b/tests/unit/stubs/FunctionParameterStub.ts @@ -0,0 +1,14 @@ +import { IFunctionParameter } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameter'; + +export class FunctionParameterStub implements IFunctionParameter { + public name: string = 'function-parameter-stub'; + public isOptional: boolean = true; + public withName(name: string) { + this.name = name; + return this; + } + public withOptionality(isOptional: boolean) { + this.isOptional = isOptional; + return this; + } +} diff --git a/tests/unit/stubs/ParameterDefinitionDataStub.ts b/tests/unit/stubs/ParameterDefinitionDataStub.ts new file mode 100644 index 00000000..4e7c1334 --- /dev/null +++ b/tests/unit/stubs/ParameterDefinitionDataStub.ts @@ -0,0 +1,14 @@ +import { ParameterDefinitionData } from 'js-yaml-loader!@/*'; + +export class ParameterDefinitionDataStub implements ParameterDefinitionData { + public name: string; + public optional?: boolean; + public withName(name: string) { + this.name = name; + return this; + } + public withOptionality(isOptional: boolean) { + this.optional = isOptional; + return this; + } +} diff --git a/tests/unit/stubs/SharedFunctionCollectionStub.ts b/tests/unit/stubs/SharedFunctionCollectionStub.ts index 8b20e676..e4997d5f 100644 --- a/tests/unit/stubs/SharedFunctionCollectionStub.ts +++ b/tests/unit/stubs/SharedFunctionCollectionStub.ts @@ -1,5 +1,6 @@ import { ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction'; import { ISharedFunctionCollection } from '@/application/Parser/Script/Compiler/Function/ISharedFunctionCollection'; +import { SharedFunctionStub } from './SharedFunctionStub'; export class SharedFunctionCollectionStub implements ISharedFunctionCollection { private readonly functions = new Map(); @@ -11,11 +12,9 @@ export class SharedFunctionCollectionStub implements ISharedFunctionCollection { if (this.functions.has(name)) { return this.functions.get(name); } - return { - name, - parameters: [], - code: 'code by SharedFunctionCollectionStub', - revertCode: 'revert-code by SharedFunctionCollectionStub', - }; + return new SharedFunctionStub() + .withName(name) + .withCode('code by SharedFunctionCollectionStub') + .withRevertCode('revert-code by SharedFunctionCollectionStub'); } } diff --git a/tests/unit/stubs/SharedFunctionStub.ts b/tests/unit/stubs/SharedFunctionStub.ts index 8a1102ea..b08c5fa7 100644 --- a/tests/unit/stubs/SharedFunctionStub.ts +++ b/tests/unit/stubs/SharedFunctionStub.ts @@ -1,10 +1,11 @@ import { ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction'; +import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection'; +import { FunctionParameterCollectionStub } from './FunctionParameterCollectionStub'; export class SharedFunctionStub implements ISharedFunction { public name = 'shared-function-stub-name'; - public parameters?: readonly string[] = [ - 'shared-function-stub-parameter', - ]; + public parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub() + .withParameterName('shared-function-stub-parameter-name'); public code = 'shared-function-stub-code'; public revertCode = 'shared-function-stub-revert-code'; @@ -20,8 +21,15 @@ export class SharedFunctionStub implements ISharedFunction { this.revertCode = revertCode; return this; } - public withParameters(...params: string[]) { - this.parameters = params; + public withParameters(parameters: IReadOnlyFunctionParameterCollection) { + this.parameters = parameters; return this; } + public withParameterNames(...parameterNames: readonly string[]) { + let collection = new FunctionParameterCollectionStub(); + for (const name of parameterNames) { + collection = collection.withParameterName(name); + } + return this.withParameters(collection); + } }