Skip to content

Commit f19ef6c

Browse files
committed
Add type forwarding test for [RequiresLocation]
1 parent de6b8ba commit f19ef6c

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/PolySharp.TypeForwards.Tests/TypeForwardTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Reflection;
34
#if NET6_0_OR_GREATER
45
using System.Runtime.CompilerServices;
@@ -58,5 +59,32 @@ public void IsExternalInit_IsForwarded()
5859
#endif
5960
}
6061

62+
[TestMethod]
63+
public void RequiresLocationAttribute_IsForwarded()
64+
{
65+
MethodInfo method = typeof(TypeForwardTests).GetMethod(nameof(MethodWithRefReadonlyParameter), BindingFlags.Static | BindingFlags.NonPublic)!;
66+
ParameterInfo parameter = method.GetParameters()[0];
67+
CustomAttributeData attribute = parameter.CustomAttributes.Last();
68+
69+
Assert.AreEqual("System.Runtime.CompilerServices.RequiresLocationAttribute", attribute.AttributeType.FullName);
70+
71+
#if NET8_0_OR_GREATER
72+
Assert.AreEqual(typeof(object).Assembly, typeof(RequiresLocationAttribute).Assembly);
73+
74+
string requiresLocationAttributeAssemblyName = typeof(RequiresLocationAttribute).Assembly.GetName().Name!;
75+
76+
// Verify the type has been forwarded correctly
77+
Assert.AreEqual(requiresLocationAttributeAssemblyName, attribute.AttributeType.Assembly.GetName().Name);
78+
Assert.AreEqual(requiresLocationAttributeAssemblyName, typeof(TypeForwardTests).Assembly.GetType("System.Runtime.CompilerServices.RequiresLocationAttribute")!.Assembly.GetName().Name);
79+
#else
80+
// If RequiresLocationAttribute is not available, it should be polyfilled in this project
81+
Assert.AreEqual("PolySharp.TypeForwards.Tests", attribute.AttributeType.Assembly.GetName().Name);
82+
#endif
83+
}
84+
6185
private sealed record Person(string Name);
86+
87+
private static void MethodWithRefReadonlyParameter(ref readonly int x)
88+
{
89+
}
6290
}

0 commit comments

Comments
 (0)