-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify tests to distinguish between emit- and interpreted-based invoke (
- Loading branch information
1 parent
daffba6
commit 61ddecc
Showing
16 changed files
with
328 additions
and
14 deletions.
There are no files selected for viewing
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
56 changes: 56 additions & 0 deletions
56
src/libraries/Common/tests/System/Reflection/InvokeEmitTests.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,56 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.CompilerServices; | ||
using Xunit; | ||
|
||
namespace System.Reflection.Tests | ||
{ | ||
public class InvokeEmitTests | ||
{ | ||
[ConditionalFact(typeof(InvokeEmitTests), nameof(InvokeEmitTests.IsEmitInvokeSupported))] | ||
public static void VerifyInvokeIsUsingEmit_Method() | ||
{ | ||
MethodInfo method = typeof(TestClassThatThrows).GetMethod(nameof(TestClassThatThrows.Throw))!; | ||
TargetInvocationException ex = Assert.Throws<TargetInvocationException>(() => method.Invoke(null, null)); | ||
Exception exInner = ex.InnerException; | ||
|
||
Assert.Contains("Here", exInner.ToString()); | ||
Assert.Contains("InvokeStub_TestClassThatThrows", exInner.ToString()); | ||
Assert.DoesNotContain(InterpretedMethodName, exInner.ToString()); | ||
} | ||
|
||
[ConditionalFact(typeof(InvokeEmitTests), nameof(InvokeEmitTests.IsEmitInvokeSupported))] | ||
public static void VerifyInvokeIsUsingEmit_Constructor() | ||
{ | ||
ConstructorInfo ctor = typeof(TestClassThatThrows).GetConstructor(Type.EmptyTypes)!; | ||
TargetInvocationException ex = Assert.Throws<TargetInvocationException>(() => ctor.Invoke(null)); | ||
Exception exInner = ex.InnerException; | ||
|
||
Assert.Contains("Here", exInner.ToString()); | ||
Assert.Contains("InvokeStub_TestClassThatThrows", exInner.ToString()); | ||
Assert.DoesNotContain(InterpretedMethodName, exInner.ToString()); | ||
} | ||
|
||
private static bool IsEmitInvokeSupported() | ||
{ | ||
// Emit is only used for Invoke when RuntimeFeature.IsDynamicCodeCompiled is true. | ||
return RuntimeFeature.IsDynamicCodeCompiled | ||
&& !PlatformDetection.IsMonoRuntime; // Temporary until Mono is updated. | ||
} | ||
|
||
private static string InterpretedMethodName => PlatformDetection.IsMonoRuntime ? | ||
"System.Reflection.MethodInvoker.InterpretedInvoke" : | ||
"System.RuntimeMethodHandle.InvokeMethod"; | ||
|
||
private class TestClassThatThrows | ||
{ | ||
public TestClassThatThrows() | ||
{ | ||
throw new Exception("Here"); | ||
} | ||
|
||
public static void Throw() => throw new Exception("Here"); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/libraries/Common/tests/System/Reflection/InvokeInterpretedTests.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,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace System.Reflection.Tests | ||
{ | ||
public class InvokeInterpretedTests | ||
{ | ||
[Fact] | ||
public static void VerifyInvokeIsUsingEmit_Method() | ||
{ | ||
MethodInfo method = typeof(TestClassThatThrows).GetMethod(nameof(TestClassThatThrows.Throw))!; | ||
TargetInvocationException ex = Assert.Throws<TargetInvocationException>(() => method.Invoke(null, null)); | ||
Exception exInner = ex.InnerException; | ||
|
||
Assert.Contains("Here", exInner.ToString()); | ||
Assert.Contains(InterpretedMethodName(), exInner.ToString()); | ||
Assert.DoesNotContain("InvokeStub_TestClassThatThrows", exInner.ToString()); | ||
|
||
string InterpretedMethodName() => PlatformDetection.IsMonoRuntime ? | ||
"System.Reflection.MethodInvoker.InterpretedInvoke" : | ||
"System.RuntimeMethodHandle.InvokeMethod"; | ||
} | ||
|
||
[Fact] | ||
public static void VerifyInvokeIsUsingEmit_Constructor() | ||
{ | ||
ConstructorInfo ctor = typeof(TestClassThatThrows).GetConstructor(Type.EmptyTypes)!; | ||
TargetInvocationException ex = Assert.Throws<TargetInvocationException>(() => ctor.Invoke(null)); | ||
Exception exInner = ex.InnerException; | ||
|
||
Assert.Contains("Here", exInner.ToString()); | ||
Assert.Contains(InterpretedMethodName(), exInner.ToString()); | ||
Assert.DoesNotContain("InvokeStub_TestClassThatThrows", exInner.ToString()); | ||
|
||
string InterpretedMethodName() => PlatformDetection.IsMonoRuntime ? | ||
"System.Reflection.ConstructorInvoker.InterpretedInvoke" : | ||
"System.RuntimeMethodHandle.InvokeMethod"; | ||
} | ||
|
||
private class TestClassThatThrows | ||
{ | ||
public TestClassThatThrows() | ||
{ | ||
throw new Exception("Here"); | ||
} | ||
|
||
public static void Throw() => throw new Exception("Here"); | ||
} | ||
} | ||
} | ||
|
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
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
22 changes: 22 additions & 0 deletions
22
src/libraries/System.Reflection/tests/InvokeEmit/System.Reflection.InvokeEmit.Tests.csproj
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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<TestRuntime>true</TestRuntime> | ||
<IncludeRemoteExecutor>true</IncludeRemoteExecutor> | ||
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="..\Common.cs" /> | ||
<Compile Include="..\ConstructorInfoTests.cs" /> | ||
<Compile Include="..\MethodInfoTests.cs" /> | ||
<Compile Include="..\PropertyInfoTests.cs" /> | ||
<Compile Include="$(CommonTestPath)\System\Reflection\InvokeEmitTests.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\TestAssembly\TestAssembly.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<!-- Assemblies that should be excluded from the bundle --> | ||
<__ExcludeFromBundle Include="TestAssembly.dll" /> | ||
</ItemGroup> | ||
</Project> |
5 changes: 5 additions & 0 deletions
5
src/libraries/System.Reflection/tests/InvokeEmit/runtimeconfig.template.json
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,5 @@ | ||
{ | ||
"configProperties": { | ||
"Switch.System.Reflection.ForceEmitInvoke": true | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ystem.Reflection/tests/InvokeInterpreted/System.Reflection.InvokeInterpreted.Tests.csproj
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,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<TestRuntime>true</TestRuntime> | ||
<IncludeRemoteExecutor>true</IncludeRemoteExecutor> | ||
<TargetFrameworks>$(NetCoreAppCurrent)</TargetFrameworks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="..\Common.cs" /> | ||
<Compile Include="..\ConstructorInfoTests.cs" /> | ||
<Compile Include="..\MethodInfoTests.cs" /> | ||
<Compile Include="..\PropertyInfoTests.cs" /> | ||
<Compile Include="$(CommonTestPath)\System\Reflection\InvokeInterpretedTests.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\TestAssembly\TestAssembly.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<!-- Assemblies that should be excluded from the bundle --> | ||
<__ExcludeFromBundle Include="TestAssembly.dll" /> | ||
</ItemGroup> | ||
</Project> |
5 changes: 5 additions & 0 deletions
5
src/libraries/System.Reflection/tests/InvokeInterpreted/runtimeconfig.template.json
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,5 @@ | ||
{ | ||
"configProperties": { | ||
"Switch.System.Reflection.ForceInterpretedInvoke": true | ||
} | ||
} |
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
Oops, something went wrong.