Skip to content

Commit

Permalink
Extend support to ignore valuetask on setup method
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmurillo committed Feb 4, 2025
1 parent c5439bb commit 9761db5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
{
case IPropertySymbol propertySymbol:
// Check if the property is Task<T>.Result and skip diagnostic if it is
if (IsTaskResultProperty(propertySymbol, knownSymbols))
if (IsTaskOrValueResultProperty(propertySymbol, knownSymbols))
{
return;
}
Expand All @@ -97,6 +97,11 @@ private static void Analyze(SyntaxNodeAnalysisContext context)
context.ReportDiagnostic(diagnostic);
}

private static bool IsTaskOrValueResultProperty(IPropertySymbol propertySymbol, MoqKnownSymbols knownSymbols)
{
return IsTaskResultProperty(propertySymbol, knownSymbols) || IsValueTaskResultProperty(propertySymbol, knownSymbols);
}

private static bool IsTaskResultProperty(IPropertySymbol propertySymbol, MoqKnownSymbols knownSymbols)
{
// Check if the property is named "Result"
Expand All @@ -108,11 +113,24 @@ private static bool IsTaskResultProperty(IPropertySymbol propertySymbol, MoqKnow
// Check if the containing type is Task<T>
INamedTypeSymbol? taskOfTType = knownSymbols.Task1;

if (taskOfTType == null)
return taskOfTType != null &&
// If Task<T> type cannot be found, we skip it

Check failure on line 117 in src/Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check failure on line 117 in src/Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check failure on line 117 in src/Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Check failure on line 117 in src/Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

SymbolEqualityComparer.Default.Equals(propertySymbol.ContainingType, taskOfTType);
}

private static bool IsValueTaskResultProperty(IPropertySymbol propertySymbol, MoqKnownSymbols knownSymbols)
{
// Check if the property is named "Result"
if (!string.Equals(propertySymbol.Name, "Result", StringComparison.Ordinal))
{
return false; // If Task<T> type cannot be found, we skip it
return false;
}

return SymbolEqualityComparer.Default.Equals(propertySymbol.ContainingType, taskOfTType);
// Check if the containing type is ValueTask<T>
INamedTypeSymbol? valueTaskOfType = knownSymbols.ValueTask1;

return valueTaskOfType != null &&
// If ValueTask<T> type cannot be found, we skip it

Check failure on line 133 in src/Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check failure on line 133 in src/Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check failure on line 133 in src/Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Check failure on line 133 in src/Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

SymbolEqualityComparer.Default.Equals(propertySymbol.ContainingType, valueTaskOfType);
}
}
10 changes: 0 additions & 10 deletions src/Common/WellKnown/KnownSymbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,11 @@ public KnownSymbols(Compilation compilation)
{
}

/// <summary>
/// Gets the class <c>System.Threading.Tasks.Task</c>.
/// </summary>
public INamedTypeSymbol? Task => TypeProvider.GetOrCreateTypeByMetadataName("System.Threading.Tasks.Task");

/// <summary>
/// Gets the class <c>System.Threading.Tasks.Task&lt;T&gt;</c>.
/// </summary>
public INamedTypeSymbol? Task1 => TypeProvider.GetOrCreateTypeByMetadataName("System.Threading.Tasks.Task`1");

/// <summary>
/// Gets the class <c>System.Threading.Tasks.ValueTask</c>.
/// </summary>
public INamedTypeSymbol? ValueTask => TypeProvider.GetOrCreateTypeByMetadataName("System.Threading.Tasks.ValueTask");

/// <summary>
/// Gets the class <c>System.Threading.Tasks.ValueTask&lt;T&gt;</c>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public static IEnumerable<object[]> TestData()
["""new Mock<ISampleInterface>().Setup(x => x.TestProperty);"""],
["""new Mock<SampleClass>().Setup(x => x.Calculate(It.IsAny<int>(), It.IsAny<int>()));"""],
["""new Mock<SampleClass>().Setup(x => x.DoSth());"""],
["""new Mock<IParameterlessAsyncMethod>().Setup(x => x.DoSomethingAsync().Result).Returns(true);"""],
["""new Mock<IAsyncMethods>().Setup(x => x.DoSomethingAsync());"""],
["""new Mock<IAsyncMethods>().Setup(x => x.GetBooleanAsync().Result).Returns(true);"""],
["""new Mock<IValueTaskMethods>().Setup(x => x.DoSomethingValueTask());"""],
["""new Mock<IValueTaskMethods>().Setup(x => x.GetNumberAsync()).Returns(ValueTask.FromResult(42));"""],
}.WithNamespaces().WithMoqReferenceAssemblyGroups();
}

Expand All @@ -33,9 +36,16 @@ public interface ISampleInterface
int TestProperty { get; set; }
}
public interface IParameterlessAsyncMethod
public interface IAsyncMethods
{
Task<bool> DoSomethingAsync();
Task DoSomethingAsync();
Task<bool> GetBooleanAsync();
}
public interface IValueTaskMethods
{
ValueTask DoSomethingValueTask();
ValueTask<int> GetNumberAsync();
}
public abstract class BaseSampleClass
Expand Down

0 comments on commit 9761db5

Please sign in to comment.