Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Analyzers/MSTest.Analyzers/DynamicDataShouldBeValidAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,32 @@ private static void AnalyzeAttribute(SymbolAnalysisContext context, AttributeDat
private static (ISymbol? Member, bool AreTooMany) TryGetMember(INamedTypeSymbol declaringType, string memberName)
{
INamedTypeSymbol? currentType = declaringType;
bool disallowPrivate = false;
while (currentType is not null)
{
(ISymbol? Member, bool AreTooMany) result = TryGetMemberCore(currentType, memberName);
(ISymbol? Member, bool AreTooMany) result = TryGetMemberCore(currentType, memberName, disallowPrivate);
if (result.Member is not null || result.AreTooMany)
{
return result;
}

// Only continue to look at base types if the member is not found on the current type and we are not hit by "too many methods" rule.
currentType = currentType.BaseType;
disallowPrivate = true;
}

return (null, false);

static (ISymbol? Member, bool AreTooMany) TryGetMemberCore(INamedTypeSymbol declaringType, string memberName)
static (ISymbol? Member, bool AreTooMany) TryGetMemberCore(INamedTypeSymbol declaringType, string memberName, bool disallowPrivate)
{
ImmutableArray<ISymbol> potentialMembers = declaringType.GetMembers(memberName);
if (disallowPrivate)
{
potentialMembers = potentialMembers.RemoveAll(m => m.DeclaredAccessibility == Accessibility.Private);
}

// If we cannot find the member on the given type, report a diagnostic.
if (declaringType.GetMembers(memberName) is { Length: 0 } potentialMembers)
if (potentialMembers is { Length: 0 })
{
return (null, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting;

internal static class DynamicDataOperations
{
private const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
private const BindingFlags MemberLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
Comment thread
Youssef1313 marked this conversation as resolved.
Comment thread
Evangelink marked this conversation as resolved.
Comment thread
Evangelink marked this conversation as resolved.

public static IEnumerable<object[]> GetData(Type? dynamicDataDeclaringType, DynamicDataSourceType dynamicDataSourceType, string dynamicDataSourceName, object?[] dynamicDataSourceArguments, MethodInfo methodInfo)
{
Expand All @@ -19,15 +19,15 @@ public static IEnumerable<object[]> GetData(Type? dynamicDataDeclaringType, Dyna
{
case DynamicDataSourceType.AutoDetect:
#pragma warning disable IDE0045 // Convert to conditional expression - it becomes less readable.
if (GetPropertyConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) is { } dynamicDataPropertyInfo)
if (dynamicDataDeclaringType.GetProperty(dynamicDataSourceName, MemberLookup) is { } dynamicDataPropertyInfo)
{
obj = GetDataFromProperty(dynamicDataPropertyInfo);
}
else if (GetMethodConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) is { } dynamicDataMethodInfo)
else if (dynamicDataDeclaringType.GetMethod(dynamicDataSourceName, MemberLookup) is { } dynamicDataMethodInfo)
{
obj = GetDataFromMethod(dynamicDataMethodInfo, dynamicDataSourceArguments);
}
else if (GetFieldConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName) is { } dynamicDataFieldInfo)
else if (dynamicDataDeclaringType.GetField(dynamicDataSourceName, MemberLookup) is { } dynamicDataFieldInfo)
{
obj = GetDataFromField(dynamicDataFieldInfo);
}
Expand All @@ -39,21 +39,21 @@ public static IEnumerable<object[]> GetData(Type? dynamicDataDeclaringType, Dyna

break;
case DynamicDataSourceType.Property:
PropertyInfo property = GetPropertyConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName)
PropertyInfo property = dynamicDataDeclaringType.GetProperty(dynamicDataSourceName, MemberLookup)
?? throw new ArgumentNullException($"{DynamicDataSourceType.Property} {dynamicDataSourceName}");

obj = GetDataFromProperty(property);
break;

case DynamicDataSourceType.Method:
MethodInfo method = GetMethodConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName)
MethodInfo method = dynamicDataDeclaringType.GetMethod(dynamicDataSourceName, MemberLookup)
?? throw new ArgumentNullException($"{DynamicDataSourceType.Method} {dynamicDataSourceName}");

obj = GetDataFromMethod(method, dynamicDataSourceArguments);
break;

case DynamicDataSourceType.Field:
FieldInfo field = GetFieldConsideringInheritance(dynamicDataDeclaringType, dynamicDataSourceName)
FieldInfo field = dynamicDataDeclaringType.GetField(dynamicDataSourceName, MemberLookup)
?? throw new ArgumentNullException($"{DynamicDataSourceType.Field} {dynamicDataSourceName}");

obj = GetDataFromField(field);
Expand Down Expand Up @@ -170,58 +170,4 @@ private static bool TryGetData(object dataSource, [NotNullWhen(true)] out IEnume
data = null;
return false;
}

private static FieldInfo? GetFieldConsideringInheritance(Type type, string fieldName)
{
// NOTE: Don't use GetRuntimeField. It considers inheritance only for instance fields.
Type? currentType = type;
while (currentType is not null)
{
FieldInfo? field = currentType.GetField(fieldName, DeclaredOnlyLookup);
if (field is not null)
{
return field;
}

currentType = currentType.BaseType;
}

return null;
}

private static PropertyInfo? GetPropertyConsideringInheritance(Type type, string propertyName)
{
// NOTE: Don't use GetRuntimeProperty. It considers inheritance only for instance properties.
Type? currentType = type;
while (currentType is not null)
{
PropertyInfo? property = currentType.GetProperty(propertyName, DeclaredOnlyLookup);
if (property is not null)
{
return property;
}

currentType = currentType.BaseType;
}

return null;
}

private static MethodInfo? GetMethodConsideringInheritance(Type type, string methodName)
{
// NOTE: Don't use GetRuntimeMethod. It considers inheritance only for instance methods.
Type? currentType = type;
while (currentType is not null)
{
MethodInfo? method = currentType.GetMethod(methodName, DeclaredOnlyLookup);
if (method is not null)
{
return method;
}

currentType = currentType.BaseType;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,43 @@ public void TestMethod2(object[] o)
await VerifyCS.VerifyAnalyzerAsync(code);
}

[TestMethod]
public async Task WhenPrivateMemberIsFromBase_Diagnostic()
{
string code = """
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;

public abstract class MyTestClassBase
{
private static IEnumerable<object[]> Data => new List<object[]>();
private static IEnumerable<object[]> GetData() => new List<object[]>();
}

[TestClass]
public class MyTestClass : MyTestClassBase
{
[{|#0:DynamicData("Data")|}]
[TestMethod]
public void TestMethod1(object[] o)
{
}

[{|#1:DynamicData("GetData", DynamicDataSourceType.Method)|}]
[TestMethod]
public void TestMethod2(object[] o)
{
}
}
""";

await VerifyCS.VerifyAnalyzerAsync(
code,
VerifyCS.Diagnostic(DynamicDataShouldBeValidAnalyzer.MemberNotFoundRule).WithLocation(0).WithArguments("MyTestClass", "Data"),
VerifyCS.Diagnostic(DynamicDataShouldBeValidAnalyzer.MemberNotFoundRule).WithLocation(1).WithArguments("MyTestClass", "GetData"));
}

[TestMethod]
public async Task WhenMethodHasParameters_Diagnostic()
{
Expand Down