From 137df3c5c0c9a716133c83ae63de833d27486cb6 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Sat, 14 Jan 2023 16:53:52 +0530 Subject: [PATCH] feat: bruno lang inline tag parser --- package.json | 1 + packages/bruno-lang/package.json | 3 + packages/bruno-lang/src/inline-tag.js | 32 +++++++++ packages/bruno-lang/src/inline-tag.spec.js | 83 ++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 packages/bruno-lang/src/inline-tag.js create mode 100644 packages/bruno-lang/src/inline-tag.spec.js diff --git a/package.json b/package.json index 8bac526f..018b3938 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "packages/bruno-electron", "packages/bruno-tauri", "packages/bruno-schema", + "packages/bruno-lang", "packages/bruno-testbench", "packages/bruno-graphql-docs" ], diff --git a/packages/bruno-lang/package.json b/packages/bruno-lang/package.json index 4fbca83c..a494ec41 100644 --- a/packages/bruno-lang/package.json +++ b/packages/bruno-lang/package.json @@ -8,5 +8,8 @@ ], "scripts": { "test": "jest" + }, + "dependencies": { + "arcsecond": "^5.0.0" } } diff --git a/packages/bruno-lang/src/inline-tag.js b/packages/bruno-lang/src/inline-tag.js new file mode 100644 index 00000000..097e2e45 --- /dev/null +++ b/packages/bruno-lang/src/inline-tag.js @@ -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; diff --git a/packages/bruno-lang/src/inline-tag.spec.js b/packages/bruno-lang/src/inline-tag.spec.js new file mode 100644 index 00000000..6a531157 --- /dev/null +++ b/packages/bruno-lang/src/inline-tag.spec.js @@ -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' }], + [] + ]); + }) +}); \ No newline at end of file