parser.spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { generateBinding } from '../../../src/platforms/weex/util/parser'
  2. describe('expression parser', () => {
  3. describe('generateBinding', () => {
  4. it('primitive literal', () => {
  5. expect(generateBinding('15')).toEqual(15)
  6. expect(generateBinding('"xxx"')).toEqual('xxx')
  7. })
  8. it('identifiers', () => {
  9. expect(generateBinding('x')).toEqual({ '@binding': 'x' })
  10. expect(generateBinding('x.y')).toEqual({ '@binding': 'x.y' })
  11. expect(generateBinding(`x.y['z']`)).toEqual({ '@binding': `x.y['z']` })
  12. })
  13. it('object literal', () => {
  14. expect(generateBinding('{}')).toEqual({})
  15. expect(generateBinding('{ abc: 25 }')).toEqual({ abc: 25 })
  16. expect(generateBinding('{ abc: 25, def: "xxx" }')).toEqual({ abc: 25, def: 'xxx' })
  17. expect(generateBinding('{ a: 3, b: { bb: "bb", bbb: { bbc: "BBC" } } }'))
  18. .toEqual({ a: 3, b: { bb: 'bb', bbb: { bbc: 'BBC' }}})
  19. })
  20. it('array literal', () => {
  21. expect(generateBinding('[]')).toEqual([])
  22. expect(generateBinding('[{ abc: 25 }]')).toEqual([{ abc: 25 }])
  23. expect(generateBinding('[{ abc: 25, def: ["xxx"] }]')).toEqual([{ abc: 25, def: ['xxx'] }])
  24. expect(generateBinding('{ a: [3,16], b: [{ bb: ["aa","bb"], bbb: [{bbc:"BBC"}] }] }'))
  25. .toEqual({ a: [3, 16], b: [{ bb: ['aa', 'bb'], bbb: [{ bbc: 'BBC' }] }] })
  26. })
  27. it('expressions', () => {
  28. expect(generateBinding(`3 + 5`)).toEqual({ '@binding': `3 + 5` })
  29. expect(generateBinding(`'x' + 2`)).toEqual({ '@binding': `'x' + 2` })
  30. expect(generateBinding(`\`xx\` + 2`)).toEqual({ '@binding': `\`xx\` + 2` })
  31. expect(generateBinding(`item.size * 23 + 'px'`)).toEqual({ '@binding': `item.size * 23 + 'px'` })
  32. })
  33. it('object bindings', () => {
  34. expect(generateBinding(`{ color: textColor }`)).toEqual({
  35. color: { '@binding': 'textColor' }
  36. })
  37. expect(generateBinding(`{ color: '#FF' + 66 * 100, fontSize: item.size }`)).toEqual({
  38. color: { '@binding': `'#FF' + 66 * 100` },
  39. fontSize: { '@binding': 'item.size' }
  40. })
  41. expect(generateBinding(`{
  42. x: { xx: obj, xy: -2 + 5 },
  43. y: {
  44. yy: { yyy: obj.y || yy },
  45. yz: typeof object.yz === 'string' ? object.yz : ''
  46. }
  47. }`)).toEqual({
  48. x: { xx: { '@binding': 'obj' }, xy: { '@binding': '-2 + 5' }},
  49. y: {
  50. yy: { yyy: { '@binding': 'obj.y || yy' }},
  51. yz: { '@binding': `typeof object.yz === 'string' ? object.yz : ''` }
  52. }
  53. })
  54. })
  55. it('array bindings', () => {
  56. expect(generateBinding(`[textColor, 3 + 5, 'string']`)).toEqual([
  57. { '@binding': 'textColor' },
  58. { '@binding': '3 + 5' },
  59. 'string'
  60. ])
  61. expect(generateBinding(`[
  62. { color: '#FF' + 66 * -100 },
  63. item && item.style,
  64. { fontSize: item.size | 0 }
  65. ]`)).toEqual([
  66. { color: { '@binding': `'#FF' + 66 * -100` }},
  67. { '@binding': 'item && item.style' },
  68. { fontSize: { '@binding': 'item.size | 0' }}
  69. ])
  70. expect(generateBinding(`[{
  71. x: [{ xx: [fn instanceof Function ? 'function' : '' , 25] }],
  72. y: {
  73. yy: [{ yyy: [obj.yy.y, obj.y.yy] }],
  74. yz: [object.yz, void 0]
  75. }
  76. }]`)).toEqual([{
  77. x: [{ xx: [{ '@binding': `fn instanceof Function ? 'function' : ''` }, 25] }],
  78. y: {
  79. yy: [{ yyy: [{ '@binding': 'obj.yy.y' }, { '@binding': 'obj.y.yy' }] }],
  80. yz: [{ '@binding': 'object.yz' }, { '@binding': 'void 0' }]
  81. }
  82. }])
  83. })
  84. it('unsupported bindings', () => {
  85. expect(generateBinding('() => {}')).toEqual('')
  86. expect(generateBinding('function(){}')).toEqual('')
  87. expect(generateBinding('(function(){})()')).toEqual('')
  88. expect(generateBinding('var abc = 35')).toEqual('')
  89. expect(generateBinding('abc++')).toEqual('')
  90. expect(generateBinding('x.y(0)')).toEqual('')
  91. expect(generateBinding('class X {}')).toEqual('')
  92. expect(generateBinding('if (typeof x == null) { 35 }')).toEqual('')
  93. expect(generateBinding('while (x == null)')).toEqual('')
  94. expect(generateBinding('new Function()')).toEqual('')
  95. })
  96. })
  97. })