From e27e865fec1726d1580e5e2c65a60f5a8c4e3bc3 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 4 Dec 2023 15:07:07 +0000 Subject: [PATCH] Prevents a user to set TextDirection to -1. --- Terminal.Gui/Text/TextFormatter.cs | 4 ++++ UnitTests/Text/TextFormatterTests.cs | 16 ++++++++++++++ UnitTests/View/ViewTests.cs | 33 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/Terminal.Gui/Text/TextFormatter.cs b/Terminal.Gui/Text/TextFormatter.cs index 62406b157..751d5b2ee 100644 --- a/Terminal.Gui/Text/TextFormatter.cs +++ b/Terminal.Gui/Text/TextFormatter.cs @@ -1139,6 +1139,10 @@ namespace Terminal.Gui { public TextDirection Direction { get => _textDirection; set { + if (value == (TextDirection)(-1)) { + return; + } + var directionWasIndef = _textDirection == (TextDirection)(-1); _textDirection = EnableNeedsFormat (value); if (directionWasIndef || (AutoSize && Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified)) { diff --git a/UnitTests/Text/TextFormatterTests.cs b/UnitTests/Text/TextFormatterTests.cs index e37f18e3f..5370b7fdf 100644 --- a/UnitTests/Text/TextFormatterTests.cs +++ b/UnitTests/Text/TextFormatterTests.cs @@ -1794,5 +1794,21 @@ ssb driver.End (); } + + [Fact] + public void TextDirection_Only_Can_Be_Set_To_Minus_One_On_The_Private_Field () + { + var tf = new TextFormatter () { Direction = (TextDirection)(-1) }; + // There was no change here because it's the same value + Assert.Equal ((TextDirection)(-1), tf.Direction); + + // Add a valid value + tf.Direction = TextDirection.TopBottom_LeftRight; + Assert.Equal (TextDirection.TopBottom_LeftRight, tf.Direction); + + // Add a invalid value + tf.Direction = (TextDirection)(-1); + Assert.Equal (TextDirection.TopBottom_LeftRight, tf.Direction); + } } } \ No newline at end of file diff --git a/UnitTests/View/ViewTests.cs b/UnitTests/View/ViewTests.cs index c31b5dff6..c89e426c9 100644 --- a/UnitTests/View/ViewTests.cs +++ b/UnitTests/View/ViewTests.cs @@ -1562,5 +1562,38 @@ At 0,0 var pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, output); Assert.Equal (new Rect (0, 0, 9, height + 2), pos); } + + [Fact] + public void TextDirection_Only_Can_Be_Set_To_Minus_One_On_The_Private_Field () + { + var view = new View () { TextDirection = (TextDirection)(-1) }; + // There was no change here because it's the same value + Assert.Equal ((TextDirection)(-1), view.TextDirection); + + // Add a valid value + view.TextDirection = TextDirection.TopBottom_LeftRight; + Assert.Equal (TextDirection.TopBottom_LeftRight, view.TextDirection); + + // Add a invalid value + view.TextDirection = (TextDirection)(-1); + Assert.Equal (TextDirection.TopBottom_LeftRight, view.TextDirection); + + view = new View (); + // It's the default value + Assert.Equal ((TextDirection)(-1), view.TextDirection); + + var container = new View (); + container.Add (view); + // It's the default value after added + Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection); + + view = new View (); + // It's the default value + Assert.Equal ((TextDirection)(-1), view.TextDirection); + + view.BeginInit (); + // It's the default value after initialized + Assert.Equal (TextDirection.LeftRight_TopBottom, view.TextDirection); + } } }