diff --git a/TUnit.Aspire/AspireFixture.cs b/TUnit.Aspire/AspireFixture.cs index 85486451dbe..2e5dc117df2 100644 --- a/TUnit.Aspire/AspireFixture.cs +++ b/TUnit.Aspire/AspireFixture.cs @@ -445,13 +445,13 @@ private async Task WaitForResourcesWithFailFastAsync( var sb = new StringBuilder(); sb.Append("Resources not ready: ["); - sb.Append(string.Join(", ", pending.Select(n => $"'{n}'"))); + sb.AppendJoin(", ", pending.Select(n => $"'{n}'")); sb.Append(']'); if (readySet.Count > 0) { sb.Append(". Resources ready: ["); - sb.Append(string.Join(", ", readySet.Select(n => $"'{n}'"))); + sb.AppendJoin(", ", readySet.Select(n => $"'{n}'")); sb.Append(']'); } diff --git a/TUnit.Assertions.SourceGenerator/Generators/AssertionMethodGenerator.cs b/TUnit.Assertions.SourceGenerator/Generators/AssertionMethodGenerator.cs index 610d8d59fb7..406ef8fcca6 100644 --- a/TUnit.Assertions.SourceGenerator/Generators/AssertionMethodGenerator.cs +++ b/TUnit.Assertions.SourceGenerator/Generators/AssertionMethodGenerator.cs @@ -780,7 +780,7 @@ private static void GenerateAssertConditionClassForMethod(SourceProductionContex // Extension method - call it like an instance method sourceBuilder.Append($" var result = actualValue.{methodName}("); var paramList = parameters.Select(p => $"_{p.Name}").ToArray(); - sourceBuilder.Append(string.Join(", ", paramList)); + sourceBuilder.AppendJoin(", ", paramList); sourceBuilder.AppendLine(");"); } else if (attributeData.TreatAsInstance) @@ -829,7 +829,7 @@ private static void GenerateAssertConditionClassForMethod(SourceProductionContex // Instance method on the target type itself sourceBuilder.Append($" var result = actualValue.{methodName}("); var paramList = parameters.Select(p => $"_{p.Name}").ToArray(); - sourceBuilder.Append(string.Join(", ", paramList)); + sourceBuilder.AppendJoin(", ", paramList); sourceBuilder.AppendLine(");"); } } @@ -854,7 +854,7 @@ private static void GenerateAssertConditionClassForMethod(SourceProductionContex sourceBuilder.AppendLine($" var instance = new {containingType.ToDisplayString()}();"); sourceBuilder.Append($" var result = instance.{methodName}("); var paramList = parameters.Select(p => $"_{p.Name}").ToArray(); - sourceBuilder.Append(string.Join(", ", paramList)); + sourceBuilder.AppendJoin(", ", paramList); sourceBuilder.AppendLine(");"); } } @@ -863,7 +863,7 @@ private static void GenerateAssertConditionClassForMethod(SourceProductionContex // Default instance method behavior sourceBuilder.Append($" var result = actualValue.{methodName}("); var paramList = parameters.Select(p => $"_{p.Name}").ToArray(); - sourceBuilder.Append(string.Join(", ", paramList)); + sourceBuilder.AppendJoin(", ", paramList); sourceBuilder.AppendLine(");"); } } diff --git a/TUnit.Core.SourceGenerator/CodeGenerationHelpers.cs b/TUnit.Core.SourceGenerator/CodeGenerationHelpers.cs index 4ebec87f725..7cb27dff05a 100644 --- a/TUnit.Core.SourceGenerator/CodeGenerationHelpers.cs +++ b/TUnit.Core.SourceGenerator/CodeGenerationHelpers.cs @@ -199,7 +199,7 @@ public static string GenerateAttributeInstantiation(AttributeData attr, Immutabl } } - writer.Append(string.Join(", ", argStrings)); + writer.AppendJoin(", ", argStrings); } writer.Append(")"); @@ -208,7 +208,7 @@ public static string GenerateAttributeInstantiation(AttributeData attr, Immutabl { writer.Append(" { "); var namedArgs = attr.NamedArguments.Select(na => $"{na.Key} = {TypedConstantParser.GetRawTypedConstantValue(na.Value)}"); - writer.Append(string.Join(", ", namedArgs)); + writer.AppendJoin(", ", namedArgs); writer.Append(" }"); } @@ -311,7 +311,7 @@ public static string GenerateTestAttributes(IMethodSymbol methodSymbol) attributeStrings.Add(GenerateAttributeInstantiation(attr)); } - writer.Append(string.Join(", ", attributeStrings)); + writer.AppendJoin(", ", attributeStrings); writer.AppendLine(" },"); } diff --git a/TUnit.Core.SourceGenerator/CodeWriter.cs b/TUnit.Core.SourceGenerator/CodeWriter.cs index f2f6013a642..04163d5e28f 100644 --- a/TUnit.Core.SourceGenerator/CodeWriter.cs +++ b/TUnit.Core.SourceGenerator/CodeWriter.cs @@ -70,6 +70,17 @@ public ICodeWriter Append(string text) return this; } + public ICodeWriter AppendJoin(string separator, IEnumerable values) + { + if (_isNewLine) + { + _builder.Append(GetIndentation(_indentLevel)); + _isNewLine = false; + } + _builder.AppendJoin(separator, values); + return this; + } + public int IndentLevel => _indentLevel; /// diff --git a/TUnit.Core.SourceGenerator/ICodeWriter.cs b/TUnit.Core.SourceGenerator/ICodeWriter.cs index 9fbda0b0f2b..f9b2b9eb1c4 100644 --- a/TUnit.Core.SourceGenerator/ICodeWriter.cs +++ b/TUnit.Core.SourceGenerator/ICodeWriter.cs @@ -16,6 +16,11 @@ public interface ICodeWriter : IDisposable /// ICodeWriter Append(string text); + /// + /// Appends the string representation of each element separated by the given separator. + /// + ICodeWriter AppendJoin(string separator, IEnumerable values); + /// /// Appends multiple lines of code. ///