public void Public_api_arguments_should_have_not_null_annotation() { var parametersMissingAttribute = (from type in GetAllTypes(TargetAssembly.GetTypes()) where type.IsVisible && !typeof(Delegate).IsAssignableFrom(type) let interfaceMappings = type.GetInterfaces().Select(type.GetInterfaceMap) let events = type.GetEvents() from method in type.GetMethods(PublicInstance | BindingFlags.Static) .Concat <MethodBase>(type.GetConstructors()) where method.DeclaringType == type where type.IsInterface || !interfaceMappings.Any(im => im.TargetMethods.Contains(method)) where !events.Any(e => e.AddMethod == method || e.RemoveMethod == method) from parameter in method.GetParameters() where !parameter.ParameterType.IsValueType && !parameter.GetCustomAttributes() .Any( a => a.GetType().Name == "NotNullAttribute" || a.GetType().Name == "CanBeNullAttribute") select type.FullName + "." + method.Name + "[" + parameter.Name + "]") .ToList(); Assert.False( parametersMissingAttribute.Any(), "\r\n-- Missing NotNull annotations --\r\n" + string.Join("\r\n", parametersMissingAttribute)); }
public virtual void Public_api_arguments_should_not_have_redundant_not_null_annotation() { var parametersWithRedundantAttribute = (from type in GetAllTypes(TargetAssembly.GetTypes()) where type.GetTypeInfo().IsVisible&& !typeof(Delegate).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) let interfaceMappings = type.GetInterfaces().Select(i => type.GetTypeInfo().GetRuntimeInterfaceMap(i)) let events = type.GetEvents() from method in type.GetMethods(AnyInstance | BindingFlags.Static | BindingFlags.DeclaredOnly) .Concat <MethodBase>(type.GetConstructors()) where method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly from parameter in method.GetParameters() let parameterType = parameter.ParameterType.IsByRef ? parameter.ParameterType.GetElementType() : parameter.ParameterType let attributes = parameter.GetCustomAttributes(inherit: false) where (!ShouldHaveNotNullAnnotation(method, type) || !type.GetTypeInfo().IsInterface&& interfaceMappings.Any(im => im.TargetMethods.Contains(method)) || events.Any(e => e.AddMethod == method || e.RemoveMethod == method) || parameterType.GetTypeInfo().IsValueType&& !parameterType.GetTypeInfo().IsNullableType()) && attributes.Any(a => a.GetType().Name == nameof(NotNullAttribute) || a.GetType().Name == nameof(CanBeNullAttribute)) || parameterType.GetTypeInfo().IsValueType && parameterType.GetTypeInfo().IsNullableType() && attributes.Any(a => a.GetType().Name == nameof(CanBeNullAttribute)) select type.FullName + "." + method.Name + "[" + parameter.Name + "]").ToList(); Assert.False( parametersWithRedundantAttribute.Count > 0, "\r\n-- Redundant NotNull annotations --\r\n" + string.Join(Environment.NewLine, parametersWithRedundantAttribute)); }
public void Public_api_arguments_should_have_not_null_annotation() { var parametersMissingAttribute = (from type in GetAllTypes(TargetAssembly.GetTypes()) where type.GetTypeInfo().IsVisible&& !typeof(Delegate).GetTypeInfo().IsAssignableFrom(type.GetTypeInfo()) let interfaceMappings = type.GetInterfaces().Select(i => type.GetTypeInfo().GetRuntimeInterfaceMap(i)) let events = type.GetEvents() from method in type.GetMethods(AnyInstance | BindingFlags.Static) .Concat <MethodBase>(type.GetConstructors()) where (method.DeclaringType == type) && (method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly) && (method is ConstructorInfo || (((MethodInfo)method).GetBaseDefinition().DeclaringType == method.DeclaringType)) //&& (method.Name != nameof(DbContext.OnConfiguring)) //&& (method.Name != nameof(DbContext.OnModelCreating)) where type.GetTypeInfo().IsInterface || !interfaceMappings.Any(im => im.TargetMethods.Contains(method)) where !events.Any(e => (e.AddMethod == method) || (e.RemoveMethod == method)) from parameter in method.GetParameters() let parameterType = parameter.ParameterType.IsByRef ? parameter.ParameterType.GetElementType() : parameter.ParameterType where !parameterType.GetTypeInfo().IsValueType && !parameter.GetCustomAttributes() .Any( a => (a.GetType().Name == nameof(NotNullAttribute)) || (a.GetType().Name == nameof(CanBeNullAttribute))) select type.FullName + "." + method.Name + "[" + parameter.Name + "]") .ToList(); Assert.False( parametersMissingAttribute.Any(), "\r\n-- Missing NotNull annotations --\r\n" + string.Join(Environment.NewLine, parametersMissingAttribute)); }
public Type GetListTypeFor(WrapperPropInfo wpi) { if (wpi.PropertyType.IsArray) { return(wpi.PropertyType.DeclaringType); } string fullPropName = wpi.FullNameDest; Type listType; string listTypeName; listType = LoadListTypeForProperty(fullPropName); if (listType != null) { return(listType); } // Try to find type from get*BySomething)) if (fullPropName.EndsWith("s")) { string singularName = wpi.Name.Substring(0, wpi.Name.Length - 1); string singularGetNameStart = "get" + singularName + "By"; Type guessedReturnType = wpi.ClassInfo.WrappedType .GetMethods(BindingFlags.Instance | BindingFlags.Public) .Where(mi => mi.ReturnType != typeof(void)) .Where(mi => mi.Name.StartsWith(singularGetNameStart)) .Where(mi => mi.Name.Length > singularGetNameStart.Length) .Select(mi => mi.ReturnType) .FirstOrDefault(); if (guessedReturnType == null) { // Try to find type from name guessedReturnType = TargetAssembly.GetTypes() .Where(t => t.Name == "Html" + singularName || t.Name == singularName).FirstOrDefault(); } if (guessedReturnType != null) { Console.WriteLine("Guessed list type " + guessedReturnType.FullName + " for " + fullPropName); return(guessedReturnType); } } listType = GetListTypeFor(fullPropName); return(listType); }
public bool GenerateForAllUsedIncomplete() { var allAssemblyTypes = new Dictionary<string, Type>(); foreach (Type t in TargetAssembly.GetTypes().Where(t => t.IsPublic)) { if (!allAssemblyTypes.ContainsKey(t.Name)) allAssemblyTypes.Add(t.Name, t); } bool added = false; foreach (Type type in UsedTypes.ToArray()) { if (!TypeIsWrapped(type)) throw new InvalidOperationException(); if (!CompleteTypes.ContainsKey(type)) { var wci = new WrapperClassInfo(type, this); CompleteTypes.Add(type, wci); foreach ( Type inheritedType in allAssemblyTypes.Values.Where(t => t.IsSubclassOf(wci.WrappedType) && t.IsPublic) .Where(TypeIsWrapped)) { if (inheritedType.GetCustomAttributes(typeof(ObsoleteAttribute), true).Count() == 0 && !UsedTypes.Contains(inheritedType)) UsedTypes.Add(inheritedType); } var sb = new StringBuilder(); if (wci.IsInterface) wci.GenerateInterfaceCode(sb); else wci.GenerateClassCode(sb); if (wci.FilePathBase.IndexOfAny(new[] { '\\', '/' }) != -1) Directory.CreateDirectory(Path.GetDirectoryName(wci.FilePathBase)); WriteTextToFile(wci.FilePathBase + ".cs", sb.ToString(), true); wci.GenerateUserClassFile(); added = true; } } return added; }
public virtual void All_test_bases_must_be_implemented() { var concreteTests = TargetAssembly.GetTypes().Where(c => c.BaseType != typeof(object) && !c.IsAbstract).ToList(); var nonImplementedBases = (from baseType in GetBaseTestClasses() where !IgnoredTestBases.Contains(baseType) && !concreteTests.Any(c => Implements(c, baseType)) select baseType.FullName) .ToList(); Assert.False( nonImplementedBases.Count > 0, "\r\n-- Missing derived classes for --\r\n" + string.Join(Environment.NewLine, nonImplementedBases)); }
public virtual void Async_methods_should_have_overload_with_cancellation_token_and_end_with_async_suffix() { var asyncMethods = (from type in GetAllTypes(TargetAssembly.GetTypes()) where type.GetTypeInfo().IsVisible from method in type.GetMethods(AnyInstance | BindingFlags.Static) where method.DeclaringType == type && (method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly) where typeof(Task).GetTypeInfo().IsAssignableFrom(method.ReturnType.GetTypeInfo()) select method).ToList(); var asyncMethodsWithToken = (from method in asyncMethods where method.GetParameters().Any(pi => pi.ParameterType == typeof(CancellationToken)) select method).ToList(); var asyncMethodsWithoutToken = (from method in asyncMethods where method.GetParameters().All(pi => pi.ParameterType != typeof(CancellationToken)) select method).ToList(); var missingOverloads = (from methodWithoutToken in asyncMethodsWithoutToken where !asyncMethodsWithToken .Any( methodWithToken => methodWithoutToken.Name == methodWithToken.Name && methodWithoutToken.DeclaringType == methodWithToken.DeclaringType) // ReSharper disable once PossibleNullReferenceException select methodWithoutToken.DeclaringType.Name + "." + methodWithoutToken.Name) .Except(GetCancellationTokenExceptions()) .ToList(); Assert.False( missingOverloads.Count > 0, "\r\n-- Missing async overloads --\r\n" + string.Join(Environment.NewLine, missingOverloads)); var missingSuffixMethods = asyncMethods .Where(method => !method.Name.EndsWith("Async") && method.DeclaringType != null) .Select(method => method.DeclaringType.Name + "." + method.Name) .Except(GetAsyncSuffixExceptions()) .ToList(); Assert.False( missingSuffixMethods.Count > 0, "\r\n-- Missing async suffix --\r\n" + string.Join(Environment.NewLine, missingSuffixMethods)); }
public void Async_methods_should_have_overload_with_cancellation_token_and_end_with_async_suffix() { var asyncMethods = (from type in GetAllTypes(TargetAssembly.GetTypes()) where type.IsVisible from method in type.GetMethods(PublicInstance /* | BindingFlags.Static*/) where GetBasestTypeInAssembly(method.DeclaringType) == type where typeof(Task).IsAssignableFrom(method.ReturnType) select method).ToList(); var asyncMethodsWithToken = (from method in asyncMethods where method.GetParameters().Any(pi => pi.ParameterType == typeof(CancellationToken)) select method).ToList(); var asyncMethodsWithoutToken = (from method in asyncMethods where method.GetParameters().All(pi => pi.ParameterType != typeof(CancellationToken)) select method).ToList(); var missingOverloads = (from methodWithoutToken in asyncMethodsWithoutToken where !asyncMethodsWithToken .Any(methodWithToken => methodWithoutToken.Name == methodWithToken.Name && methodWithoutToken.ReflectedType == methodWithToken.ReflectedType) // ReSharper disable once PossibleNullReferenceException select methodWithoutToken.DeclaringType.Name + "." + methodWithoutToken.Name) .Except(GetCancellationTokenExceptions()) .ToList(); Assert.False( missingOverloads.Any(), "\r\n-- Missing async overloads --\r\n" + string.Join("\r\n", missingOverloads)); var missingSuffixMethods = asyncMethods .Where(method => !method.Name.EndsWith("Async")) .Select(method => method.DeclaringType.Name + "." + method.Name) .Except(GetAsyncSuffixExceptions()) .ToList(); Assert.False( missingSuffixMethods.Any(), "\r\n-- Missing async suffix --\r\n" + string.Join("\r\n", missingSuffixMethods)); }
public IEnumerable <Type> GetDetectableTypes() { var list = TargetAssembly.GetTypes().Where(t => t.BaseType == BaseDetectableType) .Select(t => { var attrib = t.GetCustomAttribute(typeof(DetectAttribute), false) as DetectAttribute; var order = (attrib == null) ? 100 : attrib.Priority; return(new { Order = order, Type = t }); }) .OrderBy(t => t.Order) .Select(t => t.Type); return(list); }
public void Public_inheritable_apis_should_be_virtual() { var nonVirtualMethods = (from type in GetAllTypes(TargetAssembly.GetTypes()) where type.IsVisible && !type.IsSealed && type.GetConstructors(AnyInstance).Any(c => c.IsPublic || c.IsFamily || c.IsFamilyOrAssembly) && type.Namespace != null && !type.Namespace.EndsWith(".Compiled") from method in type.GetMethods(PublicInstance) where method.DeclaringType == type && !(method.IsVirtual && !method.IsFinal) select type.FullName + "." + method.Name) .ToList(); Assert.False( nonVirtualMethods.Any(), "\r\n-- Missing virtual APIs --\r\n" + string.Join("\r\n", nonVirtualMethods)); }
public void Public_inheritable_apis_should_be_virtual() { var nonVirtualMethods = (from t in GetAllTypes(TargetAssembly.GetTypes()) where t.IsVisible && !t.IsSealed && t.GetConstructors(PublicInstance).Any() && t.Namespace != null && !t.Namespace.EndsWith(".Compiled") from m in t.GetMethods(PublicInstance) where m.DeclaringType != null && m.DeclaringType.Assembly == TargetAssembly && !(m.IsVirtual && !m.IsFinal) select t.Name + "." + m.Name) .ToList(); Assert.False( nonVirtualMethods.Any(), "\r\n-- Missing virtual APIs --\r\n" + string.Join("\r\n", nonVirtualMethods)); }
public void Async_methods_should_end_with_async_suffix() { var asyncMethods = (from type in GetAllTypes(TargetAssembly.GetTypes()) where type.IsVisible from method in type.GetMethods(PublicInstance /* | BindingFlags.Static*/) where GetBasestTypeInAssembly(method.DeclaringType) == type where typeof(Task).IsAssignableFrom(method.ReturnType) select method).ToList(); var missingSuffixMethods = asyncMethods .Where(method => !method.Name.EndsWith("Async")) .Select(method => method.DeclaringType.Name + "." + method.Name) .Except(GetAsyncSuffixExceptions()) .ToList(); Assert.False( missingSuffixMethods.Any(), "\r\n-- Missing async suffix --\r\n" + string.Join("\r\n", missingSuffixMethods)); }
public void Async_methods_should_have_overload_with_cancellation_token_and_end_with_async_suffix() { var asyncMethodsWithToken = (from t in GetAllTypes(TargetAssembly.GetTypes()) where t.IsVisible from m in t.GetMethods(PublicInstance) where typeof(Task).IsAssignableFrom(m.ReturnType) && m.GetParameters().Any(pi => pi.ParameterType == typeof(CancellationToken)) select m).ToList(); var asyncMethodsWithoutToken = (from t in GetAllTypes(TargetAssembly.GetTypes()) where t.IsVisible from m in t.GetMethods(PublicInstance) where typeof(Task).IsAssignableFrom(m.ReturnType) && m.GetParameters().All(pi => pi.ParameterType != typeof(CancellationToken)) select m).ToList(); var missingOverloads = (from m1 in asyncMethodsWithoutToken where !asyncMethodsWithToken .Any(m2 => m1.Name == m2.Name && m1.ReflectedType == m2.ReflectedType) // ReSharper disable once PossibleNullReferenceException select m1.DeclaringType.Name + "." + m1.Name).ToList(); Assert.False( missingOverloads.Any(), "\r\n-- Missing async overloads --\r\n" + string.Join("\r\n", missingOverloads)); var missingSuffixMethods = asyncMethodsWithToken .Where(mi => !mi.Name.EndsWith("Async")) .Select(mi => mi.DeclaringType.Name + "." + mi.Name) .ToList(); Assert.False( missingSuffixMethods.Any(), "\r\n-- Missing async suffix --\r\n" + string.Join("\r\n", missingSuffixMethods)); }
public virtual void Public_inheritable_apis_should_be_virtual() { var nonVirtualMethods = (from type in GetAllTypes(TargetAssembly.GetTypes()) where type.GetTypeInfo().IsVisible && !type.GetTypeInfo().IsSealed && type.GetConstructors(AnyInstance).Any(c => c.IsPublic || c.IsFamily || c.IsFamilyOrAssembly) && type.Namespace?.EndsWith(".Compiled") == false && ShouldHaveVirtualMethods(type) from method in type.GetMethods(AnyInstance) where method.DeclaringType == type && !(method.IsVirtual && !method.IsFinal) && !method.Name.StartsWith("add_") && !method.Name.StartsWith("remove_") && (method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly) && method.Name != "GenerateCacheKeyCore" select type.FullName + "." + method.Name) .ToList(); Assert.False( nonVirtualMethods.Count > 0, "\r\n-- Missing virtual APIs --\r\n" + string.Join(Environment.NewLine, nonVirtualMethods)); }
public void Public_api_arguments_should_have_not_null_annotation() { var parametersMissingAttribute = (from t in GetAllTypes(TargetAssembly.GetTypes()) where t.IsVisible let ims = t.GetInterfaces().Select(t.GetInterfaceMap) from m in t.GetMethods(PublicInstance | BindingFlags.Static) .Concat <MethodBase>(t.GetConstructors()) where m.DeclaringType != null && m.DeclaringType.Assembly == TargetAssembly where t.IsInterface || !ims.Any(im => im.TargetMethods.Contains(m)) from p in m.GetParameters() where !p.ParameterType.IsValueType && !p.GetCustomAttributes() .Any( a => a.GetType().Name == "NotNullAttribute" || a.GetType().Name == "CanBeNullAttribute") select t.Name + "." + m.Name + "[" + p.Name + "]") .ToList(); Assert.False( parametersMissingAttribute.Any(), "\r\n-- Missing NotNull annotations --\r\n" + string.Join("\r\n", parametersMissingAttribute)); }