Skip to content

Commit

Permalink
Flatten data
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriangalliat committed Nov 23, 2014
1 parent 65017d6 commit df5b57b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
8 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,14 @@ var CommentParser = (function(){
* Generate data use in the view
*/
CommentParser.prototype.parse = function (comments) {
var result = {};
var result = [];
var posterComment = {};
var thisParseComment = parseComment.bind(this);

comments.forEach(function (comment) {
var parsedComment = thisParseComment(comment, this.annotations, posterComment);
if (parsedComment !== null){
var type = comment.context.type;
if (typeof result[type] === 'undefined') {
result[type] = [];
}
result[type].push(parsedComment);
result.push(parsedComment);
}
}, this);

Expand Down
50 changes: 24 additions & 26 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,45 +182,43 @@ describe('CDocParser', function(){
describe('#parse', function(){
it('should group comments by context type', function(){
var result = parser.parse ( comments );
assert.equal(result.testType1.length , 3);
assert.equal(result.testType2.length , 2);
assert.equal(result.testType3.length , 3);
assert.equal(result.length , 8);
});

it('should add default values', function(){
var result = parser.parse ( comments );
assert.equal(result.testType3[1].test[0] , 'Default' );
assert.equal(result[1].test[0] , 'Default' );
});

it('should join lines without annotation into description', function(){
var result = parser.parse ( comments );
assert.equal(result.testType1[0].description , 'test\ntest\n');
assert.equal(result[0].description , 'test\ntest\n');
});

it('should resolve a alias to the real name', function(){
var result = parser.parse ( comments );
assert.equal(result.testType2.length , 2);
assert.equal(result.testType2[0].test[0] , 'Working' );
assert.equal(result.length , 8);
assert.equal(result[0].test[0] , 'Working' );
});

it('should convert an annotation that returns a boolean to a flag', function(){
var result = parser.parse ( comments );
assert.equal(result.testType3[1].flag , true );
assert.equal(result[6].flag , true );
});

it('should parse a multiline annotation', function(){
var result = parser.parse ( comments );
assert.equal(result.testType3[2].multiline[0] , "\nThis is a\nmultiline\nannotation\n");
assert.equal(result[7].multiline[0] , "\nThis is a\nmultiline\nannotation\n");
});

it('should ignore annotations that aren\'t at the start of the line', function(){
var result = parser.parse ( comments );
assert.equal(result.testType1[1].test[0] , 'Default');
assert.equal(result[1].test[0] , 'Default');
});

it('should ignore annotations that won\'t return anything', function(){
var result = parser.parse ( comments );
assert.deepEqual(result.testType1[2].ignore , []);
assert.deepEqual(result[2].ignore , []);
});

it('should emit an error if annotation was not found', function(done){
Expand All @@ -234,17 +232,17 @@ describe('CDocParser', function(){
it('should apply annotations in a block poster comment to each item', function () {
var comments = getCommentsFrom('blockPoster.test.scss');
var result = parser.parse ( comments );
assert.equal(result.testCtx.length , 2);
assert.equal(result.testCtx[0].flag , undefined);
assert.equal(result.testCtx[1].flag , true);
assert.equal(result.length , 2);
assert.equal(result[0].flag , undefined);
assert.equal(result[1].flag , true);
});

it('should apply annotations in a line poster comment to each item', function () {
var comments = getCommentsFrom('linePoster.test.scss');
var result = parser.parse ( comments );
assert.equal(result.testCtx.length , 2);
assert.equal(result.testCtx[0].flag , undefined);
assert.equal(result.testCtx[1].flag , true);
assert.equal(result.length , 2);
assert.equal(result[0].flag , undefined);
assert.equal(result[1].flag , true);
});

it('should emit an error if more than one poster comment was used', function(done){
Expand Down Expand Up @@ -273,7 +271,7 @@ describe('CDocParser', function(){
lines : ['@allowedLimited'],
context : { type : 'workingType'}
}]);
assert.deepEqual(result.workingType[0].allowedLimited , []);
assert.deepEqual(result[0].allowedLimited , []);
});


Expand Down Expand Up @@ -302,7 +300,7 @@ describe('CDocParser', function(){
context : { type : 'demo' }
}]);

assert.equal(rawTestResult.demo[0].test, 'Hello');
assert.equal(rawTestResult[0].test, 'Hello');

});

Expand All @@ -318,7 +316,7 @@ describe('CDocParser', function(){
context : { type : 'demo' }
}]);

assert.equal(rawTestResult.demo[0].test, 'First');
assert.equal(rawTestResult[0].test, 'First');

});

Expand Down Expand Up @@ -353,15 +351,15 @@ describe('CDocParser', function(){
context : { type : 'demo' }
}]);

assert.deepEqual(extendedResult.demo[0].test, ['hello', 'extended']);
assert.deepEqual(extendedResult[0].test, ['hello', 'extended']);


var defaultResult = parser.parse ([{
lines : ['Just a description'],
context : { type : 'demo' }
}]);

assert.deepEqual(defaultResult.demo[0].test, ['default', 'extended']);
assert.deepEqual(defaultResult[0].test, ['default', 'extended']);

});

Expand All @@ -376,7 +374,7 @@ describe('CDocParser', function(){
context : { type : 'demo' }
}]);

assert.deepEqual(extendedResult.demo[0].test, ['hello', 'extended']);
assert.deepEqual(extendedResult[0].test, ['hello', 'extended']);


parser = new docParser.CommentParser(annotations, {
Expand All @@ -388,7 +386,7 @@ describe('CDocParser', function(){
context : { type : 'demo' }
}]);

assert.deepEqual(defaultResult.demo[0].test, ['default']);
assert.deepEqual(defaultResult[0].test, ['default']);


parser = new docParser.CommentParser(annotations, {
Expand All @@ -400,7 +398,7 @@ describe('CDocParser', function(){
context : { type : 'demo' }
}]);

assert.deepEqual(defaultTestResult.demo[0].test, ['default', 'extended']);
assert.deepEqual(defaultTestResult[0].test, ['default', 'extended']);

parser = new docParser.CommentParser(annotations, {
autofill : ['otherName']
Expand All @@ -411,7 +409,7 @@ describe('CDocParser', function(){
context : { type : 'demo' }
}]);

assert.deepEqual(otherNameResult.demo[0].test, ['default', 'extended']);
assert.deepEqual(otherNameResult[0].test, ['default', 'extended']);

});

Expand Down

0 comments on commit df5b57b

Please sign in to comment.