Skip to content

Commit 9ec9bde

Browse files
committed
More tests.
1 parent dffa11d commit 9ec9bde

4 files changed

Lines changed: 31 additions & 32 deletions

File tree

WpfAnalyzers.Test/WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEventTests/CodeFix.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public static class CodeFix
77
{
88
private static readonly RoutedEventCallbackAnalyzer Analyzer = new();
99
private static readonly RenameMemberFix Fix = new();
10-
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEvent);
10+
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(Descriptors.WPF0091CallbackNameShouldMatchEvent);
1111

1212
[Test]
1313
public static void MessageAddHandler()
@@ -61,8 +61,9 @@ private static void WrongName(object sender, RoutedEventArgs e)
6161
RoslynAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage("Rename to OnSizeChanged to match the event"), code);
6262
}
6363

64-
[Test]
65-
public static void WhenCorrectNameAddHandlerSizeChangedEvent()
64+
[TestCase("new RoutedEventHandler(↓WrongName)", "new RoutedEventHandler(OnSizeChanged)")]
65+
[TestCase("new RoutedEventHandler((o, e) => ↓WrongName(o, e))", "new RoutedEventHandler((o, e) => OnSizeChanged(o, e))")]
66+
public static void WhenCorrectNameAddHandlerSizeChangedEvent(string beforeExpression, string afterExpression)
6667
{
6768
var before = @"
6869
namespace N
@@ -82,7 +83,7 @@ private static void WrongName(object sender, RoutedEventArgs e)
8283
throw new System.NotImplementedException();
8384
}
8485
}
85-
}";
86+
}".AssertReplace("new RoutedEventHandler(↓WrongName)", beforeExpression);
8687

8788
var after = @"
8889
namespace N
@@ -102,12 +103,13 @@ private static void OnSizeChanged(object sender, RoutedEventArgs e)
102103
throw new System.NotImplementedException();
103104
}
104105
}
105-
}";
106+
}".AssertReplace("new RoutedEventHandler(OnSizeChanged)", afterExpression);
106107
RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after);
107108
}
108109

109-
[Test]
110-
public static void WhenCorrectNameRemoveHandlerSizeChangedEvent()
110+
[TestCase("new RoutedEventHandler(↓WrongName)", "new RoutedEventHandler(OnSizeChanged)")]
111+
[TestCase("new RoutedEventHandler((o, e) => ↓WrongName(o, e))", "new RoutedEventHandler((o, e) => OnSizeChanged(o, e))")]
112+
public static void WhenCorrectNameRemoveHandlerSizeChangedEvent(string beforeExpression, string afterExpression)
111113
{
112114
var before = @"
113115
namespace N
@@ -127,7 +129,7 @@ private static void WrongName(object sender, RoutedEventArgs e)
127129
throw new System.NotImplementedException();
128130
}
129131
}
130-
}";
132+
}".AssertReplace("new RoutedEventHandler(↓WrongName)", beforeExpression);
131133

132134
var after = @"
133135
namespace N
@@ -147,7 +149,7 @@ private static void OnSizeChanged(object sender, RoutedEventArgs e)
147149
throw new System.NotImplementedException();
148150
}
149151
}
150-
}";
152+
}".AssertReplace("new RoutedEventHandler(OnSizeChanged)", afterExpression);
151153
RoslynAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, before, after);
152154
}
153155

WpfAnalyzers/Analyzers/RoutedEventCallbackAnalyzer.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal class RoutedEventCallbackAnalyzer : DiagnosticAnalyzer
1515
{
1616
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
1717
Descriptors.WPF0090RegisterClassHandlerCallbackNameShouldMatchEvent,
18-
Descriptors.WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEvent,
18+
Descriptors.WPF0091CallbackNameShouldMatchEvent,
1919
Descriptors.WPF0092WrongDelegateType);
2020

2121
public override void Initialize(AnalysisContext context)
@@ -28,7 +28,7 @@ public override void Initialize(AnalysisContext context)
2828
private static void Handle(SyntaxNodeAnalysisContext context)
2929
{
3030
if (!context.IsExcludedFromAnalysis() &&
31-
context.Node is InvocationExpressionSyntax { } invocation)
31+
context.Node is InvocationExpressionSyntax invocation)
3232
{
3333
if (EventManager.RegisterClassHandler.Match(invocation, context.SemanticModel, context.CancellationToken) is { } registerClassHandler)
3434
{
@@ -57,7 +57,7 @@ private static void Handle(SyntaxNodeAnalysisContext context)
5757
{
5858
context.ReportDiagnostic(
5959
Diagnostic.Create(
60-
Descriptors.WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEvent,
60+
Descriptors.WPF0091CallbackNameShouldMatchEvent,
6161
location,
6262
nameProperties,
6363
expectedName));
@@ -78,7 +78,7 @@ private static void Handle(SyntaxNodeAnalysisContext context)
7878
{
7979
context.ReportDiagnostic(
8080
Diagnostic.Create(
81-
Descriptors.WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEvent,
81+
Descriptors.WPF0091CallbackNameShouldMatchEvent,
8282
location,
8383
nameProperties,
8484
expectedName));
@@ -113,16 +113,17 @@ private static void Handle(SyntaxNodeAnalysisContext context)
113113

114114
return null;
115115

116-
IdentifierNameSyntax? CallbackIdentifier()
116+
IdentifierNameSyntax? CallbackIdentifier() => callbackArg switch
117117
{
118-
return callbackArg switch
119-
{
120-
{ Expression: ObjectCreationExpressionSyntax { ArgumentList.Arguments: { Count: 1 } arguments } }
121-
when arguments[0].Expression is IdentifierNameSyntax name
122-
=> name,
123-
_ => null,
124-
};
125-
}
118+
{ Expression: ObjectCreationExpressionSyntax { ArgumentList.Arguments: { Count: 1 } arguments } }
119+
=> arguments[0].Expression switch
120+
{
121+
IdentifierNameSyntax name => name,
122+
ParenthesizedLambdaExpressionSyntax { Body: InvocationExpressionSyntax inner } => inner.Expression as IdentifierNameSyntax,
123+
_ => null,
124+
},
125+
_ => null,
126+
};
126127

127128
IdentifierNameSyntax? Identifier(ExpressionSyntax expression)
128129
{

WpfAnalyzers/CodeFixes/RenameMemberFix.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@ internal class RenameMemberFix : CodeFixProvider
2525
Descriptors.WPF0006CoerceValueCallbackShouldMatchRegisteredName.Id,
2626
Descriptors.WPF0007ValidateValueCallbackCallbackShouldMatchRegisteredName.Id,
2727
Descriptors.WPF0090RegisterClassHandlerCallbackNameShouldMatchEvent.Id,
28-
Descriptors.WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEvent.Id,
28+
Descriptors.WPF0091CallbackNameShouldMatchEvent.Id,
2929
Descriptors.WPF0100BackingFieldShouldMatchRegisteredName.Id,
3030
Descriptors.WPF0102EventDeclarationName.Id);
3131

3232
public override FixAllProvider? GetFixAllProvider() => null;
3333

3434
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3535
{
36-
var document = context.Document;
37-
var syntaxRoot = await document.GetSyntaxRootAsync(context.CancellationToken)
38-
.ConfigureAwait(false);
39-
var semanticModel = await document.GetSemanticModelAsync(context.CancellationToken)
40-
.ConfigureAwait(false);
36+
var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
37+
var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
4138
foreach (var diagnostic in context.Diagnostics)
4239
{
43-
if (syntaxRoot is { } &&
44-
syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start) is { Parent: { } } token &&
40+
if (syntaxRoot?.FindToken(diagnostic.Location.SourceSpan.Start) is { Parent: { } } token &&
4541
token.IsKind(SyntaxKind.IdentifierToken) &&
4642
semanticModel is { } &&
4743
semanticModel.TryGetSymbol(token, context.CancellationToken, out ISymbol? symbol) &&
@@ -51,7 +47,7 @@ semanticModel is { } &&
5147
context.RegisterCodeFix(
5248
CodeAction.Create(
5349
$"Rename to: '{newName}'.",
54-
cancellationToken => Renamer.RenameSymbolAsync(document.Project.Solution, symbol, newName, document.Project.Solution.Workspace.Options, cancellationToken),
50+
cancellationToken => Renamer.RenameSymbolAsync(context.Document.Project.Solution, symbol, newName, context.Document.Project.Solution.Workspace.Options, cancellationToken),
5551
this.GetType().FullName),
5652
diagnostic);
5753
}

WpfAnalyzers/Descriptors.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ internal static class Descriptors
474474
isEnabledByDefault: true,
475475
description: "Name the invoked method OnEventName.");
476476

477-
internal static readonly DiagnosticDescriptor WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEvent = Create(
477+
internal static readonly DiagnosticDescriptor WPF0091CallbackNameShouldMatchEvent = Create(
478478
id: "WPF0091",
479479
title: "Name the invoked method OnEventName",
480480
messageFormat: "Rename to {0} to match the event",

0 commit comments

Comments
 (0)