svn-gvsig-desktop / 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 @ 45719
History | View | Annotate | Download (13.1 KB)
1 |
package org.gvsig.expresionevaluator.impl; |
---|---|
2 |
|
3 |
import junit.framework.TestCase; |
4 |
import org.gvsig.expressionevaluator.Code; |
5 |
import org.gvsig.expressionevaluator.Compiler; |
6 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
7 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
8 |
import org.gvsig.expressionevaluator.Interpreter; |
9 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
10 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
11 |
import org.gvsig.expressionevaluator.SymbolTable; |
12 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
13 |
|
14 |
/**
|
15 |
*
|
16 |
* @author jjdelcerro
|
17 |
*/
|
18 |
public class TestGrammarCompiler extends TestCase { |
19 |
|
20 |
public TestGrammarCompiler(String testName) { |
21 |
super(testName);
|
22 |
} |
23 |
|
24 |
@Override
|
25 |
protected void setUp() throws Exception { |
26 |
super.setUp();
|
27 |
new DefaultLibrariesInitializer().fullInitialize();
|
28 |
} |
29 |
|
30 |
@Override
|
31 |
protected void tearDown() throws Exception { |
32 |
super.tearDown();
|
33 |
} |
34 |
|
35 |
// TODO add test methods here. The name must begin with 'test'. For example:
|
36 |
// public void testHello() {}
|
37 |
|
38 |
protected LexicalAnalyzer createLexicalAnalyzer() {
|
39 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
40 |
LexicalAnalyzer lexer = manager.createLexicalAnalyzer(); |
41 |
return lexer;
|
42 |
} |
43 |
|
44 |
protected org.gvsig.expressionevaluator.Compiler createCompiler() {
|
45 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
46 |
Compiler compiler = manager.createCompiler();
|
47 |
compiler.setLexicalAnalyzer(createLexicalAnalyzer()); |
48 |
return compiler;
|
49 |
} |
50 |
|
51 |
protected SymbolTable createSymbolTable() {
|
52 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
53 |
MutableSymbolTable symbolTable = manager.createSymbolTable(); |
54 |
symbolTable.setVar("precio", 200); |
55 |
symbolTable.setVar("1990", 0.168873933773767); |
56 |
return symbolTable;
|
57 |
} |
58 |
|
59 |
protected Interpreter createInterpreter(SymbolTable symbolTable) {
|
60 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
61 |
Interpreter interpreter = manager.createInterpreter(); |
62 |
interpreter.setSymbolTable(symbolTable); |
63 |
return interpreter;
|
64 |
} |
65 |
|
66 |
private void link(Code code) { |
67 |
code.link(createSymbolTable()); |
68 |
} |
69 |
|
70 |
public void testLet() { |
71 |
String source = "LET V1 = 23"; |
72 |
|
73 |
Compiler compiler = createCompiler();
|
74 |
|
75 |
Code code = compiler.compileExpression(source); |
76 |
assertEquals("LET('V1', 23)", code.toString());
|
77 |
} |
78 |
|
79 |
public void testVar() { |
80 |
String source = "VAR V1 DEFAULT 23"; |
81 |
|
82 |
Compiler compiler = createCompiler();
|
83 |
|
84 |
Code code = compiler.compileExpression(source); |
85 |
assertEquals("VAR(\"V1\", 23)", code.toString());
|
86 |
} |
87 |
|
88 |
public void testBlock() { |
89 |
String source = "BEGIN LET V1 = 23; END"; |
90 |
|
91 |
Compiler compiler = createCompiler();
|
92 |
|
93 |
Code code = compiler.compileExpression(source); |
94 |
assertEquals("BLOCK(LET('V1', 23))", code.toString());
|
95 |
} |
96 |
|
97 |
public void testBlockExcept() { |
98 |
String source = "BEGIN LET V1 = 23; EXCEPT NULL; END"; |
99 |
|
100 |
Compiler compiler = createCompiler();
|
101 |
|
102 |
Code code = compiler.compileExpression(source); |
103 |
assertEquals("BLOCK_EXCEPT(LET('V1', 23), NULL)", code.toString());
|
104 |
} |
105 |
|
106 |
public void testBlock2() { |
107 |
String source = "BEGIN LET V1 = 11; LET V2 = 22; END"; |
108 |
|
109 |
Compiler compiler = createCompiler();
|
110 |
|
111 |
Code code = compiler.compileExpression(source); |
112 |
link(code); |
113 |
assertEquals("BLOCK(LET('V1', 11), LET('V2', 22))", code.toString());
|
114 |
} |
115 |
|
116 |
public void testBlockExcept2() { |
117 |
String source = "BEGIN LET V1 = 11; LET V2 = 22; EXCEPT -1; END"; |
118 |
|
119 |
Compiler compiler = createCompiler();
|
120 |
|
121 |
Code code = compiler.compileExpression(source); |
122 |
link(code); |
123 |
assertEquals("BLOCK_EXCEPT(LET('V1', 11), LET('V2', 22), -1)", code.toString());
|
124 |
} |
125 |
|
126 |
public void testBlock3() { |
127 |
Compiler compiler = createCompiler();
|
128 |
|
129 |
String source = "BEGIN LET V1 = 11; LET V2 = 22; END"; |
130 |
Code code = compiler.compileExpression(source); |
131 |
link(code); |
132 |
assertEquals("BLOCK(LET('V1', 11), LET('V2', 22))", code.toString());
|
133 |
|
134 |
source = "BEGIN LET V1 = 11;; LET V2 = 22;;; END";
|
135 |
code = compiler.compileExpression(source); |
136 |
link(code); |
137 |
assertEquals("BLOCK(LET('V1', 11), LET('V2', 22))", code.toString());
|
138 |
} |
139 |
|
140 |
public void testIfThen() { |
141 |
String source = "IF V1 = 11 THEN LET V2 = 22; END IF"; |
142 |
|
143 |
Compiler compiler = createCompiler();
|
144 |
|
145 |
Code code = compiler.compileExpression(source); |
146 |
link(code); |
147 |
assertEquals("IF((\"V1\" = 11), LET('V2', 22))", code.toString());
|
148 |
} |
149 |
|
150 |
public void testIfThenElse() { |
151 |
String source = "IF V1 = 11 THEN LET V2 = 11; ELSE LET V2 = 22; END IF"; |
152 |
|
153 |
Compiler compiler = createCompiler();
|
154 |
|
155 |
Code code = compiler.compileExpression(source); |
156 |
link(code); |
157 |
assertEquals("IF((\"V1\" = 11), LET('V2', 11), LET('V2', 22))", code.toString());
|
158 |
} |
159 |
|
160 |
public void testIfThenBlock() { |
161 |
String source = "IF V1 = 11 THEN LET V2 = 22; LET V1 = 10; END IF"; |
162 |
|
163 |
Compiler compiler = createCompiler();
|
164 |
|
165 |
Code code = compiler.compileExpression(source); |
166 |
link(code); |
167 |
assertEquals("IF((\"V1\" = 11), BLOCK(LET('V2', 22), LET('V1', 10)))", code.toString());
|
168 |
} |
169 |
|
170 |
public void testWhile() { |
171 |
String source = "WHILE n < 10 BEGIN LET n = n + 1; END WHILE"; |
172 |
|
173 |
Compiler compiler = createCompiler();
|
174 |
|
175 |
Code code = compiler.compileExpression(source); |
176 |
link(code); |
177 |
assertEquals("WHILE((\"n\" < 10), LET('n', (\"n\" + 1)))", code.toString());
|
178 |
} |
179 |
|
180 |
public void testWhileBlock() { |
181 |
String source = "WHILE n < 10 BEGIN LET n = n + 1; print(n); END WHILE"; |
182 |
|
183 |
Compiler compiler = createCompiler();
|
184 |
|
185 |
Code code = compiler.compileExpression(source); |
186 |
link(code); |
187 |
assertEquals("WHILE((\"n\" < 10), BLOCK(LET('n', (\"n\" + 1)), print(\"n\")))", code.toString());
|
188 |
} |
189 |
|
190 |
public void testCast() { |
191 |
String source = "CAST(V1 AS INTEGER)"; |
192 |
|
193 |
Compiler compiler = createCompiler();
|
194 |
|
195 |
Code code = compiler.compileExpression(source); |
196 |
link(code); |
197 |
assertEquals("CAST(\"V1\", 'INTEGER')", code.toString());
|
198 |
} |
199 |
|
200 |
public void testCast2() { |
201 |
String source = "CAST V1 AS INTEGER"; |
202 |
|
203 |
Compiler compiler = createCompiler();
|
204 |
|
205 |
Code code = compiler.compileExpression(source); |
206 |
link(code); |
207 |
assertEquals("CAST(\"V1\", 'INTEGER')", code.toString());
|
208 |
} |
209 |
|
210 |
public void testMatch() { |
211 |
Compiler compiler = createCompiler();
|
212 |
String source;
|
213 |
Code code; |
214 |
|
215 |
source = "MATCH 'Hola', '[Hh]ola'";
|
216 |
code = compiler.compileExpression(source); |
217 |
link(code); |
218 |
assertEquals("~('Hola', '[Hh]ola')", code.toString());
|
219 |
|
220 |
source = "MATCH 'Hola' '[Hh]ola'";
|
221 |
code = compiler.compileExpression(source); |
222 |
link(code); |
223 |
assertEquals("~('Hola', '[Hh]ola')", code.toString());
|
224 |
|
225 |
source = "MATCH('Hola', '[Hh]ola')";
|
226 |
code = compiler.compileExpression(source); |
227 |
link(code); |
228 |
assertEquals("~('Hola', '[Hh]ola')", code.toString());
|
229 |
} |
230 |
|
231 |
public void testForEach() { |
232 |
String source = "FOR n in RANGE(10): print(n); END FOR"; |
233 |
|
234 |
Compiler compiler = createCompiler();
|
235 |
|
236 |
Code code = compiler.compileExpression(source); |
237 |
link(code); |
238 |
assertEquals("FOREACH('n', RANGE(10), print(\"n\"))", code.toString());
|
239 |
} |
240 |
|
241 |
// public void testFor() {
|
242 |
// String source = "FOR LET n = 0 ; n < 10 ; LET n = n + 1 print(n)";
|
243 |
//
|
244 |
// Compiler compiler = createCompiler();
|
245 |
//
|
246 |
// Code code = compiler.compileExpression(source);
|
247 |
// assertEquals("FOR(LET('n', 0), <(\"n\", 10), LET('n', +(\"n\", 1)), print(\"n\"))", code.toString());
|
248 |
// }
|
249 |
|
250 |
public void testCase() { |
251 |
String source = "CASE WHEN software LIKE '%gvSIG%' THEN 'gvSIG'; ELSE 'Other'; END CASE"; // ;) |
252 |
|
253 |
Compiler compiler = createCompiler();
|
254 |
|
255 |
Code code = compiler.compileExpression(source); |
256 |
link(code); |
257 |
assertEquals("CASE((\"software\" LIKE '%gvSIG%'), 'gvSIG', 'Other')", code.toString());
|
258 |
} |
259 |
|
260 |
public void testCase1() { |
261 |
String source = "CASE WHEN software LIKE '%gvSIG%' THEN 'gvSIG'; ELSE 'Other'; END CASE"; // ;) |
262 |
|
263 |
Compiler compiler = createCompiler();
|
264 |
|
265 |
Code code = compiler.compileExpression(source); |
266 |
link(code); |
267 |
assertEquals("CASE((\"software\" LIKE '%gvSIG%'), 'gvSIG', 'Other')", code.toString());
|
268 |
} |
269 |
|
270 |
public void testCase2() { |
271 |
String source = "CASE WHEN Field_1 >= 75 AND Field_1 <=79 THEN 100; WHEN Field_1 >=80 AND Field_1 <=84 THEN 110; END CASE"; // ;) |
272 |
|
273 |
Compiler compiler = createCompiler();
|
274 |
|
275 |
Code code = compiler.compileExpression(source); |
276 |
link(code); |
277 |
assertEquals("CASE(((\"Field_1\" >= 75) AND (\"Field_1\" <= 79)), ((\"Field_1\" >= 80) AND (\"Field_1\" <= 84)), 100, 110)", code.toString());
|
278 |
} |
279 |
|
280 |
public void testCase3() { |
281 |
String source = "CASE WHEN Field_1 >= 75 AND Field_1 <=79 THEN 100; WHEN Field_1 >=80 AND Field_1 <=84 THEN 110; ELSE 120; END CASE"; // ;) |
282 |
|
283 |
Compiler compiler = createCompiler();
|
284 |
|
285 |
Code code = compiler.compileExpression(source); |
286 |
link(code); |
287 |
assertEquals("CASE(((\"Field_1\" >= 75) AND (\"Field_1\" <= 79)), ((\"Field_1\" >= 80) AND (\"Field_1\" <= 84)), 100, 110, 120)", code.toString());
|
288 |
} |
289 |
|
290 |
public void testDef1() { |
291 |
String source = "CREATE PROCEDURE test1 AS BEGIN print('Hola'); END PROCEDURE"; |
292 |
|
293 |
Compiler compiler = createCompiler();
|
294 |
|
295 |
Code code = compiler.compileExpression(source); |
296 |
link(code); |
297 |
assertEquals("CREATE_FUNCTION('test1', NULL, print('Hola'), NULL, NULL, NULL)", code.toString());
|
298 |
|
299 |
} |
300 |
|
301 |
public void testDef2() { |
302 |
String source = "CREATE PROCEDURE test1 nombre AS BEGIN print('Hola '+nombre); END PROCEDURE"; |
303 |
|
304 |
Compiler compiler = createCompiler();
|
305 |
|
306 |
Code code = compiler.compileExpression(source); |
307 |
link(code); |
308 |
assertEquals("CREATE_FUNCTION('test1', ARRAY[ 'nombre' ], print(('Hola ' + \"nombre\")), NULL, NULL, NULL)", code.toString());
|
309 |
} |
310 |
|
311 |
public void testDef3() { |
312 |
String source = "CREATE PROCEDURE test1 nombre AS BEGIN RETURN 'Hola '||nombre; END PROCEDURE"; |
313 |
|
314 |
Compiler compiler = createCompiler();
|
315 |
|
316 |
Code code = compiler.compileExpression(source); |
317 |
link(code); |
318 |
assertEquals("CREATE_FUNCTION('test1', ARRAY[ 'nombre' ], RETURN(('Hola ' || \"nombre\")), NULL, NULL, NULL)", code.toString());
|
319 |
} |
320 |
|
321 |
public void testCreateFunctionExtern1() { |
322 |
String source = "CREATE FUNCTION test1(nombre) AS 'addons/test/test', 'test1' LANGUAGE 'cosa'"; |
323 |
|
324 |
Compiler compiler = createCompiler();
|
325 |
|
326 |
Code code = compiler.compileExpression(source); |
327 |
link(code); |
328 |
assertEquals("CREATE_FUNCTION('test1', ARRAY[ 'nombre' ], NULL, 'addons/test/test', 'test1', 'cosa')", code.toString());
|
329 |
} |
330 |
|
331 |
public void testList() { |
332 |
String source = "set x TO [ 'hola','adios','fin' ]"; |
333 |
|
334 |
Compiler compiler = createCompiler();
|
335 |
|
336 |
Code code = compiler.compileExpression(source); |
337 |
link(code); |
338 |
assertEquals("LET('x', ARRAY[ 'hola', 'adios', 'fin' ])", code.toString());
|
339 |
} |
340 |
|
341 |
public void testList2() { |
342 |
String source = "set x TO LIST('hola','adios','fin')"; |
343 |
|
344 |
Compiler compiler = createCompiler();
|
345 |
|
346 |
Code code = compiler.compileExpression(source); |
347 |
link(code); |
348 |
assertEquals("LET('x', ARRAY[ 'hola', 'adios', 'fin' ])", code.toString());
|
349 |
} |
350 |
|
351 |
public void testTuple() { |
352 |
String source = "set x TO TUPLE('hola','adios','fin')"; |
353 |
|
354 |
Compiler compiler = createCompiler();
|
355 |
|
356 |
Code code = compiler.compileExpression(source); |
357 |
link(code); |
358 |
assertEquals("LET('x', TUPLE('hola', 'adios', 'fin'))", code.toString());
|
359 |
} |
360 |
|
361 |
public void testArray() { |
362 |
String source = "set x TO ARRAY['hola','adios','fin']"; |
363 |
|
364 |
Compiler compiler = createCompiler();
|
365 |
|
366 |
Code code = compiler.compileExpression(source); |
367 |
link(code); |
368 |
assertEquals("LET('x', ARRAY[ 'hola', 'adios', 'fin' ])", code.toString());
|
369 |
} |
370 |
|
371 |
public void testUseCase1() { |
372 |
String source = "begin set x = ''; for n in ARRAY['hola','adios','fin']: let x = x || ' ' || n; end for; end"; |
373 |
|
374 |
Compiler compiler = createCompiler();
|
375 |
|
376 |
Code code = compiler.compileExpression(source); |
377 |
link(code); |
378 |
assertEquals("BLOCK(LET('x', ''), FOREACH('n', ARRAY[ 'hola', 'adios', 'fin' ], LET('x', ((\"x\" || ' ') || \"n\"))))", code.toString());
|
379 |
} |
380 |
|
381 |
public void testUseCase2() { |
382 |
String source = "begin set s = ''; set x = ARRAY[ 'hola','adios','fin' ]; FOR n in RANGE(3): set s = s || ' ' || x[n]; end for; end"; |
383 |
|
384 |
Compiler compiler = createCompiler();
|
385 |
|
386 |
Code code = compiler.compileExpression(source); |
387 |
link(code); |
388 |
assertEquals("BLOCK(LET('s', ''), LET('x', ARRAY[ 'hola', 'adios', 'fin' ]), FOREACH('n', RANGE(3), LET('s', ((\"s\" || ' ') || \"x\"[\"n\"]))))", code.toString());
|
389 |
} |
390 |
|
391 |
|
392 |
} |