forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnosticMethodInfoTests.cs
More file actions
320 lines (274 loc) · 14.6 KB
/
Copy pathDiagnosticMethodInfoTests.cs
File metadata and controls
320 lines (274 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
namespace System.Diagnostics.Tests
{
public class DiagnosticMethodInfoTests
{
[Fact]
public void Create_Null()
{
Assert.Throws<ArgumentNullException>(() => DiagnosticMethodInfo.Create((Delegate)null));
Assert.Throws<ArgumentNullException>(() => DiagnosticMethodInfo.Create((StackFrame)null));
}
public static IEnumerable<object[]> Create_OpenDelegate_TestData()
{
// Tracked at https://github.com/dotnet/runtime/issues/100748
bool hasGvmOpenDelegateBug = !PlatformDetection.IsMonoRuntime && !PlatformDetection.IsNativeAot;
const string TestNamespace = nameof(System) + "." + nameof(System.Diagnostics) + "." + nameof(System.Diagnostics.Tests) + ".";
yield return new object[] {
typeof(IInterfaceForDiagnosticMethodInfoTests).GetMethod(nameof(IInterfaceForDiagnosticMethodInfoTests.NonGenericMethod)).CreateDelegate<Action<IInterfaceForDiagnosticMethodInfoTests>>(),
nameof(IInterfaceForDiagnosticMethodInfoTests.NonGenericMethod),
TestNamespace + nameof(IInterfaceForDiagnosticMethodInfoTests)
};
if (!hasGvmOpenDelegateBug)
{
yield return new object[] {
typeof(IInterfaceForDiagnosticMethodInfoTests).GetMethod(nameof(IInterfaceForDiagnosticMethodInfoTests.GenericMethod)).MakeGenericMethod(typeof(object)).CreateDelegate<Action<IInterfaceForDiagnosticMethodInfoTests>>(),
nameof(IInterfaceForDiagnosticMethodInfoTests.GenericMethod),
TestNamespace + nameof(IInterfaceForDiagnosticMethodInfoTests)
};
}
yield return new object[] {
typeof(IGenericInterfaceForDiagnosticMethodInfoTests<object>).GetMethod(nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>.NonGenericMethod)).CreateDelegate<Action<IGenericInterfaceForDiagnosticMethodInfoTests<object>>>(),
nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>.NonGenericMethod),
TestNamespace + nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>) + "`1"
};
if (!hasGvmOpenDelegateBug)
{
yield return new object[] {
typeof(IGenericInterfaceForDiagnosticMethodInfoTests<object>).GetMethod(nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>.GenericMethod)).MakeGenericMethod(typeof(object)).CreateDelegate<Action<IGenericInterfaceForDiagnosticMethodInfoTests<object>>>(),
nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>.GenericMethod),
TestNamespace + nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>) + "`1"
};
}
yield return new object[] {
typeof(StructForDiagnosticMethodInfoTests).GetMethod(nameof(StructForDiagnosticMethodInfoTests.NonGenericMethod)).CreateDelegate<RefAction<StructForDiagnosticMethodInfoTests>>(),
nameof(StructForDiagnosticMethodInfoTests.NonGenericMethod),
TestNamespace + nameof(StructForDiagnosticMethodInfoTests)
};
yield return new object[] {
typeof(StructForDiagnosticMethodInfoTests).GetMethod(nameof(StructForDiagnosticMethodInfoTests.GenericMethod)).MakeGenericMethod(typeof(object)).CreateDelegate<RefAction<StructForDiagnosticMethodInfoTests>>(),
nameof(StructForDiagnosticMethodInfoTests.GenericMethod),
TestNamespace + nameof(StructForDiagnosticMethodInfoTests)
};
}
[Theory]
[MemberData(nameof(Create_OpenDelegate_TestData))]
public void Create_OpenDelegate(Delegate del, string expectedName, string expectedTypeName)
{
DiagnosticMethodInfo dmi = DiagnosticMethodInfo.Create(del);
Assert.Equal(expectedName, dmi.Name);
Assert.Equal(expectedTypeName, dmi.DeclaringTypeName);
AssertEqualAssemblyName(Assembly.GetExecutingAssembly().GetName(), dmi.DeclaringAssemblyName);
}
public static IEnumerable<object[]> Create_ClosedDelegate_TestData()
{
const string TestNamespace = nameof(System) + "." + nameof(System.Diagnostics) + "." + nameof(System.Diagnostics.Tests) + ".";
IInterfaceForDiagnosticMethodInfoTests o = new ClassForDiagnosticMethodInfoTests();
yield return new object[] {
(Action)o.NonGenericDefaultMethod,
nameof(IInterfaceForDiagnosticMethodInfoTests.NonGenericDefaultMethod),
TestNamespace + nameof(IInterfaceForDiagnosticMethodInfoTests)
};
yield return new object[] {
(Action)o.GenericDefaultMethod<object>,
nameof(IInterfaceForDiagnosticMethodInfoTests.GenericDefaultMethod),
TestNamespace + nameof(IInterfaceForDiagnosticMethodInfoTests)
};
yield return new object[] {
(Action)o.NonGenericMethod,
TestNamespace + nameof(IInterfaceForDiagnosticMethodInfoTests) + "." + nameof(IInterfaceForDiagnosticMethodInfoTests.NonGenericMethod),
TestNamespace + nameof(ClassForDiagnosticMethodInfoTests)
};
yield return new object[] {
(Action)o.GenericMethod<object>,
TestNamespace + nameof(IInterfaceForDiagnosticMethodInfoTests) + "." + nameof(IInterfaceForDiagnosticMethodInfoTests.GenericMethod),
TestNamespace + nameof(ClassForDiagnosticMethodInfoTests)
};
IGenericInterfaceForDiagnosticMethodInfoTests<object> og = new GenericClassForDiagnosticMethodInfoTests<object>();
// Making this work with native AOT tracked in https://github.com/dotnet/runtime/issues/103219
if (!PlatformDetection.IsNativeAot)
{
yield return new object[] {
(Action)og.NonGenericDefaultMethod,
nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>.NonGenericDefaultMethod) ,
TestNamespace + nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>) + "`1"
};
}
yield return new object[] {
(Action)og.GenericDefaultMethod<object>,
nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>.GenericDefaultMethod),
TestNamespace + nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>) + "`1"
};
yield return new object[] {
(Action)og.NonGenericMethod,
TestNamespace + nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>) + "<T>." + nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>.NonGenericMethod),
TestNamespace + nameof(GenericClassForDiagnosticMethodInfoTests<object>) + "`1"
};
yield return new object[] {
(Action)og.GenericMethod<object>,
TestNamespace + nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>) + "<T>." + nameof(IGenericInterfaceForDiagnosticMethodInfoTests<object>.GenericMethod),
TestNamespace + nameof(GenericClassForDiagnosticMethodInfoTests<object>) + "`1"
};
StructForDiagnosticMethodInfoTests s = default;
yield return new object[] {
(Action)s.NonGenericMethod,
nameof(StructForDiagnosticMethodInfoTests.NonGenericMethod),
TestNamespace + nameof(StructForDiagnosticMethodInfoTests)
};
yield return new object[] {
(Action)s.GenericMethod<object>,
nameof(StructForDiagnosticMethodInfoTests.GenericMethod),
TestNamespace + nameof(StructForDiagnosticMethodInfoTests)
};
}
[Theory]
[MemberData(nameof(Create_ClosedDelegate_TestData))]
public void Create_ClosedDelegate(Delegate del, string expectedName, string expectedTypeName)
{
DiagnosticMethodInfo dmi = DiagnosticMethodInfo.Create(del);
Assert.Equal(expectedName, dmi.Name);
Assert.Equal(expectedTypeName, dmi.DeclaringTypeName);
AssertEqualAssemblyName(Assembly.GetExecutingAssembly().GetName(), dmi.DeclaringAssemblyName);
}
private static void AssertEqualAssemblyName(AssemblyName aname, string s)
{
var ani = AssemblyNameInfo.Parse(s);
Assert.Equal(aname.Name, ani.Name);
Assert.Equal(aname.Version, ani.Version);
}
[Fact]
public void Create_MulticastDelegate()
{
var c = new ClassForDiagnosticMethodInfoTests();
Action a1 = c.Method1;
Action a2 = c.Method2;
Action a = a1 + a2;
DiagnosticMethodInfo dmi = DiagnosticMethodInfo.Create(a);
Assert.Equal(nameof(ClassForDiagnosticMethodInfoTests.Method2), dmi.Name);
}
[Fact]
[SkipOnMono("needs triage") /* Same as https://github.com/dotnet/runtime/blob/0686ce61ed1e1cb3cb420281a0154efa5d0d00d5/src/tests/Interop/MarshalAPI/FunctionPointer/FunctionPointer.cs#L9 */]
public unsafe void Create_MarshalledPointer()
{
void* pMem = NativeMemory.Alloc(1);
Action del = Marshal.GetDelegateForFunctionPointer<Action>((nint)pMem);
NativeMemory.Free(pMem);
DiagnosticMethodInfo dmi = DiagnosticMethodInfo.Create(del);
Assert.Equal(nameof(Action.Invoke), dmi.Name);
Assert.Equal(nameof(System) + "." + nameof(Action), dmi.DeclaringTypeName);
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/117165", TestPlatforms.Browser)]
public unsafe void Create_StackFrame()
{
StackTrace tr = NonGenericStackTraceClass.TestNonGeneric();
Verify(tr.GetFrame(0), "Test", "System.Diagnostics.Tests.GenericStackTraceClass`1+Nested");
Verify(tr.GetFrame(1), "TestGeneric", "System.Diagnostics.Tests.GenericStackTraceClass`1");
Verify(tr.GetFrame(2), "TestNonGeneric", "System.Diagnostics.Tests.GenericStackTraceClass`1");
Verify(tr.GetFrame(3), "TestGeneric", "System.Diagnostics.Tests.NonGenericStackTraceClass");
Verify(tr.GetFrame(4), "TestNonGeneric", "System.Diagnostics.Tests.NonGenericStackTraceClass");
static void Verify(StackFrame fr, string expectedName, string expectedDeclaringName)
{
DiagnosticMethodInfo dmi = DiagnosticMethodInfo.Create(fr);
Assert.Equal(expectedName, dmi.Name);
Assert.Equal(expectedDeclaringName, dmi.DeclaringTypeName);
}
}
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void Create_Delegate_StackTraceSupportDisabled()
{
var options = new RemoteInvokeOptions()
{
RuntimeConfigurationOptions =
{
["System.Diagnostics.StackTrace.IsSupported"] = false
}
};
RemoteExecutor.Invoke(static () =>
{
var c = new ClassForDiagnosticMethodInfoTests();
Action a = c.Method1;
DiagnosticMethodInfo dmi = DiagnosticMethodInfo.Create(a);
Assert.Null(dmi);
}, options).Dispose();
}
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public void Create_Frame_StackTraceSupportDisabled()
{
var options = new RemoteInvokeOptions()
{
RuntimeConfigurationOptions =
{
["System.Diagnostics.StackTrace.IsSupported"] = false
}
};
RemoteExecutor.Invoke(static () =>
{
StackFrame f = new StackFrame();
DiagnosticMethodInfo dmi = DiagnosticMethodInfo.Create(f);
Assert.Null(dmi);
}, options).Dispose();
}
}
interface IInterfaceForDiagnosticMethodInfoTests
{
void NonGenericMethod();
void GenericMethod<T>();
void NonGenericDefaultMethod() { }
void GenericDefaultMethod<T>() { }
}
class ClassForDiagnosticMethodInfoTests : IInterfaceForDiagnosticMethodInfoTests
{
void IInterfaceForDiagnosticMethodInfoTests.GenericMethod<T>() => throw new NotImplementedException();
void IInterfaceForDiagnosticMethodInfoTests.NonGenericMethod() => throw new NotImplementedException();
public void Method1() { }
public void Method2() { }
}
interface IGenericInterfaceForDiagnosticMethodInfoTests<T>
{
void NonGenericMethod();
void GenericMethod<U>();
void NonGenericDefaultMethod() { }
void GenericDefaultMethod<U>() { }
}
class GenericClassForDiagnosticMethodInfoTests<T> : IGenericInterfaceForDiagnosticMethodInfoTests<T>
{
void IGenericInterfaceForDiagnosticMethodInfoTests<T>.GenericMethod<U>() => throw new NotImplementedException();
void IGenericInterfaceForDiagnosticMethodInfoTests<T>.NonGenericMethod() => throw new NotImplementedException();
public void Method1() { }
public void Method2() { }
}
struct StructForDiagnosticMethodInfoTests : IInterfaceForDiagnosticMethodInfoTests
{
public void NonGenericMethod() { }
public void GenericMethod<T>() { }
}
delegate void RefAction<T>(ref T t);
class NonGenericStackTraceClass
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static StackTrace TestNonGeneric() => TestGeneric<int>();
[MethodImpl(MethodImplOptions.NoInlining)]
public static StackTrace TestGeneric<T>() => GenericStackTraceClass<object>.TestNonGeneric();
}
class GenericStackTraceClass<T>
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static StackTrace TestNonGeneric() => TestGeneric<object>();
[MethodImpl(MethodImplOptions.NoInlining)]
public static StackTrace TestGeneric<U>() => Nested.Test();
public class Nested
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static StackTrace Test() => new StackTrace();
}
}
}