mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-25 01:14:23 +01:00
feat: bruno lang inline tag parser
This commit is contained in:
parent
6ef2daebbd
commit
137df3c5c0
@ -6,6 +6,7 @@
|
||||
"packages/bruno-electron",
|
||||
"packages/bruno-tauri",
|
||||
"packages/bruno-schema",
|
||||
"packages/bruno-lang",
|
||||
"packages/bruno-testbench",
|
||||
"packages/bruno-graphql-docs"
|
||||
],
|
||||
|
@ -8,5 +8,8 @@
|
||||
],
|
||||
"scripts": {
|
||||
"test": "jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"arcsecond": "^5.0.0"
|
||||
}
|
||||
}
|
||||
|
32
packages/bruno-lang/src/inline-tag.js
Normal file
32
packages/bruno-lang/src/inline-tag.js
Normal 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;
|
83
packages/bruno-lang/src/inline-tag.spec.js
Normal file
83
packages/bruno-lang/src/inline-tag.spec.js
Normal 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' }],
|
||||
[]
|
||||
]);
|
||||
})
|
||||
});
|
Loading…
Reference in New Issue
Block a user