public void ExpandExpressionsExpand1MethodCallTest() { Expression <Func <int, int> > f2 = x => Getter().Evaluate(x) * 3; var result = f2.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(6, result.Evaluate(1)); NoEvaluationsAssertion.AssertNoEvaluations(result); }
public void ExpandExpressionsSimpleTest() { Expression <Func <int, int> > f1 = x => x + 2; var result = f1.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(3, result.Compile()(1)); NoEvaluationsAssertion.AssertNoEvaluations(result); }
public void ExpandExpressionsExpand1InvokeTest() { Expression <Func <int, int> > f1 = x => x + 2; Expression <Func <int, int> > f2 = x => f1.Compile().Invoke(x) * 3; var result = f2.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(9, result.Compile()(1)); NoEvaluationsAssertion.AssertNoEvaluations(result); }
public void ExpandExpressionsExpand1QuoteParameterizedTest() { Expression <Func <int, Expression <Func <int, int> > > > f1 = x => y => x * 2 + y; Expression <Func <int, int> > f2 = z => f1.Evaluate(z).Evaluate(z * 3) + 1; var result = f2.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(6, result.Compile()(1)); NoEvaluationsAssertion.AssertNoEvaluations(result); }
public void ExpandExpressionsDuplicateParameterComlexTest() { Expression <Func <int, int> > f1 = x => new[] { x + 1 }.Single(y => y != x); Expression <Func <int, int> > f2 = x => x * x; Expression <Func <int, int> > f3 = x => f2.Evaluate(f1.Evaluate(x)); var result = f3.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); }
public void ExpandExpressionsExpandNestedEvaluateTest() { Expression <Func <int, int> > f1 = x => x + 2; Expression <Func <int, int> > f2 = y => f1.Evaluate(f1.Evaluate(y) + f1.Evaluate(4)) * 3; var result = f2.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(33, result.Evaluate(1)); NoEvaluationsAssertion.AssertNoEvaluations(result); }
public void ExpandExpressionsExpand2EvaluateMethodGroupTest() { Expression <Func <int, bool> > f1 = x => x == 2; Expression <Func <IEnumerable <int>, IEnumerable <int> > > f2 = x => x.Where(f1.Evaluate).Concat(x.Where(f1.Evaluate)); var result = f2.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(6, result.Evaluate(new[] { 1, 2, 2, 2, 3 }).Count()); NoEvaluationsAssertion.AssertNoEvaluations(result); NoConvertAssertion.AssertNoConverts(result); }
public void ExpandExpressionsNonCompileInvocationTest() { Expression <Func <int, int> > f1 = x => GetGetter()() + x; Assert.AreEqual(2, f1.Compile()(1)); var result = f1.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(2, result.Compile()(1)); NoEvaluationsAssertion.AssertNoEvaluations(result); }
public void ExpandExpressionsVeryComplexEvaluateTest3() { Expression <Func <int, Expression <Func <int, int> > > > f1 = x => (y => y + x); Expression <Func <int, int> > f2 = z => f1.Evaluate(z).Evaluate(5); Assert.AreEqual(6, f2.Evaluate(1)); var result = f2.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(6, result.Evaluate(1)); NoEvaluationsAssertion.AssertNoEvaluations(result); }
public void ExpandExpressionsDirtyEvaluateTest() { Expression <Func <int, Expression <Func <int, int> > > > f1 = x => (y => y + x); Expression <Func <int, int> > f2 = z => f1.Evaluate(DirtyGetter(z).Evaluate(1)).Evaluate(f1.Evaluate(5).Evaluate(z)); Assert.AreEqual(8, f2.Evaluate(1)); var result = f2.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(8, result.Evaluate(1)); // The result will have Evaluate calls cause DirtyGetter(z) cannot be expanded }
public void ExpandExpressionsVeryComplexSameNamesTest2() { Expression <Func <int, Expression <Func <int, int> > > > f1 = x => (y => y + x); Expression <Func <int, int> > f2 = y => f1.Compile()(f1.Compile()(y).Compile()(1)).Compile()(f1.Compile()(y).Compile()(5)); Assert.AreEqual(8, f2.Compile()(1)); var result = f2.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(8, result.Compile()(1)); NoEvaluationsAssertion.AssertNoEvaluations(result); }
public void ExpandExpressionsExpand1QuoteTest() { Expression <Func <int, int> > f1 = y => y + 2; var x = Expression.Parameter(typeof(int), "x"); Expression <Func <int, int> > f2 = Expression.Lambda <Func <int, int> >( Expression.Multiply( Expression.Call( ReflectionExpressions.GetMethodInfo <Expression <Func <int, int> > >(expression => expression.Evaluate(default(int))), Expression.Quote(f1), x), Expression.Constant(3)), new[] { x }); var result = f2.ExpandExpressions(); NoDuplicateParameterAssertion.AssertNoDuplicateParameters(result); Assert.AreEqual(9, result.Compile()(1)); NoEvaluationsAssertion.AssertNoEvaluations(result); }