text_spec.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var textParser = require('../../../../src/parse/text')
  2. var config = require('../../../../src/config')
  3. var testCases = [
  4. {
  5. // no tags
  6. text: 'haha',
  7. expected: null
  8. },
  9. {
  10. // basic
  11. text: 'a {{ a }} c',
  12. expected: [
  13. { value: 'a ' },
  14. { tag: true, value: 'a', html: false, oneTime: false },
  15. { value: ' c' }
  16. ]
  17. },
  18. {
  19. // html
  20. text: '{{ text }} and {{{ html }}}',
  21. expected: [
  22. { tag: true, value: 'text', html: false, oneTime: false },
  23. { value: ' and ' },
  24. { tag: true, value: 'html', html: true, oneTime: false },
  25. ]
  26. },
  27. {
  28. // one time
  29. text: '{{* text }} and {{{* html }}}',
  30. expected: [
  31. { tag: true, value: 'text', html: false, oneTime: true },
  32. { value: ' and ' },
  33. { tag: true, value: 'html', html: true, oneTime: true },
  34. ]
  35. }
  36. ]
  37. function assertParse (test) {
  38. var res = textParser.parse(test.text)
  39. var exp = test.expected
  40. if (!Array.isArray(exp)) {
  41. expect(res).toBe(exp)
  42. } else {
  43. expect(res.length).toBe(exp.length)
  44. res.forEach(function (r, i) {
  45. var e = exp[i]
  46. for (var key in e) {
  47. expect(e[key]).toEqual(r[key])
  48. }
  49. })
  50. }
  51. }
  52. describe('Text Parser', function () {
  53. it('parse', function () {
  54. testCases.forEach(assertParse)
  55. })
  56. it('cache', function () {
  57. var res1 = textParser.parse('{{a}}')
  58. var res2 = textParser.parse('{{a}}')
  59. expect(res1).toBe(res2)
  60. })
  61. it('custom delimiters', function () {
  62. config.delimiters = ['[%', '%]']
  63. assertParse({
  64. text: '[%* text %] and [[% html %]]',
  65. expected: [
  66. { tag: true, value: 'text', html: false, oneTime: true },
  67. { value: ' and ' },
  68. { tag: true, value: 'html', html: true, oneTime: false },
  69. ]
  70. })
  71. })
  72. })