예제 #1
0
        public void MemberInfoSlimVisitor_VisitField_AreSame()
        {
            var visitor = new MemberInfoSlimVisitor();
            var field   = SlimType.GetField("name", fieldType: null);

            Assert.AreSame(field, visitor.Visit(field));
        }
예제 #2
0
 public void PropertyInfoSlim_ArgumentChecks()
 {
     AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetProperty(type: null, "bar", propertyType: null, indexParameterTypes: null, canWrite: false), ex => Assert.AreEqual("type", ex.ParamName));
     AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetProperty(name: null, propertyType: null, indexParameterTypes: null, canWrite: false), ex => Assert.AreEqual("name", ex.ParamName));
     AssertEx.ThrowsException <ArgumentException>(() => SlimType.GetProperty("", propertyType: null, indexParameterTypes: null, canWrite: false), ex => Assert.AreEqual("name", ex.ParamName));
     AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetProperty("foo", propertyType: null, indexParameterTypes: null, canWrite: false), ex => Assert.AreEqual("indexParameterTypes", ex.ParamName));
 }
예제 #3
0
        public void MemberInfoSlimVisitor_VisitConstructor_AreSame()
        {
            var visitor     = new MemberInfoSlimVisitor();
            var constructor = SlimType.GetConstructor(EmptyReadOnlyCollection <TypeSlim> .Instance);

            Assert.AreSame(constructor, visitor.Visit(constructor));
        }
예제 #4
0
        public void MemberInfoSlimVisitor_VisitSimpleMethod_AreSame()
        {
            var visitor = new MemberInfoSlimVisitor();
            var simple  = SlimType.GetSimpleMethod("Foo", EmptyReadOnlyCollection <TypeSlim> .Instance, returnType: null);

            Assert.AreSame(simple, visitor.Visit(simple));
        }
예제 #5
0
        public void MemberInfoSlimVisitor_VisitAndConvert_Fail()
        {
            var visitor = new A();
            var field   = SlimType.GetField("name", fieldType: null);

            Assert.ThrowsException <InvalidOperationException>(() => visitor.VisitAndConvert <FieldInfoSlim>(field));
        }
예제 #6
0
        public void MemberInfoSlimVisitor_VisitProperty_AreSame()
        {
            var visitor  = new MemberInfoSlimVisitor();
            var property = SlimType.GetProperty("Foo", propertyType: null, EmptyReadOnlyCollection <TypeSlim> .Instance, canWrite: true);

            Assert.AreSame(property, visitor.Visit(property));
        }
예제 #7
0
        public void MemberInfoSlimVisitor_VisitGenericMethod_AreNotSame()
        {
            var visitor = new A();
            var genDef  = SlimType.GetGenericDefinitionMethod("Foo", new TypeSlim[] { TypeSlim.GenericParameter("T") }.ToReadOnly(), EmptyReadOnlyCollection <TypeSlim> .Instance, returnType: null);
            var generic = SlimType.GetGenericMethod(genDef, new TypeSlim[] { SlimType }.ToReadOnly());

            Assert.AreNotSame(generic, visitor.Visit(generic));
        }
예제 #8
0
 public void GenericDefinitionMethodInfoSlim_ArgumentChecks()
 {
     AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetGenericDefinitionMethod(type: null, "bar", genericParameterTypes: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("type", ex.ParamName));
     AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetGenericDefinitionMethod(name: null, genericParameterTypes: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("name", ex.ParamName));
     AssertEx.ThrowsException <ArgumentException>(() => SlimType.GetGenericDefinitionMethod("", genericParameterTypes: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("name", ex.ParamName));
     AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetGenericDefinitionMethod("foo", genericParameterTypes: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("parameterTypes", ex.ParamName));
     AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetGenericDefinitionMethod("foo", genericParameterTypes: null, Empty, returnType: null), ex => Assert.AreEqual("genericParameterTypes", ex.ParamName));
     AssertEx.ThrowsException <ArgumentOutOfRangeException>(() => SlimType.GetGenericDefinitionMethod("foo", Empty, Empty, returnType: null), ex => Assert.AreEqual("genericParameterTypes", ex.ParamName));
 }
        public void SimpleMethodInfoSlimBase_ArgumentChecks()
        {
            AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetSimpleMethod(type: null, "bar", parameterTypes: null, returnType: null), ex => Assert.AreEqual("type", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetSimpleMethod(name: null, parameterTypes: null, returnType: null), ex => Assert.AreEqual("name", ex.ParamName));
            AssertEx.ThrowsException <ArgumentException>(() => SlimType.GetSimpleMethod("", parameterTypes: null, returnType: null), ex => Assert.AreEqual("name", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetSimpleMethod("foo", parameterTypes: null, returnType: null), ex => Assert.AreEqual("parameterTypes", ex.ParamName));

            var m = SlimType.GetSimpleMethod("foo", Empty, returnType: null);
        }
예제 #10
0
        public void FieldInfoSlim_ArgumentChecks()
        {
            AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetField(type: null, "bar", fieldType: null), ex => Assert.AreEqual("type", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetField(name: null, fieldType: null), ex => Assert.AreEqual("name", ex.ParamName));
            AssertEx.ThrowsException <ArgumentException>(() => SlimType.GetField("", fieldType: null), ex => Assert.AreEqual("name", ex.ParamName));

            var f = SlimType.GetField("value", fieldType: null);

            Assert.IsNull(f.FieldType);
        }
예제 #11
0
        public void GenericMethodInfoSlim_ArgumentChecks()
        {
            AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetGenericMethod(type: null, methodDefinition: null, arguments: null), ex => Assert.AreEqual("type", ex.ParamName));
            AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetGenericMethod(methodDefinition: null, arguments: null), ex => Assert.AreEqual("methodDefinition", ex.ParamName));

            var def = SlimType.GetGenericDefinitionMethod("Foo", new List <TypeSlim> {
                SlimType
            }.AsReadOnly(), EmptyReadOnlyCollection <TypeSlim> .Instance, returnType: null);

            AssertEx.ThrowsException <ArgumentNullException>(() => def.DeclaringType.GetGenericMethod(def, arguments: null), ex => Assert.AreEqual("arguments", ex.ParamName));
        }
예제 #12
0
        public void MemberInfoSlimVisitor_Generic_GenericMethod()
        {
            var visitor = new MyVisitor();

            var genDef  = SlimType.GetGenericDefinitionMethod("Foo", new TypeSlim[] { TypeSlim.GenericParameter("T") }.ToReadOnly(), EmptyReadOnlyCollection <TypeSlim> .Instance, returnType: null);
            var generic = SlimType.GetGenericMethod(genDef, new TypeSlim[] { SlimType }.ToReadOnly());

            var res = visitor.Visit(generic);

            Assert.AreEqual(42, res);
        }
        public void TypeSlimEqualityComparator_Pooling()
        {
            // NB: This relies on some implementation details of collection APIs in .NET. The TypeSlimEqualityComparator
            //     used a HashSet<T> and a Dictionary<K, V> which both have a version field we can use to assert progress
            //     in a white-box manner.

            var fields = (from f in typeof(TypeSlimEqualityComparator).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                          where typeof(ICollection).IsAssignableFrom(f.FieldType)
                          let v = f.FieldType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Single(i => i.FieldType == typeof(int) && i.Name.Contains("version"))
                                  select(Collection: f, Version: v))
                         .ToArray();

            Assert.IsTrue(fields.Length > 0);

            var p1 = SlimType.GetProperty("Foo", SlimType, EmptyReadOnlyCollection <TypeSlim> .Instance, canWrite: true);
            var s1 = TypeSlim.Structural(new List <PropertyInfoSlim> {
                p1
            }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record);
            var s2 = TypeSlim.Structural(new List <PropertyInfoSlim> {
                p1
            }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record);

            var pool = new ObjectPool <TypeSlimEqualityComparator>(() => new TypeSlimEqualityComparator(), 1);

            var obj = default(TypeSlimEqualityComparator);

            int[] v1, v2, v3;

            using (var eq = pool.New())
            {
                obj = eq.Object;

                v1 = GetVersions(obj);

                Assert.IsTrue(obj.Equals(s1, s2));

                v2 = GetVersions(obj); // NB: Equals for structural types will touch the tracking data structures.
            }

            v3 = GetVersions(obj); // NB: The Clear method should clear all tracking data structures.

            AssertVersionsChanged(v1, v2);
            AssertVersionsChanged(v2, v3);

            int[] GetVersions(TypeSlimEqualityComparator eq)
            {
                return((from f in fields
                        let collection = f.Collection.GetValue(eq)
                                         let version = (int)f.Version.GetValue(collection)
                                                       select version).ToArray());
            }
예제 #14
0
        public void ExpressionSlim_Update_Tests()
        {
            var boolSlim = new TypeToTypeSlimConverter().Visit(typeof(bool));
            var intExpr1 = ExpressionSlim.Parameter(SlimType);
            var intExpr2 = ExpressionSlim.Parameter(SlimType);
            var intExpr3 = ExpressionSlim.Parameter(SlimType);
            var boolExpr1 = ExpressionSlim.Parameter(boolSlim);
            var boolExpr2 = ExpressionSlim.Parameter(boolSlim);
            var prop = SlimType.GetProperty("Foo", propertyType: null, EmptyReadOnlyCollection <TypeSlim> .Instance, canWrite: true);
            var elementInits = new ElementInitSlim[] { new ElementInitSlim(addMethod: null, arguments: null) }.ToReadOnly();
            var method = SlimType.GetSimpleMethod("Foo", EmptyReadOnlyCollection <TypeSlim> .Instance, returnType: null);

            var b = ExpressionSlim.Add(intExpr1, intExpr2);

            Assert.AreNotSame(b, b.Update(intExpr1, conversion: null, intExpr3));
            Assert.AreNotSame(b, b.Update(intExpr3, conversion: null, intExpr2));

            var c = ExpressionSlim.Condition(boolExpr1, intExpr1, intExpr2);

            Assert.AreNotSame(c, c.Update(boolExpr2, intExpr1, intExpr2));
            Assert.AreNotSame(c, c.Update(boolExpr1, intExpr2, intExpr3));
            Assert.AreNotSame(c, c.Update(boolExpr1, intExpr1, intExpr3));

            var i = ExpressionSlim.MakeIndex(intExpr1, prop, new ExpressionSlim[] { intExpr2 }.ToReadOnly());

            Assert.AreNotSame(i, i.Update(intExpr2, new ExpressionSlim[] { intExpr2 }.ToReadOnly()));
            Assert.AreNotSame(i, i.Update(intExpr1, new ExpressionSlim[] { intExpr3 }.ToReadOnly()));

            var inv = ExpressionSlim.Invoke(intExpr1, new[] { intExpr2 });

            Assert.AreNotSame(inv, inv.Update(intExpr2, new ExpressionSlim[] { intExpr2 }.ToReadOnly()));
            Assert.AreNotSame(inv, inv.Update(intExpr1, new ExpressionSlim[] { intExpr3 }.ToReadOnly()));

            var l = ExpressionSlim.Lambda(intExpr1);

            Assert.AreNotSame(l, l.Update(intExpr2, EmptyReadOnlyCollection <ParameterExpressionSlim> .Instance));
            Assert.AreNotSame(l, l.Update(intExpr1, new ParameterExpressionSlim[] { ExpressionSlim.Parameter(SlimType) }.ToReadOnly()));

            var li = ExpressionSlim.ListInit(ExpressionSlim.New(SlimType));

            Assert.AreNotSame(li, li.Update(ExpressionSlim.New(boolSlim), elementInits));
            Assert.AreNotSame(li, li.Update(ExpressionSlim.New(SlimType), elementInits));

            var ma = ExpressionSlim.Bind(prop, intExpr1);

            Assert.AreNotSame(ma, ma.Update(intExpr2));

            var macc = ExpressionSlim.MakeMemberAccess(instance: null, prop);

            Assert.AreNotSame(macc, macc.Update(intExpr1));

            var ml = ExpressionSlim.ListBind(prop);

            Assert.AreNotSame(ml, ml.Update(elementInits));

            var mm = ExpressionSlim.MemberBind(prop);

            Assert.AreNotSame(mm, mm.Update(new MemberBindingSlim[] { ExpressionSlim.Bind(prop, intExpr1) }.ToReadOnly()));

            var m = ExpressionSlim.Call(method);

            Assert.AreNotSame(m, m.Update(intExpr1, new ExpressionSlim[] { intExpr2 }.ToReadOnly()));
            Assert.AreNotSame(m, m.Update(@object: null, new ExpressionSlim[] { intExpr2 }.ToReadOnly()));

            var na = ExpressionSlim.NewArrayInit(SlimType);

            Assert.AreNotSame(na, na.Update(new ExpressionSlim[] { intExpr1 }.ToReadOnly()));

            var nb = ExpressionSlim.NewArrayBounds(SlimType, intExpr1);

            Assert.AreNotSame(nb, nb.Update(new ExpressionSlim[] { intExpr2 }.ToReadOnly()));

            var ctor = SlimType.GetConstructor(new TypeSlim[] { SlimType }.ToReadOnly());
            var n    = ExpressionSlim.New(ctor, ExpressionSlim.Parameter(SlimType));

            Assert.AreNotSame(n, n.Update(new ExpressionSlim[] { ExpressionSlim.Default(SlimType) }.ToReadOnly()));

            var t = ExpressionSlim.TypeIs(intExpr1, SlimType);

            Assert.AreNotSame(t, t.Update(intExpr2));

            var u = ExpressionSlim.Not(boolExpr1);

            Assert.AreNotSame(u, u.Update(boolExpr2));
        }
예제 #15
0
 protected override MemberInfoSlim VisitGenericDefinitionMethod(GenericDefinitionMethodInfoSlim method)
 {
     return(SlimType.GetGenericDefinitionMethod("Foo", new TypeSlim[] { TypeSlim.GenericParameter("T") }.ToReadOnly(), EmptyReadOnlyCollection <TypeSlim> .Instance, returnType: null));
 }
예제 #16
0
 protected override MemberInfoSlim VisitField(FieldInfoSlim field)
 {
     return(SlimType.GetProperty("Foo", propertyType: null, EmptyReadOnlyCollection <TypeSlim> .Instance, canWrite: true));
 }
예제 #17
0
 public void ConstructorInfoSlim_ArgumentChecks()
 {
     AssertEx.ThrowsException <ArgumentNullException>(() => TypeSlimExtensions.GetConstructor(type: null, Empty), ex => Assert.AreEqual("type", ex.ParamName));
     AssertEx.ThrowsException <ArgumentNullException>(() => SlimType.GetConstructor(parameterTypes: null), ex => Assert.AreEqual("parameterTypes", ex.ParamName));
 }
        public void TypeSlimEqualityComparer_EqualsStructural()
        {
            var p1 = SlimType.GetProperty("Foo", SlimType, EmptyReadOnlyCollection <TypeSlim> .Instance, canWrite: true);
            var p2 = SlimType.GetProperty("Foo", SlimType, EmptyReadOnlyCollection <TypeSlim> .Instance, canWrite: false);
            var p3 = SlimType.GetProperty("Foo", SlimType, new TypeSlim[] { SlimType }.ToReadOnly(), canWrite: true);
            var p4 = SlimType.GetProperty("Bar", SlimType, EmptyReadOnlyCollection <TypeSlim> .Instance, canWrite: true);
            var p5 = SlimType.GetProperty("Foo", propertyType: null, EmptyReadOnlyCollection <TypeSlim> .Instance, canWrite: true);
            var s1 = TypeSlim.Structural(new List <PropertyInfoSlim> {
                p1
            }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record);
            var s2 = TypeSlim.Structural(new List <PropertyInfoSlim> {
                p1
            }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record);
            var empty = EmptyReadOnlyCollection <PropertyInfoSlim> .Instance;

            var equal = new[]
            {
                new KeyValuePair <TypeSlim, TypeSlim>(s1, s2),
            };

            var inequal = new[]
            {
                new KeyValuePair <TypeSlim, TypeSlim>(
                    TypeSlim.Structural(new List <PropertyInfoSlim> {
                    p1
                }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record),
                    TypeSlim.Structural(new List <PropertyInfoSlim> {
                    p2
                }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record)
                    ),
                new KeyValuePair <TypeSlim, TypeSlim>(
                    TypeSlim.Structural(new List <PropertyInfoSlim> {
                    p1
                }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record),
                    TypeSlim.Structural(new List <PropertyInfoSlim> {
                    p3
                }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record)
                    ),
                new KeyValuePair <TypeSlim, TypeSlim>(
                    TypeSlim.Structural(new List <PropertyInfoSlim> {
                    p1
                }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record),
                    TypeSlim.Structural(new List <PropertyInfoSlim> {
                    p4
                }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record)
                    ),
                new KeyValuePair <TypeSlim, TypeSlim>(
                    TypeSlim.Structural(new List <PropertyInfoSlim> {
                    p1
                }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record),
                    TypeSlim.Structural(new List <PropertyInfoSlim> {
                    p5
                }.AsReadOnly(), hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record)
                    ),
                new KeyValuePair <TypeSlim, TypeSlim>(
                    TypeSlim.Structural(empty, hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record),
                    TypeSlim.Structural(empty, hasValueEqualitySemantics: false, StructuralTypeSlimKind.Record)
                    ),
                new KeyValuePair <TypeSlim, TypeSlim>(
                    TypeSlim.Structural(empty, hasValueEqualitySemantics: true, StructuralTypeSlimKind.Record),
                    TypeSlim.Structural(empty, hasValueEqualitySemantics: true, StructuralTypeSlimKind.Anonymous)
                    ),
            };

            AssertAllEqual(equal);
            AssertAllNotEqual(inequal);
        }