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 @ 44750
History | View | Annotate | Download (12.6 KB)
1 |
package org.gvsig.expresionevaluator.impl; |
---|---|
2 |
|
3 |
import junit.framework.TestCase; |
4 |
import org.gvsig.expressionevaluator.Code; |
5 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
6 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
7 |
import org.gvsig.expressionevaluator.Interpreter; |
8 |
import org.gvsig.expressionevaluator.LexicalAnalyzer; |
9 |
import org.gvsig.expressionevaluator.SymbolTable; |
10 |
import org.gvsig.expressionevaluator.Compiler; |
11 |
import org.gvsig.expressionevaluator.MutableSymbolTable; |
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 testBlock() { |
80 |
String source = "BEGIN LET V1 = 23; END"; |
81 |
|
82 |
Compiler compiler = createCompiler();
|
83 |
|
84 |
Code code = compiler.compileExpression(source); |
85 |
assertEquals("BLOCK(LET('V1', 23))", code.toString());
|
86 |
} |
87 |
|
88 |
public void testBlockExcept() { |
89 |
String source = "BEGIN LET V1 = 23; EXCEPT NULL; END"; |
90 |
|
91 |
Compiler compiler = createCompiler();
|
92 |
|
93 |
Code code = compiler.compileExpression(source); |
94 |
assertEquals("BLOCK_EXCEPT(LET('V1', 23), NULL)", code.toString());
|
95 |
} |
96 |
|
97 |
public void testBlock2() { |
98 |
String source = "BEGIN LET V1 = 11; LET V2 = 22; END"; |
99 |
|
100 |
Compiler compiler = createCompiler();
|
101 |
|
102 |
Code code = compiler.compileExpression(source); |
103 |
link(code); |
104 |
assertEquals("BLOCK(LET('V1', 11), LET('V2', 22))", code.toString());
|
105 |
} |
106 |
|
107 |
public void testBlockExcept2() { |
108 |
String source = "BEGIN LET V1 = 11; LET V2 = 22; EXCEPT -1; END"; |
109 |
|
110 |
Compiler compiler = createCompiler();
|
111 |
|
112 |
Code code = compiler.compileExpression(source); |
113 |
link(code); |
114 |
assertEquals("BLOCK_EXCEPT(LET('V1', 11), LET('V2', 22), -1)", code.toString());
|
115 |
} |
116 |
|
117 |
public void testBlock3() { |
118 |
Compiler compiler = createCompiler();
|
119 |
|
120 |
String source = "BEGIN LET V1 = 11; LET V2 = 22; END"; |
121 |
Code code = compiler.compileExpression(source); |
122 |
link(code); |
123 |
assertEquals("BLOCK(LET('V1', 11), LET('V2', 22))", code.toString());
|
124 |
|
125 |
source = "BEGIN LET V1 = 11;; LET V2 = 22;;; END";
|
126 |
code = compiler.compileExpression(source); |
127 |
link(code); |
128 |
assertEquals("BLOCK(LET('V1', 11), LET('V2', 22))", code.toString());
|
129 |
} |
130 |
|
131 |
public void testIfThen() { |
132 |
String source = "IF V1 = 11 THEN LET V2 = 22; END IF"; |
133 |
|
134 |
Compiler compiler = createCompiler();
|
135 |
|
136 |
Code code = compiler.compileExpression(source); |
137 |
link(code); |
138 |
assertEquals("IFF((\"V1\" = 11), LET('V2', 22))", code.toString());
|
139 |
} |
140 |
|
141 |
public void testIfThenElse() { |
142 |
String source = "IF V1 = 11 THEN LET V2 = 11; ELSE LET V2 = 22; END IF"; |
143 |
|
144 |
Compiler compiler = createCompiler();
|
145 |
|
146 |
Code code = compiler.compileExpression(source); |
147 |
link(code); |
148 |
assertEquals("IFF((\"V1\" = 11), LET('V2', 11), LET('V2', 22))", code.toString());
|
149 |
} |
150 |
|
151 |
public void testIfThenBlock() { |
152 |
String source = "IF V1 = 11 THEN LET V2 = 22; LET V1 = 10; END IF"; |
153 |
|
154 |
Compiler compiler = createCompiler();
|
155 |
|
156 |
Code code = compiler.compileExpression(source); |
157 |
link(code); |
158 |
assertEquals("IFF((\"V1\" = 11), BLOCK(LET('V2', 22), LET('V1', 10)))", code.toString());
|
159 |
} |
160 |
|
161 |
public void testWhile() { |
162 |
String source = "WHILE n < 10 BEGIN LET n = n + 1; END WHILE"; |
163 |
|
164 |
Compiler compiler = createCompiler();
|
165 |
|
166 |
Code code = compiler.compileExpression(source); |
167 |
link(code); |
168 |
assertEquals("WHILE((\"n\" < 10), LET('n', (\"n\" + 1)))", code.toString());
|
169 |
} |
170 |
|
171 |
public void testWhileBlock() { |
172 |
String source = "WHILE n < 10 BEGIN LET n = n + 1; print(n); END WHILE"; |
173 |
|
174 |
Compiler compiler = createCompiler();
|
175 |
|
176 |
Code code = compiler.compileExpression(source); |
177 |
link(code); |
178 |
assertEquals("WHILE((\"n\" < 10), BLOCK(LET('n', (\"n\" + 1)), print(\"n\")))", code.toString());
|
179 |
} |
180 |
|
181 |
public void testCast() { |
182 |
String source = "CAST(V1 AS INTEGER)"; |
183 |
|
184 |
Compiler compiler = createCompiler();
|
185 |
|
186 |
Code code = compiler.compileExpression(source); |
187 |
link(code); |
188 |
assertEquals("CAST(\"V1\", 'INTEGER')", code.toString());
|
189 |
} |
190 |
|
191 |
public void testCast2() { |
192 |
String source = "CAST V1 AS INTEGER"; |
193 |
|
194 |
Compiler compiler = createCompiler();
|
195 |
|
196 |
Code code = compiler.compileExpression(source); |
197 |
link(code); |
198 |
assertEquals("CAST(\"V1\", 'INTEGER')", code.toString());
|
199 |
} |
200 |
|
201 |
public void testMatch() { |
202 |
Compiler compiler = createCompiler();
|
203 |
String source;
|
204 |
Code code; |
205 |
|
206 |
source = "MATCH 'Hola', '[Hh]ola'";
|
207 |
code = compiler.compileExpression(source); |
208 |
link(code); |
209 |
assertEquals("~('Hola', '[Hh]ola')", code.toString());
|
210 |
|
211 |
source = "MATCH 'Hola' '[Hh]ola'";
|
212 |
code = compiler.compileExpression(source); |
213 |
link(code); |
214 |
assertEquals("~('Hola', '[Hh]ola')", code.toString());
|
215 |
|
216 |
source = "MATCH('Hola', '[Hh]ola')";
|
217 |
code = compiler.compileExpression(source); |
218 |
link(code); |
219 |
assertEquals("~('Hola', '[Hh]ola')", code.toString());
|
220 |
} |
221 |
|
222 |
public void testForEach() { |
223 |
String source = "FOR n in RANGE(10): print(n); END FOR"; |
224 |
|
225 |
Compiler compiler = createCompiler();
|
226 |
|
227 |
Code code = compiler.compileExpression(source); |
228 |
link(code); |
229 |
assertEquals("FOREACH('n', RANGE(10), print(\"n\"))", code.toString());
|
230 |
} |
231 |
|
232 |
// public void testFor() {
|
233 |
// String source = "FOR LET n = 0 ; n < 10 ; LET n = n + 1 print(n)";
|
234 |
//
|
235 |
// Compiler compiler = createCompiler();
|
236 |
//
|
237 |
// Code code = compiler.compileExpression(source);
|
238 |
// assertEquals("FOR(LET('n', 0), <(\"n\", 10), LET('n', +(\"n\", 1)), print(\"n\"))", code.toString());
|
239 |
// }
|
240 |
|
241 |
public void testCase() { |
242 |
String source = "CASE WHEN software LIKE '%gvSIG%' THEN 'gvSIG'; ELSE 'Other'; END CASE"; // ;) |
243 |
|
244 |
Compiler compiler = createCompiler();
|
245 |
|
246 |
Code code = compiler.compileExpression(source); |
247 |
link(code); |
248 |
assertEquals("CASE((\"software\" LIKE '%gvSIG%'), 'gvSIG', 'Other')", code.toString());
|
249 |
} |
250 |
|
251 |
public void testCase1() { |
252 |
String source = "CASE WHEN software LIKE '%gvSIG%' THEN 'gvSIG'; ELSE 'Other'; END CASE"; // ;) |
253 |
|
254 |
Compiler compiler = createCompiler();
|
255 |
|
256 |
Code code = compiler.compileExpression(source); |
257 |
link(code); |
258 |
assertEquals("CASE((\"software\" LIKE '%gvSIG%'), 'gvSIG', 'Other')", code.toString());
|
259 |
} |
260 |
|
261 |
public void testCase2() { |
262 |
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"; // ;) |
263 |
|
264 |
Compiler compiler = createCompiler();
|
265 |
|
266 |
Code code = compiler.compileExpression(source); |
267 |
link(code); |
268 |
assertEquals("CASE(((\"Field_1\" >= 75) AND (\"Field_1\" <= 79)), ((\"Field_1\" >= 80) AND (\"Field_1\" <= 84)), 100, 110)", code.toString());
|
269 |
} |
270 |
|
271 |
public void testCase3() { |
272 |
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"; // ;) |
273 |
|
274 |
Compiler compiler = createCompiler();
|
275 |
|
276 |
Code code = compiler.compileExpression(source); |
277 |
link(code); |
278 |
assertEquals("CASE(((\"Field_1\" >= 75) AND (\"Field_1\" <= 79)), ((\"Field_1\" >= 80) AND (\"Field_1\" <= 84)), 100, 110, 120)", code.toString());
|
279 |
} |
280 |
|
281 |
public void testDef1() { |
282 |
String source = "CREATE PROCEDURE test1 AS BEGIN print('Hola'); END PROCEDURE"; |
283 |
|
284 |
Compiler compiler = createCompiler();
|
285 |
|
286 |
Code code = compiler.compileExpression(source); |
287 |
link(code); |
288 |
assertEquals("CREATE_FUNCTION('test1', NULL, print('Hola'), NULL, NULL, NULL)", code.toString());
|
289 |
|
290 |
} |
291 |
|
292 |
public void testDef2() { |
293 |
String source = "CREATE PROCEDURE test1 nombre AS BEGIN print('Hola '+nombre); END PROCEDURE"; |
294 |
|
295 |
Compiler compiler = createCompiler();
|
296 |
|
297 |
Code code = compiler.compileExpression(source); |
298 |
link(code); |
299 |
assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), print(('Hola ' + \"nombre\")), NULL, NULL, NULL)", code.toString());
|
300 |
} |
301 |
|
302 |
public void testDef3() { |
303 |
String source = "CREATE PROCEDURE test1 nombre AS BEGIN RETURN 'Hola '||nombre; END PROCEDURE"; |
304 |
|
305 |
Compiler compiler = createCompiler();
|
306 |
|
307 |
Code code = compiler.compileExpression(source); |
308 |
link(code); |
309 |
assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), RETURN(('Hola ' || \"nombre\")), NULL, NULL, NULL)", code.toString());
|
310 |
} |
311 |
|
312 |
public void testCreateFunctionExtern1() { |
313 |
String source = "CREATE FUNCTION test1(nombre) AS 'addons/test/test', 'test1' LANGUAGE 'cosa'"; |
314 |
|
315 |
Compiler compiler = createCompiler();
|
316 |
|
317 |
Code code = compiler.compileExpression(source); |
318 |
link(code); |
319 |
assertEquals("CREATE_FUNCTION('test1', LIST('nombre'), NULL, 'addons/test/test', 'test1', 'cosa')", code.toString());
|
320 |
} |
321 |
|
322 |
public void testList() { |
323 |
String source = "set x TO LIST('hola','adios','fin')"; |
324 |
|
325 |
Compiler compiler = createCompiler();
|
326 |
|
327 |
Code code = compiler.compileExpression(source); |
328 |
link(code); |
329 |
assertEquals("LET('x', LIST('hola', 'adios', 'fin'))", code.toString());
|
330 |
} |
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 |
|
342 |
public void testArray() { |
343 |
String source = "set x TO ARRAY('hola','adios','fin')"; |
344 |
|
345 |
Compiler compiler = createCompiler();
|
346 |
|
347 |
Code code = compiler.compileExpression(source); |
348 |
link(code); |
349 |
assertEquals("LET('x', ARRAY('hola', 'adios', 'fin'))", code.toString());
|
350 |
} |
351 |
|
352 |
public void testUseCase1() { |
353 |
String source = "begin set x = ''; for n in list('hola','adios','fin'): let x = x || ' ' || n; end for; end"; |
354 |
|
355 |
Compiler compiler = createCompiler();
|
356 |
|
357 |
Code code = compiler.compileExpression(source); |
358 |
link(code); |
359 |
assertEquals("BLOCK(LET('x', ''), FOREACH('n', list('hola', 'adios', 'fin'), LET('x', ((\"x\" || ' ') || \"n\"))))", code.toString());
|
360 |
} |
361 |
|
362 |
public void testUseCase2() { |
363 |
String source = "begin set s = ''; set x = LIST('hola','adios','fin'); FOR n in RANGE(3): set s = s || ' ' || x[n]; end for; end"; |
364 |
|
365 |
Compiler compiler = createCompiler();
|
366 |
|
367 |
Code code = compiler.compileExpression(source); |
368 |
link(code); |
369 |
assertEquals("BLOCK(LET('s', ''), LET('x', LIST('hola', 'adios', 'fin')), FOREACH('n', RANGE(3), LET('s', ((\"s\" || ' ') || GETITEM(\"x\", \"n\")))))", code.toString());
|
370 |
} |
371 |
|
372 |
|
373 |
} |