Skip to content

Commit

Permalink
Enable chaining from StringBuilder.AppendSmart() and StringBuilder.Ap…
Browse files Browse the repository at this point in the history
…pendLineSmart() (#452)

* Enable chaining from StringBuilder.AppendSmart() and StringBuilder.AppendLineSmart()
Fixes #451

* Add unit tests for chained StringBuilder.AppendSmart() and StringBuilder.AppendLineSmart()
#451
  • Loading branch information
Klikini authored Dec 4, 2024
1 parent ca6b424 commit 7eaca13
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
23 changes: 23 additions & 0 deletions src/SmartFormat.Tests/Extensions/SmartExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public void Test_AppendLine()
Assert.That(actual, Is.EqualTo("these are the args" + Environment.NewLine));
}

[Test]
public void Test_AppendLine_Chained()
{
var actual = new StringBuilder()
.AppendLineSmart("{0} {1} {2}", "these", "are", "the")
.Append("args")
.ToString();

Assert.That(actual, Is.EqualTo("these are the" + Environment.NewLine + "args"));
}

[Test]
public void Test_Append()
{
Expand All @@ -44,6 +55,18 @@ public void Test_Append()
Assert.That(actual, Is.EqualTo("these are the args"));
}

[Test]
public void Test_Append_Chained()
{
var actual = new StringBuilder()
.AppendSmart("{0} {1}", "these", "are")
.Append(' ')
.AppendSmart("{0} {1}", "the", "args")
.ToString();

Assert.That(actual, Is.EqualTo("these are the args"));
}

#endregion

#region : TextWriterTests :
Expand Down
10 changes: 6 additions & 4 deletions src/SmartFormat/SmartExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//
//
// Copyright SmartFormat Project maintainers and contributors.
// Licensed under the MIT license.

Expand All @@ -19,20 +19,22 @@ public static class SmartExtensions
/// <param name="sb">The StringBuilder that will be used for output</param>
/// <param name="format">The template that defines how the arguments are formatted</param>
/// <param name="args">A list of arguments to be used in formatting</param>
public static void AppendSmart(this StringBuilder sb, string format, params object[] args)
public static StringBuilder AppendSmart(this StringBuilder sb, string format, params object[] args)
{
var output = new StringOutput(sb);
Smart.Default.FormatInto(output, format, args);
return sb;
}

/// <summary> AppendLines a formatted string, using the same semantics as Smart.Format. </summary>
/// <param name="sb">The StringBuilder that will be used for output</param>
/// <param name="format">The template that defines how the arguments are formatted</param>
/// <param name="args">A list of arguments to be used in formatting</param>
public static void AppendLineSmart(this StringBuilder sb, string format, params object[] args)
public static StringBuilder AppendLineSmart(this StringBuilder sb, string format, params object[] args)
{
AppendSmart(sb, format, args);
sb.AppendLine();
return sb;
}

#endregion
Expand Down Expand Up @@ -72,4 +74,4 @@ public static string FormatSmart(this string format, params object[] args)
}

#endregion
}
}

0 comments on commit 7eaca13

Please sign in to comment.