Revision 44738 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.expressionevaluator/org.gvsig.expressionevaluator.lib/org.gvsig.expressionevaluator.lib.impl/src/test/java/org/gvsig/expresionevaluator/impl/TestGrammarCompiler.java
TestGrammarCompiler.java | ||
---|---|---|
62 | 62 |
interpreter.setSymbolTable(symbolTable); |
63 | 63 |
return interpreter; |
64 | 64 |
} |
65 |
|
|
66 |
private void link(Code code) { |
|
67 |
code.link(createSymbolTable()); |
|
68 |
} |
|
65 | 69 |
|
66 | 70 |
public void testLet() { |
67 | 71 |
String source = "LET V1 = 23"; |
... | ... | |
96 | 100 |
Compiler compiler = createCompiler(); |
97 | 101 |
|
98 | 102 |
Code code = compiler.compileExpression(source); |
103 |
link(code); |
|
99 | 104 |
assertEquals("BLOCK(LET('V1', 11), LET('V2', 22))", code.toString()); |
100 | 105 |
} |
101 | 106 |
|
... | ... | |
105 | 110 |
Compiler compiler = createCompiler(); |
106 | 111 |
|
107 | 112 |
Code code = compiler.compileExpression(source); |
113 |
link(code); |
|
108 | 114 |
assertEquals("BLOCK_EXCEPT(LET('V1', 11), LET('V2', 22), -1)", code.toString()); |
109 | 115 |
} |
110 | 116 |
|
... | ... | |
113 | 119 |
|
114 | 120 |
String source = "BEGIN LET V1 = 11; LET V2 = 22; END"; |
115 | 121 |
Code code = compiler.compileExpression(source); |
122 |
link(code); |
|
116 | 123 |
assertEquals("BLOCK(LET('V1', 11), LET('V2', 22))", code.toString()); |
117 | 124 |
|
118 | 125 |
source = "BEGIN LET V1 = 11;; LET V2 = 22;;; END"; |
119 | 126 |
code = compiler.compileExpression(source); |
127 |
link(code); |
|
120 | 128 |
assertEquals("BLOCK(LET('V1', 11), LET('V2', 22))", code.toString()); |
121 | 129 |
} |
122 | 130 |
|
... | ... | |
126 | 134 |
Compiler compiler = createCompiler(); |
127 | 135 |
|
128 | 136 |
Code code = compiler.compileExpression(source); |
137 |
link(code); |
|
129 | 138 |
assertEquals("IFF((\"V1\" = 11), LET('V2', 22))", code.toString()); |
130 | 139 |
} |
131 | 140 |
|
... | ... | |
135 | 144 |
Compiler compiler = createCompiler(); |
136 | 145 |
|
137 | 146 |
Code code = compiler.compileExpression(source); |
147 |
link(code); |
|
138 | 148 |
assertEquals("IFF((\"V1\" = 11), LET('V2', 11), LET('V2', 22))", code.toString()); |
139 | 149 |
} |
140 | 150 |
|
... | ... | |
144 | 154 |
Compiler compiler = createCompiler(); |
145 | 155 |
|
146 | 156 |
Code code = compiler.compileExpression(source); |
157 |
link(code); |
|
147 | 158 |
assertEquals("IFF((\"V1\" = 11), BLOCK(LET('V2', 22), LET('V1', 10)))", code.toString()); |
148 | 159 |
} |
149 | 160 |
|
... | ... | |
153 | 164 |
Compiler compiler = createCompiler(); |
154 | 165 |
|
155 | 166 |
Code code = compiler.compileExpression(source); |
167 |
link(code); |
|
156 | 168 |
assertEquals("WHILE((\"n\" < 10), LET('n', (\"n\" + 1)))", code.toString()); |
157 | 169 |
} |
158 | 170 |
|
... | ... | |
162 | 174 |
Compiler compiler = createCompiler(); |
163 | 175 |
|
164 | 176 |
Code code = compiler.compileExpression(source); |
177 |
link(code); |
|
165 | 178 |
assertEquals("WHILE((\"n\" < 10), BLOCK(LET('n', (\"n\" + 1)), print(\"n\")))", code.toString()); |
166 | 179 |
} |
167 | 180 |
|
... | ... | |
171 | 184 |
Compiler compiler = createCompiler(); |
172 | 185 |
|
173 | 186 |
Code code = compiler.compileExpression(source); |
187 |
link(code); |
|
174 | 188 |
assertEquals("CAST(\"V1\", 'INTEGER')", code.toString()); |
175 | 189 |
} |
176 | 190 |
|
... | ... | |
180 | 194 |
Compiler compiler = createCompiler(); |
181 | 195 |
|
182 | 196 |
Code code = compiler.compileExpression(source); |
197 |
link(code); |
|
183 | 198 |
assertEquals("CAST(\"V1\", 'INTEGER')", code.toString()); |
184 | 199 |
} |
185 | 200 |
|
... | ... | |
190 | 205 |
|
191 | 206 |
source = "MATCH 'Hola', '[Hh]ola'"; |
192 | 207 |
code = compiler.compileExpression(source); |
208 |
link(code); |
|
193 | 209 |
assertEquals("~('Hola', '[Hh]ola')", code.toString()); |
194 | 210 |
|
195 | 211 |
source = "MATCH 'Hola' '[Hh]ola'"; |
196 | 212 |
code = compiler.compileExpression(source); |
213 |
link(code); |
|
197 | 214 |
assertEquals("~('Hola', '[Hh]ola')", code.toString()); |
198 | 215 |
|
199 | 216 |
source = "MATCH('Hola', '[Hh]ola')"; |
200 | 217 |
code = compiler.compileExpression(source); |
218 |
link(code); |
|
201 | 219 |
assertEquals("~('Hola', '[Hh]ola')", code.toString()); |
202 | 220 |
} |
203 | 221 |
|
... | ... | |
207 | 225 |
Compiler compiler = createCompiler(); |
208 | 226 |
|
209 | 227 |
Code code = compiler.compileExpression(source); |
228 |
link(code); |
|
210 | 229 |
assertEquals("FOREACH('n', RANGE(10), print(\"n\"))", code.toString()); |
211 | 230 |
} |
212 | 231 |
|
... | ... | |
225 | 244 |
Compiler compiler = createCompiler(); |
226 | 245 |
|
227 | 246 |
Code code = compiler.compileExpression(source); |
247 |
link(code); |
|
228 | 248 |
assertEquals("CASE((\"software\" LIKE '%gvSIG%'), 'gvSIG', 'Other')", code.toString()); |
229 | 249 |
} |
230 | 250 |
|
... | ... | |
234 | 254 |
Compiler compiler = createCompiler(); |
235 | 255 |
|
236 | 256 |
Code code = compiler.compileExpression(source); |
257 |
link(code); |
|
237 | 258 |
assertEquals("CASE((\"software\" LIKE '%gvSIG%'), 'gvSIG', 'Other')", code.toString()); |
238 | 259 |
} |
239 | 260 |
|
... | ... | |
243 | 264 |
Compiler compiler = createCompiler(); |
244 | 265 |
|
245 | 266 |
Code code = compiler.compileExpression(source); |
267 |
link(code); |
|
246 | 268 |
assertEquals("CASE(((\"Field_1\" >= 75) AND (\"Field_1\" <= 79)), ((\"Field_1\" >= 80) AND (\"Field_1\" <= 84)), 100, 110)", code.toString()); |
247 | 269 |
} |
248 | 270 |
|
... | ... | |
252 | 274 |
Compiler compiler = createCompiler(); |
253 | 275 |
|
254 | 276 |
Code code = compiler.compileExpression(source); |
277 |
link(code); |
|
255 | 278 |
assertEquals("CASE(((\"Field_1\" >= 75) AND (\"Field_1\" <= 79)), ((\"Field_1\" >= 80) AND (\"Field_1\" <= 84)), 100, 110, 120)", code.toString()); |
256 | 279 |
} |
257 | 280 |
|
... | ... | |
261 | 284 |
Compiler compiler = createCompiler(); |
262 | 285 |
|
263 | 286 |
Code code = compiler.compileExpression(source); |
264 |
assertEquals("CREATE_FUNCTION('test1', print('Hola'))", code.toString()); |
|
287 |
link(code); |
|
288 |
assertEquals("CREATE_FUNCTION(FUNCTION_NAME:'test1', BODY:print('Hola'))", code.toString()); |
|
289 |
|
|
265 | 290 |
} |
266 | 291 |
|
267 | 292 |
public void testDef2() { |
... | ... | |
270 | 295 |
Compiler compiler = createCompiler(); |
271 | 296 |
|
272 | 297 |
Code code = compiler.compileExpression(source); |
273 |
assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), print(('Hola ' + \"nombre\")))", code.toString()); |
|
298 |
link(code); |
|
299 |
assertEquals("CREATE_FUNCTION(FUNCTION_NAME:'test1', PARAMETERS:LIST('nombre'), BODY:print(('Hola ' + \"nombre\")))", code.toString()); |
|
274 | 300 |
} |
275 | 301 |
|
276 | 302 |
public void testDef3() { |
... | ... | |
279 | 305 |
Compiler compiler = createCompiler(); |
280 | 306 |
|
281 | 307 |
Code code = compiler.compileExpression(source); |
282 |
assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), RETURN(('Hola ' || \"nombre\")))", code.toString()); |
|
308 |
link(code); |
|
309 |
assertEquals("CREATE_FUNCTION(FUNCTION_NAME:'test1', PARAMETERS:LIST('nombre'), BODY:RETURN(('Hola ' || \"nombre\")))", code.toString()); |
|
283 | 310 |
} |
284 | 311 |
|
285 | 312 |
public void testCreateFunctionExtern1() { |
... | ... | |
288 | 315 |
Compiler compiler = createCompiler(); |
289 | 316 |
|
290 | 317 |
Code code = compiler.compileExpression(source); |
291 |
assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), 'addons/test/test', 'test1', 'cosa')", code.toString()); |
|
318 |
link(code); |
|
319 |
assertEquals("CREATE_FUNCTION(FUNCTION_NAME:'test1', PARAMETERS:LIST('nombre'), SCRIPT_PATH:'addons/test/test', SCRIPT_FUNCTION:'test1', LANGUAGE:'cosa')", code.toString()); |
|
292 | 320 |
} |
293 | 321 |
|
294 | 322 |
public void testList() { |
... | ... | |
297 | 325 |
Compiler compiler = createCompiler(); |
298 | 326 |
|
299 | 327 |
Code code = compiler.compileExpression(source); |
328 |
link(code); |
|
300 | 329 |
assertEquals("LET('x', LIST('hola', 'adios', 'fin'))", code.toString()); |
301 | 330 |
} |
302 | 331 |
|
332 |
public void testTuple() { |
|
333 |
String source = "set x TO TUPLE('hola','adios','fin')"; |
|
334 |
|
|
335 |
Compiler compiler = createCompiler(); |
|
336 |
|
|
337 |
Code code = compiler.compileExpression(source); |
|
338 |
link(code); |
|
339 |
assertEquals("LET('x', TUPLE('hola', 'adios', 'fin'))", code.toString()); |
|
340 |
} |
|
341 |
|
|
303 | 342 |
public void testArray() { |
304 |
String source = "set x TO ARRAY['hola','adios','fin']";
|
|
343 |
String source = "set x TO ARRAY('hola','adios','fin')";
|
|
305 | 344 |
|
306 | 345 |
Compiler compiler = createCompiler(); |
307 | 346 |
|
308 | 347 |
Code code = compiler.compileExpression(source); |
309 |
assertEquals("LET('x', LIST('hola', 'adios', 'fin'))", code.toString()); |
|
348 |
link(code); |
|
349 |
assertEquals("LET('x', ARRAY('hola', 'adios', 'fin'))", code.toString()); |
|
310 | 350 |
} |
311 | 351 |
|
312 | 352 |
public void testUseCase1() { |
... | ... | |
315 | 355 |
Compiler compiler = createCompiler(); |
316 | 356 |
|
317 | 357 |
Code code = compiler.compileExpression(source); |
358 |
link(code); |
|
318 | 359 |
assertEquals("BLOCK(LET('x', ''), FOREACH('n', list('hola', 'adios', 'fin'), LET('x', ((\"x\" || ' ') || \"n\"))))", code.toString()); |
319 | 360 |
} |
320 | 361 |
|
... | ... | |
324 | 365 |
Compiler compiler = createCompiler(); |
325 | 366 |
|
326 | 367 |
Code code = compiler.compileExpression(source); |
368 |
link(code); |
|
327 | 369 |
assertEquals("BLOCK(LET('s', ''), LET('x', LIST('hola', 'adios', 'fin')), FOREACH('n', RANGE(3), LET('s', ((\"s\" || ' ') || GETITEM(\"x\", \"n\")))))", code.toString()); |
328 | 370 |
} |
329 | 371 |
|
Also available in: Unified diff