Skip to content

Commit

Permalink
#2773: Add Assert.RaisesAny and Assert.RaisesAnyAsync non-generic for…
Browse files Browse the repository at this point in the history
… EventArgs (v2)
  • Loading branch information
bradwilson committed Sep 7, 2023
1 parent c1dba28 commit d0004ae
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/xunit.assert/Asserts
Submodule Asserts updated 1 files
+143 −3 EventAsserts.cs
192 changes: 190 additions & 2 deletions test/test.xunit.assert/Asserts/EventAssertsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ public static void NoEventRaised()
);
}

[Fact]
public static void NoEventRaised_NonGeneric()
{
var obj = new RaisingClass_NonGeneric();

var ex = Record.Exception(
() => Assert.RaisesAny(
h => obj.Completed += h,
h => obj.Completed -= h,
() => { }
)
);

Assert.IsType<RaisesAnyException>(ex);
Assert.Equal(
"Assert.RaisesAny() Failure: No event was raised" + Environment.NewLine +
"Expected: typeof(System.EventArgs)" + Environment.NewLine +
"Actual: No event was raised",
ex.Message
);
}

[Fact]
public static void ExactTypeRaised()
{
Expand All @@ -111,6 +133,23 @@ public static void ExactTypeRaised()
Assert.Equal(eventObj, evt.Arguments);
}

[Fact]
public static void ExactTypeRaised_NonGeneric()
{
var obj = new RaisingClass_NonGeneric();
var eventObj = new EventArgs();

var evt = Assert.RaisesAny(
h => obj.Completed += h,
h => obj.Completed -= h,
() => obj.RaiseWithArgs(eventObj)
);

Assert.NotNull(evt);
Assert.Equal(obj, evt.Sender);
Assert.Equal(eventObj, evt.Arguments);
}

[Fact]
public static void DerivedTypeRaised()
{
Expand All @@ -127,6 +166,23 @@ public static void DerivedTypeRaised()
Assert.Equal(obj, evt.Sender);
Assert.Equal(eventObj, evt.Arguments);
}

[Fact]
public static void DerivedTypeRaised_NonGeneric()
{
var obj = new RaisingClass_NonGeneric();
var eventObj = new DerivedEventArgs();

var evt = Assert.RaisesAny(
h => obj.Completed += h,
h => obj.Completed -= h,
() => obj.RaiseWithArgs(eventObj)
);

Assert.NotNull(evt);
Assert.Equal(obj, evt.Sender);
Assert.Equal(eventObj, evt.Arguments);
}
}

public class RaisesAnyAsync_Task
Expand All @@ -153,6 +209,28 @@ public static async Task NoEventRaised()
);
}

[Fact]
public static async Task NoEventRaised_NonGeneric()
{
var obj = new RaisingClass_NonGeneric();

var ex = await Record.ExceptionAsync(
() => Assert.RaisesAnyAsync(
h => obj.Completed += h,
h => obj.Completed -= h,
() => Task.FromResult(0)
)
);

Assert.IsType<RaisesAnyException>(ex);
Assert.Equal(
"Assert.RaisesAny() Failure: No event was raised" + Environment.NewLine +
"Expected: typeof(System.EventArgs)" + Environment.NewLine +
"Actual: No event was raised",
ex.Message
);
}

[Fact]
public static async Task ExactTypeRaised()
{
Expand All @@ -170,6 +248,23 @@ public static async Task ExactTypeRaised()
Assert.Equal(eventObj, evt.Arguments);
}

[Fact]
public static async Task ExactTypeRaised_NonGeneric()
{
var obj = new RaisingClass_NonGeneric();
var eventObj = new EventArgs();

var evt = await Assert.RaisesAnyAsync(
h => obj.Completed += h,
h => obj.Completed -= h,
() => Task.Run(() => obj.RaiseWithArgs(eventObj))
);

Assert.NotNull(evt);
Assert.Equal(obj, evt.Sender);
Assert.Equal(eventObj, evt.Arguments);
}

[Fact]
public static async Task DerivedTypeRaised()
{
Expand All @@ -186,6 +281,23 @@ public static async Task DerivedTypeRaised()
Assert.Equal(obj, evt.Sender);
Assert.Equal(eventObj, evt.Arguments);
}

[Fact]
public static async Task DerivedTypeRaised_NonGeneric()
{
var obj = new RaisingClass_NonGeneric();
var eventObj = new DerivedEventArgs();

var evt = await Assert.RaisesAnyAsync(
h => obj.Completed += h,
h => obj.Completed -= h,
() => Task.Run(() => obj.RaiseWithArgs(eventObj))
);

Assert.NotNull(evt);
Assert.Equal(obj, evt.Sender);
Assert.Equal(eventObj, evt.Arguments);
}
}

#if XUNIT_VALUETASK
Expand Down Expand Up @@ -213,6 +325,28 @@ public static async ValueTask NoEventRaised()
);
}

[Fact]
public static async ValueTask NoEventRaised_NonGeneric()
{
var obj = new RaisingClass_NonGeneric();

var ex = await Record.ExceptionAsync(
() => Assert.RaisesAnyAsync(
h => obj.Completed += h,
h => obj.Completed -= h,
() => default(ValueTask)
)
);

Assert.IsType<RaisesAnyException>(ex);
Assert.Equal(
"Assert.RaisesAny() Failure: No event was raised" + Environment.NewLine +
"Expected: typeof(System.EventArgs)" + Environment.NewLine +
"Actual: No event was raised",
ex.Message
);
}

[Fact]
public static async ValueTask ExactTypeRaised()
{
Expand All @@ -234,6 +368,27 @@ public static async ValueTask ExactTypeRaised()
Assert.Equal(eventObj, evt.Arguments);
}

[Fact]
public static async ValueTask ExactTypeRaised_NonGeneric()
{
var obj = new RaisingClass_NonGeneric();
var eventObj = new EventArgs();

var evt = await Assert.RaisesAnyAsync(
h => obj.Completed += h,
h => obj.Completed -= h,
() =>
{
obj.RaiseWithArgs(eventObj);
return default(ValueTask);
}
);

Assert.NotNull(evt);
Assert.Equal(obj, evt.Sender);
Assert.Equal(eventObj, evt.Arguments);
}

[Fact]
public static async ValueTask DerivedTypeRaised()
{
Expand All @@ -254,6 +409,27 @@ public static async ValueTask DerivedTypeRaised()
Assert.Equal(obj, evt.Sender);
Assert.Equal(eventObj, evt.Arguments);
}

[Fact]
public static async ValueTask DerivedTypeRaised_NonGeneric()
{
var obj = new RaisingClass_NonGeneric();
var eventObj = new DerivedEventArgs();

var evt = await Assert.RaisesAnyAsync(
h => obj.Completed += h,
h => obj.Completed -= h,
() =>
{
obj.RaiseWithArgs(eventObj);
return default(ValueTask);
}
);

Assert.NotNull(evt);
Assert.Equal(obj, evt.Sender);
Assert.Equal(eventObj, evt.Arguments);
}
}
#endif

Expand Down Expand Up @@ -397,7 +573,7 @@ public static async ValueTask DerivedTypeRaised()
}
#endif

private class RaisingClass
class RaisingClass
{
public void RaiseWithArgs(object args)
{
Expand All @@ -407,5 +583,17 @@ public void RaiseWithArgs(object args)
public event EventHandler<object>? Completed;
}

private class DerivedObject : object { }
class RaisingClass_NonGeneric
{
public void RaiseWithArgs(EventArgs args)
{
Completed!.Invoke(this, args);
}

public event EventHandler? Completed;
}

class DerivedEventArgs : EventArgs { }

class DerivedObject : object { }
}

0 comments on commit d0004ae

Please sign in to comment.