From 7eaca1337fe4132733475c596cfe527fc213ecb5 Mon Sep 17 00:00:00 2001 From: Andy Castille Date: Wed, 4 Dec 2024 14:02:20 -0800 Subject: [PATCH] Enable chaining from StringBuilder.AppendSmart() and StringBuilder.AppendLineSmart() (#452) * Enable chaining from StringBuilder.AppendSmart() and StringBuilder.AppendLineSmart() Fixes #451 * Add unit tests for chained StringBuilder.AppendSmart() and StringBuilder.AppendLineSmart() #451 --- .../Extensions/SmartExtensionsTests.cs | 23 +++++++++++++++++++ src/SmartFormat/SmartExtensions.cs | 10 ++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/SmartFormat.Tests/Extensions/SmartExtensionsTests.cs b/src/SmartFormat.Tests/Extensions/SmartExtensionsTests.cs index 8ba77235..4a0c6165 100644 --- a/src/SmartFormat.Tests/Extensions/SmartExtensionsTests.cs +++ b/src/SmartFormat.Tests/Extensions/SmartExtensionsTests.cs @@ -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() { @@ -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 : diff --git a/src/SmartFormat/SmartExtensions.cs b/src/SmartFormat/SmartExtensions.cs index b19eda6f..91680488 100644 --- a/src/SmartFormat/SmartExtensions.cs +++ b/src/SmartFormat/SmartExtensions.cs @@ -1,4 +1,4 @@ -// +// // Copyright SmartFormat Project maintainers and contributors. // Licensed under the MIT license. @@ -19,20 +19,22 @@ public static class SmartExtensions /// The StringBuilder that will be used for output /// The template that defines how the arguments are formatted /// A list of arguments to be used in formatting - 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; } /// AppendLines a formatted string, using the same semantics as Smart.Format. /// The StringBuilder that will be used for output /// The template that defines how the arguments are formatted /// A list of arguments to be used in formatting - 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 @@ -72,4 +74,4 @@ public static string FormatSmart(this string format, params object[] args) } #endregion -} \ No newline at end of file +}