Skip to content

Commit

Permalink
Merge pull request #813 from mihnea-radulescu/bug/issue-788-Raise-Eve…
Browse files Browse the repository at this point in the history
…ntWith-default-constructor

Raise.EventWith default constructor (#788)
  • Loading branch information
dtchepak authored Jun 2, 2024
2 parents 2ce4d66 + 4bd429e commit da82bc5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
21 changes: 18 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) ?? GetInternalDefaultConstructor(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,22 @@ protected EventArgs GetDefaultForEventArgType(Type type)
return (EventArgs)defaultConstructor.Invoke([]);
}

private static ConstructorInfo? GetDefaultConstructor(Type type) => type.GetConstructor(Type.EmptyTypes);
private static ConstructorInfo? GetInternalDefaultConstructor(Type type)
{
var nonPublicDefaultConstructor = GetNonPublicDefaultConstructor(type);
var isInternalDefaultConstructor = nonPublicDefaultConstructor?.IsAssembly == true;
return isInternalDefaultConstructor ? nonPublicDefaultConstructor : null;
}

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.ExactBinding | bindingFlags, null, Type.EmptyTypes, null);

protected static void RaiseEvent(RaiseEventWrapper wrapper)
{
Expand Down
59 changes: 59 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/EventRaising.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,31 @@ 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_is_raised()
{
// Arrange
var exampleInternalMock = Substitute.For<IExampleInternal>();
var consumerInternal = new ConsumerInternal(exampleInternalMock);

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

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

[Test]
public void MyEvent_with_CustomEventArgsWithPrivateDefaultConstructor_throws_CannotCreateEventArgsException()
{
// Arrange
var examplePrivateMock = Substitute.For<IExamplePrivate>();

// Act and Assert
Assert.Throws<CannotCreateEventArgsException>(() =>
examplePrivateMock.MyEvent += Raise.EventWith<CustomEventArgsWithPrivateDefaultConstructor>(this, null!));
}

class RaisedEventRecorder<T>
{
public object Sender;
Expand Down Expand Up @@ -339,4 +364,38 @@ public class CustomEventArgs : EventArgs { }
public class CustomEventArgsWithNoDefaultCtor(string arg) : EventArgs

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net6.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net7.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net8.0)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net462)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net462)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net462)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net462)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net462)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net462)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net462)

Parameter 'arg' is unread.

Check warning on line 364 in tests/NSubstitute.Acceptance.Specs/EventRaising.cs

View workflow job for this annotation

GitHub Actions / test (windows-latest, net462)

Parameter 'arg' is unread.
{
}

public class CustomEventArgsWithInternalDefaultConstructor : EventArgs
{
internal CustomEventArgsWithInternalDefaultConstructor() { }
}
public interface IExampleInternal
{
public event EventHandler<CustomEventArgsWithInternalDefaultConstructor> MyEvent;
}
public class ConsumerInternal
{
public ConsumerInternal(IExampleInternal example)
{
example.MyEvent += OnMyEvent;
}
public bool SomethingWasDone { get; private set; }
private void OnMyEvent(object sender, CustomEventArgsWithInternalDefaultConstructor args)
{
DoSomething();
}
private void DoSomething()
{
SomethingWasDone = true;
}
}

public class CustomEventArgsWithPrivateDefaultConstructor : EventArgs
{
private CustomEventArgsWithPrivateDefaultConstructor() { }
}
public interface IExamplePrivate
{
public event EventHandler<CustomEventArgsWithPrivateDefaultConstructor> MyEvent;
}
}

0 comments on commit da82bc5

Please sign in to comment.