Files
Terminal.Gui/Tests/UnitTestsParallelizable/Text/StringTests.cs
BDisp fc818b0274 Fixes #4382. StringExtensions.GetColumns method should only return the total text width and not the sum of all runes width (#4383)
* Fixes #4382. StringExtensions.GetColumns method should only return the total text width and not the sum of all runes width

* Trying to fix unit test error

* Update StringExtensions.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Add unit test to prove that null and empty string doesn't not throws anything.

---------

Co-authored-by: Tig <tig@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-12 10:22:51 -07:00

83 lines
2.7 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace UnitTests_Parallelizable.TextTests;
#nullable enable
public class StringTests
{
[Fact]
public void TestGetColumns_Empty ()
{
var str = string.Empty;
Assert.Equal (0, str.GetColumns ());
}
[Theory]
[InlineData ("a", 1)]
[InlineData ("á", 1)]
[InlineData ("ab", 2)]
[InlineData ("áé", 2)]
[InlineData ("abc", 3)]
[InlineData ("áéí", 3)]
[InlineData ("abcd", 4)]
public void TestGetColumns_MultiRune (string str, int expected) { Assert.Equal (expected, str.GetColumns ()); }
// Test non-BMP codepoints
// Face with Tears of Joy Emoji (😂), Unicode U+1F602 is 2 columns wide
[Theory]
[InlineData ("😂", 2)]
[InlineData ("😂😂", 4)]
public void TestGetColumns_MultiRune_NonBMP (string str, int expected) { Assert.Equal (expected, str.GetColumns ()); }
// Test known wide codepoints
[Theory]
[InlineData ("🙂", 2)]
[InlineData ("a🙂", 3)]
[InlineData ("🙂a", 3)]
[InlineData ("👨‍👩‍👦‍👦", 2)]
[InlineData ("👨‍👩‍👦‍👦🙂", 4)]
[InlineData ("👨👩👦👦🙂a", 5)]
[InlineData ("👨👩👦👦a🙂", 5)]
[InlineData ("👨‍👩‍👦‍👦👨‍👩‍👦‍👦", 4)]
[InlineData ("山", 2)] // The character for "mountain" in Chinese/Japanese/Korean (山), Unicode U+5C71
[InlineData ("山🙂", 4)] // The character for "mountain" in Chinese/Japanese/Korean (山), Unicode U+5C71
//[InlineData ("\ufe20\ufe21", 2)] // Combining Ligature Left Half ︠ - U+fe20 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
// // Combining Ligature Right Half - U+fe21 -https://github.com/microsoft/terminal/blob/main/src/types/unicode_width_overrides.xml
public void TestGetColumns_MultiRune_WideBMP (string str, int expected) { Assert.Equal (expected, str.GetColumns ()); }
[Fact]
public void TestGetColumns_Null ()
{
string? str = null;
Assert.Equal (0, str!.GetColumns ());
}
[Fact]
public void TestGetColumns_SingleRune ()
{
var str = "a";
Assert.Equal (1, str.GetColumns ());
}
[Fact]
public void TestGetColumns_Zero_Width ()
{
var str = "\u200D";
Assert.Equal (0, str.GetColumns ());
}
[Theory]
[InlineData (null)]
[InlineData ("")]
public void TestGetColumns_Does_Not_Throws_With_Null_And_Empty_String (string? text)
{
if (text is null)
{
Assert.Equal (0, StringExtensions.GetColumns (text!));
}
else
{
Assert.Equal (0, text.GetColumns ());
}
}
}