public void TestScenario04_ControllingWhichMethodsRun()
        {
            var parser = new SpelExpressionParser();
            var ctx    = new StandardEvaluationContext();

            ctx.SetRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it);

            // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM
            // Might be better with a as a variable although it would work as a property too...
            // Variable references using a '#'
            // SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");
            ctx.AddMethodResolver(new MyMethodResolver());

            var expr = parser.ParseRaw("(HasRole(3) or (#a <  1.042)) and HasIpAddress('10.10.0.0/16')");

            bool value;

            ctx.SetVariable("a", 1.0d); // referenced as #a in the expression
            value = expr.GetValue <bool>(ctx);
            Assert.True((bool)value);

            // ctx.setRootObject(new Manager("Luke"));
            // ctx.setVariable("a",1.043d);
            // value = (bool)expr.GetValue(ctx,typeof(bool));
            // assertFalse(value);
        }
Beispiel #2
0
        public void TestAddingMethodResolvers()
        {
            var ctx = new StandardEvaluationContext();

            // reflective method accessor is the only one by default
            var methodResolvers = ctx.MethodResolvers;

            Assert.Single(methodResolvers);

            var dummy = new DummyMethodResolver();

            ctx.AddMethodResolver(dummy);
            Assert.Equal(2, ctx.MethodResolvers.Count);

            var copy = new List <IMethodResolver>(ctx.MethodResolvers);

            Assert.True(ctx.RemoveMethodResolver(dummy));
            Assert.False(ctx.RemoveMethodResolver(dummy));
            Assert.Single(ctx.MethodResolvers);

            ctx.MethodResolvers = copy;
            Assert.Equal(2, ctx.MethodResolvers.Count);
        }