text_spec.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.parseText(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.parseText('{{a}}')
  74. var res2 = textParser.parseText('{{a}}')
  75. expect(res1).toBe(res2)
  76. })
  77. it('custom delimiters', function () {
  78. config.delimiters = ['[%', '%]']
  79. config.unsafeDelimiters = ['{!!', '!!}']
  80. assertParse({
  81. text: '[%* text %] and {!! html !!}',
  82. expected: [
  83. { tag: true, value: 'text', html: false, oneTime: true },
  84. { value: ' and ' },
  85. { tag: true, value: 'html', html: true, oneTime: false }
  86. ]
  87. })
  88. config.delimiters = ['{{', '}}']
  89. config.unsafeDelimiters = ['{{{', '}}}']
  90. })
  91. it('tokens to expression', function () {
  92. var tokens = textParser.parseText('view-{{test + 1}}-test-{{ok + "|"}}')
  93. var exp = textParser.tokensToExp(tokens)
  94. expect(exp).toBe('"view-"+(test + 1)+"-test-"+(ok + "|")')
  95. })
  96. it('tokens to expression, single expression', function () {
  97. var tokens = textParser.parseText('{{test}}')
  98. var exp = textParser.tokensToExp(tokens)
  99. // should not have parens so it can be treated as a
  100. // simple path by the expression parser
  101. expect(exp).toBe('test')
  102. })
  103. it('tokens to expression with filters, multiple expressions', function () {
  104. var tokens = textParser.parseText('a {{b | c d | f}} e')
  105. var exp = textParser.tokensToExp(tokens)
  106. var filters = dirParser.parseDirective('b | c d | f').filters
  107. expect(exp).toBe(
  108. '"a "+this._applyFilters(b,null,' +
  109. JSON.stringify(filters) +
  110. ',false)+" e"')
  111. })
  112. })