Files
Terminal.Gui/Tests/UnitTestsParallelizable/Drivers/AddRuneTests.cs
Tig 4c772bd5f3 Fixes #4497 - makes replacement char conifgurable (#4498)
* Add Glyphs.ReplacementChar config property

Introduced Glyphs.ReplacementChar to allow overriding the Unicode replacement character, defaulting to a space (' '). Updated both config.json and Glyphs.cs with this property, scoped to ThemeScope and documented as an override for Rune.ReplacementChar.

* Standardize to Glyphs.ReplacementChar for wide char invalidation

Replaced all uses of Rune.ReplacementChar.ToString() with Glyphs.ReplacementChar.ToString() in OutputBufferImpl and related tests. This ensures consistent use of the replacement character when invalidating or overwriting wide characters in the output buffer.

* Add configurable wide glyph replacement chars to OutputBuffer

Allows setting custom replacement characters for wide glyphs that cannot fit in the available space via IOutputBuffer.SetReplacementChars. Updated IDriver to expose GetOutputBuffer. All code paths and tests now use the configurable characters, improving testability and flexibility. Tests now use '①' and '②' for clarity instead of the default replacement character.

* Fixed warnings.

* Update IOutputBuffer.cs

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

* Add tests for wide char clipping edge cases in OutputBuffer

Added three unit tests to OutputBufferWideCharTests.cs to verify and document OutputBufferImpl's behavior when wide (double-width) characters are written at the edges of a clipping region. Tests cover cases where the first or second column of a wide character is outside the clip, as well as when both columns are inside. The tests assert correct use of replacement characters, dirty flags, and column advancement, and document that certain code paths are currently unreachable due to IsValidLocation checks.

* Clarify dead code path with explanatory comments

Added comments to mark a rarely executed code path as dead code, noting it is apparently never called. Referenced the related test scenario AddStr_WideChar_FirstColumnOutsideClip_SecondColumnInside_CurrentBehavior for context. No functional changes were made.

* Remove dead code for wide char partial clip handling

Removed unreachable code that handled the case where the first column of a wide character is outside the clipping region but the second column is inside. This logic was marked as dead code and never called. Now, only the cases where the second column is outside the clip or both columns are in bounds are handled. This simplifies the code and removes unnecessary checks.

* Replaces Glyphs.ReplacementChar with Glyphs.WideGlyphReplacement to clarify its use for clipped wide glyphs. Updates IOutputBuffer to use SetWideGlyphReplacement (single Rune) instead of SetReplacementChars (two Runes). Refactors OutputBufferImpl and all test code to use the new property and method. Removes second-column replacement logic, simplifying the API and improving consistency. Updates comments and test assertions to match the new naming and behavior.

* Update themes in config.json and add new UI Catalog props

Renamed "UI Catalog Theme" to "UI Catalog" and removed the
"Glyphs.ReplacementChar" property. Added several new properties
to the "UI Catalog" theme, including default shadow, highlight
states, button alignment, and separator line style. Also added
"Glyphs.WideGlyphReplacement" to the "Hot Dog Stand" theme.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-15 09:26:52 -07:00

211 lines
6.6 KiB
C#

using System.Buffers;
using System.Text;
using UnitTests;
using Xunit.Abstractions;
namespace DriverTests;
public class AddRuneTests (ITestOutputHelper output) : FakeDriverBase
{
[Fact]
public void AddRune ()
{
IDriver driver = CreateFakeDriver ();
driver.Rows = 25;
driver.Cols = 80;
driver.AddRune (new Rune ('a'));
Assert.Equal ("a", driver.Contents? [0, 0].Grapheme);
driver.Dispose ();
}
[Fact]
public void AddRune_Accented_Letter_With_Three_Combining_Unicode_Chars ()
{
IDriver driver = CreateFakeDriver ();
var expected = "ắ";
var text = "\u1eaf";
driver.AddStr (text);
Assert.Equal (expected, driver.Contents! [0, 0].Grapheme);
Assert.Equal (" ", driver.Contents [0, 1].Grapheme);
driver.ClearContents ();
driver.Move (0, 0);
expected = "ắ";
text = "\u0103\u0301";
driver.AddStr (text);
Assert.Equal (expected, driver.Contents [0, 0].Grapheme);
Assert.Equal (" ", driver.Contents [0, 1].Grapheme);
driver.ClearContents ();
driver.Move (0, 0);
expected = "ắ";
text = "\u0061\u0306\u0301";
driver.AddStr (text);
Assert.Equal (expected, driver.Contents [0, 0].Grapheme);
Assert.Equal (" ", driver.Contents [0, 1].Grapheme);
// var s = "a\u0301\u0300\u0306";
// DriverAsserts.AssertDriverContentsWithFrameAre (@"
//ắ", output);
// tf.Text = "\u1eaf";
// Application.Refresh ();
// DriverAsserts.AssertDriverContentsWithFrameAre (@"
//ắ", output);
// tf.Text = "\u0103\u0301";
// Application.Refresh ();
// DriverAsserts.AssertDriverContentsWithFrameAre (@"
//ắ", output);
// tf.Text = "\u0061\u0306\u0301";
// Application.Refresh ();
// DriverAsserts.AssertDriverContentsWithFrameAre (@"
//ắ", output);
driver.Dispose ();
}
[Fact]
public void AddRune_InvalidLocation_DoesNothing ()
{
IDriver driver = CreateFakeDriver ();
driver.Move (driver.Cols, driver.Rows);
driver.AddRune ('a');
for (var col = 0; col < driver.Cols; col++)
{
for (var row = 0; row < driver.Rows; row++)
{
Assert.Equal (" ", driver.Contents? [row, col].Grapheme);
}
}
driver.Dispose ();
}
[Fact]
public void AddRune_MovesToNextColumn ()
{
IDriver driver = CreateFakeDriver ();
driver.AddRune ('a');
Assert.Equal ("a", driver.Contents? [0, 0].Grapheme);
Assert.Equal (0, driver.Row);
Assert.Equal (1, driver.Col);
driver.AddRune ('b');
Assert.Equal ("b", driver.Contents? [0, 1].Grapheme);
Assert.Equal (0, driver.Row);
Assert.Equal (2, driver.Col);
// Move to the last column of the first row
int lastCol = driver.Cols - 1;
driver.Move (lastCol, 0);
Assert.Equal (0, driver.Row);
Assert.Equal (lastCol, driver.Col);
// Add a rune to the last column of the first row; should increment the row or col even though it's now invalid
driver.AddRune ('c');
Assert.Equal ("c", driver.Contents? [0, lastCol].Grapheme);
Assert.Equal (lastCol + 1, driver.Col);
// Add a rune; should succeed but do nothing as it's outside of Contents
driver.AddRune ('d');
Assert.Equal (lastCol + 2, driver.Col);
for (var col = 0; col < driver.Cols; col++)
{
for (var row = 0; row < driver.Rows; row++)
{
Assert.NotEqual ("d", driver.Contents? [row, col].Grapheme);
}
}
driver.Dispose ();
}
[Fact]
public void AddRune_MovesToNextColumn_Wide ()
{
IDriver driver = CreateFakeDriver ();
// 🍕 Slice of Pizza "\U0001F355"
OperationStatus operationStatus = Rune.DecodeFromUtf16 ("\U0001F355", out Rune rune, out int charsConsumed);
Assert.Equal (OperationStatus.Done, operationStatus);
Assert.Equal (charsConsumed, rune.Utf16SequenceLength);
Assert.Equal (2, rune.GetColumns ());
driver.AddRune (rune);
Assert.Equal (rune.ToString (), driver.Contents? [0, 0].Grapheme);
Assert.Equal (0, driver.Row);
Assert.Equal (2, driver.Col);
//driver.AddRune ('b');
//Assert.Equal ((Text)'b', driver.Contents [0, 1].Text);
//Assert.Equal (0, driver.Row);
//Assert.Equal (2, driver.Col);
//// Move to the last column of the first row
//var lastCol = driver.Cols - 1;
//driver.Move (lastCol, 0);
//Assert.Equal (0, driver.Row);
//Assert.Equal (lastCol, driver.Col);
//// Add a rune to the last column of the first row; should increment the row or col even though it's now invalid
//driver.AddRune ('c');
//Assert.Equal ((Text)'c', driver.Contents [0, lastCol].Text);
//Assert.Equal (lastCol + 1, driver.Col);
//// Add a rune; should succeed but do nothing as it's outside of Contents
//driver.AddRune ('d');
//Assert.Equal (lastCol + 2, driver.Col);
//for (var col = 0; col < driver.Cols; col++) {
// for (var row = 0; row < driver.Rows; row++) {
// Assert.NotEqual ((Text)'d', driver.Contents [row, col].Text);
// }
//}
driver.Dispose ();
}
[Fact]
public void AddStr_Glyph_On_Second_Cell_Of_Wide_Glyph_Outputs_Correctly ()
{
IDriver? driver = CreateFakeDriver ();
driver.SetScreenSize (6, 3);
driver.GetOutputBuffer ().SetWideGlyphReplacement ((Rune)'①');
driver!.Clip = new (driver.Screen);
driver.Move (1, 0);
driver.AddStr ("┌");
driver.Move (2, 0);
driver.AddStr ("─");
driver.Move (3, 0);
driver.AddStr ("┐");
driver.Clip.Exclude (new Region (new (1, 0, 3, 1)));
driver.Move (0, 0);
driver.AddStr ("🍎🍎🍎🍎");
DriverAssert.AssertDriverContentsAre (
"""
🍎
""",
output,
driver);
driver.Refresh ();
DriverAssert.AssertDriverOutputIs (@"\x1b[38;2;0;0;0m\x1b[48;2;0;0;0m①┌─┐🍎\x1b[38;2;255;255;255m\x1b[48;2;0;0;0m",
output, driver);
}
}