From 2d38c6d9ccecdc17c40b6f24c4d36c7999050df8 Mon Sep 17 00:00:00 2001 From: Sophie Kirschner Date: Fri, 9 Oct 2020 11:12:00 +0300 Subject: [PATCH] Add test coverage for new render config object parameter --- mustache.js | 6 +- test/render-test.js | 158 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 158 insertions(+), 6 deletions(-) diff --git a/mustache.js b/mustache.js index 7b67866..213f313 100644 --- a/mustache.js +++ b/mustache.js @@ -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') { diff --git a/test/render-test.js b/test/render-test.js index 0088521..a21d6c3 100644 --- a/test/render-test.js +++ b/test/render-test.js @@ -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 = '<>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<>'; Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']); assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['[[', ']]']), 'foobar<>'); }); + + 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<>'; + + Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']); + assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['[[', ']]'] }), 'foobar<>'); + }); it('does not mutate Mustache.tags when given tags argument', function () { var correctMustacheTags = ['{{', '}}']; @@ -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' }, { @@ -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('>&'), '>&'); + }); + + 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, ''); + assert.equal(outputFalse, ''); + }); }); tests.forEach(function (test) {