From f19ef6cd5f85a7d68ca6199d47237625841a9960 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 14 Nov 2023 23:53:31 +0100 Subject: [PATCH] Add type forwarding test for [RequiresLocation] --- .../TypeForwardTests.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/PolySharp.TypeForwards.Tests/TypeForwardTests.cs b/tests/PolySharp.TypeForwards.Tests/TypeForwardTests.cs index 8379ffe..a9990ef 100644 --- a/tests/PolySharp.TypeForwards.Tests/TypeForwardTests.cs +++ b/tests/PolySharp.TypeForwards.Tests/TypeForwardTests.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reflection; #if NET6_0_OR_GREATER using System.Runtime.CompilerServices; @@ -58,5 +59,32 @@ public void IsExternalInit_IsForwarded() #endif } + [TestMethod] + public void RequiresLocationAttribute_IsForwarded() + { + MethodInfo method = typeof(TypeForwardTests).GetMethod(nameof(MethodWithRefReadonlyParameter), BindingFlags.Static | BindingFlags.NonPublic)!; + ParameterInfo parameter = method.GetParameters()[0]; + CustomAttributeData attribute = parameter.CustomAttributes.Last(); + + Assert.AreEqual("System.Runtime.CompilerServices.RequiresLocationAttribute", attribute.AttributeType.FullName); + +#if NET8_0_OR_GREATER + Assert.AreEqual(typeof(object).Assembly, typeof(RequiresLocationAttribute).Assembly); + + string requiresLocationAttributeAssemblyName = typeof(RequiresLocationAttribute).Assembly.GetName().Name!; + + // Verify the type has been forwarded correctly + Assert.AreEqual(requiresLocationAttributeAssemblyName, attribute.AttributeType.Assembly.GetName().Name); + Assert.AreEqual(requiresLocationAttributeAssemblyName, typeof(TypeForwardTests).Assembly.GetType("System.Runtime.CompilerServices.RequiresLocationAttribute")!.Assembly.GetName().Name); +#else + // If RequiresLocationAttribute is not available, it should be polyfilled in this project + Assert.AreEqual("PolySharp.TypeForwards.Tests", attribute.AttributeType.Assembly.GetName().Name); +#endif + } + private sealed record Person(string Name); + + private static void MethodWithRefReadonlyParameter(ref readonly int x) + { + } }