feat: bruno lang inline tag parser

This commit is contained in:
Anoop M D 2023-01-14 16:53:52 +05:30
parent 6ef2daebbd
commit 137df3c5c0
4 changed files with 119 additions and 0 deletions

View File

@ -6,6 +6,7 @@
"packages/bruno-electron", "packages/bruno-electron",
"packages/bruno-tauri", "packages/bruno-tauri",
"packages/bruno-schema", "packages/bruno-schema",
"packages/bruno-lang",
"packages/bruno-testbench", "packages/bruno-testbench",
"packages/bruno-graphql-docs" "packages/bruno-graphql-docs"
], ],

View File

@ -8,5 +8,8 @@
], ],
"scripts": { "scripts": {
"test": "jest" "test": "jest"
},
"dependencies": {
"arcsecond": "^5.0.0"
} }
} }

View File

@ -0,0 +1,32 @@
const {
sequenceOf,
whitespace,
str,
lookAhead,
choice,
endOfInput,
everyCharUntil
} = require("arcsecond");
const newline = lookAhead(str("\n"));
const newLineOrEndOfInput = choice([endOfInput, newline]);
const inlineTag = sequenceOf([
choice([
str('ver'),
str('type'),
str('name'),
str('method'),
str('url'),
str('body-mode')
]),
whitespace,
everyCharUntil(newLineOrEndOfInput)
]).map(([key, _, val]) => {
if(key === 'body-mode') {
key = 'bodyMode';
}
return { [key]: val };
});
module.exports = inlineTag;

View File

@ -0,0 +1,83 @@
const inlineTag = require('./inline-tag');
const {
sepBy,
char,
many,
choice
} = require('arcsecond');
describe('version', () => {
it('should parse version number', () => {
const input = 'ver 1.0';
const result = inlineTag.run(input);
expect(result.isError).toBe(false);
expect(result.result).toEqual({ ver: '1.0' });
});
it('should allow whitespaces while parsing version number', () => {
const input = 'ver 1.0';
const result = inlineTag.run(input);
expect(result.isError).toBe(false);
expect(result.result).toEqual({ ver: '1.0' });
});
it('should fail to parse when version number is missing', () => {
const input = 'ver';
const result = inlineTag.run(input);
expect(result.isError).toBe(true);
});
});
describe('type', () => {
it('should parse the type', () => {
const input = 'type http-request';
const result = inlineTag.run(input);
expect(result.isError).toBe(false);
expect(result.result).toEqual({ type: 'http-request' });
});
it('should allow whitespaces while parsing the type', () => {
const input = 'type http-request';
const result = inlineTag.run(input);
expect(result.isError).toBe(false);
expect(result.result).toEqual({ type: 'http-request' });
});
it('should fail to parse when type is missing', () => {
const input = 'type';
const result = inlineTag.run(input);
expect(result.isError).toBe(true);
});
});
describe('multiple inline tags', () => {
it('should parse the multiple inline tags', () => {
const input = `
ver 1.0
type http-request
name Send Bulk SMS
method GET
url https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010
body-mode json
`;
const newline = char('\n');
const line = inlineTag;
const lines = many(line);
const parser = sepBy(newline)(lines);
const result = parser.run(input);
expect(result.isError).toBe(false);
expect(result.result).toEqual([
[],
[{ ver: '1.0' }],
[{ type: 'http-request' }],
[{ name: 'Send Bulk SMS' }],
[{ method: 'GET' }],
[{ url: 'https://api.textlocal.in/bulk_json?apiKey=secret=&numbers=919988776655&message=hello&sender=600010' }],
[{ bodyMode: 'json' }],
[]
]);
})
});