-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more strict check for aspect method signatures
- Loading branch information
Ralf
committed
Jan 31, 2022
1 parent
5528fe3
commit 3608d6b
Showing
7 changed files
with
167 additions
and
13 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/MethodBoundaryAspect.Fody.UnitTests.NetFramework/DuplicateEntryMethodsAspectTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using FluentAssertions; | ||
using MethodBoundaryAspect.Fody.UnitTests.TestAssembly.NetFramework; | ||
using Xunit; | ||
|
||
namespace MethodBoundaryAspect.Fody.UnitTests.NetFramework | ||
{ | ||
public class DuplicateEntryMethodsAspectTests : MethodBoundaryAspectNetFrameworkTestBase | ||
{ | ||
private static readonly Type TestMethodsType = typeof(DuplicateEntryMethodsAspectMethods); | ||
|
||
[Fact] | ||
public void IfStaticMethodIsCalled_ThenTheOnMethodBoundaryAspectShouldBeCalled() | ||
{ | ||
// Arrange | ||
const string testMethodName = "StaticMethodCall"; | ||
WeaveAssemblyMethodAndLoad(TestMethodsType, testMethodName); | ||
|
||
// Act | ||
var result = AssemblyLoader.InvokeMethod(TestMethodsType.TypeInfo(), testMethodName); | ||
|
||
// Assert | ||
result.Should().Be("-OnEntry()-OnExit()"); | ||
} | ||
|
||
[Fact] | ||
public void IfInstanceMethodIsCalled_ThenTheOnMethodBoundaryAspectShouldBeCalled() | ||
{ | ||
// Arrange | ||
const string testMethodName = "InstanceMethodCall"; | ||
WeaveAssemblyMethodAndLoad(TestMethodsType, testMethodName); | ||
|
||
// Act | ||
var result = AssemblyLoader.InvokeMethod(TestMethodsType.TypeInfo(), testMethodName); | ||
|
||
// Assert | ||
result.Should().Be("-OnEntry()-OnExit()"); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...aryAspect.Fody.UnitTests.TestAssembly.NetFramework/Aspects/DuplicateEntryMethodsAspect.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using MethodBoundaryAspect.Fody.Attributes; | ||
|
||
namespace MethodBoundaryAspect.Fody.UnitTests.TestAssembly.NetFramework.Aspects | ||
{ | ||
/// <summary> | ||
/// remark: weaved methods with parameter MethodExecutionArgs should be ordered last | ||
/// </summary> | ||
public class DuplicateEntryMethodsAspect : OnMethodBoundaryAspect | ||
{ | ||
public virtual void OnEntry(MethodExecutionArgs arg, bool b) | ||
{ | ||
DuplicateEntryMethodsAspectMethods.Result += "-OnEntry(bool)"; | ||
} | ||
|
||
public override void OnEntry(MethodExecutionArgs arg) | ||
{ | ||
DuplicateEntryMethodsAspectMethods.Result += "-OnEntry()"; | ||
} | ||
|
||
public virtual void OnEntry<T>(MethodExecutionArgs arg) | ||
{ | ||
DuplicateEntryMethodsAspectMethods.Result += "-OnEntry<T>"; | ||
} | ||
|
||
public virtual void OnExit(MethodExecutionArgs arg, bool b) | ||
{ | ||
DuplicateEntryMethodsAspectMethods.Result += "-OnExit(bool)"; | ||
} | ||
|
||
public virtual void OnExit<T>(MethodExecutionArgs arg) | ||
{ | ||
DuplicateEntryMethodsAspectMethods.Result += "-OnExit<T>"; | ||
} | ||
|
||
public override void OnExit(MethodExecutionArgs arg) | ||
{ | ||
DuplicateEntryMethodsAspectMethods.Result += "-OnExit()"; | ||
} | ||
|
||
public virtual void OnException(MethodExecutionArgs arg, bool b) | ||
{ | ||
DuplicateEntryMethodsAspectMethods.Result += "-OnException(bool)"; | ||
} | ||
|
||
public virtual void OnException<T>(MethodExecutionArgs arg) | ||
{ | ||
DuplicateEntryMethodsAspectMethods.Result += "-OnException<T>"; | ||
} | ||
|
||
public override void OnException(MethodExecutionArgs arg) | ||
{ | ||
DuplicateEntryMethodsAspectMethods.Result += "-OnException()"; | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...daryAspect.Fody.UnitTests.TestAssembly.NetFramework/DuplicateEntryMethodsAspectMethods.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using MethodBoundaryAspect.Fody.UnitTests.TestAssembly.NetFramework.Aspects; | ||
|
||
namespace MethodBoundaryAspect.Fody.UnitTests.TestAssembly.NetFramework | ||
{ | ||
public class DuplicateEntryMethodsAspectMethods | ||
{ | ||
public static object Result { get; set; } | ||
|
||
[DuplicateEntryMethodsAspect] | ||
public static void StaticMethodCall() | ||
{ | ||
} | ||
|
||
[DuplicateEntryMethodsAspect] | ||
public void InstanceMethodCall() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Linq; | ||
using Mono.Cecil; | ||
|
||
namespace MethodBoundaryAspect.Fody | ||
{ | ||
public static class AspectMethodCriteria | ||
{ | ||
public static readonly string OnEntryMethodName = "OnEntry"; | ||
public static readonly string OnExitMethodName = "OnExit"; | ||
public static readonly string OnExceptionMethodName = "OnException"; | ||
|
||
public static bool MatchesSignature(MethodDefinition method) | ||
{ | ||
return method.IsVirtual | ||
&& !method.HasGenericParameters | ||
&& method.Parameters.Count == 1 | ||
&& method.Parameters.Single().ParameterType.FullName == | ||
"MethodBoundaryAspect.Fody.Attributes.MethodExecutionArgs"; | ||
} | ||
|
||
public static bool IsOnEntryMethod(MethodDefinition method) | ||
{ | ||
return method.Name == OnEntryMethodName && MatchesSignature(method); | ||
} | ||
|
||
public static bool IsOnExitMethod(MethodDefinition method) | ||
{ | ||
return method.Name == OnExitMethodName && MatchesSignature(method); | ||
} | ||
|
||
public static bool IsOnExceptionMethod(MethodDefinition method) | ||
{ | ||
return method.Name == OnExceptionMethodName && MatchesSignature(method); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters