Exemplo n.º 1
0
 public void TryLayeredCatch()
 {
     Flexpression <Func <string> >
     .Create()
     .Set <string>("trace", () => string.Empty)
     .Try()
     .Try()
     .Try()
     .Set <string, string>("trace", trace => trace + "a")
     .Throw(() => new ArgumentNullException("b"))
     .Catch <ArgumentNullException>("ex")
     .Set <string, ArgumentNullException, string>("trace", (trace, ex) => trace + ex.ParamName)
     .Throw()
     .Finally()
     .Set <string, string>("trace", trace => trace + "c")
     .End()
     .End()
     .Catch <ArgumentException>("ex2")
     .Set <string, ArgumentException, string>("trace", (trace, ex2) => trace + ex2.ParamName)
     .Throw(() => new NotImplementedException())
     .EndTry()
     .End()
     .Catch()
     .Set <string, string>("trace", trace => trace + "d")
     .End()
     .EndTry()
     .Return <string, string>(trace => trace)
     .CreateLambda()
     .TestExpression("abcbd");
 }
        public void FlexpressionDeclareLabelTargetWithDuplicateName()
        {
            var flexpression = Flexpression <Action <int[]> > .Create().End();

            flexpression.DeclareLabelTarget(Expression.Label(typeof(void), "a"));
            flexpression.DeclareLabelTarget(Expression.Label(typeof(void), "a"));
        }
Exemplo n.º 3
0
        public void IfWithTwoElses()
        {
            var ifBlock = Flexpression <Action <int[]> > .Create().If(() => false).End();

            ifBlock.Else();
            ifBlock.Else();
        }
Exemplo n.º 4
0
        public void ExpressionRewriterFailureWithOuterVariables()
        {
            int i = 42;

            Flexpression <Func <int> > .Create(false)
            .Return <int>(() => i);
        }
Exemplo n.º 5
0
        public void BlockInputValidation()
        {
            var block = Flexpression <Action <int[]> > .Create();

            block.ValidateInputCase
            (
                new InputCase("Act", typeof(ArgumentNullException), new object[] { null }),
                new InputCase("Declare", typeof(ArgumentException), new object[] { null }),
                new InputCase("Declare", typeof(ArgumentException), new object[] { "" }),
                new InputCase("Declare", typeof(ArgumentException), new object[] { "     " }),
                new InputCase("DeclareLabelTarget", typeof(ArgumentNullException), new object[] { null }),
                new InputCase("Do", typeof(ArgumentNullException), new object[] { null }),
                new InputCase("Foreach", typeof(ArgumentException), new object[] { null, (Expression <Func <IEnumerable> >)(() => new int[0]) }),
                new InputCase("Foreach", typeof(ArgumentException), new object[] { "", (Expression <Func <IEnumerable> >)(() => new int[0]) }),
                new InputCase("Foreach", typeof(ArgumentException), new object[] { "     ", (Expression <Func <IEnumerable> >)(() => new int[0]) }),
                new InputCase("Foreach", typeof(ArgumentNullException), new object[] { "a", null }),
                new InputCase("If", typeof(ArgumentNullException), new object[] { null }),
                new InputCase("InsertLabel", typeof(ArgumentException), new object[] { null, null }),
                new InputCase("Return", typeof(ArgumentNullException), new object[] { null }),
                new InputCase("Set", typeof(ArgumentNullException), new object[] { "a", null }),
                new InputCase("Set", typeof(ArgumentException), new object[] { null, null }),
                new InputCase("Set", typeof(ArgumentException), new object[] { "", null }),
                new InputCase("Set", typeof(ArgumentException), new object[] { "     ", null }),
                new InputCase("Switch", typeof(ArgumentNullException), new object[] { null }),
                new InputCase("Throw", typeof(ArgumentNullException), new object[] { null }),
                new InputCase("Using", typeof(ArgumentNullException), new object[] { "a", null }),
                new InputCase("Using", typeof(ArgumentException), new object[] { null, null }),
                new InputCase("Using", typeof(ArgumentException), new object[] { "", null }),
                new InputCase("Using", typeof(ArgumentException), new object[] { "     ", null }),
                new InputCase("While", typeof(ArgumentNullException), new object[] { null })
            );
        }
Exemplo n.º 6
0
        public void BlockSwitchExercise()
        {
            Expression inputAction;
            var        inputs            = Enumerable.Range(0, 17).ToArray();
            var        callCount         = 0;
            var        expectedCallCount = 0;

            Func <int[], int> interceptor = (int[] arguments) =>
            {
                ++callCount;

                Assert.IsTrue(arguments.Length <= inputs.Length);
                CollectionAssert.AreEqual(inputs.Take(arguments.Length).ToArray(), arguments);

                return(42);
            };

            var funcs = Utility.CreateFuncs <int, int>(interceptor);
            var block = Flexpression <Action <int[]> > .Create(true, "i");

            var blockType = block.GetType();

            for (int j = 0; j < inputs.Length; ++j)
            {
                int k = j;
                block.Set <int[], int>(string.Format("p{0}", j + 1), i => i[k]);
            }

            foreach (var genericMethod in blockType.GetMethods().Where(x => (x.Name == "Switch")))
            {
                var methodToInvoke = genericMethod.MakeMethodConcrete <int, int>();
                var parameterType  = methodToInvoke.GetParameters()[0].ParameterType;

                if (funcs.TryGetValue(parameterType, out inputAction))
                {
                    ++expectedCallCount;

                    if (funcs.TryGetValue(parameterType, out inputAction))
                    {
                        var switchBlock = (Switch <Block <Flexpression <Action <int[]> > >, int>)methodToInvoke.Invoke(block, new object[] { inputAction });

                        switchBlock
                        .Case(() => 0)
                        .Begin()
                        .End()
                        .EndSwitch();
                    }
                    else if (parameterType != typeof(Expression))
                    {
                        Assert.Fail("Unable to find matching action for {0}.", parameterType.GetFriendlyName());
                    }
                }
            }

            var method = block.End().Compile();

            method(inputs);

            Assert.AreEqual <int>(expectedCallCount, callCount);
        }
Exemplo n.º 7
0
 public void SwitchCreateExpressionWithNoContentInSwitch()
 {
     Flexpression <Func <int> >
     .Create()
     .Switch(() => 0)
     .CreateExpression(null);
 }
Exemplo n.º 8
0
 public void SwitchCreateExpressionWithNonNullArgument()
 {
     Flexpression <Func <int> >
     .Create()
     .Switch(() => 0)
     .CreateExpression(Enumerable.Empty <Expression>());
 }
Exemplo n.º 9
0
 public void SwitchAssigningNullDefaultBody()
 {
     Flexpression <Func <int> >
     .Create()
     .Switch(() => 0)
     .AssignDefaultBody(null);
 }
Exemplo n.º 10
0
 public void SwitchWithNoContent()
 {
     Flexpression <Func <int> >
     .Create()
     .Switch(() => 0)
     .EndSwitch();
 }
        public void IFlexpressionAllowRethrowWithinCatch()
        {
            var loopBlock = Flexpression <Action <int> > .Create().Try().End().Catch().Do(() => true);

            var usingBlock      = loopBlock.Using("iter", () => Enumerable.Empty <int>().GetEnumerator());
            var switchBlock     = usingBlock.Switch(() => 5);
            var switchCaseBlock = switchBlock.Case(() => 5);
            var block           = switchCaseBlock.Begin().Try().If(() => true);
            var ifBlock         = block.Act(() => Console.WriteLine("Fail!")).End();
            var catchBlock      = ifBlock.EndIf().End().Catch();
            var tryBlock        = catchBlock.End();

            foreach (IFlexpression fe in new IFlexpression[]
            {
                loopBlock,
                usingBlock,
                switchBlock,
                switchCaseBlock,
                block,
                ifBlock,
                catchBlock,
                tryBlock
            })
            {
                Assert.IsTrue(fe.AllowRethrow());
            }
        }
Exemplo n.º 12
0
        public void BlockInsertLabelWithDuplicateLabelName()
        {
            LabelTarget labelTarget;

            Flexpression <Action> .Create()
            .InsertLabel("a", out labelTarget)
            .InsertLabel("a", out labelTarget);
        }
Exemplo n.º 13
0
        public void ExpressionRewriterTypeCompatibility()
        {
            var method = Flexpression <Func <StringReader, int> > .Create(true)
                         .Return <TextReader, int>(p1 => p1.Peek())
                         .Compile();

            Assert.AreEqual <int>((int)'a', method(new StringReader("abc")));
        }
Exemplo n.º 14
0
        public void IfInputValidation()
        {
            var ifBlock = Flexpression <Action <int[]> > .Create().If(() => false).End();

            ifBlock.ValidateInputCase
            (
                new InputCase("ElseIf", typeof(ArgumentNullException), new object[] { null })
            );
        }
Exemplo n.º 15
0
 public void BlockSetWithNullableType()
 {
     Flexpression <Func <Nullable <int>, int> >
     .Create()
     .Set <Nullable <int>, int>("newValue", p1 => p1.Value)
     .Return <int, int>(newValue => newValue)
     .CreateLambda()
     .TestExpression(42, new Nullable <int>(42));
 }
Exemplo n.º 16
0
        public void SwitchCaseCaseInputValidation()
        {
            var switchBlock = Flexpression <Action <int[]> > .Create().Switch(() => 2).Case(() => 3);

            switchBlock.ValidateInputCase
            (
                new InputCase("Case", typeof(ArgumentNullException), new object[] { null })
            );
        }
Exemplo n.º 17
0
        public void TryCreateExpressionWithoutCatchFinally()
        {
            var tryBlock = Flexpression <Func <int> >
                           .Create()
                           .Try()
                           .End();

            tryBlock.CreateExpression(null);
        }
        public void FlexpressionCompileWithDebugInfoGenerator()
        {
            var method = Flexpression <Func <int> >
                         .Create()
                         .Return <int>(() => 234)
                         .Compile(null, false, DebugInfoGenerator.CreatePdbGenerator());

            Assert.AreEqual <int>(234, method());
        }
Exemplo n.º 19
0
 public void SwitchWithTwoDefaults()
 {
     Flexpression <Func <int> >
     .Create()
     .Switch(() => 0)
     .Default()
     .Begin()
     .Return <int>(() => 5)
     .Default();
 }
Exemplo n.º 20
0
        public void ExpressionRewriterTypeMismatch()
        {
            int i = 42;

            var method = Flexpression <Func <int, int> > .Create(true)
                         .Return <string, int>(p1 => p1.Length)
                         .Compile();

            Assert.AreEqual <int>(i, method(i));
        }
Exemplo n.º 21
0
        public void ExpressionRewriterSuccessWithParameters()
        {
            int i = 42;

            var method = Flexpression <Func <int, int> > .Create(true)
                         .Return <int, int>(p1 => p1)
                         .Compile();

            Assert.AreEqual <int>(i, method(i));
        }
Exemplo n.º 22
0
        public void ExpressionRewriterSuccessWithOuterVariables()
        {
            int i = 42;

            var method = Flexpression <Func <int> > .Create(true)
                         .Return <int>(() => i)
                         .Compile();

            Assert.AreEqual <int>(i, method());
        }
Exemplo n.º 23
0
 /// <summary>
 /// The Flexpressions version of a summation function.
 /// </summary>
 /// <returns>The <see cref="LambdaExpression"/> representing the summation function.</returns>
 private LambdaExpression FlexpressionSummation()
 {
     return(Flexpression <Func <IEnumerable <int>, int> >
            .Create()
            .Set <int>("sum", () => 0)
            .Foreach <int, IEnumerable <int>, IEnumerable <int> >("x", p1 => p1)
            .Set <int, int, int>("sum", (x, sum) => x + sum)
            .End()
            .Return <int, int>(sum => sum)
            .CreateLambda());
 }
Exemplo n.º 24
0
 public void TryWithNothingElse()
 {
     var e = Flexpression <Func <IEnumerable <int>, int> >
             .Create()
             .Try()
             .Act(() => Console.WriteLine("Fail!"))
             .End()
             .EndTry()
             .End()
             .Compile();
 }
Exemplo n.º 25
0
 public void SwitchCaseWithCaseThenDefaultWithNonNullArgument()
 {
     var method = Flexpression <Func <int> >
                  .Create()
                  .Switch(() => 0)
                  .Case(() => 4)
                  .Begin()
                  .Return <int>(() => 5)
                  .Case(() => 2)
                  .CreateExpression(Enumerable.Empty <Expression>());
 }
Exemplo n.º 26
0
 public void BlockBreak()
 {
     Flexpression <Func <bool> >
     .Create()
     .Set <int>("counter", () => 0)
     .While <int>(counter => counter < 5)
     .Set <int, int>("counter", counter => counter + 1)
     .Break()
     .Return <int, bool>(counter => counter == 1)
     .CreateLambda()
     .TestExpression(true);
 }
Exemplo n.º 27
0
 public void TryWithRethrowNotInCatch()
 {
     var e = Flexpression <Func <IEnumerable <int>, int> >
             .Create()
             .Try()
             .Throw()
             .Catch()
             .End()
             .EndTry()
             .End()
             .Compile();
 }
Exemplo n.º 28
0
        public void BlockThrowExercise()
        {
            Expression inputAction;
            var        inputs = Enumerable.Range(0, 17).ToArray();

            Func <int[], Exception> interceptor = (int[] arguments) =>
            {
                Assert.IsTrue(arguments.Length <= inputs.Length);
                CollectionAssert.AreEqual(inputs.Take(arguments.Length).ToArray(), arguments);

                return(new NotImplementedException());
            };

            var funcs     = Utility.CreateFuncs <int, Exception>(interceptor);
            var blockType = typeof(Block <Flexpression <Action <int[]> > >);

            foreach (var genericMethod in blockType.GetMethods().Where(x => (x.Name == "Throw") && (x.GetParameters().Length > 0)))
            {
                var methodToInvoke = genericMethod.MakeMethodConcrete <int, int>();
                var parameterType  = methodToInvoke.GetParameters()[0].ParameterType;

                if (funcs.TryGetValue(parameterType, out inputAction))
                {
                    if (funcs.TryGetValue(parameterType, out inputAction))
                    {
                        var block = Flexpression <Action <int[]> > .Create(true, "i");

                        for (int j = 0; j < inputs.Length; ++j)
                        {
                            int k = j;
                            block.Set <int[], int>(string.Format("p{0}", j + 1), i => i[k]);
                        }

                        methodToInvoke.Invoke(block, new object[] { inputAction });

                        try
                        {
                            block.End().Compile()(inputs);
                            Assert.Fail("NotImplementedException expected.");
                        }
                        catch (NotImplementedException)
                        {
                            /* Intentionally left blank. */
                        }
                    }
                    else if (parameterType != typeof(Expression))
                    {
                        Assert.Fail("Unable to find matching action for {0}.", parameterType.GetFriendlyName());
                    }
                }
            }
        }
Exemplo n.º 29
0
        public void TryInputValidation()
        {
            var tryBlock = Flexpression <Action <int[]> > .Create().Try().End();

            tryBlock.ValidateInputCase
            (
                new InputCase("Catch", typeof(ArgumentException), new object[] { null }),
                new InputCase("Catch", typeof(ArgumentException), new object[] { "" }),
                new InputCase("Catch", typeof(ArgumentException), new object[] { "     " }),
                new InputCase("CreateExpression", typeof(ArgumentException), new object[] { Enumerable.Empty <Expression>() }),
                new InputCase("DeclareLabelTarget", typeof(ArgumentNullException), new object[] { null })
            );
        }
Exemplo n.º 30
0
 public void TryWithCatchSpecificException()
 {
     Flexpression <Func <bool> >
     .Create()
     .Try()
     .Throw(() => new MulticastNotSupportedException())
     .Catch <MulticastNotSupportedException>()
     .Return <bool>(() => true)
     .EndTry()
     .Return <bool>(() => false)
     .CreateLambda()
     .TestExpression(true);
 }