コード例 #1
0
        public void ExpressionSlimEntityTypeRecordizer_Constants_Equal()
        {
            var q = new Qux {
                Baz = 42
            };
            var t = new Tuple <Qux, Qux>(q, q);

            var res = Roundtrip(Expression.Constant(t));

            Assert.IsNotNull(res);
            Assert.IsTrue(res.Type.IsGenericType);
            Assert.AreEqual(typeof(Tuple <,>), res.Type.GetGenericTypeDefinition());

            var a1 = res.Type.GetGenericArguments()[0];
            var a2 = res.Type.GetGenericArguments()[1];

            Assert.AreEqual(a1, a2);

            Assert.AreEqual(ExpressionType.Constant, res.NodeType);
            var c = (ConstantExpression)res;
            var o = c.Value;

            var t1 = res.Type.GetProperty("Item1").GetValue(o);
            var t2 = res.Type.GetProperty("Item2").GetValue(o);

            Assert.IsTrue(ObjectComparator.CreateInstance().Equals(t1, t2));

            Assert.AreEqual(42, t1.GetType().GetProperty("baz").GetValue(t1));
        }
コード例 #2
0
        public void ExpressionEntityTypeRecordizer_Simple()
        {
            var netrc = new NoEntityTypesRemainingChecker();

            foreach (var e in new Expression[]
            {
                (Expression <Func <Qux, int> >)(q => q.Baz),
#pragma warning disable IDE0004 // Remove Unnecessary Cast. (Only unnecessary on C# 10 or later.)
                (Expression <Func <Qux> >)(() => new Qux()),
                (Expression <Func <Qux> >)(() => new Qux()
                {
                    Baz = 1
                }),
                (Expression <Func <Qux> >)(() => new Qux(1)
                {
                    Foo = "bar"
                }),
                (Expression <Func <Bar> >)(() => new Bar {
                    Foos = new Foo[] { new Foo {
                                           Qux = new Tuple <Func <List <Qux>, long>, bool>(t => t.Count, false)
                                       } }
                }),
                (Expression <Func <IQueryable <Bar>, IQueryable <long> > >)(xs => from x in xs from y in x.Foos where y.Qux.Item2 select y.Qux.Item1(new List <Qux> {
                    new Qux(1)
                    {
                        Foo = "bar"
                    }
                })),
                (Expression <Func <IQueryable <Bar>, IQueryable <Foo> > >)(xs => from x in xs from y in x.Foos select y),
                (Expression <Func <Qux> >)(() => Activator.CreateInstance <Qux>()),
                (Expression <Action>)(() => FooIt <Qux>()),
                (Expression <Func <Qux[]> >)(() => new Qux[1]),
                (Expression <Func <Qux[]> >)(() => new Qux[] { new Qux(1), new Qux {
                                                                   Baz = 1
                                                               } }),
                (Expression <Func <Tuple <Qux, int> > >)(() => new Tuple <Qux, int>(new Qux(1), 2)),
                (Expression <Func <Holder <Qux> > >)(() => new Holder <Qux> {
                    Value = new Qux(42)
                }),
#pragma warning restore IDE0004 // Remove Unnecessary Cast
            })
            {
                var eta = new ExpressionEntityTypeRecordizer();

                var res = eta.Apply(e);

                netrc.Visit(res);

                var expressionComparer = ObjectComparator.CreateInstance().ExpressionComparator;
                expressionComparer.Equals(e, res);
            }
        }
コード例 #3
0
        public void ExpressionEntityTypeRecordizer_Constants_Simple()
        {
            var recType = RuntimeCompiler.CreateRecordType(new[] {
                new KeyValuePair <string, Type>("a", typeof(int)),
                new KeyValuePair <string, Type>("b", typeof(Qux))
            }, valueEquality: true);

            var rec = Activator.CreateInstance(recType);

            recType.GetProperty("a").SetValue(rec, 42);
            recType.GetProperty("b").SetValue(rec, new Qux(43));

            foreach (var o in new object[]
            {
                new Qux(),
                new Qux(1),
                new Qux(1)
                {
                    Foo = "bar"
                },
                new Qux[1],
                new Qux[] { new Qux(1), new Qux(2) },
                new List <Qux> {
                    new Qux(1), new Qux(2)
                },
                new Func <int, int>(x => x),
                42,
                "bar",
                (Expression <Func <Qux, Qux> >)(x => x),
#pragma warning disable IDE0004 // Remove Unnecessary Cast. (Only unnecessary on C# 10 or later.)
                (Expression <Func <Qux[]> >)(() => new Qux[] { new Qux(123) }),
#pragma warning restore IDE0004 // Remove Unnecessary Cast
                new Tuple <Qux, int>(new Qux(), 42),
                new { a = 1, b = new Qux(42), c = "bar" },
                rec,
            })
            {
                var eta = new ExpressionEntityTypeRecordizer();

                var res = (ConstantExpression)eta.Apply(Expression.Constant(o));

                Assert.IsTrue(ObjectComparator.CreateInstance().Equals(o, res.Value));
            }
        }
コード例 #4
0
        public void ExpressionSlimEntityTypeRecordizer_Constants_Simple()
        {
            var recType = RuntimeCompiler.CreateRecordType(new[] {
                new KeyValuePair <string, Type>("a", typeof(int)),
                new KeyValuePair <string, Type>("b", typeof(Qux))
            }, valueEquality: true);

            var rec = Activator.CreateInstance(recType);

            recType.GetProperty("a").SetValue(rec, 42);
            recType.GetProperty("b").SetValue(rec, new Qux(43));

            foreach (var o in new object[]
            {
                new Qux(),
                new Qux(1),
                new Qux(1)
                {
                    Foo = "bar"
                },
                new Qux[1],
                new Qux[] { new Qux(1), new Qux(2) },
                new List <Qux> {
                    new Qux(1), new Qux(2)
                },
                //new Func<int, int>(x => x),
                42,
                "bar",
                (Expression <Func <Qux, Qux> >)(x => x),
                (Expression <Func <Qux[]> >)(() => new Qux[] { new Qux(123) }),
                new Tuple <Qux, int>(new Qux(), 42),
                new { a = 1, b = new Qux(42), c = "bar" },
                rec,
            })
            {
                var res = (ConstantExpression)Roundtrip(Expression.Constant(o));

                Assert.IsTrue(ObjectComparator.CreateInstance().Equals(o, res.Value));
            }
        }
コード例 #5
0
        public void ExpressionSlimEntityTypeRecordizer_Constants_ManOrBoy1()
        {
            var c = Expression.Constant(new A {
                B = new B {
                    Cs = new C[] { new C {
                                       D = 42, Es = new List <E> {
                                           new E {
                                               F = 42
                                           }
                                       }
                                   } }
                }
            });

            var res = Roundtrip(c);

            Assert.AreNotEqual(res.Type, c.Type);

            var v = ((ConstantExpression)res).Value;

            Assert.IsTrue(ObjectComparator.CreateInstance().Equals(c.Value, v));
        }
コード例 #6
0
 public static ExpressionComparator CreateInstance()
 {
     return(new ExpressionComparator(new TypeComparator(), ObjectComparator.CreateInstance()));
 }
コード例 #7
0
 public RecordTreeComparator(TypeComparator typeComparer)
     : base(typeComparer, ObjectComparator.CreateInstance())
 {
 }