-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nullable annotation and extraction of
RaisesAsync
from modified `As…
…sert` class to new `MyAssert` class. (#249)
- Loading branch information
Showing
6 changed files
with
128 additions
and
884 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using Xunit; | ||
using TwitchLib.Communication.Events; | ||
using System.Threading.Tasks; | ||
using Xunit.Sdk; | ||
|
||
namespace TwitchLib.Client.Test; | ||
|
||
//TL;DR: XUNIT with modification to accept async event Handler | ||
public partial class MyAssert | ||
{ | ||
/// <summary> | ||
/// Verifies that a event with the exact event args (and not a derived type) is raised. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the event arguments to expect</typeparam> | ||
/// <param name="attach">Code to attach the event handler</param> | ||
/// <param name="detach">Code to detach the event handler</param> | ||
/// <param name="testCode">A delegate to the code to be tested</param> | ||
/// <returns>The event sender and arguments wrapped in an object</returns> | ||
/// <exception cref="RaisesException">Thrown when the expected event was not raised.</exception> | ||
public static async Task<RaisedEvent<T>> RaisesAsync<T>(Action<AsyncEventHandler<T>> attach, Action<AsyncEventHandler<T>> detach, Func<Task> testCode) | ||
{ | ||
var raisedEvent = await RaisesAsyncInternal(attach, detach, testCode); | ||
|
||
if (raisedEvent == null) | ||
throw new RaisesException(typeof(T)); | ||
|
||
if (raisedEvent.Arguments != null && !raisedEvent.Arguments.GetType().Equals(typeof(T))) | ||
throw new RaisesException(typeof(T), raisedEvent.Arguments.GetType()); | ||
|
||
return raisedEvent; | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that an event with the exact or a derived event args is raised. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the event arguments to expect</typeparam> | ||
/// <param name="attach">Code to attach the event handler</param> | ||
/// <param name="detach">Code to detach the event handler</param> | ||
/// <param name="testCode">A delegate to the code to be tested</param> | ||
/// <returns>The event sender and arguments wrapped in an object</returns> | ||
/// <exception cref="RaisesException">Thrown when the expected event was not raised.</exception> | ||
public static async Task<RaisedEvent<T>> RaisesAnyAsync<T>(Action<AsyncEventHandler<T>> attach, Action<AsyncEventHandler<T>> detach, Func<Task> testCode) | ||
{ | ||
var raisedEvent = await RaisesAsyncInternal(attach, detach, testCode); | ||
|
||
if (raisedEvent == null) | ||
throw new RaisesException(typeof(T)); | ||
|
||
return raisedEvent; | ||
} | ||
|
||
static async Task<RaisedEvent<T>?> RaisesAsyncInternal<T>(Action<AsyncEventHandler<T>> attach, Action<AsyncEventHandler<T>> detach, Func<Task> testCode) | ||
{ | ||
Assert.NotNull(attach); | ||
Assert.NotNull(detach); | ||
Assert.NotNull(testCode); | ||
|
||
RaisedEvent<T>? raisedEvent = null; | ||
AsyncEventHandler<T> value = (object? s, T args) => | ||
{ | ||
raisedEvent = new RaisedEvent<T>(s, args); | ||
return Task.CompletedTask; | ||
}; | ||
AsyncEventHandler<T> handler = value; | ||
attach(handler); | ||
await testCode(); | ||
detach(handler); | ||
return raisedEvent; | ||
} | ||
|
||
/// <summary> | ||
/// Represents a raised event after the fact. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the event arguments.</typeparam> | ||
public class RaisedEvent<T> | ||
{ | ||
/// <summary> | ||
/// The sender of the event. | ||
/// </summary> | ||
public object? Sender { get; } | ||
|
||
/// <summary> | ||
/// The event arguments. | ||
/// </summary> | ||
public T Arguments { get; } | ||
|
||
/// <summary> | ||
/// Creates a new instance of the <see cref="RaisedEvent{T}" /> class. | ||
/// </summary> | ||
/// <param name="sender">The sender of the event.</param> | ||
/// <param name="args">The event arguments</param> | ||
public RaisedEvent(object? sender, T args) | ||
{ | ||
Sender = sender; | ||
Arguments = args; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.