diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-03-24 17:31:15 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-03-24 17:31:15 +0100 |
commit | b38a2c11a40c8e827a0e05d482d25e279c3ea6e5 (patch) | |
tree | 36648f3d4ea28b0f413c8c30ed0712fd74824c73 | |
parent | c31acef47a734ade930aaaa0efa86ec01527a065 (diff) | |
download | gitbook-b38a2c11a40c8e827a0e05d482d25e279c3ea6e5.zip gitbook-b38a2c11a40c8e827a0e05d482d25e279c3ea6e5.tar.gz gitbook-b38a2c11a40c8e827a0e05d482d25e279c3ea6e5.tar.bz2 |
Add tests for plugins
-rw-r--r-- | test/plugins.js | 190 | ||||
-rw-r--r-- | test/plugins/blocks/index.js | 41 | ||||
-rw-r--r-- | test/plugins/blocks/package.json | 9 | ||||
-rw-r--r-- | test/plugins/empty/index.js | 1 | ||||
-rw-r--r-- | test/plugins/empty/package.json | 9 | ||||
-rw-r--r-- | test/plugins/filters/index.js | 10 | ||||
-rw-r--r-- | test/plugins/filters/package.json | 9 | ||||
-rw-r--r-- | test/plugins/invalid/index.js | 1 | ||||
-rw-r--r-- | test/plugins/invalid/package.json | 9 | ||||
-rw-r--r-- | test/plugins/resources/index.js | 12 | ||||
-rw-r--r-- | test/plugins/resources/package.json | 9 |
11 files changed, 300 insertions, 0 deletions
diff --git a/test/plugins.js b/test/plugins.js new file mode 100644 index 0000000..14f4a2f --- /dev/null +++ b/test/plugins.js @@ -0,0 +1,190 @@ +var _ = require('lodash'); +var should = require('should'); +var path = require('path'); + +var Plugin = require('../lib/plugin'); +var PLUGINS_ROOT = path.resolve(__dirname, 'plugins'); + +describe('Plugins', function () { + var book; + + before(function() { + return books.parse("basic") + .then(function(_book) { + book = _book; + }); + }); + + describe('Invalid', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "invalid"); + plugin.load("./invalid", PLUGINS_ROOT); + }); + + it('should be detected', function() { + should(plugin.isValid()).be.exactly(false); + }); + }); + + describe('Empty', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "empty"); + plugin.load("./empty", PLUGINS_ROOT); + }); + + it('should valid a plugin', function() { + should(plugin.isValid()).be.exactly(true); + }); + + it('should return an empty list of resources', function() { + return plugin.getResources() + .then(function(resources) { + _.each(Plugin.RESOURCES, function(resName) { + resources[resName].should.have.lengthOf(0); + }); + }); + }); + }); + + describe('Resources', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "resources"); + plugin.load("./resources", PLUGINS_ROOT); + + return book.plugins.load(plugin); + }); + + it('should valid a plugin', function() { + should(plugin.isValid()).be.exactly(true); + }); + + describe('Website', function() { + it('should return a valid list of resources', function() { + return plugin.getResources("website") + .then(function(resources) { + resources["js"].should.have.lengthOf(1); + }); + }); + + it('should extend books plugins', function() { + var resources = book.plugins.resources("website"); + resources["js"].should.have.lengthOf(1); + }); + }); + + describe('eBook', function() { + it('should return a valid list of resources', function() { + return plugin.getResources("ebook") + .then(function(resources) { + resources["css"].should.have.lengthOf(1); + }); + }); + + it('should extend books plugins', function() { + var resources = book.plugins.resources("ebook"); + resources["css"].should.have.lengthOf(1); + }); + }); + }); + + describe('Filters', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "filters"); + plugin.load("./filters", PLUGINS_ROOT); + + return book.plugins.load(plugin); + }); + + it('should valid a plugin', function() { + should(plugin.isValid()).be.exactly(true); + }); + + it('should return a map of filters', function() { + var filters = plugin.getFilters(); + + _.size(filters).should.equal(2); + filters.should.have.property("hello"); + filters.should.have.property("helloCtx"); + }); + + it('should correctly extend template filters', function() { + return book.template.renderString('{{ "World"|hello }}') + .then(function(content) { + content.should.equal("Hello World"); + }); + }); + + it('should correctly set book as context', function() { + return book.template.renderString('{{ "root"|helloCtx }}') + .then(function(content) { + content.should.equal("root:"+book.root); + }); + }); + }); + + describe('Blocks', function() { + var plugin; + + before(function() { + plugin = new Plugin(book, "blocks"); + plugin.load("./blocks", PLUGINS_ROOT); + + return book.plugins.load(plugin); + }); + + var testTpl = function(str, args, options) { + return book.template.renderString(str, args, options) + .then(book.template.postProcess) + }; + + it('should valid a plugin', function() { + should(plugin.isValid()).be.exactly(true); + }); + + it('should correctly extend template blocks', function() { + return testTpl('{% test %}hello{% endtest %}') + .then(function(content) { + content.should.equal("testhellotest"); + }); + }); + + it('should correctly accept shortcuts', function() { + return testTpl('$$hello$$', {}, { + type: "markdown" + }) + .then(function(content) { + content.should.equal("testhellotest"); + }); + }); + + it('should correctly extend template blocks with defined end', function() { + return testTpl('{% test2 %}hello{% endtest2end %}') + .then(function(content) { + content.should.equal("test2hellotest2"); + }); + }); + + it('should correctly extend template blocks with sub-blocks', function() { + return testTpl('{% test3join separator=";" %}hello{% also %}world{% endtest3join %}') + .then(function(content) { + content.should.equal("hello;world"); + }); + }); + + it('should correctly extend template blocks with different sub-blocks', function() { + return testTpl('{% test4join separator=";" %}hello{% also %}the{% finally %}world{% endtest4join %}') + .then(function(content) { + content.should.equal("hello;the;world"); + }); + }); + }); +}); + diff --git a/test/plugins/blocks/index.js b/test/plugins/blocks/index.js new file mode 100644 index 0000000..32f1910 --- /dev/null +++ b/test/plugins/blocks/index.js @@ -0,0 +1,41 @@ +var assert = require("assert"); + +module.exports = { + blocks: { + "test": { + shortcuts: { + parsers: ["markdown"], + start: "$$", + end: "$$" + }, + process: function(blk) { + return "test"+blk.body+"test"; + } + }, + "test2": { + end: "endtest2end", + process: function(blk) { + return "test2"+blk.body+"test2"; + } + }, + "test3join": { + blocks: [ + "also" + ], + process: function(blk) { + return [blk.body, blk.blocks[0].body].join(blk.kwargs.separator); + } + }, + "test4join": { + blocks: [ + "also", "finally" + ], + process: function(blk) { + assert(blk.blocks.length, 2); + assert(blk.blocks[0].name, "also"); + assert(blk.blocks[1].name, "finally"); + return [blk.body, blk.blocks[0].body, blk.blocks[1].body].join(blk.kwargs.separator); + } + } + } +};
\ No newline at end of file diff --git a/test/plugins/blocks/package.json b/test/plugins/blocks/package.json new file mode 100644 index 0000000..7c41fd3 --- /dev/null +++ b/test/plugins/blocks/package.json @@ -0,0 +1,9 @@ +{ + "name": "gitbook-plugin-blocks", + "description": "Test blocks", + "main": "index.js", + "version": "0.0.1", + "engines": { + "gitbook": "*" + } +}
\ No newline at end of file diff --git a/test/plugins/empty/index.js b/test/plugins/empty/index.js new file mode 100644 index 0000000..a099545 --- /dev/null +++ b/test/plugins/empty/index.js @@ -0,0 +1 @@ +module.exports = {};
\ No newline at end of file diff --git a/test/plugins/empty/package.json b/test/plugins/empty/package.json new file mode 100644 index 0000000..78c7e72 --- /dev/null +++ b/test/plugins/empty/package.json @@ -0,0 +1,9 @@ +{ + "name": "gitbook-plugin-empty", + "description": "Test empty plugin", + "main": "index.js", + "version": "0.0.1", + "engines": { + "gitbook": "*" + } +}
\ No newline at end of file diff --git a/test/plugins/filters/index.js b/test/plugins/filters/index.js new file mode 100644 index 0000000..2cf53b1 --- /dev/null +++ b/test/plugins/filters/index.js @@ -0,0 +1,10 @@ +module.exports = { + filters: { + hello: function(text) { + return "Hello "+text; + }, + helloCtx: function(text) { + return text+":"+this.book.root; + } + } +};
\ No newline at end of file diff --git a/test/plugins/filters/package.json b/test/plugins/filters/package.json new file mode 100644 index 0000000..f1d4e45 --- /dev/null +++ b/test/plugins/filters/package.json @@ -0,0 +1,9 @@ +{ + "name": "gitbook-plugin-filters", + "description": "Test filters", + "main": "index.js", + "version": "0.0.1", + "engines": { + "gitbook": "*" + } +}
\ No newline at end of file diff --git a/test/plugins/invalid/index.js b/test/plugins/invalid/index.js new file mode 100644 index 0000000..a099545 --- /dev/null +++ b/test/plugins/invalid/index.js @@ -0,0 +1 @@ +module.exports = {};
\ No newline at end of file diff --git a/test/plugins/invalid/package.json b/test/plugins/invalid/package.json new file mode 100644 index 0000000..da34090 --- /dev/null +++ b/test/plugins/invalid/package.json @@ -0,0 +1,9 @@ +{ + "name": "gitbook-plugin-invalid", + "description": "Test invalid plugin", + "main": "index.js", + "version": "0.0.1", + "engines": { + "gitbook": "<2.0.0" + } +}
\ No newline at end of file diff --git a/test/plugins/resources/index.js b/test/plugins/resources/index.js new file mode 100644 index 0000000..bafa54b --- /dev/null +++ b/test/plugins/resources/index.js @@ -0,0 +1,12 @@ +module.exports = { + website: { + js: [ + "https://cdn.mathjax.org/mathjax/2.4-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" + ] + }, + ebook: { + css: [ + "test" + ] + } +}; diff --git a/test/plugins/resources/package.json b/test/plugins/resources/package.json new file mode 100644 index 0000000..ab4320d --- /dev/null +++ b/test/plugins/resources/package.json @@ -0,0 +1,9 @@ +{ + "name": "gitbook-plugin-resources", + "description": "Test resources plugin", + "main": "index.js", + "version": "0.0.1", + "engines": { + "gitbook": "*" + } +}
\ No newline at end of file |