public static int GetGenericArgumentsToSkip(ITypeInfo type) { if (type.IsArray) { return(1 + GetGenericArgumentsToSkip(type.GetElementType())); } if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>)))) { return(0); } if (!type.IsGenericType) { return(type.IsValueType ? 0 : 1); } var count = 1; foreach (var argument in type.GetGenericArguments()) { count += GetGenericArgumentsToSkip(argument); } return(count); }
protected override TypeScriptType ReferenceFromInternal(ITypeInfo type, TypeScriptUnit targetUnit, ITypeGenerator typeGenerator) { var typeReference = typeGenerator.BuildAndImportType(targetUnit, type.GetGenericTypeDefinition()); return(new TypeScriptGenericTypeReference( (TypeScriptTypeReference)typeReference, type.GetGenericArguments().Select(x => GetArgumentType(x, typeGenerator, targetUnit)).ToArray() )); }
public static bool HasItem(this ITypeInfo type) { if (!type.IsGenericType) { return(false); } var typeDefinition = type.GetGenericTypeDefinition(); return(typeDefinition.Equals(TypeInfo.From(typeof(Task <>))) || typeDefinition.Equals(TypeInfo.From(typeof(List <>)))); }
private TypeScriptType GetTypeScriptType(TypeScriptUnit targetUnit, ITypeInfo typeInfo, IAttributeProvider?attributeProvider) { if (typeDeclarations.ContainsKey(typeInfo)) { return(typeDeclarations[typeInfo].ReferenceFrom(targetUnit, this, attributeProvider)); } if (typeInfo.IsGenericTypeDefinition && typeInfo.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>)))) { return(new TypeScriptNullableType(GetTypeScriptType(targetUnit, typeInfo.GetGenericArguments()[0], null))); } return(ResolveType(typeInfo).ReferenceFrom(targetUnit, this, attributeProvider)); }
private ITypeBuildingContext GetTypeBuildingContext(string typeLocation, ITypeInfo typeInfo) { if (BuiltinTypeBuildingContext.Accept(typeInfo)) { return(new BuiltinTypeBuildingContext(typeInfo)); } if (ArrayTypeBuildingContext.Accept(typeInfo)) { return(new ArrayTypeBuildingContext(typeInfo, Options)); } if (DictionaryTypeBuildingContext.Accept(typeInfo)) { return(new DictionaryTypeBuildingContext(typeInfo, Options)); } if (typeInfo.IsEnum) { var targetUnit = typeUnitFactory.GetOrCreateTypeUnit(typeLocation); return(Options.EnumGenerationMode == EnumGenerationMode.FixedStringsAndDictionary ? (ITypeBuildingContext) new FixedStringsAndDictionaryTypeBuildingContext(targetUnit, typeInfo) : new TypeScriptEnumTypeBuildingContext(targetUnit, typeInfo)); } if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition && typeInfo.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>)))) { var underlyingType = typeInfo.GetGenericArguments().Single(); if (Options.EnableExplicitNullability) { return(new NullableTypeBuildingContext(underlyingType, Options.UseGlobalNullable)); } return(GetTypeBuildingContext(typeLocation, underlyingType)); } if (typeInfo.IsGenericType && !typeInfo.IsGenericTypeDefinition) { return(new GenericTypeTypeBuildingContext(typeInfo, Options)); } if (typeInfo.IsGenericParameter) { return(new GenericParameterTypeBuildingContext(typeInfo)); } if (typeInfo.IsGenericTypeDefinition) { return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo)); } return(new CustomTypeTypeBuildingContext(typeUnitFactory.GetOrCreateTypeUnit(typeLocation), typeInfo)); }
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; }
private ITypeInfo GetElementType(ITypeInfo arrayType) { if (arrayType.IsArray) { return(arrayType.GetElementType() ?? throw new ArgumentNullException($"Array type's {arrayType.Name} element type is not defined")); } if (arrayType.IsGenericType && arrayType.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(List <>)))) { return(arrayType.GetGenericArguments()[0]); } throw new ArgumentException("arrayType should be either Array or List<T>", nameof(arrayType)); }
public static (bool, ITypeInfo) ProcessNullable(IAttributeProvider?attributeProvider, ITypeInfo type, NullabilityMode nullabilityMode) { if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Nullable <>)))) { var underlyingType = type.GetGenericArguments()[0]; return(true, underlyingType); } if (attributeProvider == null || !type.IsClass && !type.IsInterface) { return(false, type); } return(CanBeNull(attributeProvider, nullabilityMode), type); }
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); }
public static bool Accept(ITypeInfo type) { return(type.IsArray || type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(List <>)))); }
public static bool Accept(ITypeInfo type) { return(type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Dictionary <,>)))); }
/// <summary> /// Helper method that checks the signature of a TestMethod and /// any supplied parameters to determine if the test is valid. /// /// Currently, NUnitTestMethods are required to be public, /// non-abstract methods, either static or instance, /// returning void. They may take arguments but the _values must /// be provided or the TestMethod is not considered runnable. /// /// Methods not meeting these criteria will be marked as /// non-runnable and the method will return false in that case. /// </summary> /// <param name="testMethod">The TestMethod to be checked. If it /// is found to be non-runnable, it will be modified.</param> /// <param name="parms">Parameters to be used for this test, or null</param> /// <returns>True if the method signature is valid, false if not</returns> /// <remarks> /// The return value is no longer used internally, but is retained /// for testing purposes. /// </remarks> private static bool CheckTestMethodSignature(TestMethod testMethod, TestCaseParameters parms) { if (testMethod.Method.IsAbstract) { return(MarkAsNotRunnable(testMethod, "Method is abstract")); } if (!testMethod.Method.IsPublic) { return(MarkAsNotRunnable(testMethod, "Method is not public")); } IParameterInfo[] parameters; #if NETCF if (testMethod.Method.IsGenericMethodDefinition) { if (parms != null && parms.Arguments != null) { testMethod.Method = testMethod.Method.MakeGenericMethodEx(parms.Arguments); if (testMethod.Method == null) { return(MarkAsNotRunnable(testMethod, "Cannot determine generic types by probing")); } parameters = testMethod.Method.GetParameters(); } else { parameters = new IParameterInfo[0]; } } else { parameters = testMethod.Method.GetParameters(); } int minArgsNeeded = parameters.Length; #else parameters = testMethod.Method.GetParameters(); int minArgsNeeded = 0; foreach (var parameter in parameters) { // IsOptional is supported since .NET 1.1 if (!parameter.IsOptional) { minArgsNeeded++; } } #endif int maxArgsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.parms = parms; testMethod.RunState = parms.RunState; arglist = parms.Arguments; if (arglist != null) { argsProvided = arglist.Length; } if (testMethod.RunState != RunState.Runnable) { return(false); } } #if NETCF ITypeInfo returnType = testMethod.Method.IsGenericMethodDefinition && (parms == null || parms.Arguments == null) ? new TypeWrapper(typeof(void)) : testMethod.Method.ReturnType; #else ITypeInfo returnType = testMethod.Method.ReturnType; #endif #if NET_4_0 || NET_4_5 || PORTABLE if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method.MethodInfo)) { if (returnType.IsType(typeof(void))) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-void return type")); } var returnsGenericTask = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(System.Threading.Tasks.Task <>); if (returnsGenericTask && (parms == null || !parms.HasExpectedResult)) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-generic Task return type when no result is expected")); } if (!returnsGenericTask && parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Async test method must have Task<T> return type when a result is expected")); } } else #endif if (returnType.IsType(typeof(void))) { if (parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method returning void cannot have an expected result")); } } else if (parms == null || !parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method has non-void return value, but no result is expected")); } if (argsProvided > 0 && maxArgsNeeded == 0) { return(MarkAsNotRunnable(testMethod, "Arguments provided for method not taking any")); } if (argsProvided == 0 && minArgsNeeded > 0) { return(MarkAsNotRunnable(testMethod, "No arguments were provided")); } if (argsProvided < minArgsNeeded) { return(MarkAsNotRunnable(testMethod, string.Format("Not enough arguments provided, provide at least {0} arguments.", minArgsNeeded))); } if (argsProvided > maxArgsNeeded) { return(MarkAsNotRunnable(testMethod, string.Format("Too many arguments provided, provide at most {0} arguments.", maxArgsNeeded))); } if (testMethod.Method.IsGenericMethodDefinition && arglist != null) { var typeArguments = new GenericMethodHelper(testMethod.Method.MethodInfo).GetTypeArguments(arglist); foreach (Type o in typeArguments) { if (o == null || o == TypeHelper.NonmatchingType) { return(MarkAsNotRunnable(testMethod, "Unable to determine type arguments for method")); } } testMethod.Method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); } if (arglist != null && parameters != null) { TypeHelper.ConvertArgumentList(arglist, parameters); } return(true); }
/// <summary> /// Helper method that checks the signature of a TestMethod and /// any supplied parameters to determine if the test is valid. /// /// Currently, NUnitTestMethods are required to be public, /// non-abstract methods, either static or instance, /// returning void. They may take arguments but the values must /// be provided or the TestMethod is not considered runnable. /// /// Methods not meeting these criteria will be marked as /// non-runnable and the method will return false in that case. /// </summary> /// <param name="testMethod">The TestMethod to be checked. If it /// is found to be non-runnable, it will be modified.</param> /// <param name="parms">Parameters to be used for this test, or null</param> /// <returns>True if the method signature is valid, false if not</returns> /// <remarks> /// The return value is no longer used internally, but is retained /// for testing purposes. /// </remarks> private static bool CheckTestMethodSignature(TestMethod testMethod, TestCaseParameters parms) { if (testMethod.Method.IsAbstract) { return(MarkAsNotRunnable(testMethod, "Method is abstract")); } if (!testMethod.Method.IsPublic) { return(MarkAsNotRunnable(testMethod, "Method is not public")); } IParameterInfo[] parameters; parameters = testMethod.Method.GetParameters(); int minArgsNeeded = 0; foreach (var parameter in parameters) { // IsOptional is supported since .NET 1.1 if (!parameter.IsOptional) { minArgsNeeded++; } } int maxArgsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.parms = parms; testMethod.RunState = parms.RunState; arglist = parms.Arguments; if (arglist != null) { argsProvided = arglist.Length; } if (testMethod.RunState != RunState.Runnable) { return(false); } } ITypeInfo returnType = testMethod.Method.ReturnType; #if ASYNC if (AsyncToSyncAdapter.IsAsyncOperation(testMethod.Method.MethodInfo)) { if (returnType.IsType(typeof(void))) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-void return type")); } var returnsGenericTask = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(System.Threading.Tasks.Task <>); if (returnsGenericTask && (parms == null || !parms.HasExpectedResult)) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-generic Task return type when no result is expected")); } if (!returnsGenericTask && parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Async test method must have Task<T> return type when a result is expected")); } } else #endif if (returnType.IsType(typeof(void))) { if (parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method returning void cannot have an expected result")); } } else if (parms == null || !parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method has non-void return value, but no result is expected")); } if (argsProvided > 0 && maxArgsNeeded == 0) { return(MarkAsNotRunnable(testMethod, "Arguments provided for method with no parameters")); } if (argsProvided == 0 && minArgsNeeded > 0) { return(MarkAsNotRunnable(testMethod, "No arguments were provided")); } if (argsProvided < minArgsNeeded) { return(MarkAsNotRunnable(testMethod, string.Format("Not enough arguments provided, provide at least {0} arguments.", minArgsNeeded))); } if (argsProvided > maxArgsNeeded) { return(MarkAsNotRunnable(testMethod, string.Format("Too many arguments provided, provide at most {0} arguments.", maxArgsNeeded))); } if (testMethod.Method.IsGenericMethodDefinition && arglist != null) { Type[] typeArguments; if (!new GenericMethodHelper(testMethod.Method.MethodInfo).TryGetTypeArguments(arglist, out typeArguments)) { return(MarkAsNotRunnable(testMethod, "Unable to determine type arguments for method")); } testMethod.Method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); } if (arglist != null && parameters != null) { TypeHelper.ConvertArgumentList(arglist, parameters); } return(true); }
public Type GetGenericTypeDefinition() { return(_baseInfo.GetGenericTypeDefinition()); }
public static bool Accept(ITypeInfo type) { return(type.Equals(TypeInfo.From <Task>()) || type.IsGenericType && type.GetGenericTypeDefinition().Equals(TypeInfo.From(typeof(Task <>)))); }
private TypeScriptTypeDeclaration CreateComplexTypeScriptDeclarationWithoutDefinition(ITypeInfo type) { var result = new TypeScriptTypeDeclaration { Name = type.IsGenericType ? new Regex("`.*$").Replace(type.GetGenericTypeDefinition().Name, "") : type.Name, Definition = null, GenericTypeArguments = Type.IsGenericTypeDefinition ? Type.GetGenericArguments().Select(x => x.Name).ToArray() : new string[0] }; return(result); }