feat: made bru lang parser more robust to optional newlines and whitespaces

This commit is contained in:
Anoop M D 2023-02-07 01:18:18 +05:30
parent 963b0c257f
commit 6947860204
5 changed files with 44 additions and 23 deletions

View File

@ -30,13 +30,15 @@ const grammar = ohm.grammar(`Bru {
nl = "\\r"? "\\n"
st = " " | "\\t"
stnl = st | nl
tagend = nl "}"
optionalnl = ~tagend nl
validkey = ~(st | ":") any
validvalue = ~nl any
// Dictionary Blocks
dictionary = st* "{" pairlist? tagend
pairlist = nl* pair (~tagend nl pair)* (~tagend space)*
pairlist = optionalnl* pair (~tagend nl pair)* (~tagend space)*
pair = st* key st* ":" st* value? st*
key = ~tagend validkey*
value = ~tagend validvalue*

View File

@ -103,7 +103,7 @@ ${indentString(body.xml)}
`;
}
if(body && body.formUrlEncoded) {
if(body && body.formUrlEncoded && body.formUrlEncoded.length) {
bru += `body:form-urlencoded {`;
if(enabled(body.formUrlEncoded).length) {
bru += `\n${indentString(enabled(body.formUrlEncoded).map(item => `${item.name}: ${item.value}`).join('\n'))}`;
@ -116,7 +116,7 @@ ${indentString(body.xml)}
bru += '\n}\n\n';
}
if(body && body.multipartForm) {
if(body && body.multipartForm && body.multipartForm.length) {
bru += `body:multipart-form {`;
if(enabled(body.multipartForm).length) {
bru += `\n${indentString(enabled(body.multipartForm).map(item => `${item.name}: ${item.value}`).join('\n'))}`;

View File

@ -81,6 +81,7 @@ headers {
const input = `
headers {
content-type: application/json
Authorization: JWT secret
}`;

View File

@ -0,0 +1,38 @@
/**
* This test file is used to test the text parser.
*/
const parser = require("../src/bruToJson");
describe("script parser", () => {
it("should parse request script", () => {
const input = `
script:req {
$req.setHeader('Content-Type', 'application/json');
}
`;
const output = parser(input);
const expected = {
"script": {
"req": "$req.setHeader('Content-Type', 'application/json');"
}
};
expect(output).toEqual(expected);
});
it("should parse response script", () => {
const input = `
script:res {
expect(response.status).to.equal(200);
}
`;
const output = parser(input);
const expected = {
"script": {
"res": "expect(response.status).to.equal(200);"
}
};
expect(output).toEqual(expected);
});
});

View File

@ -1,20 +0,0 @@
/**
* This test file is used to test the text parser.
*/
const parser = require("../src/bruToJson");
describe("script parser", () => {
it("should parse script body", () => {
const input = `
script {
function onResponse(request, response) {
expect(response.status).to.equal(200);
}
}
`;
const output = parser(input);
const expected = "function onResponse(request, response) {\n expect(response.status).to.equal(200);\n}";
expect(output.script).toEqual(expected);
});
});