25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
5.4KB

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