1-
2- namespace ByteAether . WeakEvent . Tests ;
1+ namespace ByteAether . WeakEvent . Tests ;
32
43public class WeakEventGenericTests
54{
@@ -21,7 +20,7 @@ public void Unsubscribe_NullHandler_ThrowsArgumentNullException()
2120 public void Unsubscribe_NonExistentHandler_DoesNotThrow ( )
2221 {
2322 var weakEvent = new WeakEvent < string > ( ) ;
24- Action < string > handler = msg => { } ;
23+ Action < string > handler = _ => { } ;
2524 var exception = Record . Exception ( ( ) => Assert . False ( weakEvent . Unsubscribe ( handler ) ) ) ;
2625 Assert . Null ( exception ) ;
2726 }
@@ -32,12 +31,12 @@ public async Task PublishAsync_WithCancelledToken_ThrowsOperationCanceledExcepti
3231 var weakEvent = new WeakEvent < string > ( ) ;
3332 var handlerInvoked = false ;
3433 // Subscribe a handler that would set the flag to true.
35- weakEvent . Subscribe ( eventData => handlerInvoked = true ) ;
34+ weakEvent . Subscribe ( _ => handlerInvoked = true ) ;
3635 // Cancel the token immediately so that any wait operation observes the cancellation
3736 using var cts = new CancellationTokenSource ( ) ;
38- cts . Cancel ( ) ;
37+ await cts . CancelAsync ( ) ;
3938
40- // Assert that publishing an event with the cancelled token throws the expected exception.
39+ // Assert that publishing an event with the canceled token throws the expected exception.
4140 await Assert . ThrowsAnyAsync < OperationCanceledException > (
4241 ( ) => weakEvent . PublishAsync ( "Test event" , cts . Token )
4342 ) ;
@@ -53,12 +52,14 @@ void syncHandler(int x)
5352 {
5453 count += 1 * x ;
5554 }
55+
5656 async Task asyncHandler ( int x )
5757 {
5858 count += 2 * x ;
5959 await Task . CompletedTask ;
6060 }
61- async Task asyncHandlerCT ( int x , CancellationToken ct )
61+
62+ async Task asyncHandlerCt ( int x , CancellationToken ct )
6263 {
6364 count += 4 * x ;
6465 await Task . CompletedTask ;
@@ -68,11 +69,11 @@ async Task asyncHandlerCT(int x, CancellationToken ct)
6869
6970 weakEvent . Subscribe ( syncHandler ) ;
7071 weakEvent . Subscribe ( asyncHandler ) ;
71- weakEvent . Subscribe ( asyncHandlerCT ) ;
72+ weakEvent . Subscribe ( asyncHandlerCt ) ;
7273
7374 weakEvent . Unsubscribe ( syncHandler ) ;
7475 weakEvent . Unsubscribe ( asyncHandler ) ;
75- weakEvent . Unsubscribe ( asyncHandlerCT ) ;
76+ weakEvent . Unsubscribe ( asyncHandlerCt ) ;
7677
7778 await weakEvent . PublishAsync ( 2 ) ;
7879
@@ -88,12 +89,14 @@ void syncHandler(int x)
8889 {
8990 count += 1 * x ;
9091 }
92+
9193 async Task asyncHandler ( int x )
9294 {
9395 count += 2 * x ;
9496 await Task . CompletedTask ;
9597 }
96- async Task asyncHandlerCT ( int x , CancellationToken ct )
98+
99+ async Task asyncHandlerCt ( int x , CancellationToken ct )
97100 {
98101 count += 4 * x ;
99102 await Task . CompletedTask ;
@@ -103,7 +106,7 @@ async Task asyncHandlerCT(int x, CancellationToken ct)
103106
104107 weakEvent . Subscribe ( syncHandler ) ;
105108 weakEvent . Subscribe ( asyncHandler ) ;
106- weakEvent . Subscribe ( asyncHandlerCT ) ;
109+ weakEvent . Subscribe ( asyncHandlerCt ) ;
107110
108111 await weakEvent . PublishAsync ( 2 ) ;
109112
@@ -123,13 +126,14 @@ static async Task createSubscriberAndInvoke(WeakEvent<string> weakEvent, Action
123126 await weakEvent . PublishAsync ( "Test" ) ;
124127 // The subscriber goes out of scope after this method, allowing it to be GC’d.
125128 }
129+
126130 await createSubscriberAndInvoke ( weakEvent , ( ) => callCount ++ ) ;
127131
128132 // Force garbage collection to reclaim the subscriber instance.
129133 GC . Collect ( ) ;
130134 GC . WaitForPendingFinalizers ( ) ;
131135
132- // Invoke second time
136+ // Invoke a second time
133137 await weakEvent . PublishAsync ( "Test" ) ;
134138
135139 // Just one call should happen instead of 2
@@ -146,6 +150,7 @@ public void MultipleHandlers_CorrectCount()
146150 // Stage 2 - Add local handler
147151 void syncHandler ( string _ )
148152 { }
153+
149154 weakEvent . Subscribe ( syncHandler ) ;
150155
151156 // Stage 3 - Add a garbage-collected handler
@@ -155,6 +160,7 @@ void createSubscriberAndAssert()
155160 weakEvent . Subscribe ( subscriber . Handler ) ;
156161 Assert . Equal ( 2 , weakEvent . SubscriberCount ) ;
157162 }
163+
158164 createSubscriberAndAssert ( ) ;
159165
160166 // Stage 4 - Force garbage collection to reclaim the garbage-collectable subscriber instance.
@@ -171,9 +177,6 @@ private class GenericSubscriber(Action onEvent)
171177 {
172178 private readonly Action _onEvent = onEvent ;
173179
174- public void Handler ( string _ )
175- {
176- _onEvent ( ) ;
177- }
180+ public void Handler ( string _ ) => _onEvent ( ) ;
178181 }
179- }
182+ }
0 commit comments