Skip to content

Commit

Permalink
Hotfix Issue500_SpecialMethodsWithoutAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Romfos committed May 15, 2024
1 parent f5f9376 commit f8319a8
Showing 1 changed file with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using NUnit.Framework;
using System.Reflection;
using System.Reflection.Emit;
using NUnit.Framework;

namespace NSubstitute.Acceptance.Specs.FieldReports;

Expand All @@ -18,7 +18,7 @@ static Issue500_SpecialMethodsWithoutAttribute()
[Test]
public void ShouldCorrectlyConfigureProperty()
{
var substitute = Substitute.For([TypeWithMissingSpecialNameMethodAttributes], []);
var substitute = Substitute.For(new[] { TypeWithMissingSpecialNameMethodAttributes }, new object[0]);

Check warning on line 21 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified

Check warning on line 21 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified

Check warning on line 21 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified

Check warning on line 21 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified
var fixture = new GeneratedTypeFixture(substitute);

fixture.MyProperty = "42";
Expand All @@ -30,7 +30,7 @@ public void ShouldCorrectlyConfigureProperty()
[Test]
public void ShouldCorrectlyConfigureEvent()
{
object substitute = Substitute.For([TypeWithMissingSpecialNameMethodAttributes], []);
object substitute = Substitute.For(new[] { TypeWithMissingSpecialNameMethodAttributes }, new object[0]);

Check warning on line 33 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified

Check warning on line 33 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified

Check warning on line 33 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified

Check warning on line 33 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified
var fixture = new GeneratedTypeFixture(substitute);

bool wasCalled = false;
Expand All @@ -52,19 +52,20 @@ private static Type GenerateTypeWithMissingSpecialNameAttributes()
var typeBuilder = module.DefineType("TypeWithMissingSpecialAttributes",
TypeAttributes.Public | TypeAttributes.Abstract);

typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
var evBuilder = typeBuilder.DefineEvent(EventName, EventAttributes.None, typeof(EventHandler));
var evAdder = typeBuilder.DefineMethod(
$"add_{EventName}",
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract |
MethodAttributes.HideBySig | MethodAttributes.NewSlot /* | MethodAttributes.SpecialName */,
typeof(void),
[typeof(EventHandler)]);
new[] { typeof(EventHandler) });

Check warning on line 62 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified
var evRemover = typeBuilder.DefineMethod(
$"remove_{EventName}",
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract |
MethodAttributes.HideBySig | MethodAttributes.NewSlot /* | MethodAttributes.SpecialName */,
typeof(void),
[typeof(EventHandler)]);
new[] { typeof(EventHandler) });

Check warning on line 68 in tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs

View workflow job for this annotation

GitHub Actions / format-verify

Collection initialization can be simplified
evBuilder.SetAddOnMethod(evAdder);
evBuilder.SetRemoveOnMethod(evRemover);

Expand All @@ -81,25 +82,32 @@ private static Type GenerateTypeWithMissingSpecialNameAttributes()
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract |
MethodAttributes.HideBySig | MethodAttributes.NewSlot /* | MethodAttributes.SpecialName */,
typeof(void),
[typeof(object)]);
new[] { typeof(object) });
propBuilder.SetGetMethod(propGetter);
propBuilder.SetSetMethod(propSetter);

return typeBuilder.CreateTypeInfo().AsType();
}

private class GeneratedTypeFixture(object substitute)
private class GeneratedTypeFixture
{
private readonly object _substitute;

public GeneratedTypeFixture(object substitute)
{
_substitute = substitute;
}

public object MyProperty
{
get => substitute.GetType().GetProperty(PropertyName).GetValue(substitute);
set => substitute.GetType().GetProperty(PropertyName).SetValue(substitute, value);
get => _substitute.GetType().GetProperty(PropertyName).GetValue(_substitute);
set => _substitute.GetType().GetProperty(PropertyName).SetValue(_substitute, value);
}

public event EventHandler MyEvent
{
add => substitute.GetType().GetEvent(EventName).AddEventHandler(substitute, value);
remove => substitute.GetType().GetEvent(EventName).RemoveEventHandler(substitute, value);
add => _substitute.GetType().GetEvent(EventName).AddEventHandler(_substitute, value);
remove => _substitute.GetType().GetEvent(EventName).RemoveEventHandler(_substitute, value);
}
}
}

0 comments on commit f8319a8

Please sign in to comment.