text_spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var textParser = require('../../../../src/parse/text')
  2. var config = require('../../../../src/config')
  3. var Vue = require('../../../../src/vue')
  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. // partial
  39. text: '{{> hello }} and {{>hello}}',
  40. expected: [
  41. { tag: true, value: 'hello', html: false, oneTime: false, partial: true },
  42. { value: ' and ' },
  43. { tag: true, value: 'hello', html: false, oneTime: false, partial: true }
  44. ]
  45. },
  46. {
  47. text: '[{{abc}}]',
  48. expected: [
  49. { value: '[' },
  50. { tag: true, value: 'abc', html: false, oneTime: false },
  51. { value: ']' }
  52. ]
  53. }
  54. ]
  55. function assertParse (test) {
  56. var res = textParser.parse(test.text)
  57. var exp = test.expected
  58. if (!Array.isArray(exp)) {
  59. expect(res).toBe(exp)
  60. } else {
  61. expect(res.length).toBe(exp.length)
  62. res.forEach(function (r, i) {
  63. var e = exp[i]
  64. for (var key in e) {
  65. expect(e[key]).toEqual(r[key])
  66. }
  67. })
  68. }
  69. }
  70. describe('Text Parser', function () {
  71. it('parse', function () {
  72. testCases.forEach(assertParse)
  73. })
  74. it('cache', function () {
  75. var res1 = textParser.parse('{{a}}')
  76. var res2 = textParser.parse('{{a}}')
  77. expect(res1).toBe(res2)
  78. })
  79. it('custom delimiters', function () {
  80. config.delimiters = ['[%', '%]']
  81. assertParse({
  82. text: '[%* text %] and [[% html %]]',
  83. expected: [
  84. { tag: true, value: 'text', html: false, oneTime: true },
  85. { value: ' and ' },
  86. { tag: true, value: 'html', html: true, oneTime: false },
  87. ]
  88. })
  89. config.delimiters = ['{{', '}}']
  90. })
  91. it('tokens to expression', function () {
  92. var tokens = textParser.parse('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 with oneTime tags & vm', function () {
  97. var vm = new Vue({
  98. data: { test: 'a', ok: 'b' }
  99. })
  100. var tokens = textParser.parse('view-{{*test}}-test-{{ok}}')
  101. var exp = textParser.tokensToExp(tokens, vm)
  102. expect(exp).toBe('"view-"+"a"+"-test-"+(ok)')
  103. })
  104. it('tokens to expression with filters, single expression', function () {
  105. var tokens = textParser.parse('{{test | abc}}')
  106. var exp = textParser.tokensToExp(tokens)
  107. expect(exp).toBe('test | abc')
  108. })
  109. it('tokens to expression with filters, multiple expressions', function () {
  110. var tokens = textParser.parse('a {{b | c d}} e')
  111. var exp = textParser.tokensToExp(tokens)
  112. expect(exp).toBe('"a "+this.$options.filters["c"].apply(this,[b,"d"])+" e"')
  113. })
  114. })