mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
Add 2 more TextFormatter Draw tests with multi-line support (22 test cases)
Co-authored-by: tig <585482+tig@users.noreply.github.com>
This commit is contained in:
@@ -15,62 +15,6 @@ public class TextFormatterTests
|
||||
public static IEnumerable<object []> CMGlyphs =>
|
||||
new List<object []> { new object [] { $"{Glyphs.LeftBracket} Say Hello 你 {Glyphs.RightBracket}", 16, 15 } };
|
||||
|
||||
[SetupFakeDriver]
|
||||
[Theory]
|
||||
[InlineData ("A", 1, 0, "")]
|
||||
[InlineData ("A", 0, 1, "")]
|
||||
[InlineData ("AB1 2", 2, 1, "2")]
|
||||
[InlineData ("AB12", 5, 1, "21BA")]
|
||||
[InlineData ("AB\n12", 5, 2, "21\nBA")]
|
||||
[InlineData ("ABC 123 456", 7, 2, "CBA \n654 321")]
|
||||
[InlineData ("こんにちは", 1, 1, "")]
|
||||
[InlineData ("こんにちは", 2, 1, "は")]
|
||||
[InlineData ("こんにちは", 5, 1, "はち")]
|
||||
[InlineData ("こんにちは", 10, 1, "はちにんこ")]
|
||||
[InlineData ("こんにちは\nAB\n12", 10, 3, "21 \nBA \nはちにんこ")]
|
||||
public void Draw_Horizontal_RightLeft_BottomTop (string text, int width, int height, string expectedText)
|
||||
{
|
||||
TextFormatter tf = new ()
|
||||
{
|
||||
Text = text,
|
||||
Direction = TextDirection.RightLeft_BottomTop
|
||||
};
|
||||
|
||||
tf.ConstrainToWidth = width;
|
||||
tf.ConstrainToHeight = height;
|
||||
tf.Draw (new (0, 0, width, height), Attribute.Default, Attribute.Default);
|
||||
|
||||
DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
|
||||
}
|
||||
|
||||
[SetupFakeDriver]
|
||||
[Theory]
|
||||
[InlineData ("A", 1, 0, "")]
|
||||
[InlineData ("A", 0, 1, "")]
|
||||
[InlineData ("AB1 2", 2, 1, "2")]
|
||||
[InlineData ("AB12", 5, 1, "21BA")]
|
||||
[InlineData ("AB\n12", 5, 2, "BA\n21")]
|
||||
[InlineData ("ABC 123 456", 7, 2, "654 321\nCBA ")]
|
||||
[InlineData ("こんにちは", 1, 1, "")]
|
||||
[InlineData ("こんにちは", 2, 1, "は")]
|
||||
[InlineData ("こんにちは", 5, 1, "はち")]
|
||||
[InlineData ("こんにちは", 10, 1, "はちにんこ")]
|
||||
[InlineData ("こんにちは\nAB\n12", 10, 3, "はちにんこ\nBA \n21 ")]
|
||||
public void Draw_Horizontal_RightLeft_TopBottom (string text, int width, int height, string expectedText)
|
||||
{
|
||||
TextFormatter tf = new ()
|
||||
{
|
||||
Text = text,
|
||||
Direction = TextDirection.RightLeft_TopBottom
|
||||
};
|
||||
|
||||
tf.ConstrainToWidth = width;
|
||||
tf.ConstrainToHeight = height;
|
||||
tf.Draw (new (0, 0, width, height), Attribute.Default, Attribute.Default);
|
||||
|
||||
DriverAssert.AssertDriverContentsWithFrameAre (expectedText, _output);
|
||||
}
|
||||
|
||||
[SetupFakeDriver]
|
||||
[Theory]
|
||||
|
||||
|
||||
@@ -3091,33 +3091,96 @@ public class TextFormatterTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper method to extract text content from driver for testing
|
||||
/// Helper method to extract text content from driver for testing (handles multi-line)
|
||||
/// </summary>
|
||||
private string GetDriverContents (IConsoleDriver driver, int width, int height)
|
||||
{
|
||||
var sb = new System.Text.StringBuilder ();
|
||||
for (int row = 0; row < height; row++)
|
||||
// Use Application.ToString which properly handles double-wide chars
|
||||
string fullContents = Application.ToString (driver);
|
||||
|
||||
// Extract only the region we care about
|
||||
string[] allLines = fullContents.Split (new[] { '\r', '\n' }, StringSplitOptions.None);
|
||||
var lines = new List<string> ();
|
||||
|
||||
for (int i = 0; i < height && i < allLines.Length; i++)
|
||||
{
|
||||
for (int col = 0; col < width; col++)
|
||||
string line = allLines[i];
|
||||
// Take up to width characters
|
||||
if (line.Length > width)
|
||||
{
|
||||
if (col < driver.Cols && row < driver.Rows)
|
||||
{
|
||||
sb.Append ((char)driver.Contents [row, col].Rune.Value);
|
||||
}
|
||||
}
|
||||
if (row < height - 1)
|
||||
{
|
||||
sb.AppendLine ();
|
||||
line = line.Substring (0, width);
|
||||
}
|
||||
// Trim trailing spaces from each line
|
||||
lines.Add (line.TrimEnd ());
|
||||
}
|
||||
// Trim trailing whitespace from each line to match DriverAssert behavior
|
||||
string result = sb.ToString ();
|
||||
string[] lines = result.Split (new[] { '\r', '\n' }, StringSplitOptions.None);
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
|
||||
return string.Join ("\n", lines);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData ("A", 1, 0, "")]
|
||||
[InlineData ("A", 0, 1, "")]
|
||||
[InlineData ("AB1 2", 2, 1, "2")]
|
||||
[InlineData ("AB12", 5, 1, "21BA")]
|
||||
[InlineData ("AB\n12", 5, 2, "21\nBA")]
|
||||
[InlineData ("ABC 123 456", 7, 2, "CBA\n654 321")]
|
||||
[InlineData ("こんにちは", 1, 1, "")]
|
||||
[InlineData ("こんにちは", 2, 1, "は")]
|
||||
[InlineData ("こんにちは", 5, 1, "はち")]
|
||||
[InlineData ("こんにちは", 10, 1, "はちにんこ")]
|
||||
[InlineData ("こんにちは\nAB\n12", 10, 3, "21\nBA\nはちにんこ")]
|
||||
public void Draw_Horizontal_RightLeft_BottomTop (string text, int width, int height, string expectedText)
|
||||
{
|
||||
// Create a local driver instance for this test
|
||||
var factory = new FakeDriverFactory ();
|
||||
var driver = factory.Create ();
|
||||
driver.SetBufferSize (Math.Max (25, width), Math.Max (25, height));
|
||||
|
||||
TextFormatter tf = new ()
|
||||
{
|
||||
lines[i] = lines[i].TrimEnd ();
|
||||
}
|
||||
return string.Join ("", lines);
|
||||
Text = text,
|
||||
Direction = TextDirection.RightLeft_BottomTop
|
||||
};
|
||||
|
||||
tf.ConstrainToWidth = width;
|
||||
tf.ConstrainToHeight = height;
|
||||
tf.Draw (new Rectangle (0, 0, width, height), Attribute.Default, Attribute.Default, driver: driver);
|
||||
|
||||
string actualText = GetDriverContents (driver, width, height);
|
||||
Assert.Equal (expectedText, actualText);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData ("A", 1, 0, "")]
|
||||
[InlineData ("A", 0, 1, "")]
|
||||
[InlineData ("AB1 2", 2, 1, "2")]
|
||||
[InlineData ("AB12", 5, 1, "21BA")]
|
||||
[InlineData ("AB\n12", 5, 2, "BA\n21")]
|
||||
[InlineData ("ABC 123 456", 7, 2, "654 321\nCBA")]
|
||||
[InlineData ("こんにちは", 1, 1, "")]
|
||||
[InlineData ("こんにちは", 2, 1, "は")]
|
||||
[InlineData ("こんにちは", 5, 1, "はち")]
|
||||
[InlineData ("こんにちは", 10, 1, "はちにんこ")]
|
||||
[InlineData ("こんにちは\nAB\n12", 10, 3, "はちにんこ\nBA\n21")]
|
||||
public void Draw_Horizontal_RightLeft_TopBottom (string text, int width, int height, string expectedText)
|
||||
{
|
||||
// Create a local driver instance for this test
|
||||
var factory = new FakeDriverFactory ();
|
||||
var driver = factory.Create ();
|
||||
driver.SetBufferSize (Math.Max (25, width), Math.Max (25, height));
|
||||
|
||||
TextFormatter tf = new ()
|
||||
{
|
||||
Text = text,
|
||||
Direction = TextDirection.RightLeft_TopBottom
|
||||
};
|
||||
|
||||
tf.ConstrainToWidth = width;
|
||||
tf.ConstrainToHeight = height;
|
||||
tf.Draw (new Rectangle (0, 0, width, height), Attribute.Default, Attribute.Default, driver: driver);
|
||||
|
||||
string actualText = GetDriverContents (driver, width, height);
|
||||
Assert.Equal (expectedText, actualText);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user