Navigation Menu

Skip to content

Commit

Permalink
[test] [haml] [fix] Add macro and unit test for sync render(). Fix un…
Browse files Browse the repository at this point in the history
…it test error for haml's render() by setting default options to empty object
  • Loading branch information
eschmitt committed Sep 6, 2012
1 parent ad95d66 commit 6b0d343
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/engines/haml/index.js
Expand Up @@ -8,6 +8,7 @@ exports.attach = function (options) {
this.haml = {
render: function (view, data, cb) {
var html;
options = options || {};
options.locals = data;
try {
html = haml.render(view.template, options).trimLeft();
Expand Down
35 changes: 35 additions & 0 deletions test/helpers/index.js
Expand Up @@ -63,12 +63,39 @@ helpers.generateEngineTests = function generateEngineTests(engines, data) {
return batch;
};

helpers.renderSyncUnit = function renderSyncUnit(mockView, data, expected) {
expected = expected || '';
if (expected === '') {
return {
topic: function (engine) {
var msg = mockView.input
+ ' template engine cannot render synchronously';
try { engine.render(mockView, data); }
catch (err) { this.callback(err, msg); }
}
, 'should error': function (err, message) {
assert.isObject(err);
assert.equal(err.message, message);
}
};
}
return {
topic: function (engine) { return engine.render(mockView, data); }
, 'should compile expected result': function (html) {
assert.equal(html, expected);
}
};
};

helpers.generateEngineUnitTests = function generateEngineUnitTests(engines, data) {
var batch = {};
Object.keys(engines).forEach(function (key) {
var description = 'The ' + key + ' plugin'
, expected = engines[key].expected
, syncExpected = engines[key].syncRender ? expected : ''
, mockView = { template: engines[key].template
, input: key
}
;
batch[description] = {
topic: require('../../lib/engines/' + key + '/index')
Expand All @@ -90,6 +117,14 @@ helpers.generateEngineUnitTests = function generateEngineUnitTests(engines, data
assert.isFunction(plugin[key].render);
}
}
, 'when attached and initialized': {
topic: function (plugin) {
plugin.attach();
plugin.init(function (err) { if (err) console.dir(err); });
return plugin[key];
}
, 'and rendering sync': helpers.renderSyncUnit(mockView, data, syncExpected)
}
};
});
return batch;
Expand Down

0 comments on commit 6b0d343

Please sign in to comment.