Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

157 рядки
8.8KB

  1. require('./helper');
  2. // A map of templates to their expected token output. Tokens are in the format:
  3. // [type, value, startIndex, endIndex, subTokens].
  4. var expectations = {
  5. '' : [],
  6. '{{hi}}' : [ [ 'name', 'hi', 0, 6 ] ],
  7. '{{hi.world}}' : [ [ 'name', 'hi.world', 0, 12 ] ],
  8. '{{hi . world}}' : [ [ 'name', 'hi . world', 0, 14 ] ],
  9. '{{ hi}}' : [ [ 'name', 'hi', 0, 7 ] ],
  10. '{{hi }}' : [ [ 'name', 'hi', 0, 7 ] ],
  11. '{{ hi }}' : [ [ 'name', 'hi', 0, 8 ] ],
  12. '{{{hi}}}' : [ [ '&', 'hi', 0, 8 ] ],
  13. '{{!hi}}' : [ [ '!', 'hi', 0, 7 ] ],
  14. '{{! hi}}' : [ [ '!', 'hi', 0, 8 ] ],
  15. '{{! hi }}' : [ [ '!', 'hi', 0, 9 ] ],
  16. '{{ !hi}}' : [ [ '!', 'hi', 0, 8 ] ],
  17. '{{ ! hi}}' : [ [ '!', 'hi', 0, 9 ] ],
  18. '{{ ! hi }}' : [ [ '!', 'hi', 0, 10 ] ],
  19. 'a\n b' : [ [ 'text', 'a\n b', 0, 4 ] ],
  20. 'a{{hi}}' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ] ],
  21. 'a {{hi}}' : [ [ 'text', 'a ', 0, 2 ], [ 'name', 'hi', 2, 8 ] ],
  22. ' a{{hi}}' : [ [ 'text', ' a', 0, 2 ], [ 'name', 'hi', 2, 8 ] ],
  23. ' a {{hi}}' : [ [ 'text', ' a ', 0, 3 ], [ 'name', 'hi', 3, 9 ] ],
  24. 'a{{hi}}b' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', 'b', 7, 8 ] ],
  25. 'a{{hi}} b' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', ' b', 7, 9 ] ],
  26. 'a{{hi}}b ' : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', 'b ', 7, 9 ] ],
  27. 'a\n{{hi}} b \n' : [ [ 'text', 'a\n', 0, 2 ], [ 'name', 'hi', 2, 8 ], [ 'text', ' b \n', 8, 12 ] ],
  28. 'a\n {{hi}} \nb' : [ [ 'text', 'a\n ', 0, 3 ], [ 'name', 'hi', 3, 9 ], [ 'text', ' \nb', 9, 12 ] ],
  29. 'a\n {{!hi}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '!', 'hi', 3, 10 ], [ 'text', 'b', 12, 13 ] ],
  30. 'a\n{{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 8 ], [ 'text', 'b', 15, 16 ] ],
  31. 'a\n {{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 9 ], [ 'text', 'b', 16, 17 ] ],
  32. 'a\n {{#a}}{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 9 ], [ 'text', 'b', 17, 18 ] ],
  33. 'a\n{{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 9 ], [ 'text', 'b', 16, 17 ] ],
  34. 'a\n {{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ 'text', 'b', 17, 18 ] ],
  35. 'a\n {{#a}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ 'text', 'b', 18, 19 ] ],
  36. 'a\n{{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [], 9 ], [ '#', 'b', 16, 22, [], 23 ], [ 'text', 'b', 30, 31 ] ],
  37. 'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ '#', 'b', 17, 23, [], 24 ], [ 'text', 'b', 31, 32 ] ],
  38. 'a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [], 10 ], [ '#', 'b', 17, 23, [], 24 ], [ 'text', 'b', 32, 33 ] ],
  39. 'a\n{{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [ [ '#', 'b', 9, 15, [], 16 ] ], 23 ], [ 'text', 'b', 30, 31 ] ],
  40. 'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [], 17 ] ], 24 ], [ 'text', 'b', 31, 32 ] ],
  41. 'a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [], 17 ] ], 24 ], [ 'text', 'b', 32, 33 ] ],
  42. '{{>abc}}' : [ [ '>', 'abc', 0, 8, '', 0, false ] ],
  43. '{{> abc }}' : [ [ '>', 'abc', 0, 10, '', 0, false ] ],
  44. '{{ > abc }}' : [ [ '>', 'abc', 0, 11, '', 0, false ] ],
  45. ' {{> abc }}\n' : [ [ '>', 'abc', 2, 12, ' ', 0, false ] ],
  46. ' {{> abc }} {{> abc }}\n' : [ [ '>', 'abc', 2, 12, ' ', 0, false ], [ '>', 'abc', 13, 23, ' ', 1, false ] ],
  47. '{{ > abc }}' : [ [ '>', 'abc', 0, 11, '', 0, false ] ],
  48. '{{=<% %>=}}' : [ [ '=', '<% %>', 0, 11 ] ],
  49. '{{= <% %> =}}' : [ [ '=', '<% %>', 0, 13 ] ],
  50. '{{=<% %>=}}<%={{ }}=%>' : [ [ '=', '<% %>', 0, 11 ], [ '=', '{{ }}', 11, 22 ] ],
  51. '{{=<% %>=}}<%hi%>' : [ [ '=', '<% %>', 0, 11 ], [ 'name', 'hi', 11, 17 ] ],
  52. '{{#a}}{{/a}}hi{{#b}}{{/b}}\n' : [ [ '#', 'a', 0, 6, [], 6 ], [ 'text', 'hi', 12, 14 ], [ '#', 'b', 14, 20, [], 20 ], [ 'text', '\n', 26, 27 ] ],
  53. '{{a}}\n{{b}}\n\n{{#c}}\n{{/c}}\n' : [ [ 'name', 'a', 0, 5 ], [ 'text', '\n', 5, 6 ], [ 'name', 'b', 6, 11 ], [ 'text', '\n\n', 11, 13 ], [ '#', 'c', 13, 19, [], 20 ] ],
  54. '{{#foo}}\n {{#a}}\n {{b}}\n {{/a}}\n{{/foo}}\n'
  55. : [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ], 30 ] ], 37 ] ]
  56. };
  57. beforeEach(function (){
  58. Mustache.clearCache();
  59. });
  60. describe('Mustache.parse', function () {
  61. for (var template in expectations) {
  62. (function (template, tokens) {
  63. it('knows how to parse ' + JSON.stringify(template), function () {
  64. assert.deepEqual(Mustache.parse(template), tokens);
  65. });
  66. })(template, expectations[template]);
  67. }
  68. describe('when there is an unclosed tag', function () {
  69. it('throws an error', function () {
  70. assert.throws(function () {
  71. Mustache.parse('My name is {{name');
  72. }, /unclosed tag at 17/i);
  73. });
  74. });
  75. describe('when there is an unclosed section', function () {
  76. it('throws an error', function () {
  77. assert.throws(function () {
  78. Mustache.parse('A list: {{#people}}{{name}}');
  79. }, /unclosed section "people" at 27/i);
  80. });
  81. });
  82. describe('when there is an unopened section', function () {
  83. it('throws an error', function () {
  84. assert.throws(function () {
  85. Mustache.parse('The end of the list! {{/people}}');
  86. }, /unopened section "people" at 21/i);
  87. });
  88. });
  89. describe('when invalid tags are given as an argument', function () {
  90. it('throws an error', function () {
  91. assert.throws(function () {
  92. Mustache.parse('A template <% name %>', [ '<%' ]);
  93. }, /invalid tags/i);
  94. });
  95. });
  96. describe('when the template contains invalid tags', function () {
  97. it('throws an error', function () {
  98. assert.throws(function () {
  99. Mustache.parse('A template {{=<%=}}');
  100. }, /invalid tags/i);
  101. });
  102. });
  103. describe('when parsing a template without tags specified followed by the same template with tags specified', function () {
  104. it('returns different tokens for the latter parse', function () {
  105. var template = '{{foo}}[bar]';
  106. var parsedWithBraces = Mustache.parse(template);
  107. var parsedWithBrackets = Mustache.parse(template, ['[', ']']);
  108. assert.notDeepEqual(parsedWithBrackets, parsedWithBraces);
  109. });
  110. });
  111. describe('when parsing a template with tags specified followed by the same template with different tags specified', function () {
  112. it('returns different tokens for the latter parse', function () {
  113. var template = '(foo)[bar]';
  114. var parsedWithParens = Mustache.parse(template, ['(', ')']);
  115. var parsedWithBrackets = Mustache.parse(template, ['[', ']']);
  116. assert.notDeepEqual(parsedWithBrackets, parsedWithParens);
  117. });
  118. });
  119. describe('when parsing a template after already having parsed that template with a different Mustache.tags', function () {
  120. it('returns different tokens for the latter parse', function () {
  121. var template = '{{foo}}[bar]';
  122. var parsedWithBraces = Mustache.parse(template);
  123. var oldTags = Mustache.tags;
  124. Mustache.tags = ['[', ']'];
  125. var parsedWithBrackets = Mustache.parse(template);
  126. Mustache.tags = oldTags;
  127. assert.notDeepEqual(parsedWithBrackets, parsedWithBraces);
  128. });
  129. });
  130. describe('when parsing a template with the same tags second time, return the cached tokens', function () {
  131. it('returns the same tokens for the latter parse', function () {
  132. var template = '{{foo}}[bar]';
  133. var parsedResult1 = Mustache.parse(template);
  134. var parsedResult2 = Mustache.parse(template);
  135. assert.deepEqual(parsedResult1, parsedResult2);
  136. assert.ok(parsedResult1 === parsedResult2);
  137. });
  138. });
  139. });