Check whether links do exist and throw exception

This commit is contained in:
Vojta Jina 2011-05-17 22:12:23 +02:00 committed by Igor Minar
parent 22f9354c21
commit 2e0e732cad
2 changed files with 58 additions and 5 deletions

View file

@ -96,6 +96,13 @@ describe('ngdoc', function(){
expect(doc.description).toContain('<p>before </p><doc:example>' +
'<pre class="doc-scenario">\n&lt;&gt;\n</pre></doc:example><p>after</p>');
});
it('should store all links', function() {
var doc = new Doc('@description {@link api/angular.link}');
doc.parse();
expect(doc.links).toContain('api/angular.link');
});
describe('sorting', function(){
function property(name) {
@ -197,7 +204,37 @@ describe('ngdoc', function(){
expect(docs[0].methods).toEqual([methodA, methodB]);
expect(docs[0].properties).toEqual([propA, propB]);
});
describe('links checking', function() {
var docs;
beforeEach(function() {
docs = [new Doc({section: 'api', id: 'fake.id1', links: ['non-existing-link']}),
new Doc({section: 'api', id: 'fake.id2'}),
new Doc({section: 'api', id: 'fake.id3'})];
});
it('should throw exception when any link doesn\'t exist', function() {
expect(function() {
ngdoc.merge(docs);
}).toThrow();
});
it('should say which link doesn\'t exist', function() {
try {
ngdoc.merge(docs);
} catch (e) {
expect(e).toContain('non-existing-link');
}
});
it('should say where is the non-existing link', function() {
try {
ngdoc.merge(docs);
} catch (e) {
expect(e).toContain('api/fake.id1');
}
});
});
});
////////////////////////////////////////

View file

@ -29,6 +29,7 @@ function Doc(text, file, line) {
this.param = this.param || [];
this.properties = this.properties || [];
this.methods = this.methods || [];
this.links = this.links || [];
}
Doc.METADATA_IGNORE = (function(){
var words = require('fs').readFileSync(__dirname + '/ignore.words', 'utf8');
@ -124,11 +125,16 @@ Doc.prototype = {
text = text.replace(/<angular\/>/gm, '<tt>&lt;angular/&gt;</tt>');
text = text.replace(/{@link\s+([^\s}]+)\s*([^}]*?)\s*}/g,
function(_all, url, title){
var isFullUrl = url.match(IS_URL);
return '<a href="' + (isFullUrl ? '' + url : '#!' + self.sectionHuristic(url)) + '">'
+ (url.match(IS_ANGULAR) ? '<code>' : '')
var isFullUrl = url.match(IS_URL),
isAngular = url.match(IS_ANGULAR);
url = isFullUrl ? url : self.sectionHuristic(url);
self.links.push(url);
return '<a href="' + (isFullUrl ? '' + url : '#!' + url) + '">'
+ (isAngular ? '<code>' : '')
+ (title || url).replace(/\n/g, ' ')
+ (url.match(IS_ANGULAR) ? '</code>' : '')
+ (isAngular ? '</code>' : '')
+ '</a>';
});
text = new Showdown.converter().makeHtml(text);
@ -659,9 +665,12 @@ function indent(text, spaceCount) {
//////////////////////////////////////////////////////////
function merge(docs){
var byName = {};
var byName = {},
byFullId = {};
docs.forEach(function(doc){
byName[doc.name] = doc;
byFullId[doc.section + '/' + doc.id] = doc;
});
for(var i=0; i<docs.length;) {
if (findParent(docs[i], 'method') ||
@ -671,6 +680,13 @@ function merge(docs){
i++;
}
}
// check links
docs.forEach(function(doc) {
doc.links.forEach(function(link) {
if (!byFullId[link]) throw 'Not existing link "' + link + '" in ' + doc.section + '/' + doc.id;
});
});
function findParent(doc, name){
var parentName = doc[name+'Of'];