diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-03-09 10:43:12 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-03-09 10:43:12 +0100 |
commit | 34fc2831e0cf0fed01c71cec28d93472d87f455b (patch) | |
tree | a803cc907c20491ba02863b5d3dd5aedf6bfed10 /test/plugins.js | |
parent | e1594cde2c32e4ff48f6c4eff3d3d461743d74e1 (diff) | |
parent | 1bf68a5aa0703b5a1815cfe4ebb731b5fb6ed9d2 (diff) | |
download | gitbook-34fc2831e0cf0fed01c71cec28d93472d87f455b.zip gitbook-34fc2831e0cf0fed01c71cec28d93472d87f455b.tar.gz gitbook-34fc2831e0cf0fed01c71cec28d93472d87f455b.tar.bz2 |
Merge branch 'version/2.0'
Diffstat (limited to 'test/plugins.js')
-rw-r--r-- | test/plugins.js | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/test/plugins.js b/test/plugins.js new file mode 100644 index 0000000..c7d1f90 --- /dev/null +++ b/test/plugins.js @@ -0,0 +1,189 @@ +var path = require('path'); +var _ = require('lodash'); +var assert = require('assert'); +var fs = require("fs"); + +var Plugin = require("../lib/plugin"); + +var PLUGINS_ROOT = path.resolve(__dirname, "plugins"); + +describe('Plugins', function () { + describe('invalid plugin', function() { + it('should signal as invalid', function() { + var plugin = new Plugin(books[0], "invalid"); + plugin.load("./invalid", PLUGINS_ROOT); + assert(!plugin.isValid()); + }); + }); + + describe('empty plugin', function() { + var plugin = new Plugin(books[0], "invalid"); + plugin.load("./empty", PLUGINS_ROOT); + + it('should valid a plugin', function() { + assert(plugin.isValid()); + }); + + it('should return an empty list of resources', function(done) { + qdone( + plugin.getResources() + .then(function(resources) { + _.each(Plugin.RESOURCES, function(resName) { + assert.equal(resources[resName].length, 0); + }); + }), + done); + }); + }); + + describe('resources plugin', function() { + var plugin = new Plugin(books[0], "resources"); + plugin.load("./resources", PLUGINS_ROOT); + + before(function(done) { + qdone(books[0].plugins.load(plugin), done); + }); + + it('should valid a plugin', function() { + assert(plugin.isValid()); + }); + + it('should return a valid list of resources (website)', function(done) { + qdone( + plugin.getResources("website") + .then(function(resources) { + assert.equal(resources["js"].length, 1); + }), + done); + }); + + it('should return a valid list of resources (ebook)', function(done) { + qdone( + plugin.getResources("ebook") + .then(function(resources) { + assert.equal(resources["css"].length, 1); + }), + done); + }); + + it('should extend books plugins (website)', function() { + var resources = books[0].plugins.resources("website"); + assert.equal(resources["js"].length, 1); + }); + + it('should extend books plugins (ebook)', function() { + var resources = books[0].plugins.resources("ebook"); + assert.equal(resources["css"].length, 1); + }); + }); + + describe('filters', function() { + var plugin = new Plugin(books[0], "filters"); + plugin.load("./filters", PLUGINS_ROOT); + + before(function(done) { + qdone(books[0].plugins.load(plugin), done); + }); + + it('should valid a plugin', function() { + assert(plugin.isValid()); + }); + + it('should return a map of filters', function() { + var filters = plugin.getFilters(); + assert.equal(_.size(filters), 2); + assert(filters["hello"]); + assert(filters["helloCtx"]); + }); + + it('should correctly extend template filters', function(done) { + qdone( + books[0].template.renderString('{{ "World"|hello }}') + .then(function(content) { + assert.equal(content, "Hello World"); + }), + done + ); + }); + + it('should correctly set book as context', function(done) { + qdone( + books[0].template.renderString('{{ "root"|helloCtx }}') + .then(function(content) { + assert.equal(content, "root:"+books[0].root); + }), + done + ); + }); + }); + + describe('blocks', function() { + var plugin = new Plugin(books[0], "blocks"); + plugin.load("./blocks", PLUGINS_ROOT); + + var testTpl = function(str, args, options) { + return books[0].template.renderString(str, args, options) + .then(books[0].template.postProcess) + }; + + before(function(done) { + qdone(books[0].plugins.load(plugin), done); + }); + + it('should valid a plugin', function() { + assert(plugin.isValid()); + }); + + it('should correctly extend template blocks', function(done) { + qdone( + testTpl('{% test %}hello{% endtest %}') + .then(function(content) { + assert.equal(content, "testhellotest"); + }), + done + ); + }); + + it('should correctly accept shortcuts', function(done) { + qdone( + testTpl('$$hello$$', {}, { + type: "markdown" + }) + .then(function(content) { + assert.equal(content, "testhellotest"); + }), + done + ); + }); + + it('should correctly extend template blocks with defined end', function(done) { + qdone( + testTpl('{% test2 %}hello{% endtest2end %}') + .then(function(content) { + assert.equal(content, "test2hellotest2"); + }), + done + ); + }); + + it('should correctly extend template blocks with sub-blocks', function(done) { + qdone( + testTpl('{% test3join separator=";" %}hello{% also %}world{% endtest3join %}') + .then(function(content) { + assert.equal(content, "hello;world"); + }), + done + ); + }); + + it('should correctly extend template blocks with different sub-blocks', function(done) { + qdone( + testTpl('{% test4join separator=";" %}hello{% also %}the{% finally %}world{% endtest4join %}') + .then(function(content) { + assert.equal(content, "hello;the;world"); + }), + done + ); + }); + }); +}); |