Skip to content

Commit

Permalink
Add type forwarding test for [RequiresLocation]
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Nov 22, 2023
1 parent de6b8ba commit f19ef6c
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/PolySharp.TypeForwards.Tests/TypeForwardTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
#if NET6_0_OR_GREATER
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -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)
{
}
}

0 comments on commit f19ef6c

Please sign in to comment.