feat: script and vars are segmented at req and res levels separately

This commit is contained in:
Anoop M D 2023-02-06 21:18:36 +05:30
parent c8de57aa51
commit 60c96f7d27
4 changed files with 136 additions and 46 deletions

View File

@ -62,9 +62,9 @@ const grammar = ohm.grammar(`Bru {
query = "query" dictionary query = "query" dictionary
varsandassert = vars | varslocal | assert varsandassert = varsreq | varsres | assert
vars = "vars" dictionary varsreq = "vars:req" dictionary
varslocal = "vars:local" dictionary varsres = "vars:res" dictionary
assert = "assert" dictionary assert = "assert" dictionary
body = "body" st* "{" nl* textblock tagend body = "body" st* "{" nl* textblock tagend
@ -77,7 +77,9 @@ const grammar = ohm.grammar(`Bru {
bodyformurlencoded = "body:form-urlencoded" dictionary bodyformurlencoded = "body:form-urlencoded" dictionary
bodymultipart = "body:multipart-form" dictionary bodymultipart = "body:multipart-form" dictionary
script = "script" st* "{" nl* textblock tagend script = scriptreq | scriptres
scriptreq = "script:req" st* "{" nl* textblock tagend
scriptres = "script:res" st* "{" nl* textblock tagend
tests = "tests" st* "{" nl* textblock tagend tests = "tests" st* "{" nl* textblock tagend
docs = "docs" st* "{" nl* textblock tagend docs = "docs" st* "{" nl* textblock tagend
}`); }`);
@ -309,7 +311,7 @@ const sem = grammar.createSemantics().addAttribute('ast', {
} }
}; };
}, },
vars(_1, dictionary) { varsreq(_1, dictionary) {
const vars = mapPairListToKeyValPairs(dictionary.ast); const vars = mapPairListToKeyValPairs(dictionary.ast);
_.each(vars, (v) => { _.each(vars, (v) => {
let name = v.name; let name = v.name;
@ -322,7 +324,27 @@ const sem = grammar.createSemantics().addAttribute('ast', {
}); });
return { return {
vars vars: {
req: vars
}
};
},
varsres(_1, dictionary) {
const vars = mapPairListToKeyValPairs(dictionary.ast);
_.each(vars, (v) => {
let name = v.name;
if (name && name.length && name.charAt(0) === "@") {
v.name = name.slice(1);
v.local = true;
} else {
v.local = false;
}
});
return {
vars: {
res: vars
}
}; };
}, },
assert(_1, dictionary) { assert(_1, dictionary) {
@ -330,9 +352,18 @@ const sem = grammar.createSemantics().addAttribute('ast', {
assert: mapPairListToKeyValPairs(dictionary.ast) assert: mapPairListToKeyValPairs(dictionary.ast)
}; };
}, },
script(_1, _2, _3, _4, textblock, _5) { scriptreq(_1, _2, _3, _4, textblock, _5) {
return { return {
script: outdentString(textblock.sourceString) script: {
req: outdentString(textblock.sourceString)
}
};
},
scriptres(_1, _2, _3, _4, textblock, _5) {
return {
script: {
res: outdentString(textblock.sourceString)
}
}; };
}, },
tests(_1, _2, _3, _4, textblock, _5) { tests(_1, _2, _3, _4, textblock, _5) {

View File

@ -141,13 +141,41 @@ ${indentString(body.xml)}
bru += '\n}\n\n'; bru += '\n}\n\n';
} }
if(vars && vars.length) { let reqvars = _.get(vars, 'req');
const varsEnabled = _.filter(vars, (v) => v.enabled && !v.local); let resvars = _.get(vars, 'res');
const varsDisabled = _.filter(vars, (v) => !v.enabled && !v.local); if(reqvars && reqvars.length) {
const varsLocalEnabled = _.filter(vars, (v) => v.enabled && v.local); const varsEnabled = _.filter(reqvars, (v) => v.enabled && !v.local);
const varsLocalDisabled = _.filter(vars, (v) => !v.enabled && v.local); const varsDisabled = _.filter(reqvars, (v) => !v.enabled && !v.local);
const varsLocalEnabled = _.filter(reqvars, (v) => v.enabled && v.local);
const varsLocalDisabled = _.filter(reqvars, (v) => !v.enabled && v.local);
bru += `vars {`; bru += `vars:req {`;
if(varsEnabled.length) {
bru += `\n${indentString(varsEnabled.map(item => `${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsLocalEnabled.length) {
bru += `\n${indentString(varsLocalEnabled.map(item => `@${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsDisabled.length) {
bru += `\n${indentString(varsDisabled.map(item => `~${item.name}: ${item.value}`).join('\n'))}`;
}
if(varsLocalDisabled.length) {
bru += `\n${indentString(varsLocalDisabled.map(item => `~@${item.name}: ${item.value}`).join('\n'))}`;
}
bru += '\n}\n\n';
}
if(resvars && resvars.length) {
const varsEnabled = _.filter(resvars, (v) => v.enabled && !v.local);
const varsDisabled = _.filter(resvars, (v) => !v.enabled && !v.local);
const varsLocalEnabled = _.filter(resvars, (v) => v.enabled && v.local);
const varsLocalDisabled = _.filter(resvars, (v) => !v.enabled && v.local);
bru += `vars:res {`;
if(varsEnabled.length) { if(varsEnabled.length) {
bru += `\n${indentString(varsEnabled.map(item => `${item.name}: ${item.value}`).join('\n'))}`; bru += `\n${indentString(varsEnabled.map(item => `${item.name}: ${item.value}`).join('\n'))}`;
@ -182,9 +210,17 @@ ${indentString(body.xml)}
bru += '\n}\n\n'; bru += '\n}\n\n';
} }
if(script && script.length) { if(script && script.req && script.req.length) {
bru += `script { bru += `script:req {
${indentString(script)} ${indentString(script.req)}
}
`;
}
if(script && script.res && script.res.length) {
bru += `script:res {
${indentString(script.res)}
} }
`; `;

View File

@ -67,7 +67,12 @@ body:graphql:vars {
} }
} }
vars { vars:req {
departingDate: 2020-01-01
~returningDate: 2020-01-02
}
vars:res {
token: $res.body.token token: $res.body.token
@orderNumber: $res.body.orderNumber @orderNumber: $res.body.orderNumber
~petId: $res.body.id ~petId: $res.body.id
@ -79,7 +84,7 @@ assert {
~$res.body.message: success ~$res.body.message: success
} }
script { script:req {
const foo = 'bar'; const foo = 'bar';
} }

View File

@ -82,7 +82,22 @@
} }
] ]
}, },
"vars": [ "vars": {
"req": [
{
"name": "departingDate",
"value": "2020-01-01",
"local": false,
"enabled": true
},
{
"name": "returningDate",
"value": "2020-01-02",
"local": false,
"enabled": false
}
],
"res": [
{ {
"name": "token", "name": "token",
"value": "$res.body.token", "value": "$res.body.token",
@ -107,7 +122,8 @@
"local": true, "local": true,
"enabled": false "enabled": false
} }
], ]
},
"assert": [ "assert": [
{ {
"name": "$res.status", "name": "$res.status",
@ -120,7 +136,9 @@
"enabled": false "enabled": false
} }
], ],
"script": "const foo = 'bar';", "script": {
"req": "const foo = 'bar';"
},
"tests": "function onResponse(request, response) {\n expect(response.status).to.equal(200);\n}", "tests": "function onResponse(request, response) {\n expect(response.status).to.equal(200);\n}",
"docs": "This request needs auth token to be set in the headers." "docs": "This request needs auth token to be set in the headers."
} }