Exemplo n.º 1
0
        public void ConditionalMember_Compile_Field_Val()
        {
            var p = Expression.Parameter(typeof(Quz?));
            var q = new Quz(false);

            var m1 = CSharpExpression.ConditionalField(p, "Y");
            var f1 = Expression.Lambda <Func <Quz?, int?> >(m1, p);
            var d1 = f1.Compile();

            Assert.AreEqual(42, d1(q));
            Assert.IsNull(d1(null));

            var m2 = CSharpExpression.ConditionalField(p, "O");
            var f2 = Expression.Lambda <Func <Quz?, int?> >(m2, p);
            var d2 = f2.Compile();

            Assert.AreEqual(42, d2(q));
            Assert.IsNull(d2(null));

            var m3 = CSharpExpression.ConditionalField(p, "T");
            var f3 = Expression.Lambda <Func <Quz?, string> >(m3, p);
            var d3 = f3.Compile();

            Assert.AreEqual("bar", d3(q));
            Assert.IsNull(d3(null));
        }
        public void ConditionalAccess_ManOrBoy1()
        {
            var p = Expression.Parameter(typeof(C));

            var x = Expression.Constant(0);

            var F = p.Type.GetField("F");
            var P = p.Type.GetProperty("P");
            var I = p.Type.GetProperty("Item");
            var M = p.Type.GetMethod("M");

            var cF = (Func <Expression, Expression>)(e => CSharpExpression.ConditionalField(e, F));
            var cP = (Func <Expression, Expression>)(e => CSharpExpression.ConditionalProperty(e, P));
            var cI = (Func <Expression, Expression>)(e => CSharpExpression.ConditionalIndex(e, I, new[] { x }));
            var cM = (Func <Expression, Expression>)(e => CSharpExpression.ConditionalCall(e, M, new[] { x }));

            var cases = new Func <Expression, Expression>[][]
            {
                new[] { cF },
                new[] { cF, cP },
                new[] { cF, cP, cI },
                new[] { cF, cP, cI, cM },
                new[] { cM },
                new[] { cM, cF },
                new[] { cM, cF, cP },
                new[] { cM, cF, cP, cI },
                new[] { cI },
                new[] { cI, cM },
                new[] { cI, cM, cF },
                new[] { cI, cM, cF, cP },
                new[] { cP },
                new[] { cP, cI },
                new[] { cP, cI, cM },
                new[] { cP, cI, cM, cF },
            };

            foreach (var cs in cases)
            {
                var e = cs.Aggregate((Expression)p, (a, b) => b(a));

                var f = Expression.Lambda <Func <C, C> >(e, p).Compile();

                Assert.IsNull(f(null));

                for (var i = 0; i < cs.Length; i++)
                {
                    Assert.IsNull(f(new C(i)));
                }

                Assert.IsNotNull(f(new C(cs.Length)));
            }
        }
Exemplo n.º 3
0
        public void ConditionalMember_Factory_ArgumentChecking()
        {
            var expr      = Expression.Default(typeof(Bar));
            var other     = Expression.Default(typeof(string));
            var propName  = "P";
            var propInfo  = typeof(Bar).GetProperty(propName);
            var getInfo   = propInfo.GetGetMethod(true);
            var fieldName = "F";
            var fieldInfo = typeof(Bar).GetField(fieldName);

            // null
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalProperty(default(Expression), propName));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalProperty(expr, default(string)));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalProperty(default(Expression), propInfo));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalProperty(expr, default(PropertyInfo)));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalProperty(default(Expression), getInfo));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalProperty(expr, default(MethodInfo)));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalField(default(Expression), fieldName));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalField(expr, default(string)));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalField(default(Expression), fieldInfo));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.ConditionalField(expr, default(FieldInfo)));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.MakeConditionalMemberAccess(default(Expression), propInfo));
            AssertEx.Throws <ArgumentNullException>(() => CSharpExpression.MakeConditionalMemberAccess(expr, default(MemberInfo)));

            // not exist
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalProperty(expr, "X"));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalField(expr, "X"));

            // static
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalProperty(expr, expr.Type.GetProperty("SP")));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalField(expr, expr.Type.GetField("SF")));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalProperty(expr, "SP"));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalField(expr, "SF"));

            // set-only
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalProperty(expr, expr.Type.GetProperty("XP")));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalProperty(expr, "XP"));

            // indexer
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalProperty(expr, expr.Type.GetProperty("Item")));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalProperty(expr, "Item"));

            // wrong declaring type
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalProperty(other, expr.Type.GetProperty("P")));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalProperty(other, "P"));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalField(other, expr.Type.GetField("F")));
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.ConditionalField(other, "F"));

            // not field or property
            AssertEx.Throws <ArgumentException>(() => CSharpExpression.MakeConditionalMemberAccess(expr, expr.Type.GetConstructors()[0]));
        }
Exemplo n.º 4
0
        public void ConditionalMember_Update()
        {
            var expr1     = Expression.Default(typeof(Bar));
            var expr2     = Expression.Default(typeof(Bar));
            var propName  = "P";
            var propInfo  = typeof(Bar).GetProperty(propName);
            var fieldName = "F";
            var fieldInfo = typeof(Bar).GetField(fieldName);

            var res1 = CSharpExpression.ConditionalProperty(expr1, propInfo);
            var res2 = CSharpExpression.ConditionalField(expr1, fieldInfo);

            Assert.AreSame(res1, res1.Update(res1.Expression));
            Assert.AreSame(res2, res2.Update(res2.Expression));

            var upd1 = res1.Update(expr2);
            var upd2 = res2.Update(expr2);

            Assert.AreSame(expr2, upd1.Expression);
            Assert.AreSame(expr2, upd2.Expression);
        }
Exemplo n.º 5
0
        public void ConditionalMember_Properties()
        {
            var expr      = Expression.Default(typeof(Bar));
            var propName  = "P";
            var propInfo  = typeof(Bar).GetProperty(propName);
            var getInfo   = propInfo.GetGetMethod(true);
            var fieldName = "F";
            var fieldInfo = typeof(Bar).GetField(fieldName);

            foreach (var e in new[]
            {
                CSharpExpression.ConditionalField(expr, fieldInfo),
                CSharpExpression.ConditionalField(expr, fieldName),
                CSharpExpression.MakeConditionalMemberAccess(expr, fieldInfo),
            })
            {
                Assert.AreSame(expr, e.Expression);
                Assert.AreEqual(fieldInfo, e.Member);
                Assert.AreEqual(typeof(int?), e.Type);
                Assert.AreEqual(CSharpExpressionType.ConditionalAccess, e.CSharpNodeType);
            }

            foreach (var e in new[]
            {
                CSharpExpression.ConditionalProperty(expr, propInfo),
                CSharpExpression.ConditionalProperty(expr, getInfo),
                CSharpExpression.ConditionalProperty(expr, propName),
                CSharpExpression.MakeConditionalMemberAccess(expr, propInfo),
            })
            {
                Assert.AreSame(expr, e.Expression);
                Assert.AreEqual(propInfo, e.Member);
                Assert.AreEqual(typeof(int?), e.Type);
                Assert.AreEqual(CSharpExpressionType.ConditionalAccess, e.CSharpNodeType);
            }
        }