feat: assert tab allows any valid js code as keys

This commit is contained in:
Anoop M D 2023-02-08 18:17:30 +05:30
parent 45ed47ff90
commit aff6499478
3 changed files with 47 additions and 2 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@usebruno/lang",
"version": "0.1.0",
"version": "0.1.1",
"main": "src/index.js",
"files": [
"src",

View File

@ -43,6 +43,13 @@ const grammar = ohm.grammar(`Bru {
key = ~tagend validkey*
value = ~tagend validvalue*
// Dictionary for Assert Block
assertdictionary = st* "{" assertpairlist? tagend
assertpairlist = optionalnl* assertpair (~tagend stnl* assertpair)* (~tagend space)*
assertpair = st* assertkey st* ":" st* value? st*
assertkey = ~tagend assertvalidkey*
assertvalidkey = ~(":") any
// Text Blocks
textblock = textline (~tagend nl textline)*
textline = textchar*
@ -67,7 +74,7 @@ const grammar = ohm.grammar(`Bru {
varsandassert = varsreq | varsres | assert
varsreq = "vars:pre-request" dictionary
varsres = "vars:post-response" dictionary
assert = "assert" dictionary
assert = "assert" assertdictionary
body = "body" st* "{" nl* textblock tagend
bodyjson = "body:json" st* "{" nl* textblock tagend
@ -148,6 +155,20 @@ const sem = grammar.createSemantics().addAttribute('ast', {
value(chars) {
return chars.sourceString ? chars.sourceString.trim() : '';
},
assertdictionary(_1, _2, pairlist, _3) {
return pairlist.ast;
},
assertpairlist(_1, pair, _2, rest, _3) {
return [pair.ast, ...rest.ast];
},
assertpair(_1, key, _2, _3, _4, value, _5) {
let res = {};
res[key.ast] = _.get(value, 'ast[0]', '');
return res;
},
assertkey(chars) {
return chars.sourceString ? chars.sourceString.trim() : '';
},
textblock(line, _1, rest) {
return [line.ast, ...rest.ast].join('\n');
},

View File

@ -0,0 +1,24 @@
/**
* This test file is used to test the text parser.
*/
const parser = require("../src/bruToJson");
describe("assert parser", () => {
it("should parse assert statement", () => {
const input = `
assert {
res("data.airports").filter(a => a.code ==="BLR").name: "Bangalore International Airport"
}
`;
const output = parser(input);
const expected = {
"assert": [{
name: "res(\"data.airports\").filter(a => a.code ===\"BLR\").name",
value: '"Bangalore International Airport"',
enabled: true
}]
};
expect(output).toEqual(expected);
});
});