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);
        }
示例#2
0
 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})");
        }
示例#4
0
        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));
        }
示例#5
0
        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)}]");
        }
示例#7
0
        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}");
        }
示例#8
0
 public void GetSafeNameForObjectPrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(object)).SafeName, Is.EqualTo("object"));
示例#9
0
 public void GetSafeNameForStringPrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(string)).SafeName, Is.EqualTo("string"));
示例#10
0
 public void GetSafeNameForDecimalPrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(decimal)).SafeName, Is.EqualTo("decimal"));
示例#11
0
 public void GetSafeNameForFloatPrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(float)).SafeName, Is.EqualTo("float"));
示例#12
0
 public void GetSafeNameWithNestedClosedGenerics() =>
 Assert.That(TypeDissector.Create(typeof(NestedGenerics.IHaveGenerics <string>)).SafeName,
             Is.EqualTo("NestedGenerics.IHaveGenerics"));
示例#13
0
        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);
        }
示例#14
0
 public void GetSafeName() =>
 Assert.That(TypeDissector.Create(typeof(SubnestedClass.IAmSubnested)).SafeName,
             Is.EqualTo("TypeDissectorTests.SubnestedClass.IAmSubnested"));
示例#15
0
 public void GetSafeNameForSBytePrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(sbyte)).SafeName, Is.EqualTo("sbyte"));
示例#16
0
 public void GetSafeNameForDelegateWithoutSpecifiedGenericsAndNamespaces() =>
 Assert.That(TypeDissector.Create(typeof(MapForGeneric <>)).SafeName,
             Is.EqualTo("MapForGeneric"));
示例#17
0
 public void GetSafeNameForBoolPrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(bool)).SafeName, Is.EqualTo("bool"));
示例#18
0
 public void GetSafeNameForDelegateWithNoGenericsAndNamespaces() =>
 Assert.That(TypeDissector.Create(typeof(MapForNonGeneric)).SafeName,
             Is.EqualTo("MapForNonGeneric"));
示例#19
0
 public void GetSafeNameForNestedDelegateWithGenericsAndRefArguments() =>
 Assert.That(TypeDissector.Create(typeof(TypeDissectorTests.RefTargetWithGeneric <Guid>)).SafeName,
             Is.EqualTo("TypeDissectorTests.RefTargetWithGeneric"));
示例#20
0
 public void GetSafeNameForDelegateWithoutSpecifiedGenericsAndRefArguments() =>
 Assert.That(TypeDissector.Create(typeof(RefTargetWithAGeneric <>)).SafeName,
             Is.EqualTo("RefTargetWithAGeneric"));
示例#21
0
 public void GetSafeNameForArrayOfPointersType() =>
 Assert.That(TypeDissector.Create(typeof(byte *[])).SafeName, Is.EqualTo("byte"));
示例#22
0
 public void GetSafeNameForUIntPrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(uint)).SafeName, Is.EqualTo("uint"));
示例#23
0
 public void GetSafeNameWhenTypeNameCollidesWithRocksTypeName() =>
 Assert.That(TypeDissector.Create(typeof(TypeExtensionsNamespace.IMock)).SafeName,
             Is.EqualTo("TypeExtensionsNamespace.IMock"));
示例#24
0
 public void GetSafeNameForCharPrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(char)).SafeName, Is.EqualTo("char"));
示例#25
0
 internal static string GetExpectationChecks(this MethodInfo @this) =>
 string.Join(" && ",
             @this.GetParameters()
             .Where(_ => !TypeDissector.Create(_.ParameterType).IsPointer)
             .Select(_ => CodeTemplates.GetExpectation(_.Name, $"{_.ParameterType.GetFullName(_)}")));
示例#26
0
 public void GetSafeNameForDoublePrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(double)).SafeName, Is.EqualTo("double"));
示例#27
0
 public void GetSafeNameForShortPrimitiveType() =>
 Assert.That(TypeDissector.Create(typeof(short)).SafeName, Is.EqualTo("short"));
示例#28
0
 public void GetSafeNameWithOpenGenerics() =>
 Assert.That(TypeDissector.Create(typeof(IHaveGenerics <>)).SafeName,
             Is.EqualTo("IHaveGenerics"));