From 1114bbaf379ef1f2d25c254f9169fcde18f5613d Mon Sep 17 00:00:00 2001 From: mizuki_r Date: Sun, 24 Dec 2017 16:19:35 +0900 Subject: [PATCH] feat(resource/parser): support carriage '\r' in source data --- lib/hariko/resource/parser.js | 6 + test/hariko/resource/parse_spec.js | 344 ++++++++++++++++------------- 2 files changed, 195 insertions(+), 155 deletions(-) diff --git a/lib/hariko/resource/parser.js b/lib/hariko/resource/parser.js index 5e6363f..e230bbf 100644 --- a/lib/hariko/resource/parser.js +++ b/lib/hariko/resource/parser.js @@ -60,8 +60,14 @@ function toEntry (resource, action, example, request, response) { exports.parse = function (rawdata) { var entries = [], warnings = null; + + // the use of carriage return(s) '\r' in source data supported + rawdata = rawdata.replace(/\r/g, '\n') + var data = protagonist.parseSync(rawdata, {type: 'ast'}); + if (data.warnings) warnings = data.warnings; + for (var i = 0; i < data.ast.resourceGroups.length; i++) { var resourceGroup = data.ast.resourceGroups[i]; for (var ii = 0; ii < resourceGroup.resources.length; ii++) { diff --git a/test/hariko/resource/parse_spec.js b/test/hariko/resource/parse_spec.js index 5dcca0c..193494a 100644 --- a/test/hariko/resource/parse_spec.js +++ b/test/hariko/resource/parse_spec.js @@ -12,181 +12,215 @@ describe('hariko/resource/parser', function () { parser = null; }); describe('.parse(markdown_data)', function () { - it('should be parse content', function () { - var data = parser.parse(fs.readFileSync(path.join(path.dirname(__filename), 'fixture.md')).toString()); - expect(data).to.be.eql({ - entries: [ - { - "file": "index-GET.json", - "request": { - "method": "GET", - "uri": { - "path": "/", - "template": "/", - "queries": [] + describe('basic cases', function () { + it('should be parse content', function () { + var data = parser.parse(fs.readFileSync(path.join(path.dirname(__filename), 'fixture.md')).toString()); + expect(data).to.be.eql({ + entries: [ + { + "file": "index-GET.json", + "request": { + "method": "GET", + "uri": { + "path": "/", + "template": "/", + "queries": [] + } + }, + "response": { + "statusCode": 200, + "headers": [{"name": "Content-Type", "value": "text/plain"}], + "body": "Hello world\n", + "data": null } }, - "response": { - "statusCode": 200, - "headers": [{"name": "Content-Type", "value": "text/plain"}], - "body": "Hello world\n", - "data": null - } - }, - { - "file": "api/app-GET.json", - "request": { - "method": "GET", - "uri": { - "path": "/api/app", - "template": "/api/app", - "queries": [] + { + "file": "api/app-GET.json", + "request": { + "method": "GET", + "uri": { + "path": "/api/app", + "template": "/api/app", + "queries": [] + } + }, + "response": { + "statusCode": 200, + "headers": [{name: 'Content-Type', value: 'application/json'}], + "body": " {}\n", + "data": {}, } }, - "response": { - "statusCode": 200, - "headers": [{name: 'Content-Type', value: 'application/json'}], - "body": " {}\n", - "data": {}, - } - }, - { - "file": "api/user?page-GET.json", - "request": { - "method": "GET", - "uri": { - "path": "/api/user", - "template": "/api/user{?page}", - "queries": ['page'] + { + "file": "api/user?page-GET.json", + "request": { + "method": "GET", + "uri": { + "path": "/api/user", + "template": "/api/user{?page}", + "queries": ['page'] + } + }, + "response": { + "statusCode": 200, + "headers": [{name: 'Content-Type', value: 'application/json'}], + "body": " {}\n", + "data": {}, } }, - "response": { - "statusCode": 200, - "headers": [{name: 'Content-Type', value: 'application/json'}], - "body": " {}\n", - "data": {}, - } - }, - { - "file": "api/user/user_id-GET.json", - "request": { - "method": "GET", - "uri": { - "path": "/api/user/:user_id", - "template": "/api/user/{user_id}", - "queries": [] + { + "file": "api/user/user_id-GET.json", + "request": { + "method": "GET", + "uri": { + "path": "/api/user/:user_id", + "template": "/api/user/{user_id}", + "queries": [] + } + }, + "response": { + "statusCode": 200, + "headers": [{name: 'Content-Type', value: 'application/json'}], + "body": " {}\n", + "data": {}, } }, - "response": { - "statusCode": 200, - "headers": [{name: 'Content-Type', value: 'application/json'}], - "body": " {}\n", - "data": {}, - } - }, - { - "file": "api/user-POST.json", - "request": { - "method": "POST", - "uri": { - "path": "/api/user", - "template": "/api/user", - "queries": [] + { + "file": "api/user-POST.json", + "request": { + "method": "POST", + "uri": { + "path": "/api/user", + "template": "/api/user", + "queries": [] + } + }, + "response": { + "statusCode": 200, + "headers": [{name: 'Content-Type', value: 'application/json'}], + "body": " {}\n", + "data": {}, } }, - "response": { - "statusCode": 200, - "headers": [{name: 'Content-Type', value: 'application/json'}], - "body": " {}\n", - "data": {}, - } - }, - { - "file": "api/user/user_id-PUT.json", - "request": { - "method": "PUT", - "uri": { - "path": "/api/user/:user_id", - "template": "/api/user/{user_id}", - "queries": [] + { + "file": "api/user/user_id-PUT.json", + "request": { + "method": "PUT", + "uri": { + "path": "/api/user/:user_id", + "template": "/api/user/{user_id}", + "queries": [] + } + }, + "response": { + "statusCode": 200, + "headers": [{name: 'Content-Type', value: 'application/json'}], + "body": " {}\n", + "data": {}, } }, - "response": { - "statusCode": 200, - "headers": [{name: 'Content-Type', value: 'application/json'}], - "body": " {}\n", - "data": {}, - } - }, - { - "file": "api/item/hariko-GET.json", - "request": { - "method": "GET", - "uri": { - "path": "/api/item/hariko", - "template": "/api/item/{item_id}", - "queries": [] + { + "file": "api/item/hariko-GET.json", + "request": { + "method": "GET", + "uri": { + "path": "/api/item/hariko", + "template": "/api/item/{item_id}", + "queries": [] + } + }, + "response": { + "statusCode": 200, + "headers": [{name: 'Content-Type', value: 'application/json'}], + "body": " []\n", + "data": [] } }, - "response": { - "statusCode": 200, - "headers": [{name: 'Content-Type', value: 'application/json'}], - "body": " []\n", - "data": [] - } - }, - { - "file": "api/item/deco-GET.json", - "request": { - "method": "GET", - "uri": { - "path": "/api/item/deco", - "template": "/api/item/{item_id}", - "queries": [] + { + "file": "api/item/deco-GET.json", + "request": { + "method": "GET", + "uri": { + "path": "/api/item/deco", + "template": "/api/item/{item_id}", + "queries": [] + } + }, + "response": { + "statusCode": 200, + "headers": [{name: 'Content-Type', value: 'application/json'}], + "body": " []\n", + "data": [] } }, - "response": { - "statusCode": 200, - "headers": [{name: 'Content-Type', value: 'application/json'}], - "body": " []\n", - "data": [] - } - }, - { - "file": "api/item/item_id-GET.json", - "request": { - "method": "GET", - "uri": { - "path": "/api/item/:item_id", - "template": "/api/item/{item_id}", - "queries": [] + { + "file": "api/item/item_id-GET.json", + "request": { + "method": "GET", + "uri": { + "path": "/api/item/:item_id", + "template": "/api/item/{item_id}", + "queries": [] + } + }, + "response": { + "statusCode": 200, + "headers": [{name: 'Content-Type', value: 'application/json'}], + "body": " []\n", + "data": [] } }, - "response": { - "statusCode": 200, - "headers": [{name: 'Content-Type', value: 'application/json'}], - "body": " []\n", - "data": [] + ], + warnings: [ + { + "code": 5, + "message": "unexpected header block, expected a group, resource or an action definition, e.g. '# Group ', '# []' or '# '", + "location": [ + {"index": 686, "length": 7} + ] + }, + { + "code": 6, + "message": "action is missing a response", + "location": [ + {"index":693,"length":26} + ] } - }, - ], - warnings: [ - { - "code": 5, - "message": "unexpected header block, expected a group, resource or an action definition, e.g. '# Group ', '# []' or '# '", - "location": [ - {"index": 686, "length": 7} - ] - }, - { - "code": 6, - "message": "action is missing a response", - "location": [ - {"index":693,"length":26} - ] - } - ] + ] + }); }); }); + describe('when linebreak by \r', function () { + var data; + beforeEach(function () { + data = parser.parse(`\r# GET /api/message\r\r+ Response 200 (application/json)\r\r + Body\r\r {message: \"hello\"}\r\r`); + }) + it('should be enable parse', function () { + expect(data).to.be.eql({ + entries: [ + { + file: "api/message-GET.json", + request: { + method: "GET", + uri: { + path: "/api/message", + queries: [], + template: "/api/message" + } + }, + response: { + body: "{message: \"hello\"}\n", + data: {message: "hello"}, + headers: [ + { name: "Content-Type", value: "application/json" } + ], + statusCode: 200, + } + } + ], + warnings: [] + }) + }) + }); }); });