mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 01:53:26 +00:00
Improve tests for plugins with filters
This commit is contained in:
+4
-1
@@ -115,7 +115,10 @@ Plugin.prototype.getFilters = function() {
|
||||
return [
|
||||
key,
|
||||
function() {
|
||||
var ctx = this;
|
||||
var ctx = {
|
||||
ctx: this.ctx,
|
||||
book: that.book
|
||||
};
|
||||
var args = Array.prototype.slice.apply(arguments);
|
||||
var callback = _.last(args);
|
||||
|
||||
|
||||
+7
-5
@@ -69,14 +69,16 @@ var TemplateEngine = function(book) {
|
||||
|
||||
|
||||
// Add filter
|
||||
TemplateEngine.prototype.addFilter = function(key, filter) {
|
||||
if (this.env.getFilter(filterName)) {
|
||||
TemplateEngine.prototype.addFilter = function(filterName, func) {
|
||||
try {
|
||||
this.env.getFilter(filterName);
|
||||
this.log.warn.ln("conflict in filters, '"+filterName+"' is already set");
|
||||
return false;
|
||||
} catch(e) {
|
||||
this.log.debug.ln("add filter '"+filterName+"'");
|
||||
this.env.addFilter(filterName, func, true);
|
||||
return true;
|
||||
}
|
||||
this.log.debug.ln("add filter '"+key+"'");
|
||||
this.env.addFilter(key, func, true);
|
||||
return true;
|
||||
};
|
||||
|
||||
// Render a string from the book
|
||||
|
||||
+26
-1
@@ -40,14 +40,39 @@ describe('Plugins', 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), 1);
|
||||
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
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,9 @@ module.exports = {
|
||||
filters: {
|
||||
hello: function(text) {
|
||||
return "Hello "+text;
|
||||
},
|
||||
helloCtx: function(text) {
|
||||
return text+":"+this.book.root;
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user