Exemple #1
0
        public void TypeSpace_Resolve()
        {
            var ts       = new TypeSpace();
            var longSlim = ts.ConvertType(typeof(long));

            ts.MapType(typeof(int), longSlim);
            Assert.AreEqual(ts.ConvertType(typeof(int)), longSlim);
        }
Exemple #2
0
        public void TypeSpace_ArgumentChecks()
        {
            var ts = new TypeSpace();

            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetMember(member: null), ex => Assert.AreEqual(ex.ParamName, "member"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetConstructor(constructor: null), ex => Assert.AreEqual(ex.ParamName, "constructor"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetField(field: null), ex => Assert.AreEqual(ex.ParamName, "field"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetMethod(method: null), ex => Assert.AreEqual(ex.ParamName, "method"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.GetProperty(property: null), ex => Assert.AreEqual(ex.ParamName, "property"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.ConvertType(type: null), ex => Assert.AreEqual(ex.ParamName, "type"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.MapType(type: null, typeSlim: null), ex => Assert.AreEqual(ex.ParamName, "type"));
            AssertEx.ThrowsException <ArgumentNullException>(() => ts.MapType(typeof(int), typeSlim: null), ex => Assert.AreEqual(ex.ParamName, "typeSlim"));
        }
        public void InvertedTypeSpace_MapType_Success()
        {
            var ts      = new TypeSpace();
            var its     = new InvertedTypeSpace();
            var fooSlim = ts.ConvertType(typeof(Foo));

            its.MapType(fooSlim, typeof(Qux));

            var fooCtor = typeof(Foo).GetConstructors().Single();
            var quxCtor = typeof(Qux).GetConstructors().Single();

            Assert.AreEqual(quxCtor, MemberInfoRoundtrip(fooCtor, ts, its));

            var fooField = typeof(Foo).GetField("baz");
            var quxField = typeof(Qux).GetField("baz");

            Assert.AreEqual(quxField, MemberInfoRoundtrip(fooField, ts, its));

            var fooProp = typeof(Foo).GetProperty("Bar");
            var quxProp = typeof(Qux).GetProperty("Bar");

            Assert.AreEqual(quxProp, MemberInfoRoundtrip(fooProp, ts, its));

            var fooIdxProp = typeof(Foo).GetProperty("Item");
            var quxIdxProp = typeof(Qux).GetProperty("Item");

            Assert.AreEqual(quxIdxProp, MemberInfoRoundtrip(fooIdxProp, ts, its));

            var fooSimple = typeof(Foo).GetMethod("Qux1");
            var quxSimple = typeof(Qux).GetMethod("Qux1");

            Assert.AreEqual(quxSimple, MemberInfoRoundtrip(fooSimple, ts, its));

            var fooGenericDef = typeof(Foo).GetMethod("Qux2");
            var quxGenericDef = typeof(Qux).GetMethod("Qux2");

            Assert.AreEqual(quxGenericDef, MemberInfoRoundtrip(fooGenericDef, ts, its));

            var fooGeneric = fooGenericDef.MakeGenericMethod(new[] { typeof(int) });
            var quxGeneric = quxGenericDef.MakeGenericMethod(new[] { typeof(int) });

            Assert.AreEqual(quxGeneric, MemberInfoRoundtrip(fooGeneric, ts, its));
        }
        public void InvertedTypeSpace_MemberRoundtrip_BadMapping_ThrowsInvalidOperation()
        {
            var ts      = new TypeSpace();
            var its     = new InvertedTypeSpace();
            var fooSlim = ts.ConvertType(typeof(Foo));

            its.MapType(fooSlim, typeof(Bar));

            var ctor = typeof(Foo).GetConstructors().Single();

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(ctor, ts, its));

            var field = typeof(Foo).GetField("baz");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(field, ts, its));

            var prop = typeof(Foo).GetProperty("Bar");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(prop, ts, its));

            var idxProp = typeof(Foo).GetProperty("Item");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(idxProp, ts, its));

            var simple = typeof(Foo).GetMethod("Qux1");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(simple, ts, its));

            var genericDef = typeof(Foo).GetMethod("Qux2");

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(genericDef, ts, its));

            var generic = genericDef.MakeGenericMethod(new[] { typeof(int) });

            Assert.ThrowsException <InvalidOperationException>(() => MemberInfoRoundtrip(generic, ts, its));
        }
Exemple #5
0
 protected static Type TypeRoundtrip(Type t, TypeSpace ts, InvertedTypeSpace its)
 {
     return(its.ConvertType(ts.ConvertType(t)));
 }