示例#1
0
        public void AnonymousTypeTupletizer_Constant_Simple0()
        {
            var x = new { };

            var a = Expression.Constant(x);
            var b = Expression.Constant("bar");

            var t = AnonymousTypeTupletizer.Tupletize(a, b);

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#2
0
        public void AnonymousTypeTupletizer_Constant_Simple2()
        {
            var x = new { a = 42, b = "bar" };

            var a = Expression.Constant(x);
            var b = Expression.Constant(new Tuple <int, string>(42, "bar"));

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#3
0
        public void AnonymousTypeTupletizer_Constant_Simple9()
        {
            var x = new { a = 42, b = "bar", c = 12.34, d = false, e = 'a', f = 49.95m, g = 0.1f, h = (byte)128, i = new TimeSpan(1, 2, 3) };

            var a = Expression.Constant(x);
            var b = Expression.Constant(new Tuple <int, string, double, bool, char, decimal, float, Tuple <byte, TimeSpan> >(42, "bar", 12.34, false, 'a', 49.95m, 0.1f, new Tuple <byte, TimeSpan>(128, new TimeSpan(1, 2, 3))));

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#4
0
        public void AnonymousTypeTupletizer_Constant_Null()
        {
            var x = new { a = 42 };

            var a = Expression.Constant(value: null, x.GetType());
            var b = Expression.Constant(value: null, typeof(Tuple <int>));

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#5
0
        public void AnonymousTypeTupletizer_Nested3()
        {
            Expression <Func <int> > f = () => new { a = new { b = new { a = new { b = 42 } } } }.a.b.a.b;
            var a = f.Body;

            Expression <Func <int> > g = () => new Tuple <Tuple <Tuple <Tuple <int> > > >(new Tuple <Tuple <Tuple <int> > >(new Tuple <Tuple <int> >(new Tuple <int>(42)))).Item1.Item1.Item1.Item1;
            var b = g.Body;

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#6
0
        public void AnonymousTypeTupletizer_Nested1()
        {
            Expression <Func <object> > f = () => new { a = 42, b = new { c = "bar", d = false }, e = 12.34, f = new { g = 'a' } };
            var a = f.Body;

            Expression <Func <object> > g = () => new Tuple <int, Tuple <string, bool>, double, Tuple <char> >(42, new Tuple <string, bool>("bar", false), 12.34, new Tuple <char>('a'));
            var b = g.Body;

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
 public void AnonymousTypeTupletizer_NoChange()
 {
     foreach (var e in new Expression[]
     {
         Expression.Constant(1),
         (Expression <Func <int> >)(() => DateTime.Now.Year),
         (Expression <Func <TimeSpan> >)(() => new TimeSpan(1, 2, 3)),
         (Expression <Func <IEnumerable <int>, IEnumerable <string> > >)(xs => from x in xs where x % 2 == 0 select x.ToString())
     })
     {
         var t = AnonymousTypeTupletizer.Tupletize(e, Expression.Constant(value: null));
         Assert.AreSame(e, t);
     }
 }
示例#8
0
        public void AnonymousTypeTupletizer_Simple15_Accessor3()
        {
            Expression <Func <char> > f = () => new { a = 42, b = "bar", c = 3.14, d = false, e = 49.95m, f = DateTime.Now, g = new TimeSpan(1, 2, 3), h = Guid.NewGuid(), i = (byte)128, j = 12.34f, k = 24, l = true, m = "foo", n = (object)null, o = 'a' }.o;
            var a = f.Body;

            Expression <Func <char> > g = () => new Tuple <int, string, double, bool, decimal, DateTime, TimeSpan, Tuple <Guid, byte, float, int, bool, string, object, Tuple <char> > >(42, "bar", 3.14, false, 49.95m, DateTime.Now, new TimeSpan(1, 2, 3), new Tuple <Guid, byte, float, int, bool, string, object, Tuple <char> >(Guid.NewGuid(), 128, 12.34f, 24, true, "foo", null, new Tuple <char>('a'))).Rest.Rest.Item1;
            var b = g.Body;

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#9
0
        public void AnonymousTypeTupletizer_Simple10()
        {
            Expression <Func <object> > f = () => new { a = 42, b = "bar", c = 3.14, d = false, e = 49.95m, f = DateTime.Now, g = new TimeSpan(1, 2, 3), h = Guid.NewGuid(), i = (byte)128, j = 12.34f };
            var a = f.Body;

            Expression <Func <object> > g = () => new Tuple <int, string, double, bool, decimal, DateTime, TimeSpan, Tuple <Guid, byte, float> >(42, "bar", 3.14, false, 49.95m, DateTime.Now, new TimeSpan(1, 2, 3), new Tuple <Guid, byte, float>(Guid.NewGuid(), 128, 12.34f));
            var b = g.Body;

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#10
0
        public void AnonymousTypeTupletizer_Simple8_Accessor2()
        {
            Expression <Func <Guid> > f = () => new { a = 42, b = "bar", c = 3.14, d = false, e = 49.95m, f = DateTime.Now, g = new TimeSpan(1, 2, 3), h = Guid.NewGuid() }.h;
            var a = f.Body;

            Expression <Func <Guid> > g = () => new Tuple <int, string, double, bool, decimal, DateTime, TimeSpan, Tuple <Guid> >(42, "bar", 3.14, false, 49.95m, DateTime.Now, new TimeSpan(1, 2, 3), new Tuple <Guid>(Guid.NewGuid())).Rest.Item1;
            var b = g.Body;

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#11
0
        public void AnonymousTypeTupletizer_Simple2()
        {
            Expression <Func <object> > f = () => new { a = 42, b = "bar" };
            var a = f.Body;

            Expression <Func <object> > g = () => new Tuple <int, string>(42, "bar");
            var b = g.Body;

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#12
0
        public void AnonymousTypeTupletizer_Simple1_Accessor()
        {
            Expression <Func <int> > f = () => new { a = 42 }.a;
            var a = f.Body;

            Expression <Func <int> > g = () => new Tuple <int>(42).Item1;
            var b = g.Body;

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));
        }
示例#13
0
        public void AnonymousTypeTupletizer_NoChange()
        {
            foreach (var e in new Expression[]
            {
                Expression.Constant(1),
#pragma warning disable IDE0004 // Remove Unnecessary Cast. (Only unnecessary on C# 10 or later.)
                (Expression <Func <int> >)(() => DateTime.Now.Year),
                (Expression <Func <TimeSpan> >)(() => new TimeSpan(1, 2, 3)),
#pragma warning restore IDE0004
                (Expression <Func <IEnumerable <int>, IEnumerable <string> > >)(xs => from x in xs where x % 2 == 0 select x.ToString())
            })
            {
                var t = AnonymousTypeTupletizer.Tupletize(e, Expression.Constant(value: null));
                Assert.AreSame(e, t);
            }
        }
示例#14
0
        public void AnonymousTypeTupletizer_Simple2_GetHashCode()
        {
            Expression <Func <bool> > f = () => new { a = 42, b = "bar" }.GetHashCode() == new { a = 42, b = "bar" }.GetHashCode();
            var a = f.Body;

            Expression <Func <bool> > g = () => new Tuple <int, string>(42, "bar").GetHashCode() == new Tuple <int, string>(42, "bar").GetHashCode();
            var b = g.Body;

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));

            Assert.IsTrue(a.Evaluate <bool>());
            Assert.IsTrue(b.Evaluate <bool>());
            Assert.IsTrue(t.Evaluate <bool>());
        }
示例#15
0
        public void AnonymousTypeTupletizer_Simple2_ToString()
        {
            Expression <Func <string> > f = () => new { a = 42, b = "bar" }.ToString();
            var a = f.Body;

            Expression <Func <string> > g = () => new Tuple <int, string>(42, "bar").ToString();
            var b = g.Body;

            var t = AnonymousTypeTupletizer.Tupletize(a, Expression.Constant(value: null));

            var eq = new ExpressionEqualityComparer();

            Assert.IsTrue(eq.Equals(b, t));

            Assert.AreEqual("{ a = 42, b = bar }", a.Evaluate <string>());
            Assert.AreEqual("(42, bar)", b.Evaluate <string>());
            Assert.AreEqual("(42, bar)", t.Evaluate <string>());
        }
示例#16
0
        public void AnonymousTypeTupletizer_ManOrBoy()
        {
            var expr = (from x in Enumerable.Empty <object>().AsQueryable()
                        group x by x into g
                        let a = g.OfType <bool>()
                                let b = g.OfType <string>()
                                        let z = Enumerable.Zip(a, b, (x, y) => new { x, y })
                                                from t in z
                                                select t.x ? t.y : "null")
                       .Expression;

            var res = AnonymousTypeTupletizer.Tupletize(expr, Expression.Constant(value: null));

            var csharp = CompilerGeneratedNameEliminator.Prettify(res).ToCSharpString();

            Assert.AreEqual(
                StripWhitespace(@"
                    __c0.GroupBy<object, object>((object x) => x)
                        .Select<IGrouping<object, object>, Tuple<IGrouping<object, object>, IEnumerable<bool>>>(
                            (IGrouping<object, object> g) =>
                                new Tuple<IGrouping<object, object>, IEnumerable<bool>>(
                                    g,
                                    g.OfType<bool>()))
                        .Select<Tuple<IGrouping<object, object>, IEnumerable<bool>>, Tuple<Tuple<IGrouping<object, object>, IEnumerable<bool>>, IEnumerable<string>>>(
                            (Tuple<IGrouping<object, object>, IEnumerable<bool>> t) =>
                                new Tuple<Tuple<IGrouping<object, object>, IEnumerable<bool>>, IEnumerable<string>>(
                                    t,
                                    t.Item1.OfType<string>()))
                        .Select<Tuple<Tuple<IGrouping<object, object>, IEnumerable<bool>>, IEnumerable<string>>, Tuple<Tuple<Tuple<IGrouping<object, object>, IEnumerable<bool>>, IEnumerable<string>>, IEnumerable<Tuple<bool, string>>>>(
                            (Tuple<Tuple<IGrouping<object, object>, IEnumerable<bool>>, IEnumerable<string>> t) =>
                                new Tuple<Tuple<Tuple<IGrouping<object, object>, IEnumerable<bool>>, IEnumerable<string>>, IEnumerable<Tuple<bool, string>>>(
                                    t,
                                    t.Item1.Item2.Zip<bool, string, Tuple<bool, string>>(
                                        t.Item2,
                                        (bool x, string y) => new Tuple<bool, string>(x, y))))
                        .SelectMany<Tuple<Tuple<Tuple<IGrouping<object, object>, IEnumerable<bool>>, IEnumerable<string>>, IEnumerable<Tuple<bool, string>>>, Tuple<bool, string>, string>(
                            (Tuple<Tuple<Tuple<IGrouping<object, object>, IEnumerable<bool>>, IEnumerable<string>>, IEnumerable<Tuple<bool, string>>> t) =>
                                t.Item2,
                            (Tuple<Tuple<Tuple<IGrouping<object, object>, IEnumerable<bool>>, IEnumerable<string>>, IEnumerable<Tuple<bool, string>>> t, Tuple<bool, string> t0) =>
                                t0.Item1 ? t0.Item2 : ""null"")"),
                StripWhitespace(csharp)
                );
示例#17
0
            public override Expression Normalize(Expression expression)
            {
                //
                // NB: Typical Reaqtor clients talk to some service APIs on a front-end service that does
                //     further analysis of the expression tree (after deserialization) prior to sending it
                //     to one or more query evaluator nodes. For example, a query may get split to run across
                //     multiple nodes. Here, we bind directly to the query engine, so a few more steps are
                //     involved to make this happen:
                //
                //     - The engine stores definitions in "tuple normal form" such that all operators can be
                //       defined as unary. E.g. t => t.Item1.Where(t.Item2) rather than (xs, f) => xs.Where(f).
                //     - The client refers to types in the IReactive*Proxy space with async APIs but the
                //       engine has synchronous symmetric APIs in the IReactive* space. We map types across.
                //

                var normalized = base.Normalize(expression);

                var erased = AnonymousTypeTupletizer.Tupletize(normalized, Expression.Constant(null), excludeVisibleTypes: true);

                return(erased);
            }
示例#18
0
        public void ExpressionMemberAllowListScanner_ManOrBoyTest1()
        {
            var ws = new ExpressionMemberAllowListScanner
            {
                DeclaringTypes =
                {
                    typeof(Queryable),
                    typeof(string),
                    typeof(int),
                    typeof(Tuple <,   >),
                }
            };

            var e = (from x in new[] { 1, 2, 3 }.AsQueryable()
                     where x > 0
                     let y = x * x
                             let z = x.ToString()
                                     select z.ToUpper())
                    .Expression;

            e = AnonymousTypeTupletizer.Tupletize(e, Expression.Constant(value: null));

            Assert.AreSame(e, ws.Visit(e));
        }