Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add test coverage for new render config object parameter
  • Loading branch information
pineapplemachine authored and phillipj committed Oct 22, 2020
1 parent 9891d4f commit 37fdf81
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 6 deletions.
6 changes: 2 additions & 4 deletions mustache.js
Expand Up @@ -746,10 +746,8 @@
};

/**
* Renders the `template` with the given `view` and `partials` using the
* default writer. If the optional `tags` argument is given here it must be an
* array with two string values: the opening and closing tags used in the
* template (e.g. [ "<%", "%>" ]). The default is to mustache.tags.
* Renders the `template` with the given `view`, `partials`, and `config`
* using the default writer.
*/
mustache.render = function render (template, view, partials, config) {
if (typeof template !== 'string') {
Expand Down
158 changes: 156 additions & 2 deletions test/render-test.js
Expand Up @@ -24,21 +24,43 @@ describe('Mustache.render', function () {
Mustache.tags = ['{{', '}}'];
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']), 'foobar{{placeholder}}');
});

it('uses config.tags argument instead of Mustache.tags when given', function () {
var template = '<<placeholder>>bar{{placeholder}}';

Mustache.tags = ['{{', '}}'];
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['<<', '>>']}), 'foobar{{placeholder}}');
});

it('uses tags argument instead of Mustache.tags when given, even when it previous rendered the template using Mustache.tags', function () {
it('uses tags argument instead of Mustache.tags when given, even when it previously rendered the template using Mustache.tags', function () {
var template = '((placeholder))bar{{placeholder}}';

Mustache.tags = ['{{', '}}'];
Mustache.render(template, { placeholder: 'foo' });
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['((', '))']), 'foobar{{placeholder}}');
});

it('uses config.tags argument instead of Mustache.tags when given, even when it previously rendered the template using Mustache.tags', function () {
var template = '((placeholder))bar{{placeholder}}';

Mustache.tags = ['{{', '}}'];
Mustache.render(template, { placeholder: 'foo' });
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['((', '))'] }), 'foobar{{placeholder}}');
});

it('uses tags argument instead of Mustache.tags when given, even when it previous rendered the template using different tags', function () {
it('uses tags argument instead of Mustache.tags when given, even when it previously rendered the template using different tags', function () {
var template = '[[placeholder]]bar<<placeholder>>';

Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']);
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['[[', ']]']), 'foobar<<placeholder>>');
});

it('uses config.tags argument instead of Mustache.tags when given, even when it previously rendered the template using different tags', function () {
var template = '[[placeholder]]bar<<placeholder>>';

Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']);
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['[[', ']]'] }), 'foobar<<placeholder>>');
});

it('does not mutate Mustache.tags when given tags argument', function () {
var correctMustacheTags = ['{{', '}}'];
Expand All @@ -49,6 +71,16 @@ describe('Mustache.render', function () {
assert.equal(Mustache.tags, correctMustacheTags);
assert.deepEqual(Mustache.tags, ['{{', '}}']);
});

it('does not mutate Mustache.tags when given config.tags argument', function () {
var correctMustacheTags = ['{{', '}}'];
Mustache.tags = correctMustacheTags;

Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, { tags: ['((', '))'] });

assert.equal(Mustache.tags, correctMustacheTags);
assert.deepEqual(Mustache.tags, ['{{', '}}']);
});

it('uses provided tags when rendering partials', function () {
var output = Mustache.render('<%> partial %>', { name: 'Santa Claus' }, {
Expand All @@ -57,6 +89,128 @@ describe('Mustache.render', function () {

assert.equal(output, 'Santa Claus');
});

it('uses provided config.tags when rendering partials', function () {
var output = Mustache.render('<%> partial %>', { name: 'Santa Claus' }, {
partial: '<% name %>'
}, { tags: ['<%', '%>'] });

assert.equal(output, 'Santa Claus');
});

it('uses config.escape argument instead of Mustache.escape when given', function () {
var template = 'Hello, {{placeholder}}';

function escapeBang (text) {
return text + '!';
}
assert.equal(Mustache.render(template, { placeholder: 'world' }, {}, { escape: escapeBang }), 'Hello, world!');
});

it('uses config.escape argument instead of Mustache.escape when given, even when it previously rendered the template using Mustache.escape', function () {
var template = 'Hello, {{placeholder}}';

function escapeQuestion (text) {
return text + '?';
}
Mustache.render(template, { placeholder: 'world' });
assert.equal(Mustache.render(template, { placeholder: 'world' }, {}, { escape: escapeQuestion }), 'Hello, world?');
});

it('uses config.escape argument instead of Mustache.escape when given, even when it previously rendered the template using a different escape function', function () {
var template = 'Hello, {{placeholder}}';

function escapeQuestion (text) {
return text + '?';
}
function escapeBang (text) {
return text + '!';
}
Mustache.render(template, { placeholder: 'foo' }, {}, { escape: escapeQuestion });
assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { escape: escapeBang }), 'Hello, foo!');
});

it('does not mutate Mustache.escape when given config.escape argument', function () {
var correctMustacheEscape = Mustache.escape;

function escapeNone (text) {
return text;
}
Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, { escape: escapeNone });

assert.equal(Mustache.escape, correctMustacheEscape);
assert.equal(Mustache.escape('>&'), '&gt;&amp;');
});

it('uses provided config.escape when rendering partials', function () {
function escapeDoubleAmpersand (text) {
return text.replace('&', '&&');
}
var output = Mustache.render('{{> partial }}', { name: 'Ampersand &' }, {
partial: '{{ name }}'
}, { escape: escapeDoubleAmpersand });

assert.equal(output, 'Ampersand &&');
});

it('uses config.tags and config.escape arguments instead of Mustache.tags and Mustache.escape when given', function () {
var template = 'Hello, {{placeholder}} [[placeholder]]';

function escapeTwoBangs (text) {
return text + '!!';
}
var config = {
tags: ['[[', ']]'],
escape: escapeTwoBangs,
};
assert.equal(Mustache.render(template, { placeholder: 'world' }, {}, config), 'Hello, {{placeholder}} world!!');
});

it('uses provided config.tags and config.escape when rendering partials', function () {
function escapeDoubleAmpersand (text) {
return text.replace('&', '&&');
}
var config = {
tags: ['[[', ']]'],
escape: escapeDoubleAmpersand
};
var output = Mustache.render('[[> partial ]]', { name: 'Ampersand &' }, {
partial: '[[ name ]]'
}, config);

assert.equal(output, 'Ampersand &&');
});

it('uses provided config.tags and config.escape when rendering sections', function () {
var template = '<[[&value-raw]] [[#test-1]][[value-1]][[/test-1]][[^test-2]][[value-2]][[/test-2]]>';

function escapeQuotes (text) {
return '"' + text + '"';
}
var config = {
tags: ['[[', ']]'],
escape: escapeQuotes
};
var viewTestTrue = {
'value-raw': 'foo',
'test-1': true,
'value-1': 'abc',
'test-2': true,
'value-2': '123'
};
var viewTestFalse = {
'value-raw': 'foo',
'test-1': false,
'value-1': 'abc',
'test-2': false,
'value-2': '123'
};
var outputTrue = Mustache.render(template, viewTestTrue, {}, config);
var outputFalse = Mustache.render(template, viewTestFalse, {}, config);

assert.equal(outputTrue, '<foo "abc">');
assert.equal(outputFalse, '<foo "123">');
});
});

tests.forEach(function (test) {
Expand Down

0 comments on commit 37fdf81

Please sign in to comment.