Skip to content

Commit

Permalink
Replace parameters as string and object (#22)
Browse files Browse the repository at this point in the history
* Replace parameters as string and object

* Bump version

* Also replace strings with suffix

* Remove only
  • Loading branch information
abdala authored Nov 12, 2024
1 parent 9267449 commit 2d61720
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 34 deletions.
2 changes: 1 addition & 1 deletion assert/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/assert",
"version": "0.8.1",
"version": "0.8.2",
"description": "Extra assert library",
"type": "module",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion dtc-aws-plugin/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion dtc-mysql-plugin/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion dtc-playwright-plugin/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion dtc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc",
"version": "0.8.1",
"version": "0.8.2",
"description": "Declarative TestCases",
"repository": {
"type": "git",
Expand Down
2 changes: 0 additions & 2 deletions dtc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
41 changes: 28 additions & 13 deletions dtc/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>): unknown => {
const attributes = path.split('.')
let resolvedValue: unknown = params

for (const attribute of attributes) {
if (resolvedValue && typeof resolvedValue === 'object') {
resolvedValue = (resolvedValue as Record<string, unknown>)[attribute]
} else {
throw new Error(`Invalid parameter property: ${attribute} (${path}).`)
}
}

return resolvedValue
}

const replacePlaceholders = (template: unknown, params: Record<string, unknown>) =>
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[] => {
Expand Down
4 changes: 3 additions & 1 deletion dtc/src/plugins/disable-net-connect-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import nock from 'nock'

nock.disableNetConnect()
export const arrange = () => {
nock.disableNetConnect()
}
4 changes: 2 additions & 2 deletions dtc/test/fixtures/t1.dtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
},
}
4 changes: 2 additions & 2 deletions dtc/test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
2 changes: 1 addition & 1 deletion nock-aws/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion yaml/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/yaml",
"version": "0.8.1",
"version": "0.8.2",
"description": "YAML parser with extra tags",
"repository": {
"type": "git",
Expand Down

0 comments on commit 2d61720

Please sign in to comment.