mirror of
https://github.com/usebruno/bruno.git
synced 2025-02-02 02:49:48 +01:00
feat: made bru lang parser more robust to optional newlines and whitespaces
This commit is contained in:
parent
963b0c257f
commit
6947860204
@ -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*
|
||||
|
@ -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'))}`;
|
||||
|
@ -81,6 +81,7 @@ headers {
|
||||
const input = `
|
||||
headers {
|
||||
content-type: application/json
|
||||
|
||||
Authorization: JWT secret
|
||||
}`;
|
||||
|
||||
|
38
packages/bruno-lang/v2/tests/script.spec.js
Normal file
38
packages/bruno-lang/v2/tests/script.spec.js
Normal 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);
|
||||
});
|
||||
});
|
@ -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);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user