-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tweak ValueStringBuilder to support interpolated strings (#12876)
Tweak ValueStringBuilder to allow it to be used as an interpolated string handler One small usage in TypeExtensions. I'm going to be doing a larger change based on this as a follow-up.
- Loading branch information
1 parent
ec6a9f8
commit e0da5a8
Showing
4 changed files
with
144 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...ndows.Core/tests/System.Private.Windows.Core.Tests/System/Text/ValueStringBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Text; | ||
|
||
public class ValueStringBuilderTests | ||
{ | ||
[Fact] | ||
public void Append_ShouldAppendSingleString() | ||
{ | ||
using ValueStringBuilder builder = new(10); | ||
builder.Append("Hello"); | ||
builder.ToString().Should().Be("Hello"); | ||
} | ||
|
||
[Fact] | ||
public void Append_ShouldAppendMultipleStrings() | ||
{ | ||
using ValueStringBuilder builder = new(10); | ||
builder.Append("Hello"); | ||
builder.Append(", "); | ||
builder.Append("world!"); | ||
builder.ToString().Should().Be("Hello, world!"); | ||
} | ||
|
||
[Fact] | ||
public void Append_Char_ShouldAppendCharacters() | ||
{ | ||
using ValueStringBuilder builder = new(10); | ||
builder.Append('A'); | ||
builder.Append('B'); | ||
builder.Append('C'); | ||
builder.ToString().Should().Be("ABC"); | ||
} | ||
|
||
[Fact] | ||
public void Insert_ShouldInsertStringAtIndex() | ||
{ | ||
using ValueStringBuilder builder = new(10); | ||
builder.Append("Hello world!"); | ||
builder.Insert(6, "beautiful "); | ||
builder.ToString().Should().Be("Hello beautiful world!"); | ||
} | ||
|
||
[Fact] | ||
public void Length_ShouldReturnCorrectValue() | ||
{ | ||
using ValueStringBuilder builder = new(10); | ||
builder.Append("12345"); | ||
builder.Length.Should().Be(5); | ||
} | ||
|
||
[Fact] | ||
public void Capacity_ShouldIncreaseWhenExceeded() | ||
{ | ||
using ValueStringBuilder builder = new(10); | ||
builder.Append("This is a long string that exceeds the initial capacity."); | ||
builder.Capacity.Should().BeGreaterThan(10); | ||
builder.ToString().Should().Be("This is a long string that exceeds the initial capacity."); | ||
} | ||
|
||
[Theory] | ||
[InlineData(10)] | ||
[InlineData(100)] | ||
public void AsHandler_Int(int value) | ||
{ | ||
string result = TestFormat($"Hello, {value}!"); | ||
result.Should().Be($"Hello, {value}!"); | ||
} | ||
|
||
[Theory] | ||
[InlineData(DayOfWeek.Monday)] | ||
[InlineData(DayOfWeek.Friday)] | ||
public void AsHandler_Enum(DayOfWeek value) | ||
{ | ||
string result = TestFormat($"Hello, it's {value}!"); | ||
result.Should().Be($"Hello, it's {value}!"); | ||
} | ||
|
||
private static string TestFormat(ref ValueStringBuilder builder) => builder.ToStringAndClear(); | ||
} |