A Twitch.tv viewer reward and games system.
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.

106 lines
6.6 KiB

12 years ago
  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 ] ],
  43. '{{> abc }}' : [ [ '>', 'abc', 0, 10 ] ],
  44. '{{ > abc }}' : [ [ '>', 'abc', 0, 11 ] ],
  45. '{{=<% %>=}}' : [ [ '=', '<% %>', 0, 11 ] ],
  46. '{{= <% %> =}}' : [ [ '=', '<% %>', 0, 13 ] ],
  47. '{{=<% %>=}}<%={{ }}=%>' : [ [ '=', '<% %>', 0, 11 ], [ '=', '{{ }}', 11, 22 ] ],
  48. '{{=<% %>=}}<%hi%>' : [ [ '=', '<% %>', 0, 11 ], [ 'name', 'hi', 11, 17 ] ],
  49. '{{#a}}{{/a}}hi{{#b}}{{/b}}\n' : [ [ '#', 'a', 0, 6, [], 6 ], [ 'text', 'hi', 12, 14 ], [ '#', 'b', 14, 20, [], 20 ], [ 'text', '\n', 26, 27 ] ],
  50. '{{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 ] ],
  51. '{{#foo}}\n {{#a}}\n {{b}}\n {{/a}}\n{{/foo}}\n'
  52. : [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 18, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 28 ] ], 30 ] ], 37 ] ]
  53. };
  54. describe('Mustache.parse', function () {
  55. for (var template in expectations) {
  56. (function (template, tokens) {
  57. it('knows how to parse ' + JSON.stringify(template), function () {
  58. assert.deepEqual(Mustache.parse(template), tokens);
  59. });
  60. })(template, expectations[template]);
  61. }
  62. describe('when there is an unclosed tag', function () {
  63. it('throws an error', function () {
  64. assert.throws(function () {
  65. Mustache.parse('My name is {{name');
  66. }, /unclosed tag at 17/i);
  67. });
  68. });
  69. describe('when there is an unclosed section', function () {
  70. it('throws an error', function () {
  71. assert.throws(function () {
  72. Mustache.parse('A list: {{#people}}{{name}}');
  73. }, /unclosed section "people" at 27/i);
  74. });
  75. });
  76. describe('when there is an unopened section', function () {
  77. it('throws an error', function () {
  78. assert.throws(function () {
  79. Mustache.parse('The end of the list! {{/people}}');
  80. }, /unopened section "people" at 21/i);
  81. });
  82. });
  83. describe('when invalid tags are given as an argument', function () {
  84. it('throws an error', function () {
  85. assert.throws(function () {
  86. Mustache.parse('A template <% name %>', [ '<%' ]);
  87. }, /invalid tags/i);
  88. });
  89. });
  90. describe('when the template contains invalid tags', function () {
  91. it('throws an error', function () {
  92. assert.throws(function () {
  93. Mustache.parse('A template {{=<%=}}');
  94. }, /invalid tags at 11/i);
  95. });
  96. });
  97. });