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
2 changes: 1 addition & 1 deletion src/Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static void RegisterCompilationStartAction(CompilationStartAnalysisConte
// Look for the Mock.As() method and provide it to Analyze to avoid looking it up multiple times.
#pragma warning disable ECS0900 // Minimize boxing and unboxing
ImmutableArray<IMethodSymbol> asMethods = mockTypes
.SelectMany(mockType => mockType.GetMembers(WellKnownTypeNames.As))
.SelectMany(mockType => mockType.GetMembers(WellKnownMoqNames.AsMethodName))
.OfType<IMethodSymbol>()
.Where(method => method.IsGenericMethod)
.ToImmutableArray();
Expand Down
32 changes: 16 additions & 16 deletions src/Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private static bool IsExpressionMockBehavior(SyntaxNodeAnalysisContext context,
if (expression is MemberAccessExpressionSyntax memberAccessExpressionSyntax)
{
if (memberAccessExpressionSyntax.Expression is IdentifierNameSyntax identifierNameSyntax
&& string.Equals(identifierNameSyntax.Identifier.ValueText, WellKnownTypeNames.MockBehavior, StringComparison.Ordinal))
&& string.Equals(identifierNameSyntax.Identifier.ValueText, WellKnownMoqNames.MockBehaviorTypeName, StringComparison.Ordinal))
{
return true;
}
Expand Down Expand Up @@ -107,14 +107,14 @@ private static bool IsExpressionMockBehavior(SyntaxNodeAnalysisContext context,
}

if (typeSymbol != null
&& string.Equals(typeSymbol.Name, WellKnownTypeNames.MockBehavior, StringComparison.Ordinal))
&& string.Equals(typeSymbol.Name, WellKnownMoqNames.MockBehaviorTypeName, StringComparison.Ordinal))
{
return true;
}
}

// Crude fallback to check if the expression is a Moq.MockBehavior enum
if (expression.ToString().StartsWith(WellKnownTypeNames.MoqBehavior, StringComparison.Ordinal))
if (expression.ToString().StartsWith(WellKnownMoqNames.FullyQualifiedMoqBehaviorTypeName, StringComparison.Ordinal))
{
return true;
}
Expand Down Expand Up @@ -205,18 +205,18 @@ private static void AnalyzeInstanceCall(SyntaxNodeAnalysisContext context)
return;
}

switch (genericNameSyntax.Identifier.Value)
if (genericNameSyntax.Identifier.Value is not string genericNameSyntaxIdentifierValue)
{
case WellKnownTypeNames.Create:
AnalyzeInvocation(context, invocationExpressionSyntax, WellKnownTypeNames.MockFactory, true, true);
break;

case WellKnownTypeNames.Of:
AnalyzeInvocation(context, invocationExpressionSyntax, WellKnownTypeNames.MockName, false, true);
break;
return;
}

default:
return;
if (string.Equals(genericNameSyntaxIdentifierValue, WellKnownMoqNames.CreateMethodName, StringComparison.Ordinal))
{
AnalyzeInvocation(context, invocationExpressionSyntax, WellKnownMoqNames.MockFactoryTypeName, true, true);
}
else if (string.Equals(genericNameSyntaxIdentifierValue, WellKnownMoqNames.OfMethodName, StringComparison.Ordinal))
{
AnalyzeInvocation(context, invocationExpressionSyntax, WellKnownMoqNames.MockTypeName, false, true);
}
}

Expand Down Expand Up @@ -252,7 +252,7 @@ private static void AnalyzeInvocation(

// We are calling MockRepository.Create<T> or Mock.Of<T>, determine which
ArgumentListSyntax? argumentList = null;
if (WellKnownTypeNames.Of.Equals(method.Name, StringComparison.Ordinal))
if (WellKnownMoqNames.OfMethodName.Equals(method.Name, StringComparison.Ordinal))
{
// Mock.Of<T> can specify condition for construction and MockBehavior, but
// cannot specify constructor parameters
Expand Down Expand Up @@ -286,7 +286,7 @@ private static void AnalyzeNewObject(SyntaxNodeAnalysisContext context)
// Quick check
if (!string.Equals(
genericNameSyntax.Identifier.ValueText,
WellKnownTypeNames.MockName,
WellKnownMoqNames.MockTypeName,
StringComparison.Ordinal))
{
return;
Expand All @@ -297,7 +297,7 @@ private static void AnalyzeNewObject(SyntaxNodeAnalysisContext context)

if (symbolInfo.Symbol is not IMethodSymbol mockConstructorMethod
|| mockConstructorMethod.MethodKind != MethodKind.Constructor
|| !string.Equals(mockConstructorMethod.ContainingType.ConstructedFrom.ContainingSymbol.Name, WellKnownTypeNames.Moq, StringComparison.Ordinal))
|| !string.Equals(mockConstructorMethod.ContainingType.ConstructedFrom.ContainingSymbol.Name, WellKnownMoqNames.MoqSymbolName, StringComparison.Ordinal))
{
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Analyzers/SetExplicitMockBehaviorAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static void RegisterCompilationStartAction(CompilationStartAnalysisConte
}

// Look for the MockBehavior type and provide it to Analyze to avoid looking it up multiple times.
INamedTypeSymbol? mockBehaviorSymbol = context.Compilation.GetTypeByMetadataName(WellKnownTypeNames.MoqBehavior);
INamedTypeSymbol? mockBehaviorSymbol = context.Compilation.GetTypeByMetadataName(WellKnownMoqNames.FullyQualifiedMoqBehaviorTypeName);
if (mockBehaviorSymbol is null)
{
return;
Expand All @@ -51,7 +51,7 @@ private static void RegisterCompilationStartAction(CompilationStartAnalysisConte
// Look for the Mock.Of() method and provide it to Analyze to avoid looking it up multiple times.
#pragma warning disable ECS0900 // Minimize boxing and unboxing
ImmutableArray<IMethodSymbol> ofMethods = mockTypes
.SelectMany(mockType => mockType.GetMembers(WellKnownTypeNames.Of))
.SelectMany(mockType => mockType.GetMembers(WellKnownMoqNames.OfMethodName))
.OfType<IMethodSymbol>()
.Where(method => method.IsGenericMethod)
.ToImmutableArray();
Expand Down
2 changes: 1 addition & 1 deletion src/Common/CompilationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public static ImmutableArray<INamedTypeSymbol> GetTypesByMetadataNames(this Comp
/// </returns>
public static ImmutableArray<INamedTypeSymbol> GetMoqMock(this Compilation compilation)
{
return compilation.GetTypesByMetadataNames([WellKnownTypeNames.MoqMock, WellKnownTypeNames.MoqMock1, WellKnownTypeNames.MoqRepository]);
return compilation.GetTypesByMetadataNames([WellKnownMoqNames.FullyQualifiedMoqMockTypeName, WellKnownMoqNames.FullyQualifiedMoqMock1TypeName, WellKnownMoqNames.FullyQualifiedMoqRepositoryTypeName]);
}
}
4 changes: 2 additions & 2 deletions src/Common/MoqMethodDescriptorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
/// </remarks>
internal abstract class MoqMethodDescriptorBase
{
private static readonly string ContainingNamespace = WellKnownTypeNames.Moq;
private static readonly string ContainingType = WellKnownTypeNames.MockName;
private static readonly string ContainingNamespace = WellKnownMoqNames.MoqNamespace;
private static readonly string ContainingType = WellKnownMoqNames.MockTypeName;

public abstract bool IsMatch(SemanticModel semanticModel, MemberAccessExpressionSyntax memberAccessSyntax, CancellationToken cancellationToken);

Expand Down
74 changes: 74 additions & 0 deletions src/Common/WellKnownMoqNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace Moq.Analyzers.Common;

/// <summary>
/// Provides well-known names and fully qualified names for commonly used Moq types, namespaces, and members.
/// </summary>
internal static class WellKnownMoqNames
{
/// <summary>
/// Represents the namespace for the Moq library.
/// </summary>
internal static readonly string MoqNamespace = "Moq";

/// <summary>
/// Symbol name for Moq.
/// </summary>
internal static readonly string MoqSymbolName = "Moq";

/// <summary>
/// The name of the 'Moq.Mock' type.
/// </summary>
internal static readonly string MockTypeName = "Mock";

/// <summary>
/// The name of the 'Moq.MockBehavior' type.
/// This type specifies the behavior of the mock (Strict, Loose, etc.).
/// </summary>
internal static readonly string MockBehaviorTypeName = "MockBehavior";

/// <summary>
/// The name of the 'Moq.MockFactory' type.
/// This factory is used for creating multiple mock objects with a shared configuration.
/// </summary>
internal static readonly string MockFactoryTypeName = "MockFactory";

/// <summary>
/// Fully qualified name for the 'Moq.Mock' type.
/// </summary>
internal static readonly string FullyQualifiedMoqMockTypeName = $"{MoqNamespace}.{MockTypeName}";

/// <summary>
/// Fully qualified name for the generic version of 'Moq.Mock{T}'.
/// Represents mocks for specific types.
/// </summary>
internal static readonly string FullyQualifiedMoqMock1TypeName = $"{FullyQualifiedMoqMockTypeName}`1";

/// <summary>
/// Fully qualified name for the 'Moq.MockBehavior' type.
/// </summary>
internal static readonly string FullyQualifiedMoqBehaviorTypeName = $"{MoqNamespace}.{MockBehaviorTypeName}";

/// <summary>
/// Fully qualified name for the 'Moq.MockRepository' type.
/// This type acts as a container for multiple mocks and shared mock configurations.
/// </summary>
internal static readonly string FullyQualifiedMoqRepositoryTypeName = $"{MoqNamespace}.MockRepository";

/// <summary>
/// Represents the method name for the `As` method in Moq.
/// This method is used to cast mocks to interfaces.
/// </summary>
internal static readonly string AsMethodName = "As";

/// <summary>
/// Represents the method name for the `Create` method in Moq.
/// This method is used to create instances of mocks.
/// </summary>
internal static readonly string CreateMethodName = "Create";

/// <summary>
/// Represents the method name for the `Of` method in Moq.
/// This method is used to create a mock from a type without directly specifying the constructor.
/// </summary>
internal static readonly string OfMethodName = "Of";
}
18 changes: 0 additions & 18 deletions src/Common/WellKnownTypeNames.cs

This file was deleted.