| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System.Drawing;
- using Terminal.Gui.Text;
- using Xunit;
- using Xunit.Abstractions;
- namespace Terminal.Gui.TextTests;
- /// <summary>
- /// Tests for the new TextFormatter architecture that separates formatting from rendering.
- /// </summary>
- public class TextFormatterNewArchitectureTests
- {
- private readonly ITestOutputHelper _output;
- public TextFormatterNewArchitectureTests(ITestOutputHelper output)
- {
- _output = output;
- }
- [Fact]
- public void TextFormatter_NewArchitecture_BasicFormatting_Works()
- {
- Application.Init(new FakeDriver());
- var tf = new TextFormatter
- {
- Text = "Hello World"
- };
- // Test the new architecture method
- Size size = tf.GetFormattedSizeWithNewArchitecture();
-
- Assert.True(size.Width > 0);
- Assert.True(size.Height > 0);
-
- Application.Shutdown();
- }
- [Fact]
- public void TextFormatter_NewArchitecture_WithAlignment_Works()
- {
- Application.Init(new FakeDriver());
- var tf = new TextFormatter
- {
- Text = "Hello World",
- Alignment = Alignment.Center,
- VerticalAlignment = Alignment.Center
- };
- // Test that properties are synchronized
- Size size = tf.GetFormattedSizeWithNewArchitecture();
-
- Assert.True(size.Width > 0);
- Assert.True(size.Height > 0);
-
- Application.Shutdown();
- }
- [Fact]
- public void TextFormatter_NewArchitecture_Performance_IsBetter()
- {
- Application.Init(new FakeDriver());
- var tf = new TextFormatter
- {
- Text = "This is a long text that will be formatted multiple times to test performance improvements"
- };
- // Warm up
- tf.GetFormattedSizeWithNewArchitecture();
- // Test multiple calls - should use caching
- var sw = System.Diagnostics.Stopwatch.StartNew();
- for (int i = 0; i < 100; i++)
- {
- tf.GetFormattedSizeWithNewArchitecture();
- }
- sw.Stop();
- _output.WriteLine($"New architecture: 100 calls took {sw.ElapsedMilliseconds}ms");
-
- // The new architecture should be fast due to caching
- Assert.True(sw.ElapsedMilliseconds < 100, "New architecture should be fast due to caching");
-
- Application.Shutdown();
- }
- [Fact]
- public void TextFormatter_NewArchitecture_DrawRegion_Works()
- {
- Application.Init(new FakeDriver());
- var tf = new TextFormatter
- {
- Text = "Hello\nWorld"
- };
- Region region = tf.GetDrawRegionWithNewArchitecture(new Rectangle(0, 0, 10, 10));
-
- Assert.NotNull(region);
-
- Application.Shutdown();
- }
- [Fact]
- public void StandardTextFormatter_DirectlyUsed_Works()
- {
- var formatter = new StandardTextFormatter
- {
- Text = "Test Text",
- Alignment = Alignment.Center
- };
- FormattedText result = formatter.Format();
-
- Assert.NotNull(result);
- Assert.NotEmpty(result.Lines);
- Assert.True(result.RequiredSize.Width > 0);
- Assert.True(result.RequiredSize.Height > 0);
- }
- [Fact]
- public void StandardTextRenderer_DirectlyUsed_Works()
- {
- Application.Init(new FakeDriver());
-
- var formatter = new StandardTextFormatter
- {
- Text = "Test Text"
- };
-
- var renderer = new StandardTextRenderer();
- FormattedText formattedText = formatter.Format();
-
- // Should not throw
- renderer.Draw(
- formattedText,
- new Rectangle(0, 0, 10, 1),
- Attribute.Default,
- Attribute.Default);
-
- Region region = renderer.GetDrawRegion(
- formattedText,
- new Rectangle(0, 0, 10, 1));
-
- Assert.NotNull(region);
-
- Application.Shutdown();
- }
- [Fact]
- public void TextFormatter_UseNewArchitecture_Flag_Works()
- {
- Application.Init(new FakeDriver());
- var tf = new TextFormatter
- {
- Text = "Hello World",
- UseNewArchitecture = true // Enable new architecture
- };
- // This should now use the new architecture via the Draw method
- tf.Draw(new Rectangle(0, 0, 10, 1), Attribute.Default, Attribute.Default);
-
- // Test that the new architecture produces results
- Size size = tf.GetFormattedSizeWithNewArchitecture();
- Assert.True(size.Width > 0);
- Assert.True(size.Height > 0);
-
- Application.Shutdown();
- }
- }
|