UNPKG

7.77 kBJavaScriptView Raw
1var Velocity = require('../src/velocity')
2var assert = require("assert")
3var parse = Velocity.parse
4var Compile = Velocity.Compile
5
6describe('Set && Expression', function() {
7 var render = Velocity.render;
8
9 function getContext(str, context, macros) {
10 var compile = new Compile(parse(str))
11 compile.render(context, macros)
12 return compile.context
13 }
14
15 it('set equal to reference', function() {
16 var vm = '#set( $monkey = $bill ) ## variable reference'
17 assert.equal("hello", getContext(vm, {bill: 'hello'}).monkey)
18 })
19
20 it('empty map', function() {
21 var vm = '#set($foo = {})'
22 assert.deepEqual({}, getContext(vm).foo)
23 })
24
25 it('#set array', function() {
26 var vm = '#set($foo = []) #set($foo[0] = 12)'
27 assert.equal(12, getContext(vm).foo[0])
28 })
29
30 it('set equal to literal', function() {
31 var vm = "#set( $monkey.Friend = 'monica' ) ## string literal\n" +
32 '#set( $monkey.Number = 123 ) ##number literal'
33 assert.equal("monica", getContext(vm).monkey.Friend)
34 assert.equal("123", getContext(vm).monkey.Number)
35 })
36
37 it('set equal to result of method ', function () {
38 var vm = "#set( $monkey = 'monica' ) ## string literal\n" +
39 '#set( $result = $monkey.substring(1) ) ##calling method'
40 assert.equal("monica", getContext(vm).monkey)
41 assert.equal("onica", getContext(vm).result)
42 })
43
44 it('set equal to result of method ', function () {
45 var vm = "#set( $monkey = 1234 ) ## number literal\n" +
46 '#set( $result = $monkey.toString() ) ##calling method'
47 assert.equal("1234", getContext(vm).monkey)
48 assert.equal("1234", getContext(vm).result)
49 })
50
51 it('equal to method/property reference', function() {
52 var vm = "#set($monkey.Blame = $spindoctor.Leak) ## property \n" +
53 '#set( $monkey.Plan = $spindoctor.weave($web) ) ## method'
54 var obj = {
55 spindoctor: {
56 weave: function(name) {
57 return name
58 },
59 Leak: "hello world"
60 },
61 web: "name"
62 }
63
64 assert.equal("hello world", getContext(vm, obj).monkey.Blame)
65 assert.equal("name", getContext(vm, obj).monkey.Plan)
66 })
67
68
69 it('equal to map/list', function() {
70 var vms = [
71 '#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList',
72 '#set( $monkey.Map = {"banana" : "good", "roast beef" : "bad"}) ## Map'
73 ]
74
75 var list = ["Not", "my", "fault"]
76 var map = {banana: "good", 'roast beef': "bad"}
77 assert.deepEqual(list, getContext(vms[0], {my: "my"}).monkey.Say)
78 assert.deepEqual(map, getContext(vms[1]).monkey.Map)
79 })
80
81 it('expression simple math', function() {
82 assert.equal(10, getContext('#set($foo = 2 * 5)').foo)
83 assert.equal(2, getContext('#set($foo = 4 / 2)').foo)
84 assert.equal(-3, getContext('#set($foo = 2 - 5)').foo)
85 assert.equal(1, getContext('#set($foo = 5 % 2)').foo)
86 assert.equal(7, getContext('#set($foo = 7)').foo)
87 })
88
89 it('math with decimal', function() {
90 assert.equal(10.5, getContext('#set($foo = 2.1 * 5)').foo)
91 assert.equal(2.1, getContext('#set($foo = 4.2 / 2)').foo)
92 assert.equal(-7.5, getContext('#set($foo = - 2.5 - 5)').foo)
93 })
94
95 it('expression complex math', function() {
96 assert.equal(20, getContext('#set($foo = (7 + 3) * (10 - 8))').foo)
97 assert.equal(-20, getContext('#set($foo = -(7 + 3) * (10 - 8))').foo)
98 assert.equal(-1, getContext('#set($foo = -7 + 3 * (10 - 8))').foo)
99 })
100
101 it('expression compare', function() {
102 assert.equal(false, getContext('#set($foo = 10 > 11)').foo)
103 assert.equal(true, getContext('#set($foo = 10 < 11)').foo)
104 assert.equal(true, getContext('#set($foo = 10 != 11)').foo)
105 assert.equal(true, getContext('#set($foo = 10 <= 11)').foo)
106 assert.equal(true, getContext('#set($foo = 11 <= 11)').foo)
107 assert.equal(false, getContext('#set($foo = 12 <= 11)').foo)
108 assert.equal(true, getContext('#set($foo = 12 >= 11)').foo)
109 assert.equal(false, getContext('#set($foo = 10 == 11)').foo)
110 })
111
112 it('expression compare text version', function() {
113 assert.equal(false, getContext('#set($foo = 10 gt 11)').foo)
114 assert.equal(true, getContext('#set($foo = 10 lt 11)').foo)
115 assert.equal(true, getContext('#set($foo = 10 ne 11)').foo)
116 assert.equal(true, getContext('#set($foo = 10 le 11)').foo)
117 assert.equal(true, getContext('#set($foo = 11 le 11)').foo)
118 assert.equal(false, getContext('#set($foo = 12 le 11)').foo)
119 assert.equal(true, getContext('#set($foo = 12 ge 11)').foo)
120 assert.equal(false, getContext('#set($foo = 10 eq 11)').foo)
121 })
122
123 it('expression logic', function() {
124 assert.equal(false, getContext('#set($foo = 10 == 11 && 3 > 1)').foo)
125 assert.equal(true, getContext('#set($foo = 10 < 11 && 3 > 1)').foo)
126 assert.equal(true, getContext('#set($foo = 10 > 11 || 3 > 1)').foo)
127 assert.equal(true, getContext('#set($foo = !(10 > 11) && 3 > 1)').foo)
128 assert.equal(false, getContext('#set($foo = $a > $b)', {a: 1, b: 2}).foo)
129 assert.equal(false, getContext('#set($foo = $a && $b)', {a: 1, b: 0}).foo)
130 assert.equal(true, getContext('#set($foo = $a || $b)', {a: 1, b: 0}).foo)
131 })
132
133 it('expression logic text version', function() {
134 assert.equal(false, getContext('#set($foo = 10 eq 11 and 3 gt 1)').foo)
135 assert.equal(true, getContext('#set($foo = 10 lt 11 and 3 gt 1)').foo)
136 assert.equal(true, getContext('#set($foo = 10 gt 11 or 3 gt 1)').foo)
137 assert.equal(true, getContext('#set($foo = not(10 gt 11) and 3 gt 1)').foo)
138 assert.equal(false, getContext('#set($foo = $a gt $b)', {a: 1, b: 2}).foo)
139 assert.equal(false, getContext('#set($foo = $a and $b)', {a: 1, b: 0}).foo)
140 assert.equal(true, getContext('#set($foo = $a or $b)', {a: 1, b: 0}).foo)
141 })
142
143 it('var in key', function() {
144 var vm = '#set($o = {}) #set($key = "k") #set($o[$key] = "c") #set($o.f = "d") $o $o[$key]'
145 var ret = render(vm).replace(/\s+/g, '')
146 assert.equal('{k=c,f=d}c', ret)
147
148 var vm2 = `
149 #set($obj = {})
150 #set($objlist = [
151 {"k": "a"},
152 {"k": "b"},
153 {"k": "c"}
154 ])
155 #foreach( $item in $!{objlist} )
156 #set($obj[$item.k] = $item)
157 #end
158 $obj
159 `
160 var ret2 = render(vm2).replace(/\s+/g, '')
161 assert.equal('{a={k=a},b={k=b},c={k=c}}', ret2);
162 })
163
164 it('#set context should be global, #25', function() {
165 var vm = '#macro(local) #set($val =1) $val #end #local() $val'
166 var ret = render(vm).replace(/\s+/g, '')
167 assert.equal('11', ret)
168 })
169
170 describe('set mult level var', function() {
171 it('normal, fix #63', function() {
172 var tpl = `
173 #set($a = { "b": {} })
174 #set($a.b.c1 = 1)
175 #set($a.b.c2 = 2)
176 `
177 var context = getContext(tpl)
178 context.a.b.should.have.properties('c1', 'c2')
179 })
180
181 it('set fail', function() {
182 var tpl = `
183 #set($a = { "b": {} })
184 #set($a.b.c1 = 1)
185 #set($a.b.c2 = 2)
186 #set($a.d.c2 = 2)
187 `
188 var context = getContext(tpl)
189 context.a.should.not.have.property('d')
190 })
191 })
192
193 it('set with foreach', function() {
194 var tpl = `
195#foreach($item in [1..2])
196 #set($bTest = false)
197 #if($item > 1) #set($bTest = true) #end
198 <h1>$bTest</h1>
199#end`
200 const html = render(tpl)
201 html.should.containEql('true')
202 html.should.containEql('false')
203 })
204
205 it('set error #78', function() {
206 var tpl = `
207 #foreach($item in [1..4])
208 #set($bTest = "test1")
209 #if($item % 2 == 0)
210 #set($bTest = "$!{bTest} test2")
211 #end
212 #set($bTest = "$!{bTest} test3")
213 <h1>$bTest</h1>
214 #end
215 `
216 const html = render(tpl).replace(/\n\s.|\s{2}/g, '').trim();
217 html.should.eql('<h1>test1 test3</h1><h1>test1 test2 test3</h1><h1>test1 test3</h1><h1>test1 test2 test3</h1>');
218 })
219})