Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiline results #35 #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/comment-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand. It was already supported then, but missing some error handling?

if(currentExample.testCase) flush();

currentExample.testCase = parsedComments[i].string
Expand Down
58 changes: 58 additions & 0 deletions test/comment-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
])
});
});
});
29 changes: 29 additions & 0 deletions test/mocha.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});
21 changes: 21 additions & 0 deletions test/multiline-file.js
Original file line number Diff line number Diff line change
@@ -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
};
}