From 6222faad21dcb31c8244b4a3dbd05a6290692c6c Mon Sep 17 00:00:00 2001 From: Paul Irwin Date: Sun, 27 Oct 2024 14:22:16 -0600 Subject: [PATCH] Exclude generated code from ApiScanTestBase tests --- .../Support/ApiScanTestBase.cs | 81 ++++++++++--------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/src/Lucene.Net.TestFramework/Support/ApiScanTestBase.cs b/src/Lucene.Net.TestFramework/Support/ApiScanTestBase.cs index e0f9e6d12b..9fb11634e4 100644 --- a/src/Lucene.Net.TestFramework/Support/ApiScanTestBase.cs +++ b/src/Lucene.Net.TestFramework/Support/ApiScanTestBase.cs @@ -1,5 +1,6 @@ using Lucene.Net.Support; using System; +using System.CodeDom.Compiler; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -37,8 +38,8 @@ public abstract class ApiScanTestBase : LuceneTestCase internal ApiScanTestBase() { } // LUCENENET: Not for use by end users /// - /// Private fields must be upper case separated with underscores, - /// must be camelCase (optionally may be prefixed with underscore, + /// Private fields must be upper case separated with underscores, + /// must be camelCase (optionally may be prefixed with underscore, /// but it is preferred not to use the underscore to match Lucene). /// private static readonly Regex PrivateFieldName = new Regex("^_?[a-z][a-zA-Z0-9_]*$|^[A-Z0-9_]+$", RegexOptions.Compiled); @@ -199,7 +200,7 @@ public virtual void TestClassNames(Type typeFromTargetAssembly) //} Assert.IsFalse(names.Any(), names.Count() + " invalid class names detected. " + - "Class names must be Pascal case, but may not follow the interface naming " + + "Class names must be Pascal case, but may not follow the interface naming " + "convention of captial 'I' followed by another capital letter."); } @@ -235,9 +236,9 @@ public virtual void TestForPropertiesThatReturnArray(Type typeFromTargetAssembly //} Assert.IsFalse(names.Any(), names.Count() + " properties that return Array detected. " + - "Properties should generally not return Array. Change to a method (prefixed with Get) " + + "Properties should generally not return Array. Change to a method (prefixed with Get) " + "or if returning an array that can be written to was intended, decorate with the WritableArray attribute. " + - "Note that returning an array field from either a property or method means the array can be written to by " + + "Note that returning an array field from either a property or method means the array can be written to by " + "the consumer if the array is not cloned using arr.ToArray()."); } @@ -293,11 +294,11 @@ public virtual void TestForPublicMembersNamedSize(Type typeFromTargetAssembly) //} Assert.IsFalse(names.Any(), names.Count() + " member names named 'Size'. " + - "In .NET, we need to change the name 'Size' to either 'Count' or 'Length', " + + "In .NET, we need to change the name 'Size' to either 'Count' or 'Length', " + "and it should generally be made a property."); } - + //[Test, LuceneNetSpecific] public virtual void TestForPublicMembersContainingNonNetNumeric(Type typeFromTargetAssembly) @@ -312,7 +313,7 @@ public virtual void TestForPublicMembersContainingNonNetNumeric(Type typeFromTar } //} - Assert.IsFalse(names.Any(), names.Count() + " member names containing the word 'Int' not followed " + + Assert.IsFalse(names.Any(), names.Count() + " member names containing the word 'Int' not followed " + "by 16, 32, or 64, 'Long', 'Short', or 'Float' detected. " + "In .NET, we need to change to 'Short' to 'Int16', 'Int' to 'Int32', 'Long' to 'Int64', and 'Float' to 'Single'."); } @@ -406,7 +407,7 @@ private static IEnumerable GetInvalidPrivateFields(Assembly assembly, st { var result = new List(); - var classes = assembly.GetTypes().Where(t => t.IsClass); + var classes = GetTypesToTest(assembly).Where(t => t.IsClass); foreach (var c in classes) { @@ -442,7 +443,7 @@ private static IEnumerable GetInvalidProtectedFields(Assembly assembly) { var result = new List(); - var classes = assembly.GetTypes().Where(t => t.IsClass); + var classes = GetTypesToTest(assembly).Where(t => t.IsClass); foreach (var c in classes) { @@ -488,11 +489,11 @@ private static IEnumerable GetInvalidPublicFields(Assembly assembly, str { var result = new List(); - var classes = assembly.GetTypes().Where(t => t.IsClass); + var classes = GetTypesToTest(assembly).Where(t => t.IsClass); foreach (var c in classes) { - if (c.Name.StartsWith("<", StringComparison.Ordinal)) // Ignore classes produced by anonymous methods + if (c.Name.StartsWith("<", StringComparison.Ordinal)) // Ignore classes produced by anonymous methods { continue; } @@ -534,7 +535,7 @@ private static IEnumerable GetInvalidMethodParameterNames(Assembly assem { var result = new List(); - var classes = assembly.GetTypes().Where(t => t.IsClass); + var classes = GetTypesToTest(assembly).Where(t => t.IsClass); foreach (var c in classes) { @@ -566,7 +567,7 @@ private static IEnumerable GetInvalidInterfaceNames(Assembly assembly) { var result = new List(); - var interfaces = assembly.GetTypes().Where(t => t.IsInterface); + var interfaces = GetTypesToTest(assembly).Where(t => t.IsInterface); foreach (var i in interfaces) { @@ -583,11 +584,11 @@ private static IEnumerable GetInvalidClassNames(Assembly assembly) { var result = new List(); - var classes = assembly.GetTypes().Where(t => t.IsClass); + var classes = GetTypesToTest(assembly).Where(t => t.IsClass); foreach (var c in classes) { - if (c.Name.StartsWith("<", StringComparison.Ordinal)) // Ignore classes produced by anonymous methods + if (c.Name.StartsWith("<", StringComparison.Ordinal)) // Ignore classes produced by anonymous methods { continue; } @@ -610,7 +611,7 @@ private static IEnumerable GetPropertiesWithNoGetter(Assembly assembly) { var result = new List(); - var classes = assembly.GetTypes().Where(t => t.IsClass); + var classes = GetTypesToTest(assembly).Where(t => t.IsClass); foreach (var c in classes) { @@ -632,7 +633,7 @@ private static IEnumerable GetPropertiesThatReturnArray(Assembly assembl { var result = new List(); - var classes = assembly.GetTypes().Where(t => t.IsClass); + var classes = GetTypesToTest(assembly).Where(t => t.IsClass); foreach (var c in classes) { @@ -650,7 +651,7 @@ private static IEnumerable GetPropertiesThatReturnArray(Assembly assembl } var getMethod = property.GetGetMethod(); - + if (getMethod != null && getMethod.ReturnParameter != null && getMethod.ReturnParameter.ParameterType.IsArray && property.DeclaringType.Equals(c.UnderlyingSystemType)) { result.Add(string.Concat(c.FullName, ".", property.Name)); @@ -666,7 +667,7 @@ private static IEnumerable GetProtectedFieldsContainingComparer(Assembly { var result = new List(); - var classes = assembly.GetTypes().Where(t => t.IsClass); + var classes = GetTypesToTest(assembly).Where(t => t.IsClass); foreach (var c in classes) { @@ -698,7 +699,7 @@ private static IEnumerable GetMembersContainingComparer(Assembly assembl { var result = new List(); - var types = assembly.GetTypes(); + var types = GetTypesToTest(assembly); foreach (var t in types) { @@ -706,7 +707,7 @@ private static IEnumerable GetMembersContainingComparer(Assembly assembl { result.Add(t.FullName); } - + var members = t.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (var member in members) @@ -736,7 +737,7 @@ private static IEnumerable GetMembersNamedSize(Assembly assembly) { var result = new List(); - var types = assembly.GetTypes(); + var types = GetTypesToTest(assembly); foreach (var t in types) { @@ -759,7 +760,7 @@ private static IEnumerable GetMembersNamedSize(Assembly assembly) { result.Add(string.Concat(t.FullName, ".", member.Name)); } - + } } } @@ -771,7 +772,7 @@ private static IEnumerable GetMembersContainingNonNetNumeric(Assembly as { var result = new List(); - var types = assembly.GetTypes(); + var types = GetTypesToTest(assembly); foreach (var t in types) { @@ -815,7 +816,7 @@ private static IEnumerable GetTypesContainingNonNetNumeric(Assembly asse { var result = new List(); - var types = assembly.GetTypes(); + var types = GetTypesToTest(assembly); foreach (var t in types) { @@ -837,11 +838,11 @@ private static IEnumerable GetMethodsThatReturnWritableArray(Assembly as { var result = new List(); - var classes = assembly.GetTypes().Where(t => t.IsClass); + var classes = GetTypesToTest(assembly).Where(t => t.IsClass); foreach (var c in classes) { - if (c.Name.StartsWith("<", StringComparison.Ordinal)) // Ignore classes produced by anonymous methods + if (c.Name.StartsWith("<", StringComparison.Ordinal)) // Ignore classes produced by anonymous methods { continue; } @@ -865,8 +866,8 @@ private static IEnumerable GetMethodsThatReturnWritableArray(Assembly as continue; } - if (method != null && method.ReturnParameter != null - && method.ReturnParameter.ParameterType.IsArray + if (method != null && method.ReturnParameter != null + && method.ReturnParameter.ParameterType.IsArray && method.DeclaringType.Equals(c.UnderlyingSystemType)) { @@ -891,7 +892,7 @@ private static IEnumerable GetPublicNullableEnumMembers(Assembly assembl { var result = new List(); - var types = assembly.GetTypes(); + var types = GetTypesToTest(assembly); foreach (var t in types) { @@ -957,16 +958,16 @@ private static IEnumerable GetPublicNullableEnumMembers(Assembly assembl } } } - else if (member.MemberType == MemberTypes.Property - && Nullable.GetUnderlyingType(((PropertyInfo)member).PropertyType) != null - && ((PropertyInfo)member).PropertyType.GetGenericArguments()[0].IsEnum + else if (member.MemberType == MemberTypes.Property + && Nullable.GetUnderlyingType(((PropertyInfo)member).PropertyType) != null + && ((PropertyInfo)member).PropertyType.GetGenericArguments()[0].IsEnum && IsNonPrivateProperty((PropertyInfo)member)) { result.Add(string.Concat(t.FullName, ".", member.Name)); } - else if (member.MemberType == MemberTypes.Field - && Nullable.GetUnderlyingType(((FieldInfo)member).FieldType) != null - && ((FieldInfo)member).FieldType.GetGenericArguments()[0].IsEnum + else if (member.MemberType == MemberTypes.Field + && Nullable.GetUnderlyingType(((FieldInfo)member).FieldType) != null + && ((FieldInfo)member).FieldType.GetGenericArguments()[0].IsEnum && (((FieldInfo)member).IsFamily || ((FieldInfo)member).IsFamilyOrAssembly)) { result.Add(string.Concat(t.FullName, ".", member.Name, " (field)")); @@ -1003,7 +1004,7 @@ private static IEnumerable GetMembersAcceptingOrReturningType(Type lookF { var result = new List(); - var types = assembly.GetTypes(); + var types = GetTypesToTest(assembly); foreach (var t in types) { @@ -1104,5 +1105,9 @@ private static IEnumerable GetMembersAcceptingOrReturningType(Type lookF return result.ToArray(); } + + private static IEnumerable GetTypesToTest(Assembly assembly) => + assembly.GetTypes() + .Where(t => !t.HasAttribute(inherit: false)); } }