Skip to content

Commit 5929598

Browse files
sbomervitek-karas
andauthored
Add test get interfaces (#2696)
* Add test which mimics what is done in Type.ImplementInterface helper in runtime There's nothing new in this test, but it's better to have coverage for the pattern as it's used in runtime. * Add tests for properties * Simplify Co-authored-by: vitek-karas <[email protected]>
1 parent 9f2b304 commit 5929598

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Diagnostics.CodeAnalysis;
77
using System.Linq;
8+
using System.Reflection;
89
using System.Text;
910
using System.Threading.Tasks;
1011
using Mono.Linker.Tests.Cases.Expectations.Assertions;
@@ -50,6 +51,8 @@ public static void Main ()
5051
TestNoValue ();
5152

5253
Mixed_Derived.Test (typeof (TestType), 0);
54+
55+
LoopPatterns.Test ();
5356
}
5457

5558
static void TestAllPropagated ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type derivedType)
@@ -275,6 +278,62 @@ public static void Test (
275278
}
276279
}
277280

281+
class LoopPatterns
282+
{
283+
static void EnumerateInterfacesOnBaseTypes ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)] Type type)
284+
{
285+
Type? t = type;
286+
while (t != null) {
287+
Type[] interfaces = t.GetInterfaces ();
288+
t = t.BaseType;
289+
}
290+
}
291+
292+
[ExpectedWarning ("IL2070")]
293+
[ExpectedWarning ("IL2075", ProducedBy = ProducedBy.Analyzer)] // Linker doesn't implement backward branches data flow yet
294+
static void EnumerateInterfacesOnBaseTypes_Unannotated (Type type)
295+
{
296+
Type? t = type;
297+
while (t != null) {
298+
Type[] interfaces = t.GetInterfaces ();
299+
t = t.BaseType;
300+
}
301+
}
302+
303+
// Can only work with All annotation as NonPublicProperties doesn't propagate to base types
304+
static void EnumeratePrivatePropertiesOnBaseTypes ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type)
305+
{
306+
const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
307+
Type? t = type;
308+
while (t != null) {
309+
t.GetProperties (DeclaredOnlyLookup).GetEnumerator ();
310+
t = t.BaseType;
311+
}
312+
}
313+
314+
// Can only work with All annotation as NonPublicProperties doesn't propagate to base types
315+
static void EnumeratePrivatePropertiesOnBaseTypesWithForeach ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type)
316+
{
317+
const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
318+
Type? t = type;
319+
while (t != null) {
320+
foreach (var p in t.GetProperties (DeclaredOnlyLookup)) {
321+
// Do nothing
322+
}
323+
t = t.BaseType;
324+
}
325+
}
326+
327+
public static void Test ()
328+
{
329+
EnumerateInterfacesOnBaseTypes (typeof (TestType));
330+
EnumerateInterfacesOnBaseTypes_Unannotated (typeof (TestType));
331+
332+
EnumeratePrivatePropertiesOnBaseTypes (typeof (TestType));
333+
EnumeratePrivatePropertiesOnBaseTypesWithForeach (typeof (TestType));
334+
}
335+
}
336+
278337
class TestType
279338
{
280339
}

0 commit comments

Comments
 (0)