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
Show file tree
Hide file tree
Changes from all commits
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
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 @@
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 @@ -336,7 +361,41 @@
public delegate int FuncDelegateWithArgs(int intArg, string stringArg);
public delegate void CustomEventThatDoesNotInheritFromEventHandler(object sender, CustomEventArgs args);
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, 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 (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 (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 (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 (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, 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 (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, 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.

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, 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.
{
}

public class CustomEventArgsWithInternalDefaultConstructor : EventArgs
{
internal CustomEventArgsWithInternalDefaultConstructor() { }
mihnea-radulescu marked this conversation as resolved.
Show resolved Hide resolved
}
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;
}
}
Loading