use of org.mvel2.ParserContext in project mvel by mikebrock.
the class CoreConfidenceTests method testStringConcatenation.
public void testStringConcatenation() {
// debugging MVEL code, it seems that MVEL 'thinks' that the result of the expression:
// "Drop +5%: "+$sb+" avg: $"+$av+" price: $"+$pr
// is a double, and as so, he looks for a method:
// Services.log( double );
// but finds only:
// Services.log( String );
// raising the error.
String ex = "services.log((String) \"Drop +5%: \"+$sb+\" avg: $\"+$av+\" price: $\"+$pr );";
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
ctx.addInput("$sb", String.class);
ctx.addInput("$av", double.class);
ctx.addInput("$pr", double.class);
ctx.addInput("services", Services.class);
try {
ExpressionCompiler compiler = new ExpressionCompiler(ex);
compiler.compile(ctx);
} catch (Throwable e) {
e.printStackTrace();
fail("Should not raise exception: " + e.getMessage());
}
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class ArithmeticTests method testJIRA161.
public void testJIRA161() {
Serializable s = MVEL.compileExpression("1==-(-1)", ParserContext.create().stronglyTyped());
assertEquals(1 == -(-1), MVEL.executeExpression(s));
ParserContext ctx = new ParserContext();
ctx.setStrongTyping(true);
CompiledExpression compiledExpression = new ExpressionCompiler("1==-(-1)").compile(ctx);
assertEquals(1 == -(-1), MVEL.executeExpression(compiledExpression));
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class ArithmeticTests method testStrongTypingModeComparison.
public void testStrongTypingModeComparison() {
ParserContext parserContext = new ParserContext();
parserContext.setStrongTyping(true);
parserContext.addInput("a", Long.class);
CompiledExpression compiledExpression = new ExpressionCompiler("a==0").compile(parserContext);
HashMap<String, Object> variables = new HashMap<String, Object>();
variables.put("a", 0l);
MVEL.executeExpression(compiledExpression, variables);
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class MacroProcessorTest method testMacroSupport.
public void testMacroSupport() {
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("foo", new Foo());
Map<String, Interceptor> interceptors = new HashMap<String, Interceptor>();
Map<String, Macro> macros = new HashMap<String, Macro>();
interceptors.put("Modify", new Interceptor() {
public int doBefore(ASTNode node, VariableResolverFactory factory) {
((WithNode) node).getNestedStatement().getValue(null, factory);
factory.createVariable("mod", "FOOBAR!");
return 0;
}
public int doAfter(Object val, ASTNode node, VariableResolverFactory factory) {
return 0;
}
});
macros.put("modify", new Macro() {
public String doMacro() {
return "@Modify with";
}
});
ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("modify (foo) { aValue = 'poo = poo', bValue = 'poo, poo' }; mod", macros));
ParserContext ctx = new ParserContext(null, interceptors, null);
ctx.setSourceFile("test.mv");
ctx.setDebugSymbols(true);
assertEquals("FOOBAR!", executeExpression(compiler.compile(ctx), null, vars));
}
use of org.mvel2.ParserContext in project mvel by mikebrock.
the class MacroProcessorTest method testMacroSupportWithStrings.
public void testMacroSupportWithStrings() {
Map<String, Object> vars = new HashMap<String, Object>();
Foo foo = new Foo();
vars.put("foo", foo);
Map<String, Macro> macros = new HashMap<String, Macro>();
macros.put("modify", new Macro() {
public String doMacro() {
return "drools.modify";
}
});
assertEquals("", foo.aValue);
ExpressionCompiler compiler = new ExpressionCompiler(parseMacros("\"This is an modify()\"", macros));
ParserContext ctx = new ParserContext(null, null, null);
ctx.setSourceFile("test.mv");
ctx.setDebugSymbols(true);
assertEquals("This is an modify()", executeExpression(compiler.compile(ctx), null, vars));
}
Aggregations