예제 #1
0
        public void CanBeConstructedFromOpenGenericClrTypes()
        {
            using (TypeVariable.TestFactory())
            {
                var openEnumerable = new NamedType(typeof(IEnumerable <>));

                openEnumerable.ShouldEqual(
                    "System.Collections.Generic.IEnumerable",
                    "System.Collections.Generic.IEnumerable<0>",
                    new TypeVariable(0));
            }
        }
예제 #2
0
        public void CanFreshenGenericTypeVariables()
        {
            using (TypeVariable.TestFactory())
            {
                //Prevent type '1' from being freshened by marking it as non-generic:
                var typeVariable0 = TypeVariable.CreateGeneric();
                var typeVariable1 = TypeVariable.CreateNonGeneric();

                var expectedTypeAfterLookup = new NamedType("A", new TypeVariable(2), typeVariable1, new NamedType("B", new TypeVariable(2), typeVariable1));
                var definedType             = new NamedType("A", typeVariable0, typeVariable1, new NamedType("B", typeVariable0, typeVariable1));

                definedType.FreshenGenericTypeVariables().ShouldEqual(expectedTypeAfterLookup);
            }
        }
예제 #3
0
        public void HasFactoryThatCanBeTemporarilyReplacedForTestingPursposes()
        {
            using (TypeVariable.TestFactory())
            {
                var x = TypeVariable.CreateGeneric();
                var y = TypeVariable.CreateGeneric();

                x.ShouldEqual(new TypeVariable(0));
                y.ShouldEqual(new TypeVariable(1));
                TypeVariable.CreateGeneric().ShouldEqual(new TypeVariable(2));
                TypeVariable.CreateNonGeneric().ShouldEqual(new TypeVariable(3, false));
                TypeVariable.CreateGeneric().ShouldEqual(new TypeVariable(4));
            }
        }
예제 #4
0
        public void UsesFreshTypeVariablesUponEachConstructionFromAnOpenGenericClrType()
        {
            using (TypeVariable.TestFactory())
            {
                var enumerableT = new NamedType(typeof(IEnumerable <>));
                var enumerableS = new NamedType(typeof(IEnumerable <>));

                var T = enumerableT.GenericArguments.Single();
                var S = enumerableS.GenericArguments.Single();

                enumerableT.ShouldNotEqual(enumerableS);
                T.ShouldNotEqual(S);

                T.ShouldEqual(new TypeVariable(0));
                S.ShouldEqual(new TypeVariable(1));
            }
        }
예제 #5
0
        public void CanBeConstructedFromSpecializingAGenericTypeDefinition()
        {
            using (TypeVariable.TestFactory())
            {
                Action nonGenericOrigin = () => NamedType.Integer.MakeGenericType();
                nonGenericOrigin.ShouldThrow <InvalidOperationException>("int is not a generic type definition, so it cannot be used to make generic types.");

                Action closedGenericOrigin = () => new NamedType(typeof(IEnumerable <int>)).MakeGenericType();
                closedGenericOrigin.ShouldThrow <InvalidOperationException>("System.Collections.Generic.IEnumerable<int> is not a generic type definition, so it cannot be used to make generic types.");

                Action invalidTypeArgumentCount = () => new NamedType(typeof(IEnumerable <>)).MakeGenericType(NamedType.Integer, NamedType.Boolean);
                invalidTypeArgumentCount.ShouldThrow <ArgumentException>("Invalid number of generic type arguments.");

                new NamedType(typeof(IEnumerable <>)).MakeGenericType(NamedType.Integer)
                .ShouldEqual(new NamedType(typeof(IEnumerable <int>)));

                new NamedType(typeof(IDictionary <,>)).MakeGenericType(NamedType.String, NamedType.Integer)
                .ShouldEqual(new NamedType(typeof(IDictionary <string, int>)));
            }
        }