diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-01-27 22:48:23 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-01-27 22:48:23 +0100 |
commit | ddc99104d4394cc21eaa615b01d73ec001ac7a16 (patch) | |
tree | 9855406d0a162cf744576c91501480f0450324c7 | |
parent | 8d277e9108afa6a027c61feb581a2150958f8571 (diff) | |
download | gitbook-ddc99104d4394cc21eaa615b01d73ec001ac7a16.zip gitbook-ddc99104d4394cc21eaa615b01d73ec001ac7a16.tar.gz gitbook-ddc99104d4394cc21eaa615b01d73ec001ac7a16.tar.bz2 |
Add unit tests for glossary parsing
-rw-r--r-- | lib/backbone/glossary.js | 7 | ||||
-rw-r--r-- | lib/backbone/summary.js | 8 | ||||
-rw-r--r-- | lib/book.js | 20 | ||||
-rw-r--r-- | lib/generators/index.js | 2 | ||||
-rw-r--r-- | lib/output.js | 2 | ||||
-rw-r--r-- | test/glossary.js | 80 |
6 files changed, 104 insertions, 15 deletions
diff --git a/lib/backbone/glossary.js b/lib/backbone/glossary.js index b25d7c7..0f5e567 100644 --- a/lib/backbone/glossary.js +++ b/lib/backbone/glossary.js @@ -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; diff --git a/lib/backbone/summary.js b/lib/backbone/summary.js index 7eb6e0c..96815a6 100644 --- a/lib/backbone/summary.js +++ b/lib/backbone/summary.js @@ -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'; diff --git a/lib/book.js b/lib/book.js index 91dfa02..77539ce 100644 --- a/lib/book.js +++ b/lib/book.js @@ -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 diff --git a/lib/generators/index.js b/lib/generators/index.js index de8a1e6..dcb2ffe 100644 --- a/lib/generators/index.js +++ b/lib/generators/index.js @@ -1,5 +1,5 @@ var _ = require('lodash'); -var EbookGenerator = require('./ebook'); +//var EbookGenerator = require('./ebook'); module.exports = { json: require('./json'), diff --git a/lib/output.js b/lib/output.js index 1c694a1..2e5bc65 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1,4 +1,4 @@ -var Promise = require('utils/promise'); +var Promise = require('./utils/promise'); var generators = require('./generators'); var PluginsManager = require('./plugins'); diff --git a/test/glossary.js b/test/glossary.js new file mode 100644 index 0000000..e2c9e2b --- /dev/null +++ b/test/glossary.js @@ -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() { + + + + }); + }); + }); + }); + +}); + |