예제 #1
0
        private static void ValidateSlots(ITypeInfo type, IEnumerable <KeyValuePair <ISlotInfo, object> > slotValues)
        {
            foreach (KeyValuePair <ISlotInfo, object> slotValue in slotValues)
            {
                ISlotInfo slot = slotValue.Key;
                switch (slot.Kind)
                {
                case CodeElementKind.GenericParameter:
                case CodeElementKind.Field:
                case CodeElementKind.Property:
                    var member = (IMemberInfo)slot;
                    if (type.Equals(member.DeclaringType) || type.IsSubclassOf(member.DeclaringType))
                    {
                        continue;
                    }
                    break;

                case CodeElementKind.Parameter:
                    var parameter = (IParameterInfo)slot;
                    if (type.Equals(parameter.Member.DeclaringType))
                    {
                        continue;
                    }
                    break;
                }

                throw new ArgumentException(String.Format("Slot '{0}' is not valid for creating objects of type '{1}'.", slot, type), "slotValues");
            }
        }
        public static bool IsAssignableFrom(ITypeInfo self, ITypeInfo other)
        {
            if (self == null || other == null)
            {
                return(false);
            }

            if (self.Equals(other))
            {
                return(true);
            }

            if (self.IsInterface)
            {
                return(other.GetInterfaces().Any(self.Equals));
            }

            var baseType = other.BaseType;

            while (baseType != null)
            {
                if (self.Equals(baseType))
                {
                    return(true);
                }
                baseType = baseType.BaseType;
            }

            return(false);
        }
 private static TagHelperAttributeDescriptor ToAttributeDescriptor(
     IPropertyInfo property,
     string attributeName,
     bool designTime)
 {
     return(ToAttributeDescriptor(
                property,
                attributeName,
                property.PropertyType.FullName,
                isIndexer: false,
                isStringProperty: StringTypeInfo.Equals(property.PropertyType),
                designTime: designTime));
 }
 protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator)
 {
     if (type.Equals(Type))
     {
         return(typeScriptType);
     }
     throw new ArgumentException($"Expected type {Type}, but got {type}");
 }
        /// <summary>
        /// Applies semantic actions to a test to estalish its runtime behavior.
        /// </summary>
        /// <remarks>
        /// <para>
        /// This method is called after <see cref="InitializeTest" />.
        /// </para>
        /// <para>
        /// The default behavior for a <see cref="TestTypePatternAttribute" />
        /// is to configure the test actions as follows:
        /// <list type="bullet">
        /// <item><see cref="PatternTestInstanceActions.BeforeTestInstanceChain" />: Set the
        /// fixture instance name and <see cref="PatternTestInstanceState.FixtureType" />.</item>
        /// <item><see cref="PatternTestInstanceActions.InitializeTestInstanceChain" />: Create
        /// the fixture instance and set the <see cref="PatternTestInstanceState.FixtureInstance" />
        /// property accordingly.</item>
        /// <item><see cref="PatternTestInstanceActions.DisposeTestInstanceChain" />: If the fixture type
        /// implements <see cref="IDisposable" />, disposes the fixture instance.</item>
        /// <item><see cref="PatternTestInstanceActions.DecorateChildTestChain" />: Decorates the child's
        /// <see cref="PatternTestInstanceActions.BeforeTestInstanceChain" /> to set its <see cref="PatternTestInstanceState.FixtureInstance" />
        /// and <see cref="PatternTestInstanceState.FixtureType" /> properties to those
        /// of the fixture.  The child test may override these values later on but this
        /// is a reasonable default setting for test methods within a fixture.</item>
        /// </list>
        /// </para>
        /// <para>
        /// You can override this method to change the semantics as required.
        /// </para>
        /// </remarks>
        /// <param name="testBuilder">The test builder.</param>
        /// <param name="type">The test type.</param>
        protected virtual void SetTestSemantics(ITestBuilder testBuilder, ITypeInfo type)
        {
            testBuilder.TestInstanceActions.BeforeTestInstanceChain.After(
                delegate(PatternTestInstanceState testInstanceState)
            {
                ObjectCreationSpec spec = testInstanceState.GetFixtureObjectCreationSpec(type);
                testInstanceState.Data.SetValue(FixtureObjectCreationSpecKey, spec);

                testInstanceState.FixtureType = spec.ResolvedType;

                if (!testInstanceState.IsReusingPrimaryTestStep)
                {
                    testInstanceState.NameBase = spec.Format(testInstanceState.NameBase, testInstanceState.Formatter);
                }
            });

            testBuilder.TestInstanceActions.InitializeTestInstanceChain.After(
                delegate(PatternTestInstanceState testInstanceState)
            {
                if (!type.IsAbstract && !type.IsInterface)
                {
                    ObjectCreationSpec spec = testInstanceState.Data.GetValue(FixtureObjectCreationSpecKey);

                    testInstanceState.FixtureInstance = spec.CreateInstance();
                }
            });

            testBuilder.TestInstanceActions.DisposeTestInstanceChain.After(
                delegate(PatternTestInstanceState testInstanceState)
            {
                IDisposable dispose = testInstanceState.FixtureInstance as IDisposable;
                if (dispose != null)
                {
                    dispose.Dispose();
                }
            });

            testBuilder.TestInstanceActions.DecorateChildTestChain.After(
                delegate(PatternTestInstanceState testInstanceState, PatternTestActions decoratedTestActions)
            {
                decoratedTestActions.TestInstanceActions.BeforeTestInstanceChain.Before(delegate(PatternTestInstanceState childTestInstanceState)
                {
                    IMemberInfo member = childTestInstanceState.Test.CodeElement as IMemberInfo;
                    if (member != null)
                    {
                        ITypeInfo memberDeclaringType = member.DeclaringType;
                        if (memberDeclaringType != null)
                        {
                            if (type.Equals(memberDeclaringType) || type.IsSubclassOf(memberDeclaringType))
                            {
                                childTestInstanceState.FixtureType     = testInstanceState.FixtureType;
                                childTestInstanceState.FixtureInstance = testInstanceState.FixtureInstance;
                            }
                        }
                    }
                });
            });
        }
        protected override ITypeInfo ResolveReturnType(ITypeInfo typeInfo)
        {
            if (typeInfo.IsGenericType)
            {
                var genericTypeDefinition = typeInfo.GetGenericTypeDefinition();
                if (genericTypeDefinition.Equals(TypeInfo.From(typeof(Task <>))) || genericTypeDefinition.Equals(TypeInfo.From(typeof(ActionResult <>))))
                {
                    return(ResolveReturnType(typeInfo.GetGenericArguments()[0]));
                }
            }

            if (typeInfo.Equals(TypeInfo.From <Task>()) || typeInfo.Equals(TypeInfo.From <ActionResult>()))
            {
                return(TypeInfo.From(typeof(void)));
            }

            return(typeInfo);
        }
예제 #7
0
 public ITypeBuildingContext ResolveType(string initialUnitPath, ITypeGenerator typeGenerator, ITypeInfo type, ITypeScriptUnitFactory unitFactory)
 {
     if (type.Equals(TypeInfo.From <TimeGuid>()))
     {
         return(TypeBuilding.RedirectToType("TimeGuid", @"..\DataTypes\TimeGuid", type));
     }
     if (type.Equals(TypeInfo.From <Timestamp>()))
     {
         return(TypeBuilding.RedirectToType("Timestamp", @"..\DataTypes\Timestamp", type));
     }
     if (type.Equals(TypeInfo.From <TimestampRange>()))
     {
         return(TypeBuilding.RedirectToType("DateTimeRange", @"..\DataTypes\DateTimeRange", type));
     }
     if (InternalApiTypeBuildingContext.Accept(type))
     {
         return(new InternalApiTypeBuildingContext(unitFactory.GetOrCreateTypeUnit(initialUnitPath), type));
     }
     return(null);
 }
        public TypeScriptType ReferenceFrom(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator)
        {
            if (!type.Equals(Type))
            {
                throw new InvalidOperationException($"Expected type {Type} with different meta, but got different type: {type}");
            }

            return(TypeScriptGeneratorHelpers.BuildTargetNullableTypeByOptions(
                       ReferenceFromInternal(type, targetUnit, typeGenerator),
                       type.CanBeNull(typeGenerator.Options.NullabilityMode),
                       typeGenerator.Options));
        }
예제 #9
0
        protected virtual ITypeInfo ResolveReturnType(ITypeInfo typeInfo)
        {
            if (typeInfo.IsGenericType)
            {
                var genericTypeDefinition = typeInfo.GetGenericTypeDefinition();
                if (genericTypeDefinition.Equals(TypeInfo.From(typeof(Task<>))))
                    return ResolveReturnType(typeInfo.GetGenericArguments()[0]);
            }

            if (typeInfo.Equals(TypeInfo.From<Task>()))
                return TypeInfo.From(typeof(void));
            return typeInfo;
        }
예제 #10
0
        public ITypeBuildingContext?ResolveType(string initialUnitPath, ITypeGenerator typeGenerator, ITypeInfo typeInfo, ITypeScriptUnitFactory unitFactory)
        {
            if (typeInfo.Equals(TypeInfo.From <MethodRootType>()) || typeInfo.Equals(TypeInfo.From <NullableReferenceMethodType>()))
            {
                return(new MethodTypeBuildingContext(unitFactory.GetOrCreateTypeUnit(initialUnitPath), typeInfo));
            }

            if (CollectionTypeBuildingContext.Accept(typeInfo))
            {
                return(new CollectionTypeBuildingContext(typeInfo));
            }

            if (typeInfo.Equals(TypeInfo.From <TimeSpan>()))
            {
                return(new StringBuildingContext(typeInfo));
            }

            if (typeInfo.IsAbstract)
            {
                return(new AbstractTypeBuildingContext(unitFactory.GetOrCreateTypeUnit(initialUnitPath), typeInfo));
            }

            return(null);
        }
예제 #11
0
        public TypeScriptTypeMemberDeclaration?ResolveProperty(TypeScriptUnit unit, ITypeGenerator typeGenerator, ITypeInfo typeInfo, IPropertyInfo propertyInfo)
        {
            if (!typeInfo.Equals(TypeInfo.From <EnumWithConstGetterContainingRootType>()) || !propertyInfo.PropertyType.IsEnum)
            {
                return(null);
            }

            var value = typeInfo is TypeInfo
                            ? GetValueFromPropertyInfo(typeInfo, propertyInfo)
                            : GetValueFromPropertySymbol(typeInfo, propertyInfo);

            if (!string.IsNullOrEmpty(value))
            {
                return(new TypeScriptTypeMemberDeclaration
                {
                    Name = propertyInfo.Name.ToLowerCamelCase(),
                    Optional = false,
                    Type = new TypeScriptStringLiteralType(value !),
                });
예제 #12
0
        private static void AssertEqual(ITypeInfo expected, ITypeInfo actual, bool assertProperties)
        {
            var runtimeType = Assert.IsType <RuntimeTypeInfo>(expected);

            var actualFullName = actual.FullName.Replace(
                CompilationUtility.GeneratedAssemblyName,
                runtimeType.TypeInfo.Assembly.GetName().Name);

            Assert.Equal(expected.Name, actual.Name);
#if DNXCORE50
            Assert.Equal(
                RuntimeTypeInfo.SanitizeFullName(expected.FullName),
                RuntimeTypeInfo.SanitizeFullName(actualFullName));
#endif
            Assert.Equal(expected.IsPublic, actual.IsPublic);
            Assert.Equal(expected.IsAbstract, actual.IsAbstract);
            Assert.Equal(expected.IsGenericType, actual.IsGenericType);
            Assert.Equal(
                expected.ImplementsInterface(TagHelperTypeInfo),
                actual.ImplementsInterface(TagHelperTypeInfo));
            Assert.Equal(
                expected.GetGenericDictionaryParameters(),
                actual.GetGenericDictionaryParameters(),
                new DelegateAssertion <ITypeInfo>((x, y) => AssertEqual(x, y, assertProperties: false)));

            if (assertProperties)
            {
                Assert.Equal(
                    expected.Properties.OrderBy(p => p.Name),
                    actual.Properties.OrderBy(p => p.Name),
                    new DelegateAssertion <IPropertyInfo>((x, y) => AssertEqual(x, y)));
            }

            Assert.True(actual.Equals(expected));
            Assert.True(expected.Equals(actual));
            Assert.Equal(expected.GetHashCode(), actual.GetHashCode());
        }
예제 #13
0
        private static void ValidateSlots(ITypeInfo type, IEnumerable<KeyValuePair<ISlotInfo, object>> slotValues)
        {
            foreach (KeyValuePair<ISlotInfo, object> slotValue in slotValues)
            {
                ISlotInfo slot = slotValue.Key;
                switch (slot.Kind)
                {
                    case CodeElementKind.GenericParameter:
                    case CodeElementKind.Field:
                    case CodeElementKind.Property:
                        var member = (IMemberInfo) slot;
                        if (type.Equals(member.DeclaringType) || type.IsSubclassOf(member.DeclaringType))
                            continue;
                        break;

                    case CodeElementKind.Parameter:
                        var parameter = (IParameterInfo) slot;
                        if (type.Equals(parameter.Member.DeclaringType))
                            continue;
                        break;
                }

                throw new ArgumentException(String.Format("Slot '{0}' is not valid for creating objects of type '{1}'.", slot, type), "slotValues");
            }
        }
예제 #14
0
 private static bool EqualTypesFromPossiblyForeignPolicies(ITypeInfo a, ITypeInfo b)
 {
     return a.Equals(b) || a.AssemblyQualifiedName == b.AssemblyQualifiedName;
 }
 public static bool Accept(ITypeInfo type)
 {
     return(type.Equals(TypeInfo.From <Task>()) || type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Task <>))));
 }
예제 #16
0
 private static bool EqualTypesFromPossiblyForeignPolicies(ITypeInfo a, ITypeInfo b)
 {
     return(a.Equals(b) || a.AssemblyQualifiedName == b.AssemblyQualifiedName);
 }