Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 3f779db

Browse files
authored
Basic coverage for RuntimeHelpers.PrepareMethod (#27117)
1 parent 19b3eff commit 3f779db

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/System.Runtime/tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Reflection;
7+
using System.Collections;
8+
using System.Collections.Generic;
69
using System.Runtime.CompilerServices;
710
using Xunit;
811

@@ -110,6 +113,72 @@ internal class HasCctorReceiver
110113
{
111114
public static string S;
112115
}
116+
117+
[Fact]
118+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework)]
119+
public static void PrepareMethod()
120+
{
121+
foreach (MethodInfo m in typeof(RuntimeHelpersTests).GetMethods())
122+
RuntimeHelpers.PrepareMethod(m.MethodHandle);
123+
124+
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(default(RuntimeMethodHandle)));
125+
Assert.ThrowsAny<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(IList).GetMethod("Add").MethodHandle));
126+
}
127+
128+
[Fact]
129+
public static void PrepareGenericMethod()
130+
{
131+
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(default(RuntimeMethodHandle), null));
132+
133+
//
134+
// Type instantiations
135+
//
136+
137+
// Generic definition with instantiation is valid
138+
RuntimeHelpers.PrepareMethod(typeof(List<>).GetMethod("Add").MethodHandle,
139+
new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle });
140+
141+
// Instantiated method without instantiation is valid
142+
RuntimeHelpers.PrepareMethod(typeof(List<int>).GetMethod("Add").MethodHandle,
143+
null);
144+
145+
// Generic definition without instantiation is invalid
146+
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(List<>).GetMethod("Add").MethodHandle,
147+
null));
148+
149+
// Wrong instantiation
150+
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(List<>).GetMethod("Add").MethodHandle,
151+
new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle, typeof(TestStruct).TypeHandle }));
152+
153+
//
154+
// Method instantiations
155+
//
156+
157+
// Generic definition with instantiation is valid
158+
RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize").MethodHandle,
159+
new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle });
160+
161+
// Instantiated method without instantiation is valid
162+
RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize")
163+
.MakeGenericMethod(new Type[] { typeof(TestStruct) }).MethodHandle,
164+
null);
165+
166+
// Generic definition without instantiation is invalid
167+
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize").MethodHandle,
168+
null));
169+
170+
// Wrong instantiation
171+
Assert.Throws<ArgumentException>(() => RuntimeHelpers.PrepareMethod(typeof(Array).GetMethod("Resize").MethodHandle,
172+
new RuntimeTypeHandle[] { typeof(TestStruct).TypeHandle, typeof(TestStruct).TypeHandle }));
173+
}
174+
175+
[Fact]
176+
public static void PrepareDelegate()
177+
{
178+
RuntimeHelpers.PrepareDelegate((Action)(() => { }));
179+
RuntimeHelpers.PrepareDelegate((Func<int>)(() => 1) + (Func<int>)(() => 2));
180+
RuntimeHelpers.PrepareDelegate(null);
181+
}
113182
}
114183

115184
public struct Age

0 commit comments

Comments
 (0)