internal static (string arguments, string constraints) GetGenericArguments(this MethodBase @this, SortedSet <string> namespaces) { var arguments = string.Empty; var constraints = string.Empty; if (@this.IsGenericMethodDefinition) { var genericArguments = new List <string>(); var genericConstraints = new List <string>(); foreach (var argument in @this.GetGenericArguments()) { var attributeData = argument.GetCustomAttributesData(); genericArguments.Add( $"{attributeData.GetAttributes(false, namespaces, null)}{TypeDissector.Create(argument).SafeName}"); var constraint = argument.GetConstraints(namespaces); if (!string.IsNullOrWhiteSpace(constraint)) { genericConstraints.Add(constraint); } } arguments = $"<{string.Join(", ", genericArguments)}>"; // TODO: This should not add a space in front. The Maker class // should adjust the constraints to have a space in front. constraints = genericConstraints.Count == 0 ? string.Empty : $"{string.Join(" ", genericConstraints)}"; } return(arguments, constraints); }
internal static bool ContainsDelegateConditions(this MethodInfo @this) => (from parameter in @this.GetParameters() let parameterType = parameter.ParameterType where parameter.IsOut || parameterType.IsByRef || typeof(TypedReference).IsAssignableFrom(parameterType) || typeof(RuntimeArgumentHandle).IsAssignableFrom(parameterType) || TypeDissector.Create(parameterType).IsPointer select parameter).Any() || TypeDissector.Create(@this.ReturnType).IsPointer;
internal static string GetExpectationExceptionMessage(this MethodBase @this) { var hasPointerTypes = @this.GetParameters() .Where(_ => TypeDissector.Create(_.ParameterType).IsPointer).Any(); var argumentlist = hasPointerTypes ? @this.GetParameters(new SortedSet <string>()) : @this.GetLiteralArgumentNameList(); return($"{@this.Name}{@this.GetGenericArguments(new SortedSet<string>()).arguments}({argumentlist})"); }
public void DissectSimpleType() { var dissector = TypeDissector.Create(typeof(int)); Assert.That(dissector.IsArray, Is.False, nameof(dissector.IsArray)); Assert.That(dissector.IsByRef, Is.False, nameof(dissector.IsByRef)); Assert.That(dissector.IsPointer, Is.False, nameof(dissector.IsPointer)); Assert.That(dissector.RootType, Is.EqualTo(typeof(int)), nameof(dissector.RootType)); Assert.That(dissector.SafeName, Is.EqualTo("int"), nameof(dissector.SafeName)); }
public void DissectByRefPointerArrayType() { var type = this.GetType().GetMethod(nameof(this.TargetWithByRefPointerArray)) !.GetParameters()[0].ParameterType; var dissector = TypeDissector.Create(type); Assert.That(dissector.IsArray, Is.True, nameof(dissector.IsArray)); Assert.That(dissector.IsByRef, Is.True, nameof(dissector.IsByRef)); Assert.That(dissector.IsPointer, Is.True, nameof(dissector.IsPointer)); Assert.That(dissector.RootType, Is.EqualTo(typeof(int)), nameof(dissector.RootType)); Assert.That(dissector.SafeName, Is.EqualTo("int"), nameof(dissector.SafeName)); }
internal static string GetAttributes(this IList <CustomAttributeData> @this, bool isReturn, SortedSet <string> namespaces, ParameterInfo?parameter) { var attributes = new List <string>(); foreach (var attributeData in @this) { if (!attributeData.IsNullableAttribute() && !(parameter != null && parameter.IsOut && parameter.ParameterType.IsByRef && typeof(OutAttribute).IsAssignableFrom(attributeData.AttributeType) || typeof(ParamArrayAttribute).IsAssignableFrom(attributeData.AttributeType) || typeof(OptionalAttribute).IsAssignableFrom(attributeData.AttributeType))) { var attributeType = attributeData.AttributeType; var name = TypeDissector.Create(attributeType).SafeName; namespaces.Add(attributeType.Namespace); if (name.EndsWith(IListOfCustomAttributeDataExtensions.AttributeName, StringComparison.Ordinal)) { name = name.Substring(0, name.LastIndexOf(IListOfCustomAttributeDataExtensions.AttributeName, StringComparison.Ordinal)); } var constructorArguments = string.Join(", ", (from argument in attributeData.ConstructorArguments let argumentType = argument.ArgumentType let namespaceAdd = namespaces.Add(argumentType.Namespace) let typeCast = argumentType.IsEnum ? $"({TypeDissector.Create(argumentType).SafeName})" : string.Empty let argumentValue = typeof(string).IsAssignableFrom(argumentType) ? $"\"{argument.Value}\"" : typeof(bool).IsAssignableFrom(argumentType) ? argument.Value.ToString().ToLower(CultureInfo.CurrentCulture) : argument.Value select $"{typeCast}{argumentValue}").ToArray()); var namedArguments = !typeof(MarshalAsAttribute).IsAssignableFrom(attributeData.AttributeType) ? string.Join(", ", (from argument in attributeData.NamedArguments let argumentType = argument.TypedValue.ArgumentType let namespaceAdd = namespaces.Add(argumentType.Namespace) let typeCast = argumentType.IsEnum ? $"({TypeDissector.Create(argumentType).SafeName})" : string.Empty let argumentValue = typeof(string).IsAssignableFrom(argumentType) ? $"\"{argument.TypedValue.Value}\"" : typeof(bool).IsAssignableFrom(argumentType) ? argument.TypedValue.Value.ToString().ToLower(CultureInfo.CurrentCulture) : argument.TypedValue.Value select $"{argument.MemberName} = {typeCast}{argumentValue}").ToArray()) : string.Empty; var arguments = !string.IsNullOrWhiteSpace(constructorArguments) && !string.IsNullOrWhiteSpace(namedArguments) ? $"({constructorArguments}, {namedArguments})" : !string.IsNullOrWhiteSpace(constructorArguments) ? $"({constructorArguments})" : !string.IsNullOrWhiteSpace(constructorArguments) ? $"({namedArguments})" : string.Empty; attributes.Add($"{name}{arguments}"); } } var returnTarget = isReturn ? "return: " : string.Empty; return(attributes.Count == 0 ? string.Empty : $"[{returnTarget}{string.Join(", ", attributes)}]"); }
private static string GetFullName(this Type @this, SortedSet <string> namespaces, NullableContext context) { var dissector = TypeDissector.Create(@this); var pointer = dissector.IsPointer ? "*" : string.Empty; var array = dissector.IsArray ? $"[]{(context.GetNextFlag() == NullableContext.Annotated ? "?" : string.Empty)}" : string.Empty; var typeAnnotation = dissector.RootType.IsValueType ? string.Empty : context.GetNextFlag() == NullableContext.Annotated ? "?" : string.Empty; // We're discarding the "0" flag for generic value types. if (dissector.RootType.IsValueType && dissector.RootType.IsGenericType) { context.GetNextFlag(); } return($"{dissector.SafeName}{dissector.RootType.GetGenericArguments(namespaces, context).arguments}{typeAnnotation}{pointer}{array}"); }
public void GetSafeNameForObjectPrimitiveType() => Assert.That(TypeDissector.Create(typeof(object)).SafeName, Is.EqualTo("object"));
public void GetSafeNameForStringPrimitiveType() => Assert.That(TypeDissector.Create(typeof(string)).SafeName, Is.EqualTo("string"));
public void GetSafeNameForDecimalPrimitiveType() => Assert.That(TypeDissector.Create(typeof(decimal)).SafeName, Is.EqualTo("decimal"));
public void GetSafeNameForFloatPrimitiveType() => Assert.That(TypeDissector.Create(typeof(float)).SafeName, Is.EqualTo("float"));
public void GetSafeNameWithNestedClosedGenerics() => Assert.That(TypeDissector.Create(typeof(NestedGenerics.IHaveGenerics <string>)).SafeName, Is.EqualTo("NestedGenerics.IHaveGenerics"));
protected override void HandleRefOutMethod(MethodInfo baseMethod, MethodInformation methodDescription) { var returnType = baseMethod.ReturnType; this.generatedDelegates.Add(MethodTemplates.GetAssemblyDelegate( baseMethod.ReturnType == typeof(void) ? "void" : TypeDissector.Create(returnType).SafeName, methodDescription.DelegateCast, baseMethod.GetParameters(this.Namespaces), baseMethod.IsUnsafeToMock())); this.Namespaces.Add(returnType.Namespace); }
public void GetSafeName() => Assert.That(TypeDissector.Create(typeof(SubnestedClass.IAmSubnested)).SafeName, Is.EqualTo("TypeDissectorTests.SubnestedClass.IAmSubnested"));
public void GetSafeNameForSBytePrimitiveType() => Assert.That(TypeDissector.Create(typeof(sbyte)).SafeName, Is.EqualTo("sbyte"));
public void GetSafeNameForDelegateWithoutSpecifiedGenericsAndNamespaces() => Assert.That(TypeDissector.Create(typeof(MapForGeneric <>)).SafeName, Is.EqualTo("MapForGeneric"));
public void GetSafeNameForBoolPrimitiveType() => Assert.That(TypeDissector.Create(typeof(bool)).SafeName, Is.EqualTo("bool"));
public void GetSafeNameForDelegateWithNoGenericsAndNamespaces() => Assert.That(TypeDissector.Create(typeof(MapForNonGeneric)).SafeName, Is.EqualTo("MapForNonGeneric"));
public void GetSafeNameForNestedDelegateWithGenericsAndRefArguments() => Assert.That(TypeDissector.Create(typeof(TypeDissectorTests.RefTargetWithGeneric <Guid>)).SafeName, Is.EqualTo("TypeDissectorTests.RefTargetWithGeneric"));
public void GetSafeNameForDelegateWithoutSpecifiedGenericsAndRefArguments() => Assert.That(TypeDissector.Create(typeof(RefTargetWithAGeneric <>)).SafeName, Is.EqualTo("RefTargetWithAGeneric"));
public void GetSafeNameForArrayOfPointersType() => Assert.That(TypeDissector.Create(typeof(byte *[])).SafeName, Is.EqualTo("byte"));
public void GetSafeNameForUIntPrimitiveType() => Assert.That(TypeDissector.Create(typeof(uint)).SafeName, Is.EqualTo("uint"));
public void GetSafeNameWhenTypeNameCollidesWithRocksTypeName() => Assert.That(TypeDissector.Create(typeof(TypeExtensionsNamespace.IMock)).SafeName, Is.EqualTo("TypeExtensionsNamespace.IMock"));
public void GetSafeNameForCharPrimitiveType() => Assert.That(TypeDissector.Create(typeof(char)).SafeName, Is.EqualTo("char"));
internal static string GetExpectationChecks(this MethodInfo @this) => string.Join(" && ", @this.GetParameters() .Where(_ => !TypeDissector.Create(_.ParameterType).IsPointer) .Select(_ => CodeTemplates.GetExpectation(_.Name, $"{_.ParameterType.GetFullName(_)}")));
public void GetSafeNameForDoublePrimitiveType() => Assert.That(TypeDissector.Create(typeof(double)).SafeName, Is.EqualTo("double"));
public void GetSafeNameForShortPrimitiveType() => Assert.That(TypeDissector.Create(typeof(short)).SafeName, Is.EqualTo("short"));
public void GetSafeNameWithOpenGenerics() => Assert.That(TypeDissector.Create(typeof(IHaveGenerics <>)).SafeName, Is.EqualTo("IHaveGenerics"));