1 | 'use strict';
|
2 | var parse = require('../src/velocity').parse
|
3 | var assert = require("assert")
|
4 |
|
5 | describe('Parser', function() {
|
6 |
|
7 | describe('simple references', function() {
|
8 |
|
9 | it('self define block', function() {
|
10 | var vm = '#cms(1)<div class="abs-right"> #H(1,"第一个链接") </div> #end';
|
11 | var ast = parse(vm, { cms: true });
|
12 | assert(ast.length, 1);
|
13 | assert(ast[0][0].type, 'cms');
|
14 | });
|
15 |
|
16 | it('simple references', function() {
|
17 | var vm = 'hello world: $foo'
|
18 | var ret = parse(vm)
|
19 |
|
20 | assert.ok(ret instanceof Array)
|
21 | assert.equal(2, ret.length)
|
22 | assert.equal('hello world: ', ret[0])
|
23 | assert.equal('foo', ret[1].id)
|
24 | })
|
25 |
|
26 | it('valid variable references', function() {
|
27 | var vm = '$mud-Slinger_1'
|
28 | assert.equal('mud-Slinger_1', parse(vm)[0].id)
|
29 | })
|
30 |
|
31 | it('wraped references', function() {
|
32 | var vm = '${mudSlinger}'
|
33 | var ast = parse(vm)[0]
|
34 | assert.equal(true, ast.isWraped)
|
35 | assert.equal('mudSlinger', ast.id)
|
36 | })
|
37 |
|
38 | it('function call references', function() {
|
39 | var ast = parse('$foo()')[0]
|
40 | assert.equal(false, ast.args)
|
41 | assert.equal('references', ast.type)
|
42 | })
|
43 | })
|
44 |
|
45 | describe('Properties', function() {
|
46 |
|
47 | it('simple property', function() {
|
48 | var vm = '$customer.Address'
|
49 | var asts = parse(vm)
|
50 | assert.deepEqual(asts[0], {
|
51 | id: "customer",
|
52 | prue: true,
|
53 | type: "references",
|
54 | path: [{type: 'property', id: 'Address'}],
|
55 | leader: '$',
|
56 | pos: { first_line: 1, last_line: 1, first_column: 0, last_column: 17 }
|
57 | })
|
58 | })
|
59 |
|
60 | })
|
61 |
|
62 | describe('Methods ', function() {
|
63 |
|
64 | it('with no arguments', function() {
|
65 | var vm = '$foo.bar()'
|
66 | var ast = parse(vm)[0]
|
67 |
|
68 | assert.deepEqual(ast['path'], [{
|
69 | type: "method",
|
70 | id: "bar",
|
71 | args: false
|
72 | }])
|
73 | })
|
74 |
|
75 | it('with arguments integer', function() {
|
76 | var vm = '$foo.bar(10)'
|
77 | var ast = parse(vm)[0]
|
78 |
|
79 | assert.deepEqual(ast['path'], [{
|
80 | type: "method",
|
81 | id: "bar",
|
82 | args: [{
|
83 | type: "integer",
|
84 | value: "10"
|
85 | }]
|
86 | }])
|
87 | })
|
88 |
|
89 | it('with arguments references', function() {
|
90 | var vm = '$foo.bar($bar)'
|
91 | var ast = parse(vm)[0]
|
92 |
|
93 | assert.equal(ast.prue, true)
|
94 |
|
95 | assert.deepEqual(ast.path[0].args, [{
|
96 | type: "references",
|
97 | leader: "$",
|
98 | id: "bar"
|
99 | }])
|
100 | })
|
101 |
|
102 | })
|
103 |
|
104 | describe('Index', function() {
|
105 |
|
106 | it('all kind of indexs', function() {
|
107 | var vm = '$foo[0] $foo[$i] $foo["bar"]'
|
108 | var asts = parse(vm)
|
109 |
|
110 | assert.equal(5, asts.length)
|
111 |
|
112 |
|
113 |
|
114 | assert.equal('index', asts[0].path[0].type)
|
115 | assert.equal('integer', asts[0].path[0].id.type)
|
116 | assert.equal('0', asts[0].path[0].id.value)
|
117 |
|
118 |
|
119 |
|
120 | assert.equal('index', asts[2].path[0].type)
|
121 | assert.equal('references', asts[2].path[0].id.type)
|
122 | assert.equal('i', asts[2].path[0].id.id)
|
123 |
|
124 |
|
125 |
|
126 | assert.equal('index', asts[4].path[0].type)
|
127 | assert.equal('string', asts[4].path[0].id.type)
|
128 | assert.equal('bar', asts[4].path[0].id.value)
|
129 |
|
130 | })
|
131 |
|
132 | })
|
133 |
|
134 | describe('complex references', function() {
|
135 |
|
136 | it('property + index + property', function() {
|
137 | var vm = '$foo.bar[1].junk'
|
138 | var ast = parse(vm)[0]
|
139 |
|
140 | assert.equal('foo', ast.id)
|
141 | assert.equal(3, ast.path.length)
|
142 |
|
143 | var paths = ast.path
|
144 |
|
145 | assert.equal('property', paths[0].type)
|
146 | assert.equal('index', paths[1].type)
|
147 | assert.equal('property', paths[2].type)
|
148 |
|
149 | })
|
150 |
|
151 |
|
152 | it('method + index', function() {
|
153 | var vm = '$foo.callMethod()[1]'
|
154 | var ast = parse(vm)[0]
|
155 |
|
156 | assert.equal(2, ast.path.length)
|
157 |
|
158 | assert.equal('method', ast.path[0].type)
|
159 | assert.equal('callMethod', ast.path[0].id)
|
160 |
|
161 | assert.equal('index', ast.path[1].type)
|
162 | assert.equal('1', ast.path[1].id.value)
|
163 | assert.equal('integer', ast.path[1].id.type)
|
164 |
|
165 | })
|
166 |
|
167 | it('property should not start with alphabet', function() {
|
168 | var asts = parse('$foo.124')
|
169 | var ast2 = parse('$foo.-24')[0]
|
170 |
|
171 | assert.equal(3, asts.length)
|
172 | assert.equal('foo', asts[0].id)
|
173 | assert.equal(undefined, asts[0].path)
|
174 |
|
175 | assert.equal(undefined, ast2.path)
|
176 |
|
177 | })
|
178 |
|
179 | it('index should end with close bracket', function() {
|
180 | assert.throws(function() {
|
181 | parse("$foo.bar['a'12]")
|
182 | }, /Parse error/)
|
183 | })
|
184 |
|
185 | })
|
186 |
|
187 | describe('Directives', function() {
|
188 |
|
189 | it('#macro', function() {
|
190 | var vm = '#macro( d $a $b)#if($b)$a#end#end #d($foo $bar)'
|
191 | var asts = parse(vm)
|
192 |
|
193 | var ifAst = asts[0][1]
|
194 |
|
195 | assert.equal(ifAst[0].condition.type, 'references')
|
196 | assert.equal(ifAst[0].condition.id, 'b')
|
197 | assert.equal(ifAst[0].condition.prue, undefined)
|
198 |
|
199 | assert.equal(ifAst[1].type, 'references')
|
200 | assert.equal(ifAst[1].id, 'a')
|
201 | assert.equal(ifAst[1].prue, true)
|
202 |
|
203 | assert.equal(asts.length, 3)
|
204 | assert.equal(ifAst.length, 2)
|
205 | })
|
206 |
|
207 | it('#setter will work fine', function() {
|
208 | var vm = '<a href="#setter" target="_blank"></a>';
|
209 | var asts = parse(vm);
|
210 | asts.every(function(ast) {
|
211 | return typeof ast === 'string';
|
212 | }).should.equal(true);
|
213 | var vm2 = '<a href="#setter()" target="_blank"></a>';
|
214 | asts = parse(vm2);
|
215 | asts[1].type.should.equal('macro_call');
|
216 | });
|
217 |
|
218 | })
|
219 |
|
220 | describe('comment identify', function() {
|
221 |
|
222 | it('one line comment', function() {
|
223 | var asts = parse('#set( $monkey.Number = 123)##number literal')
|
224 |
|
225 | assert.equal(2, asts.length)
|
226 | assert.equal('comment', asts[1].type)
|
227 | })
|
228 |
|
229 | it('all comment', function() {
|
230 | var asts = parse('##number literal')
|
231 |
|
232 | asts.length.should.equal(1)
|
233 | asts[0].type.should.equal('comment');
|
234 |
|
235 | asts = parse('##');
|
236 | asts.length.should.equal(1)
|
237 | asts[0].type.should.equal('comment');
|
238 | });
|
239 |
|
240 | })
|
241 |
|
242 | describe('raw identify', function() {
|
243 | it('raw content', function() {
|
244 | var asts = parse('#[[\nThis content is ignored.\n]]#');
|
245 |
|
246 | assert.equal('raw', asts[0].type);
|
247 | assert.equal('\nThis content is ignored.\n', asts[0].value);
|
248 | });
|
249 | });
|
250 |
|
251 | })
|