diff --git a/assert/package.json b/assert/package.json index 6045967..9881959 100644 --- a/assert/package.json +++ b/assert/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/assert", - "version": "0.8.1", + "version": "0.8.2", "description": "Extra assert library", "type": "module", "repository": { diff --git a/dtc-aws-plugin/package.json b/dtc-aws-plugin/package.json index 623036c..a612b79 100644 --- a/dtc-aws-plugin/package.json +++ b/dtc-aws-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/dtc-aws-plugin", - "version": "0.8.1", + "version": "0.8.2", "description": "AWS plugin for Declarative TestCases", "repository": { "type": "git", diff --git a/dtc-mysql-plugin/package.json b/dtc-mysql-plugin/package.json index 32eafd8..b846328 100644 --- a/dtc-mysql-plugin/package.json +++ b/dtc-mysql-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/dtc-mysql-plugin", - "version": "0.8.1", + "version": "0.8.2", "description": "MySQL plugin for Declarative TestCases", "repository": { "type": "git", diff --git a/dtc-playwright-plugin/package.json b/dtc-playwright-plugin/package.json index 3175ce2..9fe29b0 100644 --- a/dtc-playwright-plugin/package.json +++ b/dtc-playwright-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/dtc-playwright-plugin", - "version": "0.8.1", + "version": "0.8.2", "description": "Playwright plugin for Declarative TestCases", "repository": { "type": "git", diff --git a/dtc/package.json b/dtc/package.json index 72e2faf..d8bc571 100644 --- a/dtc/package.json +++ b/dtc/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/dtc", - "version": "0.8.1", + "version": "0.8.2", "description": "Declarative TestCases", "repository": { "type": "git", diff --git a/dtc/src/index.ts b/dtc/src/index.ts index a75cfad..debb1ed 100644 --- a/dtc/src/index.ts +++ b/dtc/src/index.ts @@ -7,8 +7,6 @@ export type * from './domain' export * from './utils.js' export * from './config.js' export * from './loader.js' -export * as DisableNetConnectPlugin from './plugins/disable-net-connect-plugin.js' -export * as FunctionCallPlugin from './plugins/function-call-plugin.js' export const defaultTestRunner = async (testCaseExecutions: TestCaseExecution[], plugins: string[]) => { for (const {filePath, testCase} of testCaseExecutions) { diff --git a/dtc/src/loader.ts b/dtc/src/loader.ts index fe90f90..68839ee 100644 --- a/dtc/src/loader.ts +++ b/dtc/src/loader.ts @@ -42,24 +42,39 @@ const recursiveMap = (obj: any, callback: (v: any) => any): any => { return callback(obj) } -const replacePlaceholders = (obj: any, params: any) => - recursiveMap(obj, (value: string | number | boolean) => { +const getValueByPath = (path: string, params: Record): unknown => { + const attributes = path.split('.') + let resolvedValue: unknown = params + + for (const attribute of attributes) { + if (resolvedValue && typeof resolvedValue === 'object') { + resolvedValue = (resolvedValue as Record)[attribute] + } else { + throw new Error(`Invalid parameter property: ${attribute} (${path}).`) + } + } + + return resolvedValue +} + +const replacePlaceholders = (template: unknown, params: Record) => + recursiveMap(template, (value: string | number | boolean) => { if (typeof value !== 'string') { return value } - return value.replace(/\${(.*?)}/g, (match, group) => { - const path = group.split('.') - let value = params - for (const prop of path) { - if (value && typeof value === 'object') { - value = value[prop] - } else { - return match - } + const regex = new RegExp(/\${(.*?)}/g) + const matches = regex.exec(value) + + if (matches !== null) { + if (matches[0] !== matches.input) { + return value.replace(regex, (_, path) => getValueByPath(path, params) as string) } - return value - }) + + return getValueByPath(matches[1], params) + } + + return value }) const resolveParameters = (testCaseExecution: TestCaseExecution): TestCaseExecution[] => { diff --git a/dtc/src/plugins/disable-net-connect-plugin.ts b/dtc/src/plugins/disable-net-connect-plugin.ts index 802fa23..c640fd2 100644 --- a/dtc/src/plugins/disable-net-connect-plugin.ts +++ b/dtc/src/plugins/disable-net-connect-plugin.ts @@ -1,3 +1,5 @@ import nock from 'nock' -nock.disableNetConnect() +export const arrange = () => { + nock.disableNetConnect() +} diff --git a/dtc/test/fixtures/t1.dtc.ts b/dtc/test/fixtures/t1.dtc.ts index e5f79e7..79a5b21 100644 --- a/dtc/test/fixtures/t1.dtc.ts +++ b/dtc/test/fixtures/t1.dtc.ts @@ -11,10 +11,10 @@ export default { import: 'syncFunction', from: 'functions.js', arguments: [ - {a: '${a}', d: '${c.d}'}, + {a: 'content ${a} more ${b}', b: '${b} content', c: '${c}', d: '${c.d}'}, ], }, assert: { - a: '${b}', + a: 'content ${a} more ${b}' }, } diff --git a/dtc/test/loader.test.ts b/dtc/test/loader.test.ts index f325ee5..0cb1006 100644 --- a/dtc/test/loader.test.ts +++ b/dtc/test/loader.test.ts @@ -32,8 +32,8 @@ test('It replaces parameters placeholders', async () => { nodeAssert.equal(`${__dirname}/./fixtures/t1.dtc.ts`, testCaseExecutions[0].filePath) nodeAssert.equal('Test 1', testCaseExecutions[0].testCase.name) - nodeAssert.deepStrictEqual(testCaseExecutions[0].testCase.act?.arguments, [{a: 'b', d: 'e'}]) - nodeAssert.deepStrictEqual(testCaseExecutions[0].testCase.assert, {a: 'b'}) + nodeAssert.deepStrictEqual(testCaseExecutions[0].testCase.act?.arguments, [{a: 'content b more b', b: 'b content', c: {d: 'e'}, d: 'e'}]) + nodeAssert.deepStrictEqual(testCaseExecutions[0].testCase.assert, {a: 'content b more b'}) }) test('It replaces multiple parameters placeholders', async () => { diff --git a/nock-aws/package.json b/nock-aws/package.json index 72fe340..357d503 100644 --- a/nock-aws/package.json +++ b/nock-aws/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/nock-aws", - "version": "0.8.1", + "version": "0.8.2", "description": "AWS Request mocker based on Nock", "repository": { "type": "git", diff --git a/package-lock.json b/package-lock.json index 139015a..3f90207 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,12 +23,12 @@ }, "assert": { "name": "@cgauge/assert", - "version": "0.8.1", + "version": "0.8.2", "license": "LGPL-3.0-or-later" }, "dtc": { "name": "@cgauge/dtc", - "version": "0.8.1", + "version": "0.8.2", "license": "LGPL-3.0-or-later", "dependencies": { "@cgauge/assert": "^0.8.0", @@ -41,7 +41,7 @@ }, "dtc-aws-plugin": { "name": "@cgauge/dtc-aws-plugin", - "version": "0.8.1", + "version": "0.8.2", "license": "LGPL-3.0-or-later", "dependencies": { "@aws-sdk/client-dynamodb": "^3.645.0", @@ -57,7 +57,7 @@ }, "dtc-mysql-plugin": { "name": "@cgauge/dtc-mysql-plugin", - "version": "0.8.1", + "version": "0.8.2", "license": "LGPL-3.0-or-later", "dependencies": { "@cgauge/assert": "^0.8.0", @@ -68,7 +68,7 @@ }, "dtc-playwright-plugin": { "name": "@cgauge/dtc-playwright-plugin", - "version": "0.8.1", + "version": "0.8.2", "license": "LGPL-3.0-or-later", "dependencies": { "@cgauge/assert": "^0.8.0", @@ -78,7 +78,7 @@ }, "nock-aws": { "name": "@cgauge/nock-aws", - "version": "0.8.1", + "version": "0.8.2", "license": "LGPL-3.0-or-later", "dependencies": { "nock": "^14.0.0-beta.15" @@ -2790,7 +2790,7 @@ }, "yaml": { "name": "@cgauge/yaml", - "version": "0.8.1", + "version": "0.8.2", "license": "LGPL-3.0-or-later", "dependencies": { "js-yaml": "^4.1.0" diff --git a/yaml/package.json b/yaml/package.json index 658f6f2..a939784 100644 --- a/yaml/package.json +++ b/yaml/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/yaml", - "version": "0.8.1", + "version": "0.8.2", "description": "YAML parser with extra tags", "repository": { "type": "git",