Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise.EventWith default constructor (#788) #813

Merged
merged 4 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/NSubstitute/Core/Events/RaiseEventWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ protected EventArgs GetDefaultForEventArgType(Type type)
{
if (type == typeof(EventArgs)) return EventArgs.Empty;

var defaultConstructor = GetDefaultConstructor(type);
if (defaultConstructor == null)
var defaultConstructor = GetPublicDefaultConstructor(type) ?? GetNonPublicDefaultConstructor(type);
if (defaultConstructor is null)
{
var message = string.Format(
"Cannot create {0} for this event as it has no default constructor. " +
Expand All @@ -24,7 +24,12 @@ protected EventArgs GetDefaultForEventArgType(Type type)
return (EventArgs)defaultConstructor.Invoke([]);
}

private static ConstructorInfo? GetDefaultConstructor(Type type) => type.GetConstructor(Type.EmptyTypes);
private static ConstructorInfo? GetPublicDefaultConstructor(Type type)
=> GetDefaultConstructor(type, BindingFlags.Public);
private static ConstructorInfo? GetNonPublicDefaultConstructor(Type type)
=> GetDefaultConstructor(type, BindingFlags.NonPublic);
private static ConstructorInfo? GetDefaultConstructor(Type type, BindingFlags bindingFlags)
=> type.GetConstructor(BindingFlags.Instance | bindingFlags, null, Type.EmptyTypes, null);

protected static void RaiseEvent(RaiseEventWrapper wrapper)
{
Expand Down
39 changes: 39 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/EventRaising.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ public void Raise_custom_event_that_has_sender_and_args_but_does_not_inherit_fro
Assert.That(eventRecorder.Sender, Is.SameAs(sender));
}

[Test]
public void MyEvent_With_CustomEventArgsWithInternalDefaultConstructor_IsRaised()
{
// Arrange
IExample mockExample = Substitute.For<IExample>();
var consumer = new Consumer(mockExample);

// Act
mockExample.MyEvent += Raise.EventWith<CustomEventArgsWithInternalDefaultConstructor>(this, null!);

// Assert
Assert.That(consumer.SomethingWasDone);
}

class RaisedEventRecorder<T>
{
public object Sender;
Expand Down Expand Up @@ -340,4 +354,29 @@ public class CustomEventArgsWithNoDefaultCtor : EventArgs
{
public CustomEventArgsWithNoDefaultCtor(string arg) { }
}

public class CustomEventArgsWithInternalDefaultConstructor : EventArgs
{
internal CustomEventArgsWithInternalDefaultConstructor() { }
mihnea-radulescu marked this conversation as resolved.
Show resolved Hide resolved
}
public interface IExample
{
public event EventHandler<CustomEventArgsWithInternalDefaultConstructor> MyEvent;
}
public class Consumer
{
public Consumer(IExample example)
{
example.MyEvent += OnMyEvent;
}
public bool SomethingWasDone { get; private set; }
private void OnMyEvent(object sender, CustomEventArgsWithInternalDefaultConstructor args)
{
DoSomething();
}
private void DoSomething()
{
SomethingWasDone = true;
}
}
}
Loading