diff --git a/src/Analyzers/SetExplicitMockBehaviorAnalyzer.cs b/src/Analyzers/SetExplicitMockBehaviorAnalyzer.cs index 949aaa71..7f8747b7 100644 --- a/src/Analyzers/SetExplicitMockBehaviorAnalyzer.cs +++ b/src/Analyzers/SetExplicitMockBehaviorAnalyzer.cs @@ -102,10 +102,10 @@ private static void AnalyzeCore(OperationAnalysisContext context, IMethodSymbol return; } - ImmutableDictionary properties = new Dictionary(StringComparer.Ordinal) + ImmutableDictionary properties = new DiagnosticEditProperties { - { DiagnosticEditProperties.EditTypeKey, DiagnosticEditProperties.EditType.Insert.ToString() }, - { DiagnosticEditProperties.EditPositionKey, parameterMatch.Ordinal.ToString() }, + TypeOfEdit = DiagnosticEditProperties.EditType.Insert, + EditPosition = parameterMatch.Ordinal, }.ToImmutableDictionary(); // Using a method that doesn't accept a MockBehavior parameter, however there's an overload that does @@ -123,10 +123,10 @@ private static void AnalyzeCore(OperationAnalysisContext context, IMethodSymbol return; } - ImmutableDictionary properties = new Dictionary(StringComparer.Ordinal) + ImmutableDictionary properties = new DiagnosticEditProperties { - { DiagnosticEditProperties.EditTypeKey, DiagnosticEditProperties.EditType.Insert.ToString() }, - { DiagnosticEditProperties.EditPositionKey, parameterMatch.Ordinal.ToString() }, + TypeOfEdit = DiagnosticEditProperties.EditType.Insert, + EditPosition = parameterMatch.Ordinal, }.ToImmutableDictionary(); context.ReportDiagnostic(context.Operation.CreateDiagnostic(Rule, properties)); @@ -143,10 +143,10 @@ private static void AnalyzeCore(OperationAnalysisContext context, IMethodSymbol return; } - ImmutableDictionary properties = new Dictionary(StringComparer.Ordinal) + ImmutableDictionary properties = new DiagnosticEditProperties { - { DiagnosticEditProperties.EditTypeKey, DiagnosticEditProperties.EditType.Replace.ToString() }, - { DiagnosticEditProperties.EditPositionKey, parameterMatch.Ordinal.ToString() }, + TypeOfEdit = DiagnosticEditProperties.EditType.Replace, + EditPosition = parameterMatch.Ordinal, }.ToImmutableDictionary(); context.ReportDiagnostic(context.Operation.CreateDiagnostic(Rule, properties)); diff --git a/src/CodeFixes/CodeFixContextExtensions.cs b/src/CodeFixes/CodeFixContextExtensions.cs index 528376d8..9616c4af 100644 --- a/src/CodeFixes/CodeFixContextExtensions.cs +++ b/src/CodeFixes/CodeFixContextExtensions.cs @@ -9,34 +9,6 @@ public static bool TryGetEditProperties(this CodeFixContext context, [NotNullWhe { ImmutableDictionary properties = context.Diagnostics[0].Properties; - // Try parsing; if anything fails return false - editProperties = null; - if (!properties.TryGetValue(DiagnosticEditProperties.EditTypeKey, out string? editTypeString)) - { - return false; - } - - if (!properties.TryGetValue(DiagnosticEditProperties.EditPositionKey, out string? editPositionString)) - { - return false; - } - - if (!Enum.TryParse(editTypeString, out DiagnosticEditProperties.EditType editType)) - { - return false; - } - - if (!int.TryParse(editPositionString, out int editPosition)) - { - return false; - } - - editProperties = new DiagnosticEditProperties - { - TypeOfEdit = editType, - EditPosition = editPosition, - }; - - return true; + return DiagnosticEditProperties.TryGetFromImmutableDictionary(properties, out editProperties); } } diff --git a/src/Common/DiagnosticEditProperties.cs b/src/Common/DiagnosticEditProperties.cs index 3ee8efb6..b485c54a 100644 --- a/src/Common/DiagnosticEditProperties.cs +++ b/src/Common/DiagnosticEditProperties.cs @@ -1,4 +1,7 @@ -namespace Moq.Analyzers.Common; +using System.Diagnostics.CodeAnalysis; +using System.Globalization; + +namespace Moq.Analyzers.Common; internal record class DiagnosticEditProperties { @@ -30,4 +33,55 @@ internal enum EditType /// Gets the zero-based position where the edit should be applied. /// public int EditPosition { get; init; } + + /// + /// Returns the current object as an . + /// + /// The current objbect as an immutable dictionary. + public ImmutableDictionary ToImmutableDictionary() + { + return new Dictionary(StringComparer.Ordinal) + { + { EditTypeKey, TypeOfEdit.ToString() }, + { EditPositionKey, EditPosition.ToString(CultureInfo.InvariantCulture) }, + }.ToImmutableDictionary(); + } + + /// + /// Tries to convert an immuatble dictionary to a . + /// + /// The dictionary to try to convert. + /// The output edit properties if parsing suceeded, otherwise null. + /// true if parsing succeeded; false otherwise. + public static bool TryGetFromImmutableDictionary(ImmutableDictionary dictionary, [NotNullWhen(true)] out DiagnosticEditProperties? editProperties) + { + editProperties = null; + if (!dictionary.TryGetValue(EditTypeKey, out string? editTypeString)) + { + return false; + } + + if (!dictionary.TryGetValue(EditPositionKey, out string? editPositionString)) + { + return false; + } + + if (!Enum.TryParse(editTypeString, out EditType editType)) + { + return false; + } + + if (!int.TryParse(editPositionString, NumberStyles.Integer, CultureInfo.InvariantCulture, out int editPosition)) + { + return false; + } + + editProperties = new DiagnosticEditProperties + { + TypeOfEdit = editType, + EditPosition = editPosition, + }; + + return true; + } }