parse-test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. var originalTemplateCache;
  58. before(function () {
  59. originalTemplateCache = Mustache.templateCache;
  60. });
  61. beforeEach(function (){
  62. Mustache.clearCache();
  63. Mustache.templateCache = originalTemplateCache;
  64. });
  65. describe('Mustache.parse', function () {
  66. for (var template in expectations) {
  67. (function (template, tokens) {
  68. it('knows how to parse ' + JSON.stringify(template), function () {
  69. assert.deepEqual(Mustache.parse(template), tokens);
  70. });
  71. })(template, expectations[template]);
  72. }
  73. describe('when there is an unclosed tag', function () {
  74. it('throws an error', function () {
  75. assert.throws(function () {
  76. Mustache.parse('My name is {{name');
  77. }, /unclosed tag at 17/i);
  78. });
  79. });
  80. describe('when there is an unclosed section', function () {
  81. it('throws an error', function () {
  82. assert.throws(function () {
  83. Mustache.parse('A list: {{#people}}{{name}}');
  84. }, /unclosed section "people" at 27/i);
  85. });
  86. });
  87. describe('when there is an unopened section', function () {
  88. it('throws an error', function () {
  89. assert.throws(function () {
  90. Mustache.parse('The end of the list! {{/people}}');
  91. }, /unopened section "people" at 21/i);
  92. });
  93. });
  94. describe('when invalid tags are given as an argument', function () {
  95. it('throws an error', function () {
  96. assert.throws(function () {
  97. Mustache.parse('A template <% name %>', [ '<%' ]);
  98. }, /invalid tags/i);
  99. });
  100. });
  101. describe('when the template contains invalid tags', function () {
  102. it('throws an error', function () {
  103. assert.throws(function () {
  104. Mustache.parse('A template {{=<%=}}');
  105. }, /invalid tags/i);
  106. });
  107. });
  108. describe('when parsing a template without tags specified followed by the same template with tags specified', function () {
  109. it('returns different tokens for the latter parse', function () {
  110. var template = '{{foo}}[bar]';
  111. var parsedWithBraces = Mustache.parse(template);
  112. var parsedWithBrackets = Mustache.parse(template, ['[', ']']);
  113. assert.notDeepEqual(parsedWithBrackets, parsedWithBraces);
  114. });
  115. });
  116. describe('when parsing a template with tags specified followed by the same template with different tags specified', function () {
  117. it('returns different tokens for the latter parse', function () {
  118. var template = '(foo)[bar]';
  119. var parsedWithParens = Mustache.parse(template, ['(', ')']);
  120. var parsedWithBrackets = Mustache.parse(template, ['[', ']']);
  121. assert.notDeepEqual(parsedWithBrackets, parsedWithParens);
  122. });
  123. });
  124. describe('when parsing a template after already having parsed that template with a different Mustache.tags', function () {
  125. it('returns different tokens for the latter parse', function () {
  126. var template = '{{foo}}[bar]';
  127. var parsedWithBraces = Mustache.parse(template);
  128. var oldTags = Mustache.tags;
  129. Mustache.tags = ['[', ']'];
  130. var parsedWithBrackets = Mustache.parse(template);
  131. Mustache.tags = oldTags;
  132. assert.notDeepEqual(parsedWithBrackets, parsedWithBraces);
  133. });
  134. });
  135. describe('when parsing a template with the same tags second time, return the cached tokens', function () {
  136. it('returns the same tokens for the latter parse', function () {
  137. var template = '{{foo}}[bar]';
  138. var parsedResult1 = Mustache.parse(template);
  139. var parsedResult2 = Mustache.parse(template);
  140. assert.deepEqual(parsedResult1, parsedResult2);
  141. assert.ok(parsedResult1 === parsedResult2);
  142. });
  143. });
  144. describe('when parsing a template with caching disabled and the same tags second time, do not return the cached tokens', function () {
  145. it('returns different tokens for the latter parse', function () {
  146. Mustache.templateCache = undefined;
  147. var template = '{{foo}}[bar]';
  148. var parsedResult1 = Mustache.parse(template);
  149. var parsedResult2 = Mustache.parse(template);
  150. assert.deepEqual(parsedResult1, parsedResult2);
  151. assert.ok(parsedResult1 !== parsedResult2);
  152. });
  153. });
  154. describe('when parsing a template with custom caching and the same tags second time, do not return the cached tokens', function () {
  155. it('returns the same tokens for the latter parse', function () {
  156. Mustache.templateCache = {
  157. _cache: [],
  158. set: function set (key, value) {
  159. this._cache.push([key, value]);
  160. },
  161. get: function get (key) {
  162. var cacheLength = this._cache.length;
  163. for (var i = 0; i < cacheLength; i++) {
  164. var entry = this._cache[i];
  165. if (entry[0] === key) {
  166. return entry[1];
  167. }
  168. }
  169. return undefined;
  170. },
  171. clear: function clear () {
  172. this._cache = [];
  173. }
  174. };
  175. var template = '{{foo}}[bar]';
  176. var parsedResult1 = Mustache.parse(template);
  177. var parsedResult2 = Mustache.parse(template);
  178. assert.deepEqual(parsedResult1, parsedResult2);
  179. assert.ok(parsedResult1 === parsedResult2);
  180. });
  181. });
  182. });