Skip to content

Commit

Permalink
Exclude generated code from ApiScanTestBase tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Oct 27, 2024
1 parent f5c0eed commit 6222faa
Showing 1 changed file with 43 additions and 38 deletions.
81 changes: 43 additions & 38 deletions src/Lucene.Net.TestFramework/Support/ApiScanTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Lucene.Net.Support;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -37,8 +38,8 @@ public abstract class ApiScanTestBase : LuceneTestCase
internal ApiScanTestBase() { } // LUCENENET: Not for use by end users

/// <summary>
/// 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).
/// </summary>
private static readonly Regex PrivateFieldName = new Regex("^_?[a-z][a-zA-Z0-9_]*$|^[A-Z0-9_]+$", RegexOptions.Compiled);
Expand Down Expand Up @@ -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.");
}

Expand Down Expand Up @@ -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().");
}

Expand Down Expand Up @@ -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)
Expand All @@ -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'.");
}
Expand Down Expand Up @@ -406,7 +407,7 @@ private static IEnumerable<string> GetInvalidPrivateFields(Assembly assembly, st
{
var result = new List<string>();

var classes = assembly.GetTypes().Where(t => t.IsClass);
var classes = GetTypesToTest(assembly).Where(t => t.IsClass);

foreach (var c in classes)
{
Expand Down Expand Up @@ -442,7 +443,7 @@ private static IEnumerable<string> GetInvalidProtectedFields(Assembly assembly)
{
var result = new List<string>();

var classes = assembly.GetTypes().Where(t => t.IsClass);
var classes = GetTypesToTest(assembly).Where(t => t.IsClass);

foreach (var c in classes)
{
Expand Down Expand Up @@ -488,11 +489,11 @@ private static IEnumerable<string> GetInvalidPublicFields(Assembly assembly, str
{
var result = new List<string>();

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;
}
Expand Down Expand Up @@ -534,7 +535,7 @@ private static IEnumerable<string> GetInvalidMethodParameterNames(Assembly assem
{
var result = new List<string>();

var classes = assembly.GetTypes().Where(t => t.IsClass);
var classes = GetTypesToTest(assembly).Where(t => t.IsClass);

foreach (var c in classes)
{
Expand Down Expand Up @@ -566,7 +567,7 @@ private static IEnumerable<string> GetInvalidInterfaceNames(Assembly assembly)
{
var result = new List<string>();

var interfaces = assembly.GetTypes().Where(t => t.IsInterface);
var interfaces = GetTypesToTest(assembly).Where(t => t.IsInterface);

foreach (var i in interfaces)
{
Expand All @@ -583,11 +584,11 @@ private static IEnumerable<string> GetInvalidClassNames(Assembly assembly)
{
var result = new List<string>();

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;
}
Expand All @@ -610,7 +611,7 @@ private static IEnumerable<string> GetPropertiesWithNoGetter(Assembly assembly)
{
var result = new List<string>();

var classes = assembly.GetTypes().Where(t => t.IsClass);
var classes = GetTypesToTest(assembly).Where(t => t.IsClass);

foreach (var c in classes)
{
Expand All @@ -632,7 +633,7 @@ private static IEnumerable<string> GetPropertiesThatReturnArray(Assembly assembl
{
var result = new List<string>();

var classes = assembly.GetTypes().Where(t => t.IsClass);
var classes = GetTypesToTest(assembly).Where(t => t.IsClass);

foreach (var c in classes)
{
Expand All @@ -650,7 +651,7 @@ private static IEnumerable<string> 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));
Expand All @@ -666,7 +667,7 @@ private static IEnumerable<string> GetProtectedFieldsContainingComparer(Assembly
{
var result = new List<string>();

var classes = assembly.GetTypes().Where(t => t.IsClass);
var classes = GetTypesToTest(assembly).Where(t => t.IsClass);

foreach (var c in classes)
{
Expand Down Expand Up @@ -698,15 +699,15 @@ private static IEnumerable<string> GetMembersContainingComparer(Assembly assembl
{
var result = new List<string>();

var types = assembly.GetTypes();
var types = GetTypesToTest(assembly);

foreach (var t in types)
{
if (ContainsComparer.IsMatch(t.Name) && t.IsVisible)
{
result.Add(t.FullName);
}

var members = t.GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

foreach (var member in members)
Expand Down Expand Up @@ -736,7 +737,7 @@ private static IEnumerable<string> GetMembersNamedSize(Assembly assembly)
{
var result = new List<string>();

var types = assembly.GetTypes();
var types = GetTypesToTest(assembly);

foreach (var t in types)
{
Expand All @@ -759,7 +760,7 @@ private static IEnumerable<string> GetMembersNamedSize(Assembly assembly)
{
result.Add(string.Concat(t.FullName, ".", member.Name));
}

}
}
}
Expand All @@ -771,7 +772,7 @@ private static IEnumerable<string> GetMembersContainingNonNetNumeric(Assembly as
{
var result = new List<string>();

var types = assembly.GetTypes();
var types = GetTypesToTest(assembly);

foreach (var t in types)
{
Expand Down Expand Up @@ -815,7 +816,7 @@ private static IEnumerable<string> GetTypesContainingNonNetNumeric(Assembly asse
{
var result = new List<string>();

var types = assembly.GetTypes();
var types = GetTypesToTest(assembly);

foreach (var t in types)
{
Expand All @@ -837,11 +838,11 @@ private static IEnumerable<string> GetMethodsThatReturnWritableArray(Assembly as
{
var result = new List<string>();

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;
}
Expand All @@ -865,8 +866,8 @@ private static IEnumerable<string> 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))
{

Expand All @@ -891,7 +892,7 @@ private static IEnumerable<string> GetPublicNullableEnumMembers(Assembly assembl
{
var result = new List<string>();

var types = assembly.GetTypes();
var types = GetTypesToTest(assembly);

foreach (var t in types)
{
Expand Down Expand Up @@ -957,16 +958,16 @@ private static IEnumerable<string> 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)"));
Expand Down Expand Up @@ -1003,7 +1004,7 @@ private static IEnumerable<string> GetMembersAcceptingOrReturningType(Type lookF
{
var result = new List<string>();

var types = assembly.GetTypes();
var types = GetTypesToTest(assembly);

foreach (var t in types)
{
Expand Down Expand Up @@ -1104,5 +1105,9 @@ private static IEnumerable<string> GetMembersAcceptingOrReturningType(Type lookF

return result.ToArray();
}

private static IEnumerable<Type> GetTypesToTest(Assembly assembly) =>
assembly.GetTypes()
.Where(t => !t.HasAttribute<GeneratedCodeAttribute>(inherit: false));
}
}

0 comments on commit 6222faa

Please sign in to comment.