Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

88 lignes
3.0KB

  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('preserve indentation when using partials', function() {
  16. it ('should preserve indentation with whitespaces', function() {
  17. var template = 'a\n {{>p1}}';
  18. var renderResult = Mustache.render(template, {}, {p1: 'l1\nl2'});
  19. assert.equal(renderResult, 'a\n l1\n l2');
  20. });
  21. });
  22. describe('custom tags', function () {
  23. it('uses tags argument instead of Mustache.tags when given', function () {
  24. var template = '<<placeholder>>bar{{placeholder}}';
  25. Mustache.tags = ['{{', '}}'];
  26. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']), 'foobar{{placeholder}}');
  27. });
  28. it('uses tags argument instead of Mustache.tags when given, even when it previous rendered the template using Mustache.tags', function () {
  29. var template = '((placeholder))bar{{placeholder}}';
  30. Mustache.tags = ['{{', '}}'];
  31. Mustache.render(template, { placeholder: 'foo' });
  32. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['((', '))']), 'foobar{{placeholder}}');
  33. });
  34. it('uses tags argument instead of Mustache.tags when given, even when it previous rendered the template using different tags', function () {
  35. var template = '[[placeholder]]bar<<placeholder>>';
  36. Mustache.render(template, { placeholder: 'foo' }, {}, ['<<', '>>']);
  37. assert.equal(Mustache.render(template, { placeholder: 'foo' }, {}, ['[[', ']]']), 'foobar<<placeholder>>');
  38. });
  39. it('does not mutate Mustache.tags when given tags argument', function() {
  40. var correctMustacheTags = ['{{', '}}'];
  41. Mustache.tags = correctMustacheTags;
  42. Mustache.render('((placeholder))', { placeholder: 'foo' }, {}, ['((', '))']);
  43. assert.equal(Mustache.tags, correctMustacheTags);
  44. assert.deepEqual(Mustache.tags, ['{{', '}}']);
  45. });
  46. it('uses provided tags when rendering partials', function () {
  47. var output = Mustache.render('<%> partial %>', { name: 'Santa Claus' }, {
  48. partial: '<% name %>'
  49. }, ['<%', '%>']);
  50. assert.equal(output, 'Santa Claus');
  51. })
  52. })
  53. tests.forEach(function (test) {
  54. var view = eval(test.view);
  55. it('knows how to render ' + test.name, function () {
  56. var output;
  57. if (test.partial) {
  58. output = Mustache.render(test.template, view, { partial: test.partial });
  59. } else {
  60. output = Mustache.render(test.template, view);
  61. }
  62. output.should.equal(test.expect);
  63. });
  64. });
  65. });