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