use of org.mvel2.compiler.ExecutableStatement in project mvel by mikebrock.
the class CoreConfidenceTests method testContextObjMethodCall.
public void testContextObjMethodCall() {
String str = "getName() == \"bob\"";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("this", Bar.class);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
Bar ctx = new Bar();
ctx.setName("bob");
Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx);
assertTrue(result);
}
use of org.mvel2.compiler.ExecutableStatement in project mvel by mikebrock.
the class ArithmeticTests method testModExpr.
public void testModExpr() {
String str = "$y % 4 == 0 && $y % 100 != 0 || $y % 400 == 0 ";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrictTypeEnforcement(true);
pctx.setStrongTyping(true);
pctx.addInput("$y", int.class);
Map<String, Object> vars = new HashMap<String, Object>();
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
for (int i = 0; i < 500; i++) {
int y = i;
boolean expected = y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
vars.put("$y", y);
assertEquals(expected, MVEL.eval(str, vars));
assertEquals(expected, ((Boolean) MVEL.executeExpression(stmt, null, vars)).booleanValue());
}
}
use of org.mvel2.compiler.ExecutableStatement in project mvel by mikebrock.
the class CoreConfidenceTests method testStrTriangleEqualsEquals.
public void testStrTriangleEqualsEquals() {
MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
try {
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.addInput("this", Triangle.class);
pctx.setStrongTyping(true);
String str = "this.strLabel == this";
try {
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
fail("should have failed");
} catch (CompileException e) {
System.out.println();
return;
}
} finally {
MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = false;
}
}
use of org.mvel2.compiler.ExecutableStatement in project mvel by mikebrock.
the class CoreConfidenceTests method testStrictModeAddAll.
public void testStrictModeAddAll() {
String str = "list.addAll( o );";
ParserConfiguration pconf = new ParserConfiguration();
ParserContext pctx = new ParserContext(pconf);
pctx.setStrongTyping(true);
pctx.addInput("o", Object.class);
pctx.addInput("list", ArrayList.class);
try {
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
fail("This should not compileShared, as o is not of a type Collection");
} catch (Exception e) {
}
}
use of org.mvel2.compiler.ExecutableStatement in project mvel by mikebrock.
the class TypesAndInferenceTests method testForLoopTypeCoercion.
public void testForLoopTypeCoercion() {
ParserContext pCtx = ParserContext.create();
pCtx.setStrongTyping(true);
pCtx.addInput("$type", String.class);
pCtx.addInput("l", List.class);
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("$type", "pc!!");
List list = new ArrayList();
vars.put("l", list);
ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression("for (byte bt:$type.getBytes()) {l.add( bt);}", pCtx);
MVEL.executeExpression(stmt, null, vars);
byte[] exp = "pc!!".getBytes();
for (int i = 0; i < exp.length; i++) {
assertEquals(exp[i], list.get(i));
}
}
Aggregations