Add unit tests for glossary parsing

This commit is contained in:
Samy Pessé
2016-01-27 22:48:23 +01:00
parent 8d277e9108
commit ddc99104d4
6 changed files with 104 additions and 15 deletions
+6 -1
View File
@@ -7,7 +7,7 @@ A glossary entry is represented by a name and a short description
An unique id for the entry is generated using its name
*/
function GlossaryEntry(name, description) {
if (!(this instanceof Glossary)) return new Glossary();
if (!(this instanceof GlossaryEntry)) return new GlossaryEntry(name, description);
this.name = name;
this.description = description;
@@ -54,4 +54,9 @@ Glossary.prototype.get = function(id) {
});
};
// Return false if glossary has entries (and exists)
Glossary.prototype.isEmpty = function(id) {
return _.size(this.entries) === 0;
};
module.exports = Glossary;
+7 -1
View File
@@ -1,8 +1,14 @@
var util = require('util');
var BackboneFile = require('./file');
var Article = require('./article');
function Summary() {
if (!(this instanceof Summary)) return new Summary();
BackboneFile.apply(this, arguments);
this.articles = [];
}
util.inherits(Summary, BackboneFile);
Summary.prototype.type = 'summary';
+9 -11
View File
@@ -1,5 +1,4 @@
var _ = require('lodash');
var Q = require('q');
var path = require('path');
var Ignore = require('ignore');
var parsers = require('gitbook-parsers');
@@ -12,6 +11,7 @@ var Summary = require('./backbone/summary');
var Langs = require('./backbone/langs');
var Page = require('./backbone/page');
var pathUtil = require('./utils/path');
var Promise = require('./utils/promise');
function Book(opts) {
if (!(this instanceof Book)) return new Book(opts);
@@ -106,25 +106,23 @@ Book.prototype.resolve = function() {
Book.prototype.parseIgnoreRules = function() {
var that = this;
return _.reduce([
return Promise.series([
'.ignore',
'.gitignore',
'.bookignore'
], function(prev, filename) {
return prev.then(function() {
return that.readFile(filename);
})
], function(filename) {
return that.readFile(filename)
.then(function(content) {
that.ignore.addPattern(content.toString().split(/\r?\n/));
});
}, Q());
});
};
// Parse the whole book
Book.prototype.parse = function() {
var that = this;
return Q()
return Promise()
.then(this.prepareConfig)
.then(this.parseIgnoreRules)
@@ -139,7 +137,7 @@ Book.prototype.parse = function() {
return;
}
return Q()
return Promise()
.then(that.readme.load)
.then(function() {
if (that.readme.exists()) return;
@@ -185,7 +183,7 @@ Book.prototype.isFileIgnored = function(filename) {
// Read a file in the book, throw error if ignored
Book.prototype.readFile = function(filename) {
if (this.isFileIgnored(filename)) return Q.reject(new Error('File "'+filename+'" is ignored'));
if (this.isFileIgnored(filename)) return Promise.reject(new Error('File "'+filename+'" is ignored'));
return this.fs.readAsString(this.resolve(filename));
};
@@ -217,7 +215,7 @@ Book.prototype.findParsableFile = function(filename) {
};
});
});
}, Q(null));
}, Promise(null));
};
// Return true if book is associated to a language
+1 -1
View File
@@ -1,5 +1,5 @@
var _ = require('lodash');
var EbookGenerator = require('./ebook');
//var EbookGenerator = require('./ebook');
module.exports = {
json: require('./json'),
+1 -1
View File
@@ -1,4 +1,4 @@
var Promise = require('utils/promise');
var Promise = require('./utils/promise');
var generators = require('./generators');
var PluginsManager = require('./plugins');
+80
View File
@@ -0,0 +1,80 @@
var should = require('should');
var mock = require('./mock');
describe('Glossary', function() {
describe('Parsing', function() {
it('should parse empty glossary', function() {
return mock.setupDefaultBook({
'GLOSSARY.md': ''
})
.then(function(book) {
return book.prepareConfig()
.then(function() {
return book.glossary.load();
})
.then(function() {
book.glossary.isEmpty().should.be.true();
});
});
});
describe('Non-empty glossary', function() {
var book;
before(function() {
return mock.setupDefaultBook({
'GLOSSARY.md': '# Glossary\n\n### Hello World\n\nThis is an entry'
})
.then(function(_book) {
book = _book;
return book.prepareConfig();
})
.then(function() {
return book.glossary.load();
});
});
it('should not be empty', function() {
book.glossary.isEmpty().should.be.false();
});
describe('glossary.get', function() {
it('should return an existing entry', function() {
var entry = book.glossary.get('hello_world');
should.exist(entry);
entry.name.should.equal('Hello World');
entry.description.should.equal('This is an entry');
entry.id.should.equal('hello_world');
});
it('should undefined return non existing entry', function() {
var entry = book.glossary.get('cool');
should.not.exist(entry);
});
});
});
it('should parse glossary entries', function() {
return mock.setupDefaultBook({
'GLOSSARY.md': '# Glossary\n\n### Hello World\n\nThis is an entry'
})
.then(function(book) {
return book.prepareConfig()
.then(function() {
return book.glossary.load();
})
.then(function() {
});
});
});
});
});