Files
Terminal.Gui/Tests/UnitTestsParallelizable/View/Layout/Dim.Tests.cs
Tig b0f32811eb Fixes #3930 - Splits tests to Tests/UnitTests, Tests/IntegrationTests, Tests/StressTests (#3954)
* Tons of API doc updates

* Removed stale test

* Removed stale tests

* Fixed Skipped Shadow test 1

* Fixed Skipped Shadow test 2

* Fixed Skipped Shadow test 3

* Removed stale test

* Removed stale test2

* Explicit unregister of event handler on Application.Driver!.ClearedContents

* Added Toplevels to dict

* code cleanup

* spelling error

* Removed stale test3

* Removed stale test4

* Removed stale test5

* added script

* tweaked script

* tweaked script

* Created StressTests project; moved some tests

* Created IntegrationTests project; moved some tests

* New yml

* made old yml just unit tests

* Tweaked Button_IsDefault_Raises_Accepted_Correctly

* tweaked script

* cleaned up ymls

* tweakled up ymls

* stress tests...

* stress tests on ubuntu only

* Fixed WindowsDriver in InvokeLeakTest

* Fixed WindowsDriver in InvokeLeakTest2

* Added Directory.Packages.props.
Added Directory.Build.props

* Shortened StressTest time

* Removed dupe file.

* DemoFiles

* Moved all tests to ./Tests dir.

* Fixed release build issue

* Fixed .sln file

* Fixed .sl* files

* Fixing ymls

* Fixing interation tests

* Create link to the file TestHelpers.

* Created Tests/UnitTestsParallelizable.
Moved all obviously parallelizable tests.
Updated yml.

* fixing logs

* fixing logs2

* fixing logs3

* don't require stress to pass for PRs

* Fix a failure?

* tweaked script

* Coudl this be it?

* Moved tons of tests to parallelizable

* Fixed some stuff

* Script to find duplicate tests

* Testing workflows

* Updated to v4

* Fix RelativeBasePath issue

* Replace powershell to pwsh

* Add ignore projects.

* Removed dupe unit tests

* Code cleanup of tests

* Cleaned up test warnings

* yml tweak

* Moved setter

* tweak ymls

* just randomly throwing spaghetti at a wall

* Enable runing 5 test runners in par

* Turned off DEBUG_DISPOSABLE for par tests

* RunningUnitTests=true

* code cleanup (forcing more Action runs)

* DISABLE_DEBUG_IDISPOSABLE

* Added View.DebugIDisposable. False by default.

* Remobed bogus tareet

* Remobed bogus tareet2

* fixed warning

* added api doc

* fixed warning

* fixed warning

* fixed warning2

* fixed warning3

* fixed warning4

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2025-03-05 23:44:27 -07:00

246 lines
7.0 KiB
C#

using System.Globalization;
using System.Text;
using UnitTests;
using Xunit.Abstractions;
using static Terminal.Gui.Dim;
namespace Terminal.Gui.LayoutTests;
public class DimTests
{
[Fact]
public void DimAbsolute_Calculate_ReturnsCorrectValue ()
{
var dim = new DimAbsolute (10);
int result = dim.Calculate (0, 100, null, Dimension.None);
Assert.Equal (10, result);
}
// TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
// TODO: A new test that calls SetRelativeLayout directly is needed.
[Fact]
public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
{
var t = new View { Width = 80, Height = 25, Text = "top" };
var w = new View
{
BorderStyle = LineStyle.Single,
X = 1,
Y = 2,
Width = 4,
Height = 5,
Title = "w"
};
t.Add (w);
t.LayoutSubviews ();
Assert.Equal (3, w.Width = 3);
Assert.Equal (4, w.Height = 4);
t.Dispose ();
}
[Fact]
public void DimHeight_Set_To_Null_Throws ()
{
Dim dim = Height (null);
Assert.Throws<NullReferenceException> (() => dim.ToString ());
}
[Fact]
public void DimHeight_SetsValue ()
{
var testVal = Rectangle.Empty;
var testValview = new View { Frame = testVal };
Dim dim = Height (testValview);
Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
testValview.Dispose ();
testVal = new (1, 2, 3, 4);
testValview = new () { Frame = testVal };
dim = Height (testValview);
Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
testValview.Dispose ();
}
[Fact]
public void Internal_Tests ()
{
var dimFactor = new DimPercent (10);
Assert.Equal (10, dimFactor.GetAnchor (100));
var dimAbsolute = new DimAbsolute (10);
Assert.Equal (10, dimAbsolute.GetAnchor (0));
var dimFill = new DimFill (1);
Assert.Equal (99, dimFill.GetAnchor (100));
var dimCombine = new DimCombine (AddOrSubtract.Add, dimFactor, dimAbsolute);
Assert.Equal (dimCombine.Left, dimFactor);
Assert.Equal (dimCombine.Right, dimAbsolute);
Assert.Equal (20, dimCombine.GetAnchor (100));
var view = new View { Frame = new (20, 10, 20, 1) };
var dimViewHeight = new DimView (view, Dimension.Height);
Assert.Equal (1, dimViewHeight.GetAnchor (0));
var dimViewWidth = new DimView (view, Dimension.Width);
Assert.Equal (20, dimViewWidth.GetAnchor (0));
view.Dispose ();
}
// TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
// TODO: A new test that calls SetRelativeLayout directly is needed.
[Fact]
public void Referencing_SuperView_Does_Not_Throw ()
{
var super = new View { Width = 10, Height = 10, Text = "super" };
var view = new View
{
Width = Width (super), // this is allowed
Height = Height (super), // this is allowed
Text = "view"
};
super.Add (view);
super.BeginInit ();
super.EndInit ();
Exception exception = Record.Exception (super.LayoutSubviews);
Assert.Null (exception);
super.Dispose ();
}
[Fact]
public void DimSized_Equals ()
{
var n1 = 0;
var n2 = 0;
Dim dim1 = Absolute (n1);
Dim dim2 = Absolute (n2);
Assert.Equal (dim1, dim2);
n1 = n2 = 1;
dim1 = Absolute (n1);
dim2 = Absolute (n2);
Assert.Equal (dim1, dim2);
n1 = n2 = -1;
dim1 = Absolute (n1);
dim2 = Absolute (n2);
Assert.Equal (dim1, dim2);
n1 = 0;
n2 = 1;
dim1 = Absolute (n1);
dim2 = Absolute (n2);
Assert.NotEqual (dim1, dim2);
}
[Fact]
public void DimSized_SetsValue ()
{
Dim dim = Absolute (0);
Assert.Equal ("Absolute(0)", dim.ToString ());
var testVal = 5;
dim = Absolute (testVal);
Assert.Equal ($"Absolute({testVal})", dim.ToString ());
testVal = -1;
dim = Absolute (testVal);
Assert.Equal ($"Absolute({testVal})", dim.ToString ());
}
// TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
// TODO: A new test that calls SetRelativeLayout directly is needed.
[Fact]
public void Validation_Does_Not_Throw_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
{
var t = new View { Width = 80, Height = 25, Text = "top" };
var w = new Window
{
X = 1,
Y = 2,
Width = 4,
Height = 5,
Title = "w"
};
t.Add (w);
t.LayoutSubviews ();
Assert.Equal (3, w.Width = 3);
Assert.Equal (4, w.Height = 4);
t.Dispose ();
}
[Fact]
public void DimWidth_Equals ()
{
var testRect1 = Rectangle.Empty;
var view1 = new View { Frame = testRect1 };
var testRect2 = Rectangle.Empty;
var view2 = new View { Frame = testRect2 };
Dim dim1 = Width (view1);
Dim dim2 = Width (view1);
// FIXED: Dim.Width should support Equals() and this should change to Equal.
Assert.Equal (dim1, dim2);
dim2 = Width (view2);
Assert.NotEqual (dim1, dim2);
testRect1 = new (0, 1, 2, 3);
view1 = new () { Frame = testRect1 };
testRect2 = new (0, 1, 2, 3);
dim1 = Width (view1);
dim2 = Width (view1);
// FIXED: Dim.Width should support Equals() and this should change to Equal.
Assert.Equal (dim1, dim2);
testRect1 = new (0, -1, 2, 3);
view1 = new () { Frame = testRect1 };
testRect2 = new (0, -1, 2, 3);
dim1 = Width (view1);
dim2 = Width (view1);
// FIXED: Dim.Width should support Equals() and this should change to Equal.
Assert.Equal (dim1, dim2);
testRect1 = new (0, -1, 2, 3);
view1 = new () { Frame = testRect1 };
testRect2 = Rectangle.Empty;
view2 = new () { Frame = testRect2 };
dim1 = Width (view1);
dim2 = Width (view2);
Assert.NotEqual (dim1, dim2);
}
[Fact]
public void DimWidth_Set_To_Null_Throws ()
{
Dim dim = Width (null);
Assert.Throws<NullReferenceException> (() => dim.ToString ());
}
[Fact]
public void DimWidth_SetsValue ()
{
var testVal = Rectangle.Empty;
var testValView = new View { Frame = testVal };
Dim dim = Width (testValView);
Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
testValView.Dispose ();
testVal = new (1, 2, 3, 4);
testValView = new () { Frame = testVal };
dim = Width (testValView);
Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
testValView.Dispose ();
}
}