/**
* Module dependencies.
*/
var dox = require('../')
, should = require('should')
, fs = require('fs');
function fixture(name, fn) {
fs.readFile(__dirname + '/fixtures/' + name, 'utf8', fn);
}
module.exports = {
'test .version': function(){
dox.version.should.match(/^\d+\.\d+\.\d+$/);
},
'test .parseComments() blocks': function(done){
fixture('a.js', function(err, str){
var comments = dox.parseComments(str)
, file = comments.shift()
, version = comments.shift();
file.should.have.property('ignore', true);
file.description.full.should.equal('
A
Copyright (c) 2010 Author Name
MIT Licensed
');
file.description.summary.should.equal('A
Copyright (c) 2010 Author Name
MIT Licensed
');
file.description.body.should.equal('');
file.tags.should.be.empty;
version.should.have.property('ignore', false);
version.description.full.should.equal('Library version.
');
version.description.summary.should.equal('Library version.
');
version.description.body.should.equal('');
version.tags.should.be.empty;
done();
});
},
'test .parseComments() tags': function(done){
fixture('b.js', function(err, str){
var comments = dox.parseComments(str);
var version = comments.shift();
version.description.summary.should.equal('Library version.
');
version.description.full.should.equal('Library version.
');
version.tags.should.have.length(2);
version.tags[0].type.should.equal('type');
version.tags[0].types.should.eql(['String']);
version.tags[1].type.should.equal('api');
version.tags[1].visibility.should.equal('public');
version.ctx.type.should.equal('property');
version.ctx.receiver.should.equal('exports');
version.ctx.name.should.equal('version');
version.ctx.value.should.equal("'0.0.1'");
var parse = comments.shift();
parse.description.summary.should.equal('Parse the given str
.
');
parse.description.body.should.equal('Examples
\n\nparse(str)\n// => "wahoo"\n
');
parse.description.full.should.equal('Parse the given str
.
\n\nExamples
\n\nparse(str)\n// => "wahoo"\n
');
parse.tags[0].type.should.equal('param');
parse.tags[0].name.should.equal('str');
parse.tags[0].description.should.equal('to parse');
parse.tags[0].types.should.eql(['String', 'Buffer']);
parse.tags[1].type.should.equal('return');
parse.tags[1].types.should.eql(['String']);
parse.tags[2].visibility.should.equal('public');
done();
});
},
'test .parseComments() complex': function(done){
fixture('c.js', function(err, str){
var comments = dox.parseComments(str);
var file = comments.shift();
file.tags.should.be.empty;
// the following doesn't work as gh-md now obfuscates emails different on every pass
//file.description.full.should.equal('Dox
Copyright (c) 2010 TJ Holowaychuk tj@vision-media.ca
MIT Licensed
');
file.description.full.should.be.a('string');
file.ignore.should.be.true;
var mods = comments.shift();
mods.tags.should.be.empty;
mods.description.full.should.equal('Module dependencies.
');
mods.description.summary.should.equal('Module dependencies.
');
mods.description.body.should.equal('');
mods.ignore.should.be.false;
mods.code.should.equal('var markdown = require(\'github-flavored-markdown\').parse;');
mods.ctx.type.should.equal('declaration');
mods.ctx.name.should.equal('markdown');
mods.ctx.value.should.equal('require(\'github-flavored-markdown\').parse');
var version = comments.shift();
version.tags.should.be.empty;
version.description.full.should.equal('Library version.
');
var parseComments = comments.shift();
parseComments.tags.should.have.length(4);
parseComments.ctx.type.should.equal('method');
parseComments.ctx.receiver.should.equal('exports');
parseComments.ctx.name.should.equal('parseComments');
parseComments.description.full.should.equal('Parse comments in the given string of js
.
');
parseComments.description.summary.should.equal('Parse comments in the given string of js
.
');
parseComments.description.body.should.equal('');
var parseComment = comments.shift();
parseComment.tags.should.have.length(4);
parseComment.description.summary.should.equal('Parse the given comment str
.
');
parseComment.description.full.should.equal('Parse the given comment str
.
\n\nThe comment object returned contains the following
\n\n\ntags
array of tag objects \ndescription
the first line of the comment \nbody
lines following the description \ncontent
both the description and the body \nisPrivate
true when "@api private" is used \n
');
parseComment.description.body.should.equal('The comment object returned contains the following
\n\n\ntags
array of tag objects \ndescription
the first line of the comment \nbody
lines following the description \ncontent
both the description and the body \nisPrivate
true when "@api private" is used \n
');
var escape = comments.pop();
escape.tags.should.have.length(3);
escape.description.full.should.equal('Escape the given html
.
');
escape.ctx.type.should.equal('function');
escape.ctx.name.should.equal('escape');
done();
});
},
'test .parseComments() tags': function (done){
fixture('d.js', function(err, str){
var comments = dox.parseComments(str);
var first = comments.shift();
first.tags.should.have.length(4);
first.description.full.should.equal('Parse tag type string "{Array|Object}" etc.
');
first.description.summary.should.equal('Parse tag type string "{Array|Object}" etc.
');
first.description.body.should.equal('');
first.ctx.type.should.equal('method');
first.ctx.receiver.should.equal('exports');
first.ctx.name.should.equal('parseTagTypes');
first.code.should.equal('exports.parseTagTypes = function(str) {\n return str\n .replace(/[{}]/g, \'\')\n .split(/ *[|,\\/] */);\n};');
done();
});
},
'test .parseComments() code': function(done){
fixture('b.js', function(err, str){
var comments = dox.parseComments(str)
, version = comments.shift()
, parse = comments.shift();
version.code.should.equal("exports.version = '0.0.1';");
parse.code.should.equal('exports.parse = function(str) {\n return "wahoo";\n}');
done();
});
},
'test .parseComments() titles': function(done){
fixture('titles.js', function(err, str){
var comments = dox.parseComments(str);
comments[0].description.body.should.include('Some examples
');
comments[0].description.body.should.not.include('for example
');
comments[0].description.body.should.include('Some longer thing
for example:
');
done();
});
},
'test .parseCodeContext() function statement': function(){
var ctx = dox.parseCodeContext('function foo(){\n\n}');
ctx.type.should.equal('function');
ctx.name.should.equal('foo');
},
'test .parseCodeContext() function expression': function(){
var ctx = dox.parseCodeContext('var foo = function(){\n\n}');
ctx.type.should.equal('function');
ctx.name.should.equal('foo');
},
'test .parseCodeContext() prototype method': function(){
var ctx = dox.parseCodeContext('User.prototype.save = function(){}');
ctx.type.should.equal('method');
ctx.constructor.should.equal('User');
ctx.name.should.equal('save');
},
'test .parseCodeContext() prototype property': function(){
var ctx = dox.parseCodeContext('Database.prototype.enabled = true;\nasdf');
ctx.type.should.equal('property');
ctx.constructor.should.equal('Database');
ctx.name.should.equal('enabled');
ctx.value.should.equal('true');
},
'test .parseCodeContext() method': function(){
var ctx = dox.parseCodeContext('user.save = function(){}');
ctx.type.should.equal('method');
ctx.receiver.should.equal('user');
ctx.name.should.equal('save');
},
'test .parseCodeContext() property': function(){
var ctx = dox.parseCodeContext('user.name = "tj";\nasdf');
ctx.type.should.equal('property');
ctx.receiver.should.equal('user');
ctx.name.should.equal('name');
ctx.value.should.equal('"tj"');
},
'test .parseCodeContext() declaration': function(){
var ctx = dox.parseCodeContext('var name = "tj";\nasdf');
ctx.type.should.equal('declaration');
ctx.name.should.equal('name');
ctx.value.should.equal('"tj"');
},
'test .parseTag() @constructor': function(){
var tag = dox.parseTag('@constructor');
tag.type.should.equal('constructor');
},
'test .parseTag() @see': function(){
var tag = dox.parseTag('@see http://google.com');
tag.type.should.equal('see');
tag.title.should.equal('');
tag.url.should.equal('http://google.com');
var tag = dox.parseTag('@see Google http://google.com');
tag.type.should.equal('see');
tag.title.should.equal('Google');
tag.url.should.equal('http://google.com');
var tag = dox.parseTag('@see exports.parseComment');
tag.type.should.equal('see');
tag.local.should.equal('exports.parseComment');
},
'test .parseTag() @api': function(){
var tag = dox.parseTag('@api private');
tag.type.should.equal('api');
tag.visibility.should.equal('private');
},
'test .parseTag() @type': function(){
var tag = dox.parseTag('@type {String}');
tag.type.should.equal('type');
tag.types.should.eql(['String']);
},
'test .parseTag() @param': function(){
var tag = dox.parseTag('@param {String|Buffer}');
tag.type.should.equal('param');
tag.types.should.eql(['String', 'Buffer']);
tag.name.should.equal('');
tag.description.should.equal('');
},
'test .parseTag() @return': function(){
var tag = dox.parseTag('@return {String} a normal string');
tag.type.should.equal('return');
tag.types.should.eql(['String']);
tag.description.should.equal('a normal string');
},
'test .parseTag() @augments': function(){
var tag = dox.parseTag('@augments otherClass');
tag.type.should.equal('augments');
tag.otherClass.should.equal('otherClass')
},
'test .parseTag() @author': function(){
var tag = dox.parseTag('@author Bob Bobson');
tag.type.should.equal('author');
tag.string.should.equal('Bob Bobson');
},
'test .parseTag() @borrows': function(){
var tag = dox.parseTag('@borrows foo as bar');
tag.type.should.equal('borrows');
tag.otherMemberName.should.equal('foo');
tag.thisMemberName.should.equal('bar');
},
'test .parseTag() @memberOf': function(){
var tag = dox.parseTag('@memberOf Foo.bar')
tag.type.should.equal('memberOf')
tag.parent.should.equal('Foo.bar')
},
'test .parseTag() default': function(){
var tag = dox.parseTag('@hello universe is better than world');
tag.type.should.equal('hello');
tag.string.should.equal('universe is better than world');
}
};