diff --git a/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs b/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs index a63be6062..55c7bea4f 100644 --- a/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs +++ b/Source/Moq.Analyzers/AsShouldBeUsedOnlyForInterfaceAnalyzer.cs @@ -3,16 +3,20 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class AsShouldBeUsedOnlyForInterfaceAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1300"; + private const string Title = "Moq: Invalid As type parameter"; + private const string Message = "Mock.As() should take interfaces only"; + private static readonly MoqMethodDescriptorBase MoqAsMethodDescriptor = new MoqAsMethodDescriptor(); private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.AsShouldBeUsedOnlyForInterfaceId, - Diagnostics.AsShouldBeUsedOnlyForInterfaceTitle, - Diagnostics.AsShouldBeUsedOnlyForInterfaceMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Error, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.AsShouldBeUsedOnlyForInterfaceId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); diff --git a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs index 5212b0ec7..5adb9d677 100644 --- a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs +++ b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs @@ -3,14 +3,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class CallbackSignatureShouldMatchMockedMethodAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1100"; + private const string Title = "Moq: Bad callback parameters"; + private const string Message = "Callback signature must match the signature of the mocked method"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.CallbackSignatureShouldMatchMockedMethodId, - Diagnostics.CallbackSignatureShouldMatchMockedMethodTitle, - Diagnostics.CallbackSignatureShouldMatchMockedMethodMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.CallbackSignatureShouldMatchMockedMethodId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs index 5e88c8794..938cb6e98 100644 --- a/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs +++ b/Source/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodCodeFix.cs @@ -12,7 +12,7 @@ public class CallbackSignatureShouldMatchMockedMethodCodeFix : CodeFixProvider { public sealed override ImmutableArray FixableDiagnosticIds { - get { return ImmutableArray.Create(Diagnostics.CallbackSignatureShouldMatchMockedMethodId); } + get { return ImmutableArray.Create(CallbackSignatureShouldMatchMockedMethodAnalyzer.RuleId); } } public sealed override FixAllProvider GetFixAllProvider() diff --git a/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs b/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs index 5e7fb8368..8496b235d 100644 --- a/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs +++ b/Source/Moq.Analyzers/ConstructorArgumentsShouldMatchAnalyzer.cs @@ -5,14 +5,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class ConstructorArgumentsShouldMatchAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1002"; + private const string Title = "Moq: No matching constructor"; + private const string Message = "Parameters provided into mock do not match any existing constructors"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.ConstructorArgumentsShouldMatchId, - Diagnostics.ConstructorArgumentsShouldMatchTitle, - Diagnostics.ConstructorArgumentsShouldMatchMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.ConstructorArgumentsShouldMatchId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/DiagnosticCategory.cs b/Source/Moq.Analyzers/DiagnosticCategory.cs new file mode 100644 index 000000000..b80e27bf5 --- /dev/null +++ b/Source/Moq.Analyzers/DiagnosticCategory.cs @@ -0,0 +1,6 @@ +namespace Moq.Analyzers; + +internal static class DiagnosticCategory +{ + public static string Moq { get; } = "Moq"; +} diff --git a/Source/Moq.Analyzers/Diagnostics.cs b/Source/Moq.Analyzers/Diagnostics.cs deleted file mode 100644 index 008f6f2c7..000000000 --- a/Source/Moq.Analyzers/Diagnostics.cs +++ /dev/null @@ -1,38 +0,0 @@ -namespace Moq.Analyzers; - -internal static class Diagnostics -{ - internal const string Category = "Moq"; - - internal const string NoSealedClassMocksId = "Moq1000"; - internal const string NoSealedClassMocksTitle = "Moq: Sealed class mocked"; - internal const string NoSealedClassMocksMessage = "Sealed classes cannot be mocked."; - - internal const string NoConstructorArgumentsForInterfaceMockId = "Moq1001"; - internal const string NoConstructorArgumentsForInterfaceMockTitle = "Moq: Parameters specified for mocked interface"; - internal const string NoConstructorArgumentsForInterfaceMockMessage = "Mocked interfaces cannot have constructor parameters."; - - internal const string ConstructorArgumentsShouldMatchId = "Moq1002"; - internal const string ConstructorArgumentsShouldMatchTitle = "Moq: No matching constructor"; - internal const string ConstructorArgumentsShouldMatchMessage = "Parameters provided into mock do not match any existing constructors."; - - internal const string CallbackSignatureShouldMatchMockedMethodId = "Moq1100"; - internal const string CallbackSignatureShouldMatchMockedMethodTitle = "Moq: Bad callback parameters"; - internal const string CallbackSignatureShouldMatchMockedMethodMessage = "Callback signature must match the signature of the mocked method."; - - internal const string NoMethodsInPropertySetupId = "Moq1101"; - internal const string NoMethodsInPropertySetupTitle = "Moq: Property setup used for a method"; - internal const string NoMethodsInPropertySetupMessage = "SetupGet/SetupSet should be used for properties, not for methods."; - - internal const string SetupShouldBeUsedOnlyForOverridableMembersId = "Moq1200"; - internal const string SetupShouldBeUsedOnlyForOverridableMembersTitle = "Moq: Invalid setup parameter"; - internal const string SetupShouldBeUsedOnlyForOverridableMembersMessage = "Setup should be used only for overridable members."; - - internal const string SetupShouldNotIncludeAsyncResultId = "Moq1201"; - internal const string SetupShouldNotIncludeAsyncResultTitle = SetupShouldBeUsedOnlyForOverridableMembersTitle; - internal const string SetupShouldNotIncludeAsyncResultMessage = "Setup of async methods should use ReturnsAsync instead of .Result"; - - internal const string AsShouldBeUsedOnlyForInterfaceId = "Moq1300"; - internal const string AsShouldBeUsedOnlyForInterfaceTitle = "Moq: Invalid As type parameter"; - internal const string AsShouldBeUsedOnlyForInterfaceMessage = "Mock.As() should take interfaces only"; -} diff --git a/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs b/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs index 46da7c415..591e33919 100644 --- a/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs +++ b/Source/Moq.Analyzers/NoConstructorArgumentsForInterfaceMockAnalyzer.cs @@ -5,14 +5,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class NoConstructorArgumentsForInterfaceMockAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1001"; + private const string Title = "Moq: Parameters specified for mocked interface"; + private const string Message = "Mocked interfaces cannot have constructor parameters"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.NoConstructorArgumentsForInterfaceMockId, - Diagnostics.NoConstructorArgumentsForInterfaceMockTitle, - Diagnostics.NoConstructorArgumentsForInterfaceMockMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.NoConstructorArgumentsForInterfaceMockId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs b/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs index 1c870bee8..871d49159 100644 --- a/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs +++ b/Source/Moq.Analyzers/NoMethodsInPropertySetupAnalyzer.cs @@ -3,14 +3,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class NoMethodsInPropertySetupAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1101"; + private const string Title = "Moq: Property setup used for a method"; + private const string Message = "SetupGet/SetupSet should be used for properties, not for methods"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.NoMethodsInPropertySetupId, - Diagnostics.NoMethodsInPropertySetupTitle, - Diagnostics.NoMethodsInPropertySetupMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.NoMethodsInPropertySetupId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs b/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs index 6f252d5d4..4e3a74b8c 100644 --- a/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs +++ b/Source/Moq.Analyzers/NoSealedClassMocksAnalyzer.cs @@ -3,14 +3,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class NoSealedClassMocksAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1000"; + private const string Title = "Moq: Sealed class mocked"; + private const string Message = "Sealed classes cannot be mocked"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.NoSealedClassMocksId, - Diagnostics.NoSealedClassMocksTitle, - Diagnostics.NoSealedClassMocksMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Warning, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.NoSealedClassMocksId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics { diff --git a/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs b/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs index 8d24a6766..f6bec1cde 100644 --- a/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs +++ b/Source/Moq.Analyzers/SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs @@ -3,14 +3,18 @@ namespace Moq.Analyzers; [DiagnosticAnalyzer(LanguageNames.CSharp)] public class SetupShouldBeUsedOnlyForOverridableMembersAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1200"; + private const string Title = "Moq: Invalid setup parameter"; + private const string Message = "Setup should be used only for overridable members"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.SetupShouldBeUsedOnlyForOverridableMembersId, - Diagnostics.SetupShouldBeUsedOnlyForOverridableMembersTitle, - Diagnostics.SetupShouldBeUsedOnlyForOverridableMembersMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Error, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.SetupShouldBeUsedOnlyForOverridableMembersId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); diff --git a/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs b/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs index 0cf4b111b..e0c0606c1 100644 --- a/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs +++ b/Source/Moq.Analyzers/SetupShouldNotIncludeAsyncResultAnalyzer.cs @@ -3,14 +3,18 @@ [DiagnosticAnalyzer(LanguageNames.CSharp)] public class SetupShouldNotIncludeAsyncResultAnalyzer : DiagnosticAnalyzer { + internal const string RuleId = "Moq1201"; + private const string Title = "Moq: Invalid setup parameter"; + private const string Message = "Setup of async methods should use ReturnsAsync instead of .Result"; + private static readonly DiagnosticDescriptor Rule = new( - Diagnostics.SetupShouldNotIncludeAsyncResultId, - Diagnostics.SetupShouldNotIncludeAsyncResultTitle, - Diagnostics.SetupShouldNotIncludeAsyncResultMessage, - Diagnostics.Category, + RuleId, + Title, + Message, + DiagnosticCategory.Moq, DiagnosticSeverity.Error, isEnabledByDefault: true, - helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{Diagnostics.SetupShouldNotIncludeAsyncResultId}.md"); + helpLinkUri: $"https://github.com/rjmurillo/moq.analyzers/blob/main/docs/rules/{RuleId}.md"); public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule);