From 76cbcf5647b5845e0626bf977e5111e71e216e81 Mon Sep 17 00:00:00 2001 From: Tom Longhurst <30480171+thomhurst@users.noreply.github.com> Date: Mon, 27 Jan 2025 10:59:50 +0000 Subject: [PATCH] Fix Null Constants in Argument Attributes --- .../Bugs/1692/Tests1692.cs | 17 ++ .../Tests1692.Test.verified.txt | 168 ++++++++++++++++++ .../FullyQualifiedWithGlobalPrefixRewriter.cs | 2 +- TUnit.TestProject/Bugs/1692/Tests.cs | 14 ++ 4 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 TUnit.Core.SourceGenerator.Tests/Bugs/1692/Tests1692.cs create mode 100644 TUnit.Core.SourceGenerator.Tests/Tests1692.Test.verified.txt create mode 100644 TUnit.TestProject/Bugs/1692/Tests.cs diff --git a/TUnit.Core.SourceGenerator.Tests/Bugs/1692/Tests1692.cs b/TUnit.Core.SourceGenerator.Tests/Bugs/1692/Tests1692.cs new file mode 100644 index 0000000000..eff568b723 --- /dev/null +++ b/TUnit.Core.SourceGenerator.Tests/Bugs/1692/Tests1692.cs @@ -0,0 +1,17 @@ +using TUnit.Core.SourceGenerator.CodeGenerators; + +namespace TUnit.Core.SourceGenerator.Tests.Bugs._1692; + +internal class Tests1692 : TestsBase +{ + [Test] + public Task Test() => RunTest(Path.Combine(Git.RootDirectory.FullName, + "TUnit.TestProject", + "Bugs", + "1692", + "Tests.cs"), + async generatedFiles => + { + await Assert.That(generatedFiles.Length).IsEqualTo(1); + }); +} \ No newline at end of file diff --git a/TUnit.Core.SourceGenerator.Tests/Tests1692.Test.verified.txt b/TUnit.Core.SourceGenerator.Tests/Tests1692.Test.verified.txt new file mode 100644 index 0000000000..d4ea1237c4 --- /dev/null +++ b/TUnit.Core.SourceGenerator.Tests/Tests1692.Test.verified.txt @@ -0,0 +1,168 @@ +[ +// +#pragma warning disable +using global::System.Linq; +using global::System.Reflection; +using global::TUnit.Core; +using global::TUnit.Core.Extensions; + +namespace TUnit.SourceGenerated; + +[global::System.Diagnostics.StackTraceHidden] +[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] +file partial class Tests : global::TUnit.Core.Interfaces.SourceGenerator.ITestSource +{ + [global::System.Runtime.CompilerServices.ModuleInitializer] + public static void Initialise() + { + global::TUnit.Core.SourceRegistrar.Register(new Tests()); + } + public global::System.Collections.Generic.IReadOnlyList CollectTests(string sessionId) + { + return + [ + ..Tests0(sessionId), + ..Tests1(sessionId), + ]; + } + private global::System.Collections.Generic.List Tests0(string sessionId) + { + global::System.Collections.Generic.List nodes = []; + var classDataIndex = 0; + var testMethodDataIndex = 0; + try + { + var testClassType = typeof(global::TUnit.TestProject.Bugs._1692.Tests); + var methodInfo = global::TUnit.Core.Helpers.MethodInfoRetriever.GetMethodInfo(typeof(global::TUnit.TestProject.Bugs._1692.Tests), "NullTest", 0, [typeof(string)]); + + var testBuilderContext = new global::TUnit.Core.TestBuilderContext(); + var testBuilderContextAccessor = new global::TUnit.Core.TestBuilderContextAccessor(testBuilderContext); + + string methodArg = (string)(null); + + var resettableClassFactoryDelegate = () => new ResettableLazy(() => + new global::TUnit.TestProject.Bugs._1692.Tests() + , sessionId, testBuilderContext); + + var resettableClassFactory = resettableClassFactoryDelegate(); + + nodes.Add(new TestMetadata + { + TestId = $"global::TUnit.Core.ArgumentsAttribute:{testMethodDataIndex}:TL-ARGS0:TUnit.TestProject.Bugs._1692.Tests.NullTest(string):0", + TestClassArguments = [], + TestMethodArguments = [methodArg], + TestClassProperties = [], + CurrentRepeatAttempt = 0, + RepeatLimit = 0, + MethodInfo = methodInfo, + ResettableClassFactory = resettableClassFactory, + TestMethodFactory = (classInstance, cancellationToken) => AsyncConvert.Convert(() => classInstance.NullTest(methodArg)), + TestFilePath = @"", + TestLineNumber = 10, + TestAttributes = [ new global::TUnit.Core.TestAttribute() +{ + +}, new global::TUnit.Core.ArgumentsAttribute(null) +{ + +}, new global::TUnit.Core.ArgumentsAttribute(null) +{ + +} ], + ClassAttributes = [ ], + AssemblyAttributes = [ ], + DataAttributes = [ ], + TestBuilderContext = testBuilderContext, + }); + resettableClassFactory = resettableClassFactoryDelegate(); + testBuilderContext = new(); + testBuilderContextAccessor.Current = testBuilderContext; + } + catch (global::System.Exception exception) + { + nodes.Add(new global::TUnit.Core.FailedInitializationTest + { + TestId = $"global::TUnit.Core.ArgumentsAttribute:{testMethodDataIndex}:TL-ARGS0:TUnit.TestProject.Bugs._1692.Tests.NullTest(string):0", + TestClass = typeof(global::TUnit.TestProject.Bugs._1692.Tests), + ReturnType = global::TUnit.Core.Helpers.MethodInfoRetriever.GetMethodInfo(typeof(global::TUnit.TestProject.Bugs._1692.Tests), "NullTest", 0, [typeof(string)]).ReturnType, + ParameterTypeFullNames = [typeof(string)], + TestName = "NullTest", + TestFilePath = @"", + TestLineNumber = 10, + Exception = exception, + }); + } + return nodes; + } + private global::System.Collections.Generic.List Tests1(string sessionId) + { + global::System.Collections.Generic.List nodes = []; + var classDataIndex = 0; + var testMethodDataIndex = 0; + try + { + var testClassType = typeof(global::TUnit.TestProject.Bugs._1692.Tests); + var methodInfo = global::TUnit.Core.Helpers.MethodInfoRetriever.GetMethodInfo(typeof(global::TUnit.TestProject.Bugs._1692.Tests), "NullTest", 0, [typeof(string)]); + + var testBuilderContext = new global::TUnit.Core.TestBuilderContext(); + var testBuilderContextAccessor = new global::TUnit.Core.TestBuilderContextAccessor(testBuilderContext); + + string methodArg = null; + + var resettableClassFactoryDelegate = () => new ResettableLazy(() => + new global::TUnit.TestProject.Bugs._1692.Tests() + , sessionId, testBuilderContext); + + var resettableClassFactory = resettableClassFactoryDelegate(); + + nodes.Add(new TestMetadata + { + TestId = $"global::TUnit.Core.ArgumentsAttribute:{testMethodDataIndex}:TL-ARGS1:TUnit.TestProject.Bugs._1692.Tests.NullTest(string):0", + TestClassArguments = [], + TestMethodArguments = [methodArg], + TestClassProperties = [], + CurrentRepeatAttempt = 0, + RepeatLimit = 0, + MethodInfo = methodInfo, + ResettableClassFactory = resettableClassFactory, + TestMethodFactory = (classInstance, cancellationToken) => AsyncConvert.Convert(() => classInstance.NullTest(methodArg)), + TestFilePath = @"", + TestLineNumber = 10, + TestAttributes = [ new global::TUnit.Core.TestAttribute() +{ + +}, new global::TUnit.Core.ArgumentsAttribute(null) +{ + +}, new global::TUnit.Core.ArgumentsAttribute(null) +{ + +} ], + ClassAttributes = [ ], + AssemblyAttributes = [ ], + DataAttributes = [ ], + TestBuilderContext = testBuilderContext, + }); + resettableClassFactory = resettableClassFactoryDelegate(); + testBuilderContext = new(); + testBuilderContextAccessor.Current = testBuilderContext; + } + catch (global::System.Exception exception) + { + nodes.Add(new global::TUnit.Core.FailedInitializationTest + { + TestId = $"global::TUnit.Core.ArgumentsAttribute:{testMethodDataIndex}:TL-ARGS1:TUnit.TestProject.Bugs._1692.Tests.NullTest(string):0", + TestClass = typeof(global::TUnit.TestProject.Bugs._1692.Tests), + ReturnType = global::TUnit.Core.Helpers.MethodInfoRetriever.GetMethodInfo(typeof(global::TUnit.TestProject.Bugs._1692.Tests), "NullTest", 0, [typeof(string)]).ReturnType, + ParameterTypeFullNames = [typeof(string)], + TestName = "NullTest", + TestFilePath = @"", + TestLineNumber = 10, + Exception = exception, + }); + } + return nodes; + } +} + +] \ No newline at end of file diff --git a/TUnit.Core.SourceGenerator/CodeGenerators/Helpers/FullyQualifiedWithGlobalPrefixRewriter.cs b/TUnit.Core.SourceGenerator/CodeGenerators/Helpers/FullyQualifiedWithGlobalPrefixRewriter.cs index 54a75e7647..3068626c21 100644 --- a/TUnit.Core.SourceGenerator/CodeGenerators/Helpers/FullyQualifiedWithGlobalPrefixRewriter.cs +++ b/TUnit.Core.SourceGenerator/CodeGenerators/Helpers/FullyQualifiedWithGlobalPrefixRewriter.cs @@ -97,7 +97,7 @@ private static SyntaxNode Literal(object? constantValue) { return constantValue switch { - null => SyntaxFactory.LiteralExpression(SyntaxKind.NullKeyword), + null => SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression), string strValue => SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(strValue)), char charValue => SyntaxFactory.LiteralExpression(SyntaxKind.CharacterLiteralExpression, SyntaxFactory.Literal(charValue)), bool boolValue => boolValue ? SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression) diff --git a/TUnit.TestProject/Bugs/1692/Tests.cs b/TUnit.TestProject/Bugs/1692/Tests.cs new file mode 100644 index 0000000000..98a2afee2e --- /dev/null +++ b/TUnit.TestProject/Bugs/1692/Tests.cs @@ -0,0 +1,14 @@ +using TUnit.Assertions; +using TUnit.Assertions.Extensions; + +namespace TUnit.TestProject.Bugs._1692; + +public class Tests +{ + private const string? NullContent = null; + + [Test] + [Arguments(NullContent)] + [Arguments(null)] + public async Task NullTest(string? t) => await Assert.That(t).IsNull(); +} \ No newline at end of file