From 2b57ab9af0249094ead35ab3daafc1b1315c0943 Mon Sep 17 00:00:00 2001 From: Dmitry Mazurok Date: Wed, 26 Jun 2019 22:38:03 +0700 Subject: [PATCH] Add support for multiline results #35 --- lib/comment-parser.js | 2 +- test/comment-parser.test.js | 58 +++++++++++++++++++++++++++++++++++++ test/mocha.test.js | 29 +++++++++++++++++++ test/multiline-file.js | 21 ++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 test/multiline-file.js diff --git a/lib/comment-parser.js b/lib/comment-parser.js index 5816f5a..683702a 100644 --- a/lib/comment-parser.js +++ b/lib/comment-parser.js @@ -91,7 +91,7 @@ exports.parseExamples = function commentParser$parseExamples(parsedComments) { var currentCaption; for(var i = 0, len = parsedComments.length; i < len; i++) { - if(parsedComments[i].type === 'code') { + if(parsedComments[i].type === 'code' && parsedComments[i].string) { if(currentExample.testCase) flush(); currentExample.testCase = parsedComments[i].string diff --git a/test/comment-parser.test.js b/test/comment-parser.test.js index 39e79e2..8fbcf9e 100644 --- a/test/comment-parser.test.js +++ b/test/comment-parser.test.js @@ -47,5 +47,63 @@ describe('jsdoctest/comment-parser', function() { }, ]); }); + + it('handles multiple line results', function() { + commentParser.parseExamples(commentParser.parseComments( + 'map([1, 2, 3], function(x) {\n' + + ' return x + 10\n' + + '});\n' + + '// => [\n' + + '// => 11,\n' + + '// => 12,\n' + + '// => 13\n' + + '// => ]' + )).should.eql([ + { + displayTestCase: 'map([1, 2, 3], function(x) {; return x + 10;})', + testCase: 'map([1, 2, 3], function(x) {\n return x + 10\n})', + expectedResult: '[ 11, 12, 13]', + } + ]) + }); + + it('ignores multiple line results if without arrow', function() { + commentParser.parseExamples(commentParser.parseComments( + 'map([1, 2, 3], function(x) {\n' + + ' return x + 10\n' + + '});\n' + + '// => [\n' + + '// 11,\n' + + '// 12,\n' + + '// 13\n' + + '// ]' + )).should.eql([ + { + displayTestCase: 'map([1, 2, 3], function(x) {; return x + 10;})', + testCase: 'map([1, 2, 3], function(x) {\n return x + 10\n})', + expectedResult: '[', + } + ]) + }); + + it('handles multiple line async results', function() { + commentParser.parseExamples(commentParser.parseComments( + 'map([1, 2, 3], function(x) {\n' + + ' return x + 10\n' + + '});\n' + + '// async => [\n' + + '// async => 11,\n' + + '// async => 12,\n' + + '// async => 13\n' + + '// async => ]' + )).should.eql([ + { + displayTestCase: 'map([1, 2, 3], function(x) {; return x + 10;})', + testCase: 'map([1, 2, 3], function(x) {\n return x + 10\n})', + expectedResult: '[ 11, 12, 13]', + isAsync: true + } + ]) + }); }); }); diff --git a/test/mocha.test.js b/test/mocha.test.js index a0ea360..63ba4b3 100644 --- a/test/mocha.test.js +++ b/test/mocha.test.js @@ -77,5 +77,34 @@ describe('jsdoctest/mocha', function() { called.should.be.true; }); + + it('handles multiline examples', function() { + var called = false; + var mockModule = { + _compile: onCompile + }; + + mocha.loadDoctests(mockModule, path.join(__dirname, './multiline-file.js')); + + function onCompile(content, filename) { + content.should.containEql( + '\ndescribe(\'subtract()\', function() {'+ + 'it(\'subtract(1, 2)\', function() {'+ + '(subtract(1, 2)).should.eql({ normal: -1, reverse: 1 });'+ + '});\n' + + 'it(\'subtract(1, 2)\', function() {'+ + '(subtract(1, 2)).should.eql({ normal: -1, reverse: 1});'+ + '});\n' + + 'it(\'subtract(3, 6)\', function() {'+ + '(subtract(3, 6)).should.eql({ normal: -3, reverse: 3 });'+ + '});'+ + '});' + ); + called = true; + filename.should.equal(path.join(__dirname, 'multiline-file.js')); + } + + called.should.be.true; + }); }); }); diff --git a/test/multiline-file.js b/test/multiline-file.js new file mode 100644 index 0000000..5b0fad7 --- /dev/null +++ b/test/multiline-file.js @@ -0,0 +1,21 @@ +/** + * @example + * subtract(1, 2) + * // => { normal: -1, reverse: 1 } + * + * subtract(1, 2) + * // => { + * // => normal: -1, + * // => reverse: 1 + * // => } + * + * subtract(3, 6) + * // => { normal: -3, reverse: 3 } + */ + +function subtract(x, y) { + return { + normal: x - y, + reverse: y - x + }; +}