From 5929598c16cd5d791eb33d4b5978f3140a136262 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Fri, 18 Mar 2022 16:28:27 -0700 Subject: 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 <10670590+vitek-karas@users.noreply.github.com> --- .../DataFlow/TypeBaseTypeDataFlow.cs | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'test/Mono.Linker.Tests.Cases') diff --git a/test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs b/test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs index 270d43616..b639a8233 100644 --- a/test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs +++ b/test/Mono.Linker.Tests.Cases/DataFlow/TypeBaseTypeDataFlow.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using Mono.Linker.Tests.Cases.Expectations.Assertions; @@ -50,6 +51,8 @@ namespace Mono.Linker.Tests.Cases.DataFlow TestNoValue (); Mixed_Derived.Test (typeof (TestType), 0); + + LoopPatterns.Test (); } static void TestAllPropagated ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type derivedType) @@ -275,6 +278,62 @@ namespace Mono.Linker.Tests.Cases.DataFlow } } + class LoopPatterns + { + static void EnumerateInterfacesOnBaseTypes ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)] Type type) + { + Type? t = type; + while (t != null) { + Type[] interfaces = t.GetInterfaces (); + t = t.BaseType; + } + } + + [ExpectedWarning ("IL2070")] + [ExpectedWarning ("IL2075", ProducedBy = ProducedBy.Analyzer)] // Linker doesn't implement backward branches data flow yet + static void EnumerateInterfacesOnBaseTypes_Unannotated (Type type) + { + Type? t = type; + while (t != null) { + Type[] interfaces = t.GetInterfaces (); + t = t.BaseType; + } + } + + // Can only work with All annotation as NonPublicProperties doesn't propagate to base types + static void EnumeratePrivatePropertiesOnBaseTypes ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type) + { + const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; + Type? t = type; + while (t != null) { + t.GetProperties (DeclaredOnlyLookup).GetEnumerator (); + t = t.BaseType; + } + } + + // Can only work with All annotation as NonPublicProperties doesn't propagate to base types + static void EnumeratePrivatePropertiesOnBaseTypesWithForeach ([DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.All)] Type type) + { + const BindingFlags DeclaredOnlyLookup = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; + Type? t = type; + while (t != null) { + foreach (var p in t.GetProperties (DeclaredOnlyLookup)) { + // Do nothing + } + t = t.BaseType; + } + } + + public static void Test () + { + EnumerateInterfacesOnBaseTypes (typeof (TestType)); + EnumerateInterfacesOnBaseTypes_Unannotated (typeof (TestType)); + + EnumeratePrivatePropertiesOnBaseTypes (typeof (TestType)); + EnumeratePrivatePropertiesOnBaseTypesWithForeach (typeof (TestType)); + } + } + class TestType { } -- cgit v1.2.3