Skip to content

Commit

Permalink
Fixes gui-cs#3064. Pos.View returns PosCombine (gui-cs#3065)
Browse files Browse the repository at this point in the history
  • Loading branch information
BDisp authored Dec 19, 2023
1 parent 06ad562 commit 74afd18
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 70 deletions.
14 changes: 7 additions & 7 deletions Terminal.Gui/View/Layout/PosDim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public override string ToString ()
default: tside = "unknown"; break;
}
// Note: We do not checkt `Target` for null here to intentionally throw if so
return $"View({tside},{Target.ToString ()})";
return $"View(side={tside},target={Target.ToString ()})";
}

public override int GetHashCode () => Target.GetHashCode ();
Expand All @@ -346,42 +346,42 @@ public override string ToString ()
/// </summary>
/// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
/// <param name="view">The <see cref="View"/> that will be tracked.</param>
public static Pos Left (View view) => new PosCombine (true, new PosView (view, 0), new Pos.PosAbsolute (0));
public static Pos Left (View view) => new PosView (view, 0);

/// <summary>
/// Returns a <see cref="Pos"/> object tracks the Left (X) position of the specified <see cref="View"/>.
/// </summary>
/// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
/// <param name="view">The <see cref="View"/> that will be tracked.</param>
public static Pos X (View view) => new PosCombine (true, new PosView (view, 0), new Pos.PosAbsolute (0));
public static Pos X (View view) => new PosView (view, 0);

/// <summary>
/// Returns a <see cref="Pos"/> object tracks the Top (Y) position of the specified <see cref="View"/>.
/// </summary>
/// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
/// <param name="view">The <see cref="View"/> that will be tracked.</param>
public static Pos Top (View view) => new PosCombine (true, new PosView (view, 1), new Pos.PosAbsolute (0));
public static Pos Top (View view) => new PosView (view, 1);

/// <summary>
/// Returns a <see cref="Pos"/> object tracks the Top (Y) position of the specified <see cref="View"/>.
/// </summary>
/// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
/// <param name="view">The <see cref="View"/> that will be tracked.</param>
public static Pos Y (View view) => new PosCombine (true, new PosView (view, 1), new Pos.PosAbsolute (0));
public static Pos Y (View view) => new PosView(view, 1);

/// <summary>
/// Returns a <see cref="Pos"/> object tracks the Right (X+Width) coordinate of the specified <see cref="View"/>.
/// </summary>
/// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
/// <param name="view">The <see cref="View"/> that will be tracked.</param>
public static Pos Right (View view) => new PosCombine (true, new PosView (view, 2), new Pos.PosAbsolute (0));
public static Pos Right (View view) => new PosView (view, 2);

/// <summary>
/// Returns a <see cref="Pos"/> object tracks the Bottom (Y+Height) coordinate of the specified <see cref="View"/>
/// </summary>
/// <returns>The <see cref="Pos"/> that depends on the other view.</returns>
/// <param name="view">The <see cref="View"/> that will be tracked.</param>
public static Pos Bottom (View view) => new PosCombine (true, new PosView (view, 3), new Pos.PosAbsolute (0));
public static Pos Bottom (View view) => new PosView (view, 3);

/// <summary>Serves as the default hash function. </summary>
/// <returns>A hash code for the current object.</returns>
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/View/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ bool CanBeVisible (View view)
/// <returns></returns>
public override string ToString ()
{
return $"{GetType ().Name}({Id})({Frame})";
return $"{GetType ().Name}({Id}){Frame}";
}

/// <inheritdoc/>
Expand Down
25 changes: 12 additions & 13 deletions UnitTests/View/Layout/DimTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,14 @@ public void SetsValue ()
var testVal = Rect.Empty;
var testValView = new View (testVal);
var dim = Dim.Width (testValView);
Assert.Equal ($"View(Width,View()({testVal}))", dim.ToString ());
Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
testValView.Dispose ();

testVal = new Rect (1, 2, 3, 4);
testValView = new View (testVal);
dim = Dim.Width (testValView);
Assert.Equal ($"View(Width,View()({testVal}))", dim.ToString ());
Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
testValView.Dispose ();

}

[Fact, TestRespondersDisposed]
Expand Down Expand Up @@ -157,13 +156,13 @@ public void Height_SetsValue ()
var testVal = Rect.Empty;
var testValview = new View (testVal);
var dim = Dim.Height (testValview);
Assert.Equal ($"View(Height,View()({testVal}))", dim.ToString ());
Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
testValview.Dispose ();

testVal = new Rect (1, 2, 3, 4);
testValview = new View (testVal);
dim = Dim.Height (testValview);
Assert.Equal ($"View(Height,View()({testVal}))", dim.ToString ());
Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
testValview.Dispose ();
}

Expand Down Expand Up @@ -438,12 +437,12 @@ public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assignin
Assert.Equal (49, f2.Frame.Width); // 50-1=49
Assert.Equal (5, f2.Frame.Height);
Assert.Equal ("Combine(View(Width,FrameView(f1)((0,0,49,5)))-Absolute(2))", v1.Width.ToString ());
Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,49,5))-Absolute(2))", v1.Width.ToString ());
Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
Assert.Equal (47, v1.Frame.Width); // 49-2=47
Assert.Equal (89, v1.Frame.Height); // 98-5-2-2=89
Assert.Equal ("Combine(View(Width,FrameView(f2)((49,0,49,5)))-Absolute(2))", v2.Width.ToString ());
Assert.Equal ("Combine(View(Width,FrameView(f2)(49,0,49,5))-Absolute(2))", v2.Width.ToString ());
Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
Assert.Equal (47, v2.Frame.Width); // 49-2=47
Assert.Equal (89, v2.Frame.Height); // 98-5-2-2=89
Expand All @@ -458,8 +457,8 @@ public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assignin
Assert.Equal (50, v4.Frame.Width);
Assert.Equal (50, v4.Frame.Height);
Assert.Equal ("Combine(View(Width,Button(v1)((2,7,47,89)))-View(Width,Button(v3)((0,0,9,9))))", v5.Width.ToString ());
Assert.Equal ("Combine(View(Height,Button(v1)((2,7,47,89)))-View(Height,Button(v3)((0,0,9,9))))", v5.Height.ToString ());
Assert.Equal ("Combine(View(Width,Button(v1)(2,7,47,89))-View(Width,Button(v3)(0,0,9,9)))", v5.Width.ToString ());
Assert.Equal ("Combine(View(Height,Button(v1)(2,7,47,89))-View(Height,Button(v3)(0,0,9,9)))", v5.Height.ToString ());
Assert.Equal (38, v5.Frame.Width); // 47-9=38
Assert.Equal (80, v5.Frame.Height); // 89-9=80
Expand Down Expand Up @@ -491,13 +490,13 @@ public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assignin
Assert.Equal (5, f2.Frame.Height);
v1.Text = "Button1";
Assert.Equal ("Combine(View(Width,FrameView(f1)((0,0,99,5)))-Absolute(2))", v1.Width.ToString ());
Assert.Equal ("Combine(View(Width,FrameView(f1)(0,0,99,5))-Absolute(2))", v1.Width.ToString ());
Assert.Equal ("Combine(Fill(0)-Absolute(2))", v1.Height.ToString ());
Assert.Equal (97, v1.Frame.Width); // 99-2=97
Assert.Equal (189, v1.Frame.Height); // 198-2-7=189
v2.Text = "Button2";
Assert.Equal ("Combine(View(Width,FrameView(f2)((99,0,99,5)))-Absolute(2))", v2.Width.ToString ());
Assert.Equal ("Combine(View(Width,FrameView(f2)(99,0,99,5))-Absolute(2))", v2.Width.ToString ());
Assert.Equal ("Combine(Fill(0)-Absolute(2))", v2.Height.ToString ());
Assert.Equal (97, v2.Frame.Width); // 99-2=97
Assert.Equal (189, v2.Frame.Height); // 198-2-7=189
Expand All @@ -521,8 +520,8 @@ public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assignin
Assert.Equal (1, v4.Frame.Height); // 1 because is Dim.DimAbsolute
v5.Text = "Button5";
Assert.Equal ("Combine(View(Width,Button(v1)((2,7,97,189)))-View(Width,Button(v3)((0,0,19,19))))", v5.Width.ToString ());
Assert.Equal ("Combine(View(Height,Button(v1)((2,7,97,189)))-View(Height,Button(v3)((0,0,19,19))))", v5.Height.ToString ());
Assert.Equal ("Combine(View(Width,Button(v1)(2,7,97,189))-View(Width,Button(v3)(0,0,19,19)))", v5.Width.ToString ());
Assert.Equal ("Combine(View(Height,Button(v1)(2,7,97,189))-View(Height,Button(v3)(0,0,19,19)))", v5.Height.ToString ());
Assert.Equal (78, v5.Frame.Width); // 97-9=78
Assert.Equal (170, v5.Frame.Height); // 189-19=170
Expand Down
96 changes: 66 additions & 30 deletions UnitTests/View/Layout/PosTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,140 +390,140 @@ public void PosSide_SetsValue ()
testInt = 0;
testRect = Rect.Empty;
pos = Pos.Left (new View ());
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

pos = Pos.Left (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

testRect = new Rect (1, 2, 3, 4);
pos = Pos.Left (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

// Pos.Left(win) + 0
pos = Pos.Left (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = 1;
// Pos.Left(win) +1
pos = Pos.Left (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = -1;
// Pos.Left(win) -1
pos = Pos.Left (new View (testRect)) - testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

// Pos.X
side = "x";
testInt = 0;
testRect = Rect.Empty;
pos = Pos.X (new View ());
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

pos = Pos.X (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

testRect = new Rect (1, 2, 3, 4);
pos = Pos.X (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

// Pos.X(win) + 0
pos = Pos.X (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = 1;
// Pos.X(win) +1
pos = Pos.X (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = -1;
// Pos.X(win) -1
pos = Pos.X (new View (testRect)) - testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

// Pos.Top
side = "y";
testInt = 0;
testRect = Rect.Empty;
pos = Pos.Top (new View ());
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

pos = Pos.Top (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

testRect = new Rect (1, 2, 3, 4);
pos = Pos.Top (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

// Pos.Top(win) + 0
pos = Pos.Top (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = 1;
// Pos.Top(win) +1
pos = Pos.Top (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = -1;
// Pos.Top(win) -1
pos = Pos.Top (new View (testRect)) - testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

// Pos.Y
side = "y";
testInt = 0;
testRect = Rect.Empty;
pos = Pos.Y (new View ());
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

pos = Pos.Y (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

testRect = new Rect (1, 2, 3, 4);
pos = Pos.Y (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

// Pos.Y(win) + 0
pos = Pos.Y (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = 1;
// Pos.Y(win) +1
pos = Pos.Y (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = -1;
// Pos.Y(win) -1
pos = Pos.Y (new View (testRect)) - testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

// Pos.Bottom
side = "bottom";
testRect = Rect.Empty;
testInt = 0;
pos = Pos.Bottom (new View ());
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

pos = Pos.Bottom (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

testRect = new Rect (1, 2, 3, 4);
pos = Pos.Bottom (new View (testRect));
Assert.Equal ($"Combine(View({side},View()({testRect})){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"View(side={side},target=View(){testRect})", pos.ToString ());

// Pos.Bottom(win) + 0
pos = Pos.Bottom (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = 1;
// Pos.Bottom(win) +1
pos = Pos.Bottom (new View (testRect)) + testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

testInt = -1;
// Pos.Bottom(win) -1
pos = Pos.Bottom (new View (testRect)) - testInt;
Assert.Equal ($"Combine(Combine(View({side},View()({testRect}))+Absolute(0)){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());
Assert.Equal ($"Combine(View(side={side},target=View(){testRect}){(testInt < 0 ? '-' : '+')}Absolute({testInt}))", pos.ToString ());

#if DEBUG_IDISPOSABLE
// HACK: Force clean up of Responders to avoid having to Dispose all the Views created above.
Expand Down Expand Up @@ -1084,5 +1084,41 @@ public void PosCombine_Referencing_Same_View ()

super.Dispose ();
}

[Fact]
public void DoNotReturnPosCombine ()
{
var v = new View () { Id = "V" };

var pos = Pos.Left (v);
Assert.Equal (
"View(side=x,target=View(V)(0,0,0,0))",
pos.ToString ());

pos = Pos.X (v);
Assert.Equal (
"View(side=x,target=View(V)(0,0,0,0))",
pos.ToString ());

pos = Pos.Top (v);
Assert.Equal (
"View(side=y,target=View(V)(0,0,0,0))",
pos.ToString ());

pos = Pos.Y (v);
Assert.Equal (
"View(side=y,target=View(V)(0,0,0,0))",
pos.ToString ());

pos = Pos.Right (v);
Assert.Equal (
"View(side=right,target=View(V)(0,0,0,0))",
pos.ToString ());

pos = Pos.Bottom (v);
Assert.Equal (
"View(side=bottom,target=View(V)(0,0,0,0))",
pos.ToString ());
}
}
}
Loading

0 comments on commit 74afd18

Please sign in to comment.