diff --git a/src/TUnit.Core.SourceGenerator/Generators/AotConverterGenerator.cs b/src/TUnit.Core.SourceGenerator/Generators/AotConverterGenerator.cs index 949d76d3be..e9a89610f3 100644 --- a/src/TUnit.Core.SourceGenerator/Generators/AotConverterGenerator.cs +++ b/src/TUnit.Core.SourceGenerator/Generators/AotConverterGenerator.cs @@ -325,9 +325,29 @@ private static void CollectConversionsForType(ITypeSymbol type, List + /// Returns whether the public static Parse overload to call takes an , or null + /// when there is none (e.g. Parse implemented explicitly). Older frameworks lack Guid.Parse(string, IFormatProvider). + /// + private static bool? FindPublicParse(INamedTypeSymbol type) + { + bool? found = null; + foreach (var method in type.GetMembers("Parse").OfType()) + { + if (method is not { IsStatic: true, IsGenericMethod: false, DeclaredAccessibility: Accessibility.Public } || IsObsoleteAsError(method) + || method.Parameters.Length == 0 || method.Parameters[0].Type.SpecialType != SpecialType.System_String + || method.Parameters.Any(static p => p.RefKind is RefKind.Ref or RefKind.Out) + || !SymbolEqualityComparer.Default.Equals(method.ReturnType, type)) + { + continue; + } + + if (method.Parameters is [_, { Type.Name: "IFormatProvider", Type.ContainingNamespace.Name: "System" }]) + { + return true; + } + + if (method.Parameters.Length == 1) + { + found = false; + } + } + + return found; + } } diff --git a/tests/TUnit.Core.SourceGenerator.Tests/AotConverterScanTests.cs b/tests/TUnit.Core.SourceGenerator.Tests/AotConverterScanTests.cs index 547b251bfb..0f1550baa9 100644 --- a/tests/TUnit.Core.SourceGenerator.Tests/AotConverterScanTests.cs +++ b/tests/TUnit.Core.SourceGenerator.Tests/AotConverterScanTests.cs @@ -41,6 +41,100 @@ public void Decimal(decimal value) { } await Assert.That(generated.Contains("int converted = (int)global::System.Convert.ChangeType(sourceTypedValue, typeof(int));")).IsTrue(); } + [Test] + public async Task GeneratesParseConversionsFromString() + { + const string source = """ + using System; + using TUnit.Core; + public class Tests + { + [Test, Arguments("a", "b", "c", "d", "e", "f", "g")] + public void Parse(Guid guid, TimeSpan? timeSpan, DateTime dateTime, nint nativeInt, nuint nativeUInt, int number, string text) { } + } + """; + var generated = await GenerateTypeConverters(source); + +#if NET + await Assert.That(generated).Contains("global::System.Guid converted = global::System.Guid.Parse(sourceTypedValue, global::System.Globalization.CultureInfo.InvariantCulture);"); + await Assert.That(generated).Contains("nint converted = nint.Parse(sourceTypedValue, global::System.Globalization.CultureInfo.InvariantCulture);"); + await Assert.That(generated).Contains("nuint converted = nuint.Parse(sourceTypedValue, global::System.Globalization.CultureInfo.InvariantCulture);"); +#else + // .NET Framework only has Guid.Parse(string). + await Assert.That(generated).Contains("global::System.Guid converted = global::System.Guid.Parse(sourceTypedValue);"); +#endif + await Assert.That(generated).Contains("global::System.TimeSpan converted = global::System.TimeSpan.Parse(sourceTypedValue, global::System.Globalization.CultureInfo.InvariantCulture);"); + await Assert.That(generated).Contains("global::System.DateTime converted = global::System.DateTime.Parse(sourceTypedValue, global::System.Globalization.CultureInfo.InvariantCulture);"); + // Primitives keep their IConvertible path. + await Assert.That(generated).DoesNotContain(" int.Parse("); + await Assert.That(generated).DoesNotContain(" string.Parse("); + } + +#if NET + [Test] + public async Task GeneratesParseConversionsOnlyWhereParseIsCallable() + { + const string source = """ + using System; + using TUnit.Core; + public readonly struct Public : IParsable + { + public static Public Parse(string s, IFormatProvider? provider) => default; + public static bool TryParse(string? s, IFormatProvider? provider, out Public result) { result = default; return true; } + } + public readonly struct Explicit : IParsable + { + static Explicit IParsable.Parse(string s, IFormatProvider? provider) => default; + static bool IParsable.TryParse(string? s, IFormatProvider? provider, out Explicit result) { result = default; return true; } + } + public readonly struct Forbidden : IParsable + { + [Obsolete("x", true)] public static Forbidden Parse(string s, IFormatProvider? provider) => default; + public static bool TryParse(string? s, IFormatProvider? provider, out Forbidden result) { result = default; return true; } + } + public readonly struct ByRef : IParsable + { + public static ByRef Parse(ref string s, IFormatProvider? provider) => default; + static ByRef IParsable.Parse(string s, IFormatProvider? provider) => default; + static bool IParsable.TryParse(string? s, IFormatProvider? provider, out ByRef result) { result = default; return true; } + } + public readonly struct GenericMethod : IParsable + { + public static GenericMethod Parse(string s, IFormatProvider? provider) => default; + static GenericMethod IParsable.Parse(string s, IFormatProvider? provider) => default; + static bool IParsable.TryParse(string? s, IFormatProvider? provider, out GenericMethod result) { result = default; return true; } + } + public readonly struct WithOperator : IParsable + { + public static WithOperator Parse(string s, IFormatProvider? provider) => default; + public static bool TryParse(string? s, IFormatProvider? provider, out WithOperator result) { result = default; return true; } + public static implicit operator WithOperator(string value) => default; + } + public readonly struct Box : IParsable> + { + public static Box Parse(string s, IFormatProvider? provider) => default; + public static bool TryParse(string? s, IFormatProvider? provider, out Box result) { result = default; return true; } + } + public class Tests + { + [Test] + public void Generic(Box box) { } + [Test, Arguments("a", "b", "c", "d", "e", "f")] + public void Parse(Public a, Explicit b, Forbidden c, WithOperator d, ByRef e, GenericMethod f) { } + } + """; + var generated = await GenerateTypeConverters(source); + + await Assert.That(generated).Contains("global::Public converted = global::Public.Parse(sourceTypedValue, global::System.Globalization.CultureInfo.InvariantCulture);"); + await Assert.That(generated).DoesNotContain("Explicit.Parse("); + await Assert.That(generated).DoesNotContain("Forbidden.Parse("); + await Assert.That(generated).DoesNotContain("WithOperator.Parse("); + await Assert.That(generated).DoesNotContain("ByRef.Parse("); + await Assert.That(generated).DoesNotContain("GenericMethod.Parse("); + await Assert.That(generated).DoesNotContain("global::Box<"); + } +#endif + [Test] public async Task GeneratesTypeConverterConversionsFromString() { diff --git a/tests/TUnit.TestProject/StringToParsableArgumentsTests.cs b/tests/TUnit.TestProject/StringToParsableArgumentsTests.cs index de84a541a0..eee8f81c52 100644 --- a/tests/TUnit.TestProject/StringToParsableArgumentsTests.cs +++ b/tests/TUnit.TestProject/StringToParsableArgumentsTests.cs @@ -44,6 +44,20 @@ public async Task TimeOnly_From_String(TimeOnly time) { await Assert.That(time).IsEqualTo(new TimeOnly(14, 30, 0)); } + + [Test] + [Arguments("123")] + public async Task NInt_From_String(nint value) + { + await Assert.That(value).IsEqualTo((nint)123); + } + + [Test] + [Arguments("123")] + public async Task NUInt_From_String(nuint value) + { + await Assert.That(value).IsEqualTo((nuint)123); + } #endif [Test]