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