Search in sources :

Example 11 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mvel.

the class CoreConfidenceTests method testInterfaceMethodCallWithMacro.

@SuppressWarnings({ "unchecked" })
public void testInterfaceMethodCallWithMacro() {
    Map macros = new HashMap(1);
    macros.put("retract", new Macro() {

        public String doMacro() {
            return "drools.retract";
        }
    });
    Map map = new HashMap();
    DefaultKnowledgeHelper helper = new DefaultKnowledgeHelper();
    map.put("drools", helper);
    Cheese cheese = new Cheese("stilton", 15);
    map.put("cheese", cheese);
    executeExpression(compileExpression(parseMacros("retract(cheese)", macros)), map);
    assertSame(cheese, helper.retracted.get(0));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Macro(org.mvel2.Macro) DefaultKnowledgeHelper(org.mvel2.tests.core.res.DefaultKnowledgeHelper) Cheese(org.mvel2.tests.core.res.Cheese) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mvel.

the class CoreConfidenceTests method testStringConcatenation2.

public void testStringConcatenation2() {
    String ex = "services.log( $cheese + \" some string \" );";
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("$cheese", Cheese.class);
    ctx.addInput("services", Services.class);
    try {
        ExpressionCompiler compiler = new ExpressionCompiler(ex, ctx);
        compiler.compile();
    } catch (Throwable e) {
        e.printStackTrace();
        fail("Should not raise exception: " + e.getMessage());
    }
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Example 13 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mikebrock.

the class CoreConfidenceTests method testIncrementAndAssignWithInputs.

public void testIncrementAndAssignWithInputs() {
    ExpressionCompiler compiler = new ExpressionCompiler("total += cheese");
    compiler.compile();
    assertTrue(compiler.getParserContextState().getInputs().containsKey("total"));
    assertTrue(compiler.getParserContextState().getInputs().containsKey("cheese"));
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler)

Example 14 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mikebrock.

the class CoreConfidenceTests method testContextFieldNotFound.

public void testContextFieldNotFound() {
    String str = "'stilton'.equals( type );";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("this", Cheese.class);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    MVEL.executeExpression(stmt, new Cheese(), new HashMap());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Example 15 with Cheese

use of org.mvel2.tests.core.res.Cheese in project mvel by mikebrock.

the class DebuggerTests method testBreakpointsAcrossWith.

public void testBreakpointsAcrossWith() {
    String line1 = "System.out.println( \"a1\" );\n";
    String line2 = "c = new Cheese();\n";
    String line3 = "with ( c ) { type = 'cheddar',\n" + "             price = 10 };\n";
    String line4 = "System.out.println( \"a1\" );\n";
    String expr = line1 + line2 + line3 + line4;
    System.out.println(expr);
    ExpressionCompiler compiler = new ExpressionCompiler(expr);
    ParserContext context = new ParserContext();
    context.addImport("System", System.class);
    context.addImport("Cheese", Cheese.class);
    context.setStrictTypeEnforcement(true);
    context.setDebugSymbols(true);
    context.setSourceFile("mysource");
    String s = org.mvel2.debug.DebugTools.decompile(compiler.compile(context));
    System.out.println("output: " + s);
    int fromIndex = 0;
    int count = 0;
    while ((fromIndex = s.indexOf("DEBUG_SYMBOL", fromIndex + 1)) > -1) {
        count++;
    }
    assertEquals(5, count);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext)

Aggregations

ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)12 ParserContext (org.mvel2.ParserContext)11 HashMap (java.util.HashMap)9 Cheese (org.mvel2.tests.core.res.Cheese)9 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 Map (java.util.Map)6 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)5 LinkedHashMap (java.util.LinkedHashMap)4 List (java.util.List)4 ParserConfiguration (org.mvel2.ParserConfiguration)4 ClassObjectType (org.drools.core.base.ClassObjectType)3 Declaration (org.drools.core.rule.Declaration)3 PatternExtractor (org.drools.core.spi.PatternExtractor)3 Test (org.junit.Test)3 SimpleTemplateRegistry (org.mvel2.templates.SimpleTemplateRegistry)3 TemplateRegistry (org.mvel2.templates.TemplateRegistry)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Cheese (org.drools.compiler.Cheese)2 Person (org.drools.compiler.Person)2 ClassImportResolverFactory (org.mvel2.integration.impl.ClassImportResolverFactory)2