github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/vue-1.0.24/test/unit/specs/parsers/text_spec.js (about) 1 var textParser = require('src/parsers/text') 2 var dirParser = require('src/parsers/directive') 3 var config = require('src/config') 4 5 var testCases = [ 6 { 7 // no tags 8 text: 'foo', 9 expected: null 10 }, 11 { 12 // basic 13 text: 'a {{ a }} c', 14 expected: [ 15 { value: 'a ' }, 16 { tag: true, value: 'a', html: false, oneTime: false }, 17 { value: ' c' } 18 ] 19 }, 20 { 21 // html 22 text: '{{ text }} and {{{ html }}}', 23 expected: [ 24 { tag: true, value: 'text', html: false, oneTime: false }, 25 { value: ' and ' }, 26 { tag: true, value: 'html', html: true, oneTime: false } 27 ] 28 }, 29 { 30 // one time 31 text: '{{* text }} and {{{* html }}}', 32 expected: [ 33 { tag: true, value: 'text', html: false, oneTime: true }, 34 { value: ' and ' }, 35 { tag: true, value: 'html', html: true, oneTime: true } 36 ] 37 }, 38 { 39 text: '[{{abc}}]', 40 expected: [ 41 { value: '[' }, 42 { tag: true, value: 'abc', html: false, oneTime: false }, 43 { value: ']' } 44 ] 45 }, 46 // multiline 47 { 48 text: '{{\n value \n}}', 49 expected: [ 50 { tag: true, value: 'value', html: false, oneTime: false } 51 ] 52 }, 53 // new lines preserved outside of tags 54 { 55 text: 'hello\n{{value}}\nworld', 56 expected: [ 57 { value: 'hello\n' }, 58 { tag: true, value: 'value', html: false, oneTime: false }, 59 { value: '\nworld' } 60 ] 61 } 62 ] 63 64 function assertParse (test) { 65 var res = textParser.parseText(test.text) 66 var exp = test.expected 67 if (!Array.isArray(exp)) { 68 expect(res).toBe(exp) 69 } else { 70 expect(res.length).toBe(exp.length) 71 res.forEach(function (r, i) { 72 var e = exp[i] 73 for (var key in e) { 74 expect(e[key]).toEqual(r[key]) 75 } 76 }) 77 } 78 } 79 80 describe('Text Parser', function () { 81 it('parse', function () { 82 testCases.forEach(assertParse) 83 }) 84 85 it('cache', function () { 86 var res1 = textParser.parseText('{{a}}') 87 var res2 = textParser.parseText('{{a}}') 88 expect(res1).toBe(res2) 89 }) 90 91 it('custom delimiters', function () { 92 config.delimiters = ['[%', '%]'] 93 config.unsafeDelimiters = ['{!!', '!!}'] 94 assertParse({ 95 text: '[%* text %] and {!! html !!}', 96 expected: [ 97 { tag: true, value: 'text', html: false, oneTime: true }, 98 { value: ' and ' }, 99 { tag: true, value: 'html', html: true, oneTime: false } 100 ] 101 }) 102 config.delimiters = ['{{', '}}'] 103 config.unsafeDelimiters = ['{{{', '}}}'] 104 }) 105 106 it('tokens to expression', function () { 107 var tokens = textParser.parseText('view-{{test + 1}}-test-{{ok + "|"}}') 108 var exp = textParser.tokensToExp(tokens) 109 expect(exp).toBe('"view-"+(test + 1)+"-test-"+(ok + "|")') 110 }) 111 112 it('tokens to expression, single expression', function () { 113 var tokens = textParser.parseText('{{test}}') 114 var exp = textParser.tokensToExp(tokens) 115 // should not have parens so it can be treated as a 116 // simple path by the expression parser 117 expect(exp).toBe('test') 118 }) 119 120 it('tokens to expression with filters, multiple expressions', function () { 121 var tokens = textParser.parseText('a {{b | c d | f}} e') 122 var exp = textParser.tokensToExp(tokens) 123 var filters = dirParser.parseDirective('b | c d | f').filters 124 expect(exp).toBe( 125 '"a "+this._applyFilters(b,null,' + 126 JSON.stringify(filters) + 127 ',false)+" e"') 128 }) 129 })