TUnit: Bug - Code Generator fails on string escape sequences and nullable annotated bytes.

I have a branch for my project, trying to convert to TUnit - https://github.com/SharpMUSH/SharpMUSH/tree/TUnit

However, when I run a build, it fails on the CodeGeneration step of TUnit. I would inspect it, myself to see what lies at the source of this, but the files are not there when the generation fails.

Build failure: https://github.com/SharpMUSH/SharpMUSH/actions/runs/10967113289/job/30456269303

D:\SharpMUSH\SharpMUSH.Tests\obj\Debug\net8.0\TUnit.Engine.SourceGenerator\TUnit.Engine.SourceGenerator.CodeGenerators.TestsGenerator\ANSI_StringFunctionUnitTests_6d4f9a184b1643f1aa38939779ad444e.Generated.cs(21,38): error CS1525: Invalid expression term ';' [D:\SharpMUSH\SharpMUSH.Tests\SharpMUSH.Tests.csproj]
D:\SharpMUSH\SharpMUSH.Tests\obj\Debug\net8.0\TUnit.Engine.SourceGenerator\TUnit.Engine.SourceGenerator.CodeGenerators.TestsGenerator\ANSI_StringFunctionUnitTests_63f9bdb954cb4113951ebca02dec1a3f.Generated.cs(21,38): error CS1525: Invalid expression term ';' [D:\SharpMUSH\SharpMUSH.Tests\SharpMUSH.Tests.csproj]
D:\SharpMUSH\SharpMUSH.Tests\obj\Debug\net8.0\TUnit.Engine.SourceGenerator\TUnit.Engine.SourceGenerator.CodeGenerators.TestsGenerator\ANSIBackground_StringFunctionUnitTests_e019f1f3687a4844b495b947601aaf7f.Generated.cs(21,38): error CS1525: Invalid expression term ';' [D:\SharpMUSH\SharpMUSH.Tests\SharpMUSH.Tests.csproj]
D:\SharpMUSH\SharpMUSH.Tests\obj\Debug\net8.0\TUnit.Engine.SourceGenerator\TUnit.Engine.SourceGenerator.CodeGenerators.TestsGenerator\ANSIBackground_StringFunctionUnitTests_57c663cdb02d4e30bc56189193dec533.Generated.cs(21,38): error CS1525: Invalid expression term ';' [D:\SharpMUSH\SharpMUSH.Tests\SharpMUSH.Tests.csproj]
D:\SharpMUSH\SharpMUSH.Tests\obj\Debug\net8.0\TUnit.Engine.SourceGenerator\TUnit.Engine.SourceGenerator.CodeGenerators.TestsGenerator\Test_FunctionUnitTests_2fa6f6d356ab4af3b6a65198d6e38199.Generated.cs(18,50): error CS1009: Unrecognized escape sequence [D:\SharpMUSH\SharpMUSH.Tests\SharpMUSH.Tests.csproj]
D:\SharpMUSH\SharpMUSH.Tests\obj\Debug\net8.0\TUnit.Engine.SourceGenerator\TUnit.Engine.SourceGenerator.CodeGenerators.TestsGenerator\Test_FunctionUnitTests_2fa6f6d356ab4af3b6a65198d6e38199.Generated.cs(54,37): error CS1009: Unrecognized escape sequence [D:\SharpMUSH\SharpMUSH.Tests\SharpMUSH.Tests.csproj]
D:\SharpMUSH\SharpMUSH.Tests\obj\Debug\net8.0\TUnit.Engine.SourceGenerator\TUnit.Engine.SourceGenerator.CodeGenerators.TestsGenerator\Test_FunctionUnitTests_e11ab05ffb2c4b0496addd4cb43df1ed.Generated.cs(19,43): error CS1009: Unrecognized escape sequence [D:\SharpMUSH\SharpMUSH.Tests\SharpMUSH.Tests.csproj]
D:\SharpMUSH\SharpMUSH.Tests\obj\Debug\net8.0\TUnit.Engine.SourceGenerator\TUnit.Engine.SourceGenerator.CodeGenerators.TestsGenerator\Test_FunctionUnitTests_e11ab05ffb2c4b0496addd4cb43df1ed.Generated.cs(54,53): error CS1009: Unrecognized escape sequence [D:\SharpMUSH\SharpMUSH.Tests\SharpMUSH.Tests.csproj]
D:\SharpMUSH\SharpMUSH.Tests\obj\Debug\net8.0\TestPlatformEntryPoint.cs(10,66): error CS8892: Method 'TestingPlatformEntryPoint.Main(string[])' will not be used as an entry point because a synchronous entry point 'AutoGeneratedProgram.Main(string[])' was found. [D:\SharpMUSH\SharpMUSH.Tests\SharpMUSH.Tests.csproj]

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (8 by maintainers)

Most upvoted comments

Cheers for the repro. Can you raise a separate issue for the byte issue and I’ll take a look tomorrow if I can!

That did the trick. And the tests are running. Some fixing left to do on my end, and I’ll get back to you again.

Will do in a few hours, and I’ll report back to you!

I’ll see if I can figure out TUnit’s source generation on my side while you are busy, and try to generate a Pull Review. But no guarantees.

https://github.com/HarryCordewener/TUnit/tree/ISSUE%23575-Failing-CodeGeneration

Edit: I am likely missing something obvious. Because changes to TypedConstantParser.cs and Arguments.cs’s MapValue are not adjusting the code generation when I emit them into TUnit.TestProject.

// <------- this should not be empty

What’s your test class/method look like?

Just added the origins of these.

Thanks! I’m a bit busy the next few days so might have to leave it with me but I’ll try and reproduce and get a fix out. Thanks for the report

// <auto-generated/>
using global::TUnit.Core;
using global::TUnit.Engine;

namespace TUnit.Engine;

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
file partial class Test_FunctionUnitTests
{
	[global::System.Runtime.CompilerServices.ModuleInitializer]
	public static void Initialise()
	{
		try
		{
			var methodInfo = typeof(global::SharpMUSH.Tests.Parser.FunctionUnitTests).GetMethod("Test", 0, [typeof(global::System.String), typeof(global::System.String)]);
			var resettableClassFactory = new ResettableLazy<global::SharpMUSH.Tests.Parser.FunctionUnitTests>(() => new global::SharpMUSH.Tests.Parser.FunctionUnitTests());

			global::System.String methodArg0 = "strcat(foo\,dog)"; // <--- This fails, due to an invalid escape sequence.
			global::System.String methodArg1 = "foo,dog";

			TestRegistrar.RegisterTest<global::SharpMUSH.Tests.Parser.FunctionUnitTests>(new TestMetadata<global::SharpMUSH.Tests.Parser.FunctionUnitTests>
			{
				TestId = $"ArgumentsAttribute:TDAI2:SharpMUSH.Tests.Parser.FunctionUnitTests.Test:(System.String,System.String):0",
				TestClassArguments = [],
				TestMethodArguments = [methodArg0, methodArg1],
				InternalTestClassArguments = [],
				InternalTestMethodArguments = [new TestData(methodArg0, typeof(global::System.String), InjectedDataType.None)
				{
    				DisposeAfterTest = false,
				}, new TestData(methodArg1, typeof(global::System.String), InjectedDataType.None)
				{
    				DisposeAfterTest = false,
				}],
				CurrentRepeatAttempt = 0,
				RepeatLimit = 0,
				MethodInfo = methodInfo,
				ResettableClassFactory = resettableClassFactory,
				TestMethodFactory = (classInstance, cancellationToken) => AsyncConvert.Convert(() => classInstance.Test(methodArg0, methodArg1)),
				TestExecutor = DefaultExecutor.Instance,
				ClassConstructor = null,
				ParallelLimit = null,
				DisplayName = $"Test({methodArg0}, {methodArg1})",
				TestFilePath = @"D:\SharpMUSH\SharpMUSH.Tests\Parser\FunctionUnitTests.cs",
				TestLineNumber = 5,
				AttributeTypes = [ typeof(global::TUnit.Core.TestAttribute), typeof(global::TUnit.Core.ArgumentsAttribute) ],
			});
		}
		catch (global::System.Exception exception)
		{
			TestRegistrar.Failed($"ArgumentsAttribute:TDAI2:SharpMUSH.Tests.Parser.FunctionUnitTests.Test:(System.String,System.String):0", new FailedInitializationTest
			{
				TestId = $"ArgumentsAttribute:TDAI2:SharpMUSH.Tests.Parser.FunctionUnitTests.Test:(System.String,System.String):0",
				TestName = "Test",
				DisplayName = "Test(\"strcat(foo\,dog)\", \"foo,dog\")",
				TestFilePath = @"D:\SharpMUSH\SharpMUSH.Tests\Parser\FunctionUnitTests.cs",
				TestLineNumber = 5,
				Exception = exception,
			});
		}
	}
}

This originates from here. You may be able to source-generate using @"string" string formats instead of a plain "string".

public class FunctionUnitTests : BaseUnitTest
{
	[Test]
	[Arguments("strcat(strcat(),wi`th a[strcat(strcat(strcat(depth of 5)))])","wi`th adepth of 5")]
	// [Arguments("strcat(strcat(dog)", "strcat(dog")] // Currently Illegal according to the Parser. Fix maybe needed.
	[Arguments("strcat(foo\\,dog)", "foo,dog")]
	[Arguments("strcat(foo\\\\,dog)", "foo\\dog")]
	[Arguments("strcat(foo,-dog))", "foo-dog)")]
	[Arguments("\\t", "t")]
	[Arguments("add(1,5)","6")]
	[Arguments("add(1,add(2,3),add(2,2))", "10")]
	[Arguments("add(1,2)[add(5,5)]", "310")]
	[Arguments("add(1,2)[add(5,5)]word()", "310word()")]
	public async Task Test(string str, string? expected = null)
	{
		Console.WriteLine("Testing: {0}", str);
		var parser = TestParser();
		var result = (await parser.FunctionParse(MModule.single(str)))?.Message?.ToString();

		Console.WriteLine(string.Join("", result));

		if (expected != null)
		{
			await Assert.That(result).IsEqualTo(expected);
		}
	}
}