25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

parse_test.js 5.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. 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{{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, 3 ], [ '!', 'hi', 3, 10 ], [ 'text', 'b', 10, 13 ] ],
  30. 'a\n{{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [] ], [ 'text', 'b', 14, 16 ] ],
  31. 'a\n {{#a}}{{/a}}\nb' : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 15, 17 ] ],
  32. 'a\n {{#a}}{{/a}} \nb' : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 15, 18 ] ],
  33. 'a\n{{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 2 ], [ '#', 'a', 2, 8, [] ], [ 'text', 'b', 15, 17 ] ],
  34. 'a\n {{#a}}\n{{/a}}\nb' : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 16, 18 ] ],
  35. 'a\n {{#a}}\n{{/a}} \nb' : [ [ 'text', 'a\n', 0, 3 ], [ '#', 'a', 3, 9, [] ], [ 'text', 'b', 16, 19 ] ],
  36. '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 ] ],
  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, 32 ] ],
  38. '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 ] ],
  39. '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 ] ],
  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, 32 ] ],
  41. '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 ] ],
  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, [] ], [ 'text', 'hi', 12, 14 ], [ '#', 'b', 14, 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, [] ] ],
  51. '{{#foo}}\n {{#a}}\n {{b}}\n {{/a}}\n{{/foo}}\n'
  52. : [ [ '#', 'foo', 0, 8, [ [ '#', 'a', 11, 17, [ [ 'text', ' ', 17, 22 ], [ 'name', 'b', 22, 27 ], [ 'text', '\n', 27, 30 ] ] ] ] ] ]
  53. };
  54. var spec = {};
  55. for (var template in expectations) {
  56. (function (template, tokens) {
  57. spec['knows how to parse ' + JSON.stringify(template)] = function () {
  58. assert.deepEqual(parse(template), tokens);
  59. };
  60. })(template, expectations[template]);
  61. }
  62. vows.describe('Mustache.parse').addBatch({
  63. 'parse': spec
  64. }).export(module);