Skip to content

Commit dffa11d

Browse files
committed
More tests.
1 parent f0e42eb commit dffa11d

6 files changed

Lines changed: 77 additions & 5 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace ValidCode.RoutedEvents;
2+
3+
using System;
4+
using System.Windows;
5+
6+
internal static class RoutedEventHelper
7+
{
8+
internal static void UpdateHandler(this UIElement element, RoutedEvent routedEvent, Delegate handler)
9+
{
10+
element.RemoveHandler(routedEvent, handler);
11+
element.AddHandler(routedEvent, handler);
12+
}
13+
14+
internal static void UpdateHandler(this UIElement element, RoutedEvent routedEvent, Delegate handler, bool handledEventsToo)
15+
{
16+
element.RemoveHandler(routedEvent, handler);
17+
element.AddHandler(routedEvent, handler, handledEventsToo);
18+
}
19+
}

ValidCode/RoutedEvents/WithCustomEventHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ public event ValueChangedEventHandler ValueChanged
2121
}
2222
}
2323
}
24+

WpfAnalyzers.Test/ReproBox.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public static class ReproBox
2020
.ToArray();
2121

2222
private static readonly Solution Solution = CodeFactory.CreateSolution(
23-
new FileInfo("C:\\Git\\_GuOrg\\Gu.Wpf.DataGrid2D\\Gu.Wpf.DataGrid2D\\Gu.Wpf.DataGrid2D.csproj"));
23+
new FileInfo("C:\\Git\\_GuOrg\\Gu.Wpf.DataGrid2D\\Gu.Wpf.DataGrid2D\\Gu.Wpf.DataGrid2D.csproj"),
24+
Settings.Default.WithCompilationOptions(Settings.Default.CompilationOptions.WithSuppressedDiagnostics("CS8019", "CS8602", "CS8603", "CS8604", "CS8622", "CS8765")));
2425

2526
[TestCaseSource(nameof(AllAnalyzers))]
2627
public static void SolutionRepro(DiagnosticAnalyzer analyzer)
@@ -35,7 +36,6 @@ public static void Repro(DiagnosticAnalyzer analyzer)
3536
namespace N
3637
{
3738
using System;
38-
using Window = System.Windows.Window;
3939
4040
class C
4141
{

WpfAnalyzers.Test/WPF0091AddAndRemoveHandlerCallbackNameShouldMatchEventTests/Valid.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,32 @@ private static void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
205205
throw new System.NotImplementedException();
206206
}
207207
}
208+
}";
209+
RoslynAssert.Valid(Analyzer, code);
210+
}
211+
212+
[Test]
213+
public static void RoutedEventHelper()
214+
{
215+
var code = @"
216+
namespace ValidCode.RoutedEvents;
217+
218+
using System;
219+
using System.Windows;
220+
221+
internal static class RoutedEventHelper
222+
{
223+
internal static void UpdateHandler(this UIElement element, RoutedEvent routedEvent, Delegate handler)
224+
{
225+
element.RemoveHandler(routedEvent, handler);
226+
element.AddHandler(routedEvent, handler);
227+
}
228+
229+
internal static void UpdateHandler(this UIElement element, RoutedEvent routedEvent, Delegate handler, bool handledEventsToo)
230+
{
231+
element.RemoveHandler(routedEvent, handler);
232+
element.AddHandler(routedEvent, handler, handledEventsToo);
233+
}
208234
}";
209235
RoslynAssert.Valid(Analyzer, code);
210236
}

WpfAnalyzers.Test/WPF0092RegisterClassHandlerDelegateType/Valid.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,32 @@ public event RoutedEventHandler ValueChanged
8989
add { this.AddHandler(ValueChangedEvent, value); }
9090
remove { this.RemoveHandler(ValueChangedEvent, value); }
9191
}
92+
}";
93+
RoslynAssert.Valid(Analyzer, code);
94+
}
95+
96+
[Test]
97+
public static void RoutedEventHelper()
98+
{
99+
var code = @"
100+
namespace ValidCode.RoutedEvents;
101+
102+
using System;
103+
using System.Windows;
104+
105+
internal static class RoutedEventHelper
106+
{
107+
internal static void UpdateHandler(this UIElement element, RoutedEvent routedEvent, Delegate handler)
108+
{
109+
element.RemoveHandler(routedEvent, handler);
110+
element.AddHandler(routedEvent, handler);
111+
}
112+
113+
internal static void UpdateHandler(this UIElement element, RoutedEvent routedEvent, Delegate handler, bool handledEventsToo)
114+
{
115+
element.RemoveHandler(routedEvent, handler);
116+
element.AddHandler(routedEvent, handler, handledEventsToo);
117+
}
92118
}";
93119
RoslynAssert.Valid(Analyzer, code);
94120
}

WpfAnalyzers/Analyzers/RoutedEventCallbackAnalyzer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ private static void Handle(SyntaxNodeAnalysisContext context)
117117
{
118118
return callbackArg switch
119119
{
120-
{ Expression: IdentifierNameSyntax { Identifier.ValueText: "value" } } => null,
121120
{ Expression: ObjectCreationExpressionSyntax { ArgumentList.Arguments: { Count: 1 } arguments } }
122-
=> Identifier(arguments[0].Expression),
123-
_ => Identifier(callbackArg.Expression),
121+
when arguments[0].Expression is IdentifierNameSyntax name
122+
=> name,
123+
_ => null,
124124
};
125125
}
126126

0 commit comments

Comments
 (0)