Пример #1
0
        public void IntegrationTest_WithExpressionNodes()
        {
            var query = from a in ExpressionHelper.CreateQueryable <Cook> ()
                        from b in ExpressionHelper.CreateQueryable <Cook>()
                        where a.ID > 5
                        select a.ID;

            var nodeTypeRegistry = new MethodInfoBasedNodeTypeRegistry();

            nodeTypeRegistry.Register(SelectExpressionNode.GetSupportedMethods(), typeof(SelectExpressionNode));
            nodeTypeRegistry.Register(SelectManyExpressionNode.GetSupportedMethods(), typeof(SelectManyExpressionNode));
            nodeTypeRegistry.Register(WhereExpressionNode.GetSupportedMethods(), typeof(WhereExpressionNode));

            var selectNode = (SelectExpressionNode) new ExpressionTreeParser(nodeTypeRegistry, new NullExpressionTreeProcessor()).ParseTree(query.Expression);
            var clauseGenerationContext = new ClauseGenerationContext(new MethodInfoBasedNodeTypeRegistry());

            var selectManyNode           = (SelectManyExpressionNode)selectNode.Source.Source;
            var mainSourceExpressionNode = (MainSourceExpressionNode)selectManyNode.Source;

            var queryModel     = mainSourceExpressionNode.Apply(null, clauseGenerationContext);
            var mainFromClause = queryModel.MainFromClause;

            selectManyNode.Apply(queryModel, clauseGenerationContext);                      // only to add the clause to the mapping

            var selectProjection = selectNode.GetResolvedSelector(clauseGenerationContext); // new ( a = IR (a), b = IR (b) ).a.ID

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(selectProjection);

            // IR(a).ID
            Assert.That(result, Is.InstanceOf(typeof(MemberExpression)));
            Expression innerExpression = ((MemberExpression)result).Expression;

            Assert.That(innerExpression, Is.InstanceOf(typeof(QuerySourceReferenceExpression)));
            Assert.That(((QuerySourceReferenceExpression)innerExpression).ReferencedQuerySource, Is.SameAs(mainFromClause));
        }
Пример #2
0
        public void VisitUnknownNonExtensionExpression_Ignored()
        {
            var expression = new UnknownExpression(typeof(object));
            var result     = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(expression);

            Assert.That(result, Is.SameAs(expression));
        }
Пример #3
0
        public void MemberExpression_WithNewExpression_WithoutMembers_ReturnsOriginalExpression()
        {
            // new AnonymousType ().a => same
            var memberExpression = Expression.MakeMemberAccess(
                _anonymousTypeNewExpression,
                _anonymousTypeA);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(memberExpression);

            Assert.That(result, Is.SameAs(memberExpression));
        }
Пример #4
0
        public void MemberExpression_WithNewExpression_WithMembers_ReplacedByAssignedExpression()
        {
            // new AnonymousType ( a = 5 ).a => 5
            var memberExpression = Expression.MakeMemberAccess(
                _anonymousTypeNewExpressionWithAssignments,
                _anonymousTypeA);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(memberExpression);

            Assert.That(result, Is.SameAs(_assignedExpressionA));
        }
Пример #5
0
        public Expression GetResolvedExpression(
            Expression unresolvedExpression, ParameterExpression parameterToBeResolved, ClauseGenerationContext clauseGenerationContext)
        {
            ArgumentUtility.CheckNotNull("unresolvedExpression", unresolvedExpression);
            ArgumentUtility.CheckNotNull("parameterToBeResolved", parameterToBeResolved);

            var sourceNode         = _currentNode.Source;
            var resolvedExpression = sourceNode.Resolve(parameterToBeResolved, unresolvedExpression, clauseGenerationContext);

            resolvedExpression = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(resolvedExpression);
            return(resolvedExpression);
        }
Пример #6
0
        public void MemberExpression_WithMemberInitExpression_ReplacedByAssignedExpression()
        {
            // new AnonymousType { a = 5 }.a => 5
            var memberExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeA, _assignedExpressionA)),
                _anonymousTypeA);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(memberExpression);

            Assert.That(result, Is.SameAs(_assignedExpressionA));
        }
Пример #7
0
        public void MemberExpression_WithoutMemberInit_ReturnsOriginalExpression()
        {
            var anon = new AnonymousType();

            // Constant(anon).a => same
            var memberExpression = Expression.MakeMemberAccess(
                Expression.Constant(anon),
                _anonymousTypeA);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(memberExpression);

            Assert.That(result, Is.SameAs(memberExpression));
        }
Пример #8
0
        public void MemberExpression_WithNoMatchingAssignment_ReturnsOriginalExpression()
        {
            // new AnonymousType { a = 5 }.b => same
            var memberExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeA, _assignedExpressionA)),
                _anonymousTypeB);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(memberExpression);

            Assert.That(result, Is.SameAs(memberExpression));
        }
Пример #9
0
        public void MemberExpression_WithCollectionAssignment_ReturnsOriginalExpression()
        {
            // new AnonymousType { List = { 7 }, a = 5 }.List => same
            var memberExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.ListBind(_anonymousTypeList, Expression.ElementInit(_listAddMethod, Expression.Constant(7))),
                    Expression.Bind(_anonymousTypeA, _assignedExpressionA)),
                _anonymousTypeList);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(memberExpression);

            Assert.That(result, Is.SameAs(memberExpression));
        }
Пример #10
0
        public void MemberExpression_WithNewExpression_WithMethod_ReplacedByAssignedExpression()
        {
            // This strange expression, where a MethodInfo is specified in a NewExpression - instead of a PropertyInfo - is created by the
            // C# compiler for transparent identifiers...

            // new AnonymousType ( get_a() = 5 ).a => 5
            var memberExpression = Expression.MakeMemberAccess(
                _anonymousTypeNewExpressionWithMethodAssignments,
                _anonymousTypeA);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(memberExpression);

            Assert.That(result, Is.SameAs(_assignedExpressionA));
        }
Пример #11
0
        public void VisitExtensionExpression_ChildrenAreProcessed()
        {
            // new TestExtensionExpression (new AnonymousType ( a = 5 ).a) => new TestExtensionExpression (5)
            var memberExpression = Expression.MakeMemberAccess(
                _anonymousTypeNewExpressionWithAssignments,
                _anonymousTypeA);
            var extensionExpression = new ReducibleExtensionExpression(memberExpression);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(extensionExpression);

            var expectedExpression = new ReducibleExtensionExpression(_assignedExpressionA);

            ExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, result);
        }
Пример #12
0
        public void TransparentIdentifier_WithinAnotherExpression()
        {
            // () => new AnonymousType {a = 5}.a => () => 5
            var memberExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeA, _assignedExpressionA)),
                _anonymousTypeA);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(Expression.Lambda(memberExpression));

            Assert.That(result, Is.InstanceOf(typeof(LambdaExpression)));
            Assert.That(((LambdaExpression)result).Body, Is.SameAs(_assignedExpressionA));
        }
Пример #13
0
        public void MemberExpression_WithTwoMatchingAssignments_ReplacedByLastAssignedExpression()
        {
            Expression assignedExpression1 = Expression.Constant(1);
            Expression assignedExpression2 = Expression.Constant(2);

            // new AnonymousType { a = 1, a = 2 }.a => 2
            var memberExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeA, assignedExpression1),
                    Expression.Bind(_anonymousTypeA, assignedExpression2)),
                _anonymousTypeA);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(memberExpression);

            Assert.That(result, Is.SameAs(assignedExpression2));
        }
Пример #14
0
        public void IntegrationTest_WithSimpleExpressions()
        {
            var expression1 = ExpressionHelper.MakeExpression <int, int> (i => new AnonymousType {
                a = i, b = 1
            }.a);
            var expression2 = ExpressionHelper.MakeExpression <int, int> (i => new AnonymousType {
                a = i, b = 1
            }.b);
            var expectedResult1 = ExpressionHelper.MakeExpression <int, int> (i => i);
            var expectedResult2 = ExpressionHelper.MakeExpression <int, int> (i => 1);

            var result1 = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(expression1);
            var result2 = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(expression2);

            ExpressionTreeComparer.CheckAreEqualTrees(expectedResult1, result1);
            ExpressionTreeComparer.CheckAreEqualTrees(expectedResult2, result2);
        }
Пример #15
0
        public void TransparentIdentifier_WithinAnotherMemberExpression()
        {
            Expression assignedExpression = Expression.Constant(null, typeof(List <int>));

            // new AnonymousType {List = null}.List.Count => () => null.Count
            var memberExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeList, assignedExpression)),
                _anonymousTypeList);
            var outerMemberExpression = Expression.MakeMemberAccess(
                memberExpression,
                typeof(List <int>).GetProperty("Count"));

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(outerMemberExpression);

            Assert.That(result, Is.InstanceOf(typeof(MemberExpression)));
            Assert.That(((MemberExpression)result).Expression, Is.SameAs(assignedExpression));
        }
Пример #16
0
        public void ReplaceableExpression_GivingAnotherReplaceableExpression()
        {
            // new AnonymousType { a = (new AnonymousType { a = 5 }.a) }.a => 5
            var replaceableExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeA, _assignedExpressionA)),
                _anonymousTypeA);

            var outerExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeA, replaceableExpression)),
                _anonymousTypeA);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(outerExpression);

            Assert.That(result, Is.SameAs(_assignedExpressionA));
        }
Пример #17
0
        public void TransparentIdentifier_WithinSubQueries()
        {
            // new AnonymousType { a = 5 }.a => 5
            var memberExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeA, _assignedExpressionA)),
                _anonymousTypeA);

            var subQuery = ExpressionHelper.CreateQueryModel_Int();

            subQuery.SelectClause.Selector = memberExpression;

            var subQueryExpression = new SubQueryExpression(subQuery);

            var result = (SubQueryExpression)TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(subQueryExpression);

            Assert.That(result.QueryModel.SelectClause.Selector, Is.SameAs(_assignedExpressionA));
        }
Пример #18
0
        public void TransparentIdentifier_WithinAnotherMemberExpression_WithUnrelatedAssignments()
        {
            // new AnonymousType {a = new AnonymousType {a = 5}.a )}.b => new AnonymousType { a = 5 }.b
            var memberExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeA, _assignedExpressionA)),
                _anonymousTypeA);
            var outerMemberExpression = Expression.MakeMemberAccess(
                Expression.MemberInit(
                    _anonymousTypeNewExpression,
                    Expression.Bind(_anonymousTypeA, memberExpression)),
                _anonymousTypeB);

            var result = TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(outerMemberExpression);

            Assert.That(result, Is.InstanceOf(typeof(MemberExpression)));
            var innerExpression = ((MemberExpression)result).Expression;

            Assert.That(innerExpression, Is.InstanceOf(typeof(MemberInitExpression)));
            Assert.That(((MemberAssignment)((MemberInitExpression)innerExpression).Bindings[0]).Expression, Is.SameAs(_assignedExpressionA));
        }
Пример #19
0
 public Expression Swap(Expression expression)
 {
     return(TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(Visit(expression)));
 }
Пример #20
0
        public static Expression ReWrite(Expression expression, GroupResultOperator groupBy, QueryModel model)
        {
            var visitor = new GroupBySelectClauseRewriter(groupBy, model);

            return(TransparentIdentifierRemovingExpressionVisitor.ReplaceTransparentIdentifiers(visitor.Visit(expression)));
        }