text_spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. var textParser = require('../../../../src/parsers/text')
  2. var dirParser = require('../../../../src/parsers/directive')
  3. var config = require('../../../../src/config')
  4. var testCases = [
  5. {
  6. // no tags
  7. text: 'haha',
  8. expected: null
  9. },
  10. {
  11. // basic
  12. text: 'a {{ a }} c',
  13. expected: [
  14. { value: 'a ' },
  15. { tag: true, value: 'a', html: false, oneTime: false },
  16. { value: ' c' }
  17. ]
  18. },
  19. {
  20. // html
  21. text: '{{ text }} and {{{ html }}}',
  22. expected: [
  23. { tag: true, value: 'text', html: false, oneTime: false },
  24. { value: ' and ' },
  25. { tag: true, value: 'html', html: true, oneTime: false }
  26. ]
  27. },
  28. {
  29. // one time
  30. text: '{{* text }} and {{{* html }}}',
  31. expected: [
  32. { tag: true, value: 'text', html: false, oneTime: true },
  33. { value: ' and ' },
  34. { tag: true, value: 'html', html: true, oneTime: true }
  35. ]
  36. },
  37. {
  38. text: '[{{abc}}]',
  39. expected: [
  40. { value: '[' },
  41. { tag: true, value: 'abc', html: false, oneTime: false },
  42. { value: ']' }
  43. ]
  44. },
  45. // multiline
  46. {
  47. text: '{{\n value \n}}',
  48. expected: [
  49. { tag: true, value: 'value', html: false, oneTime: false }
  50. ]
  51. }
  52. ]
  53. function assertParse (test) {
  54. var res = textParser.parse(test.text)
  55. var exp = test.expected
  56. if (!Array.isArray(exp)) {
  57. expect(res).toBe(exp)
  58. } else {
  59. expect(res.length).toBe(exp.length)
  60. res.forEach(function (r, i) {
  61. var e = exp[i]
  62. for (var key in e) {
  63. expect(e[key]).toEqual(r[key])
  64. }
  65. })
  66. }
  67. }
  68. describe('Text Parser', function () {
  69. it('parse', function () {
  70. testCases.forEach(assertParse)
  71. })
  72. it('cache', function () {
  73. var res1 = textParser.parse('{{a}}')
  74. var res2 = textParser.parse('{{a}}')
  75. expect(res1).toBe(res2)
  76. })
  77. it('custom delimiters', function () {
  78. config.delimiters = ['[%', '%]']
  79. assertParse({
  80. text: '[%* text %] and [[% html %]]',
  81. expected: [
  82. { tag: true, value: 'text', html: false, oneTime: true },
  83. { value: ' and ' },
  84. { tag: true, value: 'html', html: true, oneTime: false }
  85. ]
  86. })
  87. config.delimiters = ['{{', '}}']
  88. })
  89. it('tokens to expression', function () {
  90. var tokens = textParser.parse('view-{{test + 1}}-test-{{ok + "|"}}')
  91. var exp = textParser.tokensToExp(tokens)
  92. expect(exp).toBe('"view-"+(test + 1)+"-test-"+(ok + "|")')
  93. })
  94. it('tokens to expression, single expression', function () {
  95. var tokens = textParser.parse('{{test}}')
  96. var exp = textParser.tokensToExp(tokens)
  97. // should not have parens so it can be treated as a
  98. // simple path by the expression parser
  99. expect(exp).toBe('test')
  100. })
  101. it('tokens to expression with filters, multiple expressions', function () {
  102. var tokens = textParser.parse('a {{b | c d | f}} e')
  103. var exp = textParser.tokensToExp(tokens)
  104. var filters = dirParser.parse('b | c d | f').filters
  105. expect(exp).toBe(
  106. '"a "+this._applyFilters(b,null,' +
  107. JSON.stringify(filters) +
  108. ',false)+" e"')
  109. })
  110. })