public void TypeToTypeSlimConverter_Resolve_ThrowsInvalidOperation()
        {
            var visitor      = new TypeToTypeSlimConverter();
            var intTypeSlim  = visitor.Visit(typeof(int));
            var longTypeSlim = visitor.Visit(typeof(long));

            Assert.ThrowsException <InvalidOperationException>(() => visitor.MapType(typeof(int), longTypeSlim));
        }
        public void TypeToTypeSlimConverter_Resolve_Success()
        {
            var visitor      = new TypeToTypeSlimConverter();
            var longTypeSlim = visitor.Visit(typeof(long));

            visitor.MapType(typeof(int), longTypeSlim);
            var intTypeSlim = visitor.Visit(typeof(int));

            Assert.AreEqual(intTypeSlim, longTypeSlim);
            visitor.MapType(typeof(int), longTypeSlim);
        }
        public void TypeToTypeSlimConverter_ThrowsNotSupported()
        {
            var refType = typeof(Foo).GetConstructors().Single().GetParameters()[0].ParameterType;

            Assert.IsTrue(refType.IsByRef);

            var pointerType = typeof(int *);

            Assert.IsTrue(pointerType.IsPointer);

            var converter = new TypeToTypeSlimConverter();

            Assert.ThrowsException <NotSupportedException>(() => converter.Visit(refType));
            Assert.ThrowsException <NotSupportedException>(() => converter.Visit(pointerType));
        }
Ejemplo n.º 4
0
        public void UnifyStructural_DifferentPropertyTypes_Pass()
        {
            var unifier    = new TypeUnifier();
            var typeToSlim = new TypeToTypeSlimConverter();

            var rtc         = new RuntimeCompiler();
            var typeBuilder = rtc.GetNewRecordTypeBuilder();

            rtc.DefineRecordType(
                typeBuilder,
                (new Dictionary <string, Type> {
                { "foo", typeof(int) }
            }).AsEnumerable(),
                true
                );

            var structuralType     = typeBuilder.CreateType();
            var slimStructuralType = typeToSlim.Visit(structuralType);

            Assert.IsTrue(unifier.Unify(structuralType, slimStructuralType));

            Assert.AreEqual(1, unifier.Entries.Count);

            Assert.IsTrue(unifier.Entries.ContainsKey(slimStructuralType));
            Assert.AreEqual(unifier.Entries[slimStructuralType], structuralType);

            Assert.IsTrue(slimStructuralType is StructuralTypeSlim);
        }
Ejemplo n.º 5
0
        public void UnifyStructuralEquivalence_Pass()
        {
            var rt1 = RuntimeCompiler.CreateRecordType(
                new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(string) }
            },
                true);

            var rt2 = RuntimeCompiler.CreateRecordType(
                new Dictionary <string, Type>
            {
                { "Foo", typeof(int) },
                { "Bar", typeof(string) }
            },
                true);

            var typeToSlim = new TypeToTypeSlimConverter();
            var slim       = typeToSlim.Visit(rt1);
            var unifier    = new TypeUnifier();

            Assert.IsTrue(unifier.Unify(rt1, slim));
            Assert.IsTrue(unifier.Unify(rt2, slim));
            Assert.AreEqual(1, unifier.Entries.Count);
            Assert.AreSame(rt1, unifier.Entries[slim]);
        }
Ejemplo n.º 6
0
        public void UnifyStructural_MissingProperty_Fail()
        {
            var unifier    = new TypeUnifier();
            var typeToSlim = new TypeToTypeSlimConverter();

            var rtc          = new RuntimeCompiler();
            var typeBuilder1 = rtc.GetNewRecordTypeBuilder();
            var typeBuilder2 = rtc.GetNewRecordTypeBuilder();

            rtc.DefineRecordType(
                typeBuilder1,
                (new Dictionary <string, Type> {
                { "foo", typeof(int) }
            }).AsEnumerable(),
                true
                );

            rtc.DefineRecordType(
                typeBuilder2,
                (new Dictionary <string, Type> {
                { "bar", typeof(double) }
            }).AsEnumerable(),
                true
                );

            var structuralType1    = typeBuilder1.CreateType();
            var structuralType2    = typeBuilder2.CreateType();
            var slimStructuralType = typeToSlim.Visit(structuralType2);

            unifier.Unify(structuralType1, slimStructuralType);
        }
Ejemplo n.º 7
0
        public void UnifyArray_WithNonArrayClrType_Fail()
        {
            var unifier      = new TypeUnifier();
            var typeToSlim   = new TypeToTypeSlimConverter();
            var intArraySlim = typeToSlim.Visit(typeof(int[]));

            unifier.Unify(typeof(int), intArraySlim);
        }
Ejemplo n.º 8
0
        public void UnifyArray_DifferentRankMultidimensionalSlim_Fail()
        {
            var unifier      = new TypeUnifier();
            var typeToSlim   = new TypeToTypeSlimConverter();
            var intArraySlim = typeToSlim.Visit(typeof(int[, ]));

            unifier.Unify(typeof(int[]), intArraySlim);
        }
Ejemplo n.º 9
0
        public void TryUnifyArray_DifferentElementType_Fail()
        {
            var unifier         = new TypeUnifier(safe: true);
            var typeToSlim      = new TypeToTypeSlimConverter();
            var doubleArraySlim = typeToSlim.Visit(typeof(double[]));

            Assert.IsFalse(unifier.Unify(typeof(int[]), doubleArraySlim));
        }
Ejemplo n.º 10
0
        public void UnifyArray_DifferentElementType_Fail()
        {
            var unifier         = new TypeUnifier();
            var typeToSlim      = new TypeToTypeSlimConverter();
            var doubleArraySlim = typeToSlim.Visit(typeof(double[]));

            unifier.Unify(typeof(int[]), doubleArraySlim);
        }
Ejemplo n.º 11
0
        public void UnifySimple_DifferentTypeName_Fail()
        {
            var unifier    = new TypeUnifier();
            var typeToSlim = new TypeToTypeSlimConverter();
            var doubleSlim = typeToSlim.Visit(typeof(double));

            unifier.Unify(typeof(int), doubleSlim);
        }
Ejemplo n.º 12
0
        public void TypeToTypeSlimConverter_GenericParameter()
        {
            var genericParameterType = typeof(List <>).GetGenericArguments()[0];

            var visitor = new TypeToTypeSlimConverter();

            Assert.ThrowsException <InvalidOperationException>(() => visitor.Visit(genericParameterType));
        }
Ejemplo n.º 13
0
        public void TypeToTypeSlimConverter_ArgumentChecks()
        {
            var converter = new TypeToTypeSlimConverter();

            AssertEx.ThrowsException <ArgumentNullException>(() => converter.Visit(type: null), ex => Assert.AreEqual(ex.ParamName, "type"));
            AssertEx.ThrowsException <ArgumentNullException>(() => converter.MapType(type: null, typeSlim: null), ex => Assert.AreEqual(ex.ParamName, "type"));
            AssertEx.ThrowsException <ArgumentNullException>(() => converter.MapType(typeof(int), typeSlim: null), ex => Assert.AreEqual(ex.ParamName, "typeSlim"));
        }
Ejemplo n.º 14
0
        public void TryUnify_AlreadyMapped()
        {
            var unifier    = new TypeUnifier(safe: true);
            var typeToSlim = new TypeToTypeSlimConverter();
            var intSlim    = typeToSlim.Visit(typeof(int));

            unifier.Unify(typeof(int), intSlim);
            Assert.IsFalse(unifier.Unify(typeof(double), intSlim));
        }
Ejemplo n.º 15
0
        public void Unify_Wildcard_Fail()
        {
            var unifier    = new TypeUnifier(safe: true);
            var typeToSlim = new TypeToTypeSlimConverter();
            var slim       = typeToSlim.Visit(typeof(Func <int, bool>));

            Assert.IsFalse(unifier.Unify(typeof(Func <T, T>), slim));
            Assert.AreEqual(0, unifier.Entries.Count);
        }
Ejemplo n.º 16
0
        public void UnifyArray_Vector_Pass()
        {
            var unifier      = new TypeUnifier();
            var typeToSlim   = new TypeToTypeSlimConverter();
            var intArraySlim = typeToSlim.Visit(typeof(int[]));

            Assert.IsTrue(unifier.Unify(typeof(int[]), intArraySlim));
            Assert.AreEqual(0, unifier.Entries.Count);
        }
Ejemplo n.º 17
0
        public void Unify_AlreadyMapped()
        {
            var unifier    = new TypeUnifier();
            var typeToSlim = new TypeToTypeSlimConverter();
            var intSlim    = typeToSlim.Visit(typeof(int));

            unifier.Unify(typeof(int), intSlim);
            unifier.Unify(typeof(double), intSlim);
        }
Ejemplo n.º 18
0
        public void UnifyGeneric_DifferentArguments_Fail()
        {
            var unifier     = new TypeUnifier();
            var typeToSlim  = new TypeToTypeSlimConverter();
            var generic     = typeof(List <int>);
            var slimGeneric = typeToSlim.Visit(generic);

            unifier.Unify(typeof(List <double>), slimGeneric);
        }
Ejemplo n.º 19
0
        public void UnifyGeneric_DifferentDefinition_Fail()
        {
            var unifier     = new TypeUnifier();
            var typeToSlim  = new TypeToTypeSlimConverter();
            var generic     = typeof(List <int>);
            var slimGeneric = typeToSlim.Visit(generic);

            unifier.Unify(typeof(Nullable <int>), slimGeneric);
        }
Ejemplo n.º 20
0
        public void TryUnifyGeneric_DifferentDefinition_Fail()
        {
            var unifier     = new TypeUnifier(safe: true);
            var typeToSlim  = new TypeToTypeSlimConverter();
            var generic     = typeof(List <int>);
            var slimGeneric = typeToSlim.Visit(generic);

            Assert.IsFalse(unifier.Unify(typeof(Nullable <int>), slimGeneric));
        }
Ejemplo n.º 21
0
        public void UnifyGeneric_WithGenericDefClrType_Fail()
        {
            var unifier     = new TypeUnifier();
            var typeToSlim  = new TypeToTypeSlimConverter();
            var generic     = typeof(List <int>);
            var slimGeneric = typeToSlim.Visit(generic);

            unifier.Unify(typeof(List <>), slimGeneric);
        }
Ejemplo n.º 22
0
        public void UnifyGenericDefinition_WithNonGenericDefClrType_Fail()
        {
            var unifier        = new TypeUnifier();
            var typeToSlim     = new TypeToTypeSlimConverter();
            var definition     = typeof(List <>);
            var slimDefinition = typeToSlim.Visit(definition);

            unifier.Unify(typeof(int), slimDefinition);
        }
Ejemplo n.º 23
0
        public void TryUnify_AlreadyMapped_Pass()
        {
            var unifier    = new TypeUnifier();
            var typeToSlim = new TypeToTypeSlimConverter();
            var intSlim    = typeToSlim.Visit(typeof(int));

            unifier.Unify(typeof(int), intSlim);
            Assert.IsTrue(unifier.Unify(typeof(int), intSlim));
        }
Ejemplo n.º 24
0
        public void UnifyGeneric_Pass()
        {
            var unifier     = new TypeUnifier();
            var typeToSlim  = new TypeToTypeSlimConverter();
            var generic     = typeof(List <int>);
            var slimGeneric = typeToSlim.Visit(generic);

            Assert.IsTrue(unifier.Unify(generic, slimGeneric));
            Assert.AreEqual(0, unifier.Entries.Count);
        }
Ejemplo n.º 25
0
        public void UnifyGenericDefinition_Pass()
        {
            var unifier        = new TypeUnifier();
            var typeToSlim     = new TypeToTypeSlimConverter();
            var definition     = typeof(List <>);
            var slimDefinition = typeToSlim.Visit(definition);

            Assert.IsTrue(unifier.Unify(definition, slimDefinition));
            Assert.AreEqual(0, unifier.Entries.Count);
        }
Ejemplo n.º 26
0
        public void UnifyManOrBoy_Pass()
        {
            var record1 = RuntimeCompiler.CreateRecordType(
                new Dictionary <string, Type> {
                { "foo", typeof(int) }
            },
                true);
            var record2 = RuntimeCompiler.CreateRecordType(
                new Dictionary <string, Type> {
                { "bar", record1 },
                { "qux", typeof(string) }
            },
                true);
            var list       = typeof(List <>).MakeGenericType(new[] { record2 });
            var unifier    = new TypeUnifier();
            var typeToSlim = new TypeToTypeSlimConverter();
            var slimList   = typeToSlim.Visit(list);

            Assert.IsTrue(unifier.Unify(list, slimList));
            Assert.AreEqual(2, unifier.Entries.Count);
        }