123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- 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 ();
- }
- }
|