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.

83 lines
5.5KB

  1. var assert = require("assert");
  2. var vows = require("vows");
  3. var parse = require("./../mustache").parse;
  4. // A map of templates to their expected token output.
  5. var expectations = {
  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{{hi}}" : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ] ],
  20. "a {{hi}}" : [ [ 'text', 'a ', 0, 2 ], [ 'name', 'hi', 2, 8 ] ],
  21. " a{{hi}}" : [ [ 'text', ' a', 0, 2 ], [ 'name', 'hi', 2, 8 ] ],
  22. " a {{hi}}" : [ [ 'text', ' a ', 0, 3 ], [ 'name', 'hi', 3, 9 ] ],
  23. "a{{hi}}b" : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', 'b', 7, 8 ] ],
  24. "a{{hi}} b" : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', ' b', 7, 9 ] ],
  25. "a{{hi}}b " : [ [ 'text', 'a', 0, 1 ], [ 'name', 'hi', 1, 7 ], [ 'text', 'b ', 7, 9 ] ],
  26. "a\n{{hi}} b \n" : [ [ 'text', 'a\n', 0, 2 ], [ 'name', 'hi', 2, 8 ], [ 'text', ' b \n', 8, 12 ] ],
  27. "a\n {{hi}} \nb" : [ [ 'text', 'a\n ', 0, 3 ], [ 'name', 'hi', 3, 9 ], [ 'text', ' \nb', 9, 12 ] ],
  28. "a\n {{!hi}} \nb" : [ [ 'text', 'a\n', 0, 3 ], [ '!', 'hi', 3, 10 ], [ 'text', 'b', 10, 13 ] ],
  29. "a\n{{#a}}{{/a}}\nb" : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [] ], [ 'text', 'b', 14, 16 ] ],
  30. "a\n {{#a}}{{/a}}\nb" : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 15, 17 ] ],
  31. "a\n {{#a}}{{/a}} \nb" : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 15, 18 ] ],
  32. "a\n{{#a}}\n{{/a}}\nb" : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [] ], [ 'text', 'b', 15, 17 ] ],
  33. "a\n {{#a}}\n{{/a}}\nb" : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 16, 18 ] ],
  34. "a\n {{#a}}\n{{/a}} \nb" : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 16, 19 ] ],
  35. "a\n{{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb" : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [] ], [ '#', 'b', 16, 22, [] ], [ 'text', 'b', 29, 31 ] ],
  36. "a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}}\nb" : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ '#', 'b', 17, 23, [] ], [ 'text', 'b', 30, 32 ] ],
  37. "a\n {{#a}}\n{{/a}}\n{{#b}}\n{{/b}} \nb" : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ '#', 'b', 17, 23, [] ], [ 'text', 'b', 30, 33 ] ],
  38. "a\n{{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb" : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [ [ '#', 'b', 9, 15, [] ] ] ], [ 'text', 'b', 29, 31 ] ],
  39. "a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}}\nb" : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [] ] ] ], [ 'text', 'b', 30, 32 ] ],
  40. "a\n {{#a}}\n{{#b}}\n{{/b}}\n{{/a}} \nb" : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [ [ '#', 'b', 10, 16, [] ] ] ], [ 'text', 'b', 30, 33 ] ],
  41. "{{>abc}}" : [ [ '>', 'abc', 0, 8 ] ],
  42. "{{> abc }}" : [ [ '>', 'abc', 0, 10 ] ],
  43. "{{ > abc }}" : [ [ '>', 'abc', 0, 11 ] ],
  44. "{{=<% %>=}}" : [ [ '=', '<% %>', 0, 11 ] ],
  45. "{{= <% %> =}}" : [ [ '=', '<% %>', 0, 13 ] ],
  46. "{{=<% %>=}}<%={{ }}=%>" : [ [ '=', '<% %>', 0, 11 ], [ '=', '{{ }}', 11, 22 ] ],
  47. "{{=<% %>=}}<%hi%>" : [ [ '=', '<% %>', 0, 11 ], [ 'name', 'hi', 11, 17 ] ],
  48. "{{#a}}{{/a}}hi{{#b}}{{/b}}\n" : [ [ '#', 'a', 0, 6, [] ], [ 'text', 'hi', 12, 14 ], [ '#', 'b', 14, 20, [] ], [ 'text', '\n', 26, 27 ] ],
  49. "{{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, [] ] ],
  50. "{{#foo}}\n {{#a}}\n {{b}}\n {{/a}}\n{{/foo}}\n"
  51. : [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 17, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 30 ] ] ] ] ] ]
  52. };
  53. function makeToken(tokenArray) {
  54. var token = {
  55. type: tokenArray[0],
  56. value: tokenArray[1],
  57. start: tokenArray[2],
  58. end: tokenArray[3]
  59. };
  60. if (tokenArray[4]) {
  61. token.tokens = tokenArray[4].map(makeToken);
  62. }
  63. return token;
  64. }
  65. var spec = {};
  66. for (var template in expectations) {
  67. (function (template, tokens) {
  68. spec["knows how to parse " + JSON.stringify(template)] = function () {
  69. assert.deepEqual(parse(template), tokens.map(makeToken));
  70. };
  71. })(template, expectations[template]);
  72. }
  73. vows.describe("Mustache.parse").addBatch({
  74. "parse": spec
  75. }).export(module);