You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
8.5KB

  1. require('./helper');
  2. var renderHelper = require('./render-helper');
  3. var tests = renderHelper.getTests();
  4. describe('Mustache.render', function () {
  5. beforeEach(function () {
  6. Mustache.clearCache();
  7. });
  8. it('requires template to be a string', function () {
  9. assert.throws(function () {
  10. Mustache.render(['dummy template'], ['foo', 'bar']);
  11. }, TypeError, 'Invalid template! Template should be a "string" but ' +
  12. '"array" was given as the first argument ' +
  13. 'for mustache#render(template, view, partials)');
  14. });
  15. describe('custom tags', function () {
  16. it('uses tags argument instead of Mustache.tags when given', function () {
  17. var template = '<<placeholder>>bar{{placeholder}}';
  18. Mustache.tags = ['{{', '}}'];
  19. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']), 'foobar{{placeholder}}');
  20. });
  21. it('uses config.tags argument instead of Mustache.tags when given', function () {
  22. var template = '<<placeholder>>bar{{placeholder}}';
  23. Mustache.tags = ['{{', '}}'];
  24. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['<<', '>>']}), 'foobar{{placeholder}}');
  25. });
  26. it('uses tags argument instead of Mustache.tags when given, even when it previously rendered the template using Mustache.tags', function () {
  27. var template = '((placeholder))bar{{placeholder}}';
  28. Mustache.tags = ['{{', '}}'];
  29. Mustache.render(template, { placeholder: 'foo' });
  30. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['((', '))']), 'foobar{{placeholder}}');
  31. });
  32. it('uses config.tags argument instead of Mustache.tags when given, even when it previously rendered the template using Mustache.tags', function () {
  33. var template = '((placeholder))bar{{placeholder}}';
  34. Mustache.tags = ['{{', '}}'];
  35. Mustache.render(template, { placeholder: 'foo' });
  36. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['((', '))'] }), 'foobar{{placeholder}}');
  37. });
  38. it('uses tags argument instead of Mustache.tags when given, even when it previously rendered the template using different tags', function () {
  39. var template = '[[placeholder]]bar<<placeholder>>';
  40. Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']);
  41. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['[[', ']]']), 'foobar<<placeholder>>');
  42. });
  43. it('uses config.tags argument instead of Mustache.tags when given, even when it previously rendered the template using different tags', function () {
  44. var template = '[[placeholder]]bar<<placeholder>>';
  45. Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']);
  46. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { tags: ['[[', ']]'] }), 'foobar<<placeholder>>');
  47. });
  48. it('does not mutate Mustache.tags when given tags argument', function () {
  49. var correctMustacheTags = ['{{', '}}'];
  50. Mustache.tags = correctMustacheTags;
  51. Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, ['((', '))']);
  52. assert.equal(Mustache.tags, correctMustacheTags);
  53. assert.deepEqual(Mustache.tags, ['{{', '}}']);
  54. });
  55. it('does not mutate Mustache.tags when given config.tags argument', function () {
  56. var correctMustacheTags = ['{{', '}}'];
  57. Mustache.tags = correctMustacheTags;
  58. Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, { tags: ['((', '))'] });
  59. assert.equal(Mustache.tags, correctMustacheTags);
  60. assert.deepEqual(Mustache.tags, ['{{', '}}']);
  61. });
  62. it('uses provided tags when rendering partials', function () {
  63. var output = Mustache.render('<%> partial %>', { name: 'Santa Claus' }, {
  64. partial: '<% name %>'
  65. }, ['<%', '%>']);
  66. assert.equal(output, 'Santa Claus');
  67. });
  68. it('uses provided config.tags when rendering partials', function () {
  69. var output = Mustache.render('<%> partial %>', { name: 'Santa Claus' }, {
  70. partial: '<% name %>'
  71. }, { tags: ['<%', '%>'] });
  72. assert.equal(output, 'Santa Claus');
  73. });
  74. it('uses config.escape argument instead of Mustache.escape when given', function () {
  75. var template = 'Hello, {{placeholder}}';
  76. function escapeBang (text) {
  77. return text + '!';
  78. }
  79. assert.equal(Mustache.render(template, { placeholder: 'world' }, {}, { escape: escapeBang }), 'Hello, world!');
  80. });
  81. it('uses config.escape argument instead of Mustache.escape when given, even when it previously rendered the template using Mustache.escape', function () {
  82. var template = 'Hello, {{placeholder}}';
  83. function escapeQuestion (text) {
  84. return text + '?';
  85. }
  86. Mustache.render(template, { placeholder: 'world' });
  87. assert.equal(Mustache.render(template, { placeholder: 'world' }, {}, { escape: escapeQuestion }), 'Hello, world?');
  88. });
  89. it('uses config.escape argument instead of Mustache.escape when given, even when it previously rendered the template using a different escape function', function () {
  90. var template = 'Hello, {{placeholder}}';
  91. function escapeQuestion (text) {
  92. return text + '?';
  93. }
  94. function escapeBang (text) {
  95. return text + '!';
  96. }
  97. Mustache.render(template, { placeholder: 'foo' }, {}, { escape: escapeQuestion });
  98. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, { escape: escapeBang }), 'Hello, foo!');
  99. });
  100. it('does not mutate Mustache.escape when given config.escape argument', function () {
  101. var correctMustacheEscape = Mustache.escape;
  102. function escapeNone (text) {
  103. return text;
  104. }
  105. Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, { escape: escapeNone });
  106. assert.equal(Mustache.escape, correctMustacheEscape);
  107. assert.equal(Mustache.escape('>&'), '&gt;&amp;');
  108. });
  109. it('uses provided config.escape when rendering partials', function () {
  110. function escapeDoubleAmpersand (text) {
  111. return text.replace('&', '&&');
  112. }
  113. var output = Mustache.render('{{> partial }}', { name: 'Ampersand &' }, {
  114. partial: '{{ name }}'
  115. }, { escape: escapeDoubleAmpersand });
  116. assert.equal(output, 'Ampersand &&');
  117. });
  118. it('uses config.tags and config.escape arguments instead of Mustache.tags and Mustache.escape when given', function () {
  119. var template = 'Hello, {{placeholder}} [[placeholder]]';
  120. function escapeTwoBangs (text) {
  121. return text + '!!';
  122. }
  123. var config = {
  124. tags: ['[[', ']]'],
  125. escape: escapeTwoBangs,
  126. };
  127. assert.equal(Mustache.render(template, { placeholder: 'world' }, {}, config), 'Hello, {{placeholder}} world!!');
  128. });
  129. it('uses provided config.tags and config.escape when rendering partials', function () {
  130. function escapeDoubleAmpersand (text) {
  131. return text.replace('&', '&&');
  132. }
  133. var config = {
  134. tags: ['[[', ']]'],
  135. escape: escapeDoubleAmpersand
  136. };
  137. var output = Mustache.render('[[> partial ]]', { name: 'Ampersand &' }, {
  138. partial: '[[ name ]]'
  139. }, config);
  140. assert.equal(output, 'Ampersand &&');
  141. });
  142. it('uses provided config.tags and config.escape when rendering sections', function () {
  143. var template = '<[[&value-raw]] [[#test-1]][[value-1]][[/test-1]][[^test-2]][[value-2]][[/test-2]]>';
  144. function escapeQuotes (text) {
  145. return '"' + text + '"';
  146. }
  147. var config = {
  148. tags: ['[[', ']]'],
  149. escape: escapeQuotes
  150. };
  151. var viewTestTrue = {
  152. 'value-raw': 'foo',
  153. 'test-1': true,
  154. 'value-1': 'abc',
  155. 'test-2': true,
  156. 'value-2': '123'
  157. };
  158. var viewTestFalse = {
  159. 'value-raw': 'foo',
  160. 'test-1': false,
  161. 'value-1': 'abc',
  162. 'test-2': false,
  163. 'value-2': '123'
  164. };
  165. var outputTrue = Mustache.render(template, viewTestTrue, {}, config);
  166. var outputFalse = Mustache.render(template, viewTestFalse, {}, config);
  167. assert.equal(outputTrue, '<foo "abc">');
  168. assert.equal(outputFalse, '<foo "123">');
  169. });
  170. });
  171. tests.forEach(function (test) {
  172. var view = eval(test.view);
  173. it('knows how to render ' + test.name, function () {
  174. var output;
  175. if (test.partial) {
  176. output = Mustache.render(test.template, view, { partial: test.partial });
  177. } else {
  178. output = Mustache.render(test.template, view);
  179. }
  180. output.should.equal(test.expect);
  181. });
  182. });
  183. });