Moved AutoSizeTrue tests to more logical locations; nuked invalid tests

This commit is contained in:
Tig
2024-05-13 12:22:43 -06:00
parent c8690af514
commit 4e01779e0b
3 changed files with 1998 additions and 2060 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +1,5 @@
using System.ComponentModel;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Xunit.Abstractions;
namespace Terminal.Gui.ViewsTests;
@@ -541,4 +542,940 @@ e
);
}
// These tests were formally in AutoSizetrue.cs. They are (poor) Label tests.
private readonly string [] expecteds = new string [21]
{
@"
┌────────────────────┐
│View with long text │
│ │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 0 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 1 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 2 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 3 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 4 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 5 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 6 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 7 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 8 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 9 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 10 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 11 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 12 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 13 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 14 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 15 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 16 │
│Label 16 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 16 │
│Label 17 │
│Label 17 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 16 │
│Label 17 │
│Label 18 │
│Label 18 │
└────────────────────┘",
@"
┌────────────────────┐
│View with long text │
│Label 0 │
│Label 1 │
│Label 2 │
│Label 3 │
│Label 4 │
│Label 5 │
│Label 6 │
│Label 7 │
│Label 8 │
│Label 9 │
│Label 10 │
│Label 11 │
│Label 12 │
│Label 13 │
│Label 14 │
│Label 15 │
│Label 16 │
│Label 17 │
│Label 18 │
│Label 19 │
│Label 19 │
└────────────────────┘"
};
private static readonly Size _size1x1 = new (1, 1);
// TODO: This is a Label test. Move to label tests if there's not already a test for this.
[Fact]
[AutoInitShutdown]
public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window ()
{
var win = new Window ();
var label = new Label
{
Text = "This should be the last line.",
ColorScheme = Colors.ColorSchemes ["Menu"],
//Width = Dim.Fill (),
X = 0, // keep unit test focused; don't use Center here
Y = Pos.AnchorEnd (1)
};
win.Add (label);
Toplevel top = new ();
top.Add (win);
RunState rs = Application.Begin (top);
((FakeDriver)Application.Driver).SetBufferSize (40, 10);
Assert.Equal (29, label.Text.Length);
Assert.Equal (new Rectangle (0, 0, 40, 10), top.Frame);
Assert.Equal (new Rectangle (0, 0, 40, 10), win.Frame);
Assert.Equal (new Rectangle (0, 7, 29, 1), label.Frame);
var expected = @"
┌──────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────┘
"
;
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
[Fact]
[AutoInitShutdown]
public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
{
var win = new Window ();
// Label is AutoSize == true
var label = new Label
{
Text = "This should be the last line.",
ColorScheme = Colors.ColorSchemes ["Menu"],
X = 0,
Y = Pos.AnchorEnd (1)
};
win.Add (label);
var menu = new MenuBar { Menus = new MenuBarItem [] { new ("Menu", "", null) } };
var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
Toplevel top = new ();
top.Add (win, menu, status);
RunState rs = Application.Begin (top);
Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
Assert.Equal (new Rectangle (0, 0, 80, 1), menu.Frame);
Assert.Equal (new Rectangle (0, 24, 80, 1), status.Frame);
Assert.Equal (new Rectangle (0, 1, 80, 23), win.Frame);
Assert.Equal (new Rectangle (0, 20, 29, 1), label.Frame);
var expected = @"
Menu
┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────────────────────────────────────────────┘
F1 Help
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
// TODO: This is a Label test. Move to label tests if there's not already a test for this.
[Fact]
[AutoInitShutdown]
public void Bottom_Equal_Inside_Window ()
{
var win = new Window ();
// Label is AutoSize == true
var label = new Label
{
Text = "This should be the last line.",
ColorScheme = Colors.ColorSchemes ["Menu"],
//Width = Dim.Fill (),
X = 0,
Y = Pos.Bottom (win)
- 3 // two lines top and bottom borders more one line above the bottom border
};
win.Add (label);
Toplevel top = new ();
top.Add (win);
RunState rs = Application.Begin (top);
((FakeDriver)Application.Driver).SetBufferSize (40, 10);
Assert.Equal (new Rectangle (0, 0, 40, 10), top.Frame);
Assert.Equal (new Rectangle (0, 0, 40, 10), win.Frame);
Assert.Equal (new Rectangle (0, 7, 29, 1), label.Frame);
var expected = @"
┌──────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────┘
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
// TODO: This is a Label test. Move to label tests if there's not already a test for this.
[Fact]
[AutoInitShutdown]
public void Bottom_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
{
var win = new Window ();
// Label is AutoSize == true
var label = new Label
{
Text = "This should be the last line.",
ColorScheme = Colors.ColorSchemes ["Menu"],
//Width = Dim.Fill (),
X = 0,
Y = Pos.Bottom (win) - 4 // two lines top and bottom borders more two lines above border
};
win.Add (label);
var menu = new MenuBar { Menus = new MenuBarItem [] { new ("Menu", "", null) } };
var status = new StatusBar (new StatusItem [] { new (KeyCode.F1, "~F1~ Help", null) });
Toplevel top = new ();
top.Add (win, menu, status);
RunState rs = Application.Begin (top);
Assert.Equal (new Rectangle (0, 0, 80, 25), top.Frame);
Assert.Equal (new Rectangle (0, 0, 80, 1), menu.Frame);
Assert.Equal (new Rectangle (0, 24, 80, 1), status.Frame);
Assert.Equal (new Rectangle (0, 1, 80, 23), win.Frame);
Assert.Equal (new Rectangle (0, 20, 29, 1), label.Frame);
var expected = @"
Menu
┌──────────────────────────────────────────────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│This should be the last line. │
└──────────────────────────────────────────────────────────────────────────────┘
F1 Help
";
TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Application.End (rs);
}
// TODO: This is a Dim test. Move to Dim tests.
[Fact]
[AutoInitShutdown]
public void Dim_Subtract_Operator_With_Text ()
{
Toplevel top = new ();
var view = new View
{
Text = "View with long text",
X = 0,
Y = 0,
Width = 20,
Height = 1
};
var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
var count = 20;
// Label is AutoSize == true
List<Label> listLabels = new ();
for (var i = 0; i < count; i++)
{
field.Text = $"Label {i}";
var label = new Label { Text = field.Text, X = 0, Y = i + 1 /*, Width = 10*/ };
view.Add (label);
Assert.Equal ($"Label {i}", label.Text);
Assert.Equal ($"Absolute({i + 1})", label.Y.ToString ());
listLabels.Add (label);
if (i == 0)
{
Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
view.Height += 1;
Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
}
else
{
Assert.Equal ($"Absolute({i + 1})", view.Height.ToString ());
view.Height += 1;
Assert.Equal ($"Absolute({i + 2})", view.Height.ToString ());
}
}
field.KeyDown += (s, k) =>
{
if (k.KeyCode == KeyCode.Enter)
{
((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
Assert.Equal (new Rectangle (0, 0, 22, count + 4), pos);
if (count > 0)
{
Assert.Equal ($"Label {count - 1}", listLabels [count - 1].Text);
view.Remove (listLabels [count - 1]);
listLabels [count - 1].Dispose ();
listLabels.RemoveAt (count - 1);
Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
view.Height -= 1;
count--;
if (listLabels.Count > 0)
{
field.Text = listLabels [count - 1].Text;
}
else
{
field.Text = string.Empty;
}
}
Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
}
};
Application.Iteration += (s, a) =>
{
while (count > -1)
{
field.NewKeyDownEvent (Key.Enter);
if (count == 0)
{
field.NewKeyDownEvent (Key.Enter);
break;
}
}
Application.RequestStop ();
};
var win = new Window ();
win.Add (view);
win.Add (field);
top.Add (win);
Application.Run (top);
Assert.Equal (0, count);
Assert.Equal (count, listLabels.Count);
}
// TODO: This is a Label test. Move to Label tests.
[Fact]
[SetupFakeDriver]
public void Label_Height_Zero_Stays_Zero ()
{
((FakeDriver)Application.Driver).SetBufferSize (10, 4);
var text = "Label";
var label = new Label
{
Text = text,
};
label.Width = Dim.Fill () - text.Length;
label.Height = 0;
var win = new FrameView { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (label);
win.BeginInit ();
win.EndInit ();
win.LayoutSubviews ();
win.Draw ();
Assert.Equal (5, text.Length);
Assert.Equal (new (0, 0, 3, 0), label.Frame);
//Assert.Equal (new (5, 1), label.TextFormatter.Size);
Assert.Single (label.TextFormatter.GetLines ());
Assert.Equal (new (0, 0, 10, 4), win.Frame);
var expected = @"
┌────────┐
│ │
│ │
└────────┘
";
Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Assert.Equal (new (0, 0, 10, 4), pos);
text = "0123456789";
Assert.Equal (10, text.Length);
label.Width = Dim.Fill () - text.Length;
win.LayoutSubviews ();
win.Clear ();
win.Draw ();
Assert.Equal (Rectangle.Empty, label.Frame);
// Assert.Equal (new (5, 1), label.TextFormatter.Size);
//Exception exception = Record.Exception (
// () => Assert.Equal (
// new List<string> { string.Empty },
// label.TextFormatter.GetLines ()
// )
// );
//Assert.Null (exception);
expected = @"
┌────────┐
│ │
│ │
└────────┘
";
pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Assert.Equal (new (0, 0, 10, 4), pos);
}
[Fact]
[AutoInitShutdown]
public void Dim_Add_Operator_With_Text ()
{
Toplevel top = new ();
var view = new View
{
Text = "View with long text",
X = 0,
Y = 0,
Width = 20,
Height = 1
};
var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 };
var count = 0;
// Label is AutoSize == true
List<Label> listLabels = new ();
field.KeyDown += (s, k) =>
{
if (k.KeyCode == KeyCode.Enter)
{
((FakeDriver)Application.Driver).SetBufferSize (22, count + 4);
Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expecteds [count], _output);
Assert.Equal (new Rectangle (0, 0, 22, count + 4), pos);
if (count < 20)
{
field.Text = $"Label {count}";
// Label is AutoSize = true
var label = new Label { Text = field.Text, X = 0, Y = view.Viewport.Height /*, Width = 10*/ };
view.Add (label);
Assert.Equal ($"Label {count}", label.Text);
Assert.Equal ($"Absolute({count + 1})", label.Y.ToString ());
listLabels.Add (label);
//if (count == 0) {
// Assert.Equal ($"Absolute({count})", view.Height.ToString ());
// view.Height += 2;
//} else {
Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
view.Height += 1;
//}
count++;
}
Assert.Equal ($"Absolute({count + 1})", view.Height.ToString ());
}
};
Application.Iteration += (s, a) =>
{
while (count < 21)
{
field.NewKeyDownEvent (Key.Enter);
if (count == 20)
{
field.NewKeyDownEvent (Key.Enter);
break;
}
}
Application.RequestStop ();
};
var win = new Window ();
win.Add (view);
win.Add (field);
top.Add (win);
Application.Run (top);
Assert.Equal (20, count);
Assert.Equal (count, listLabels.Count);
}
[Fact]
[AutoInitShutdown]
public void Label_IsEmpty_False_Minimum_Height ()
{
var text = "Label";
var label = new Label
{
//Width = Dim.Fill () - text.Length,
Text = text
};
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (label);
var top = new Toplevel ();
top.Add (win);
Application.Begin (top);
((FakeDriver)Application.Driver).SetBufferSize (10, 4);
Assert.Equal (5, text.Length);
Assert.Equal (new (0, 0, 5, 1), label.Frame);
Assert.Equal (new (5, 1), label.TextFormatter.Size);
Assert.Equal (["Label"], label.TextFormatter.GetLines ());
Assert.Equal (new (0, 0, 10, 4), win.Frame);
Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
var expected = @"
┌────────┐
│Label │
│ │
└────────┘
";
Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Assert.Equal (new (0, 0, 10, 4), pos);
text = "0123456789";
Assert.Equal (10, text.Length);
//label.Width = Dim.Fill () - text.Length;
Application.Refresh ();
Assert.Equal (new (0, 0, 5, 1), label.Frame);
Assert.Equal (new (5, 1), label.TextFormatter.Size);
Exception exception = Record.Exception (() => Assert.Single (label.TextFormatter.GetLines ()));
Assert.Null (exception);
expected = @"
┌────────┐
│Label │
│ │
└────────┘
";
pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Assert.Equal (new (0, 0, 10, 4), pos);
}
[Fact]
[AutoInitShutdown]
public void Label_IsEmpty_False_Never_Return_Null_Lines ()
{
var text = "Label";
var label = new Label
{
//Width = Dim.Fill () - text.Length,
//Height = 1,
Text = text
};
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
win.Add (label);
var top = new Toplevel ();
top.Add (win);
Application.Begin (top);
((FakeDriver)Application.Driver).SetBufferSize (10, 4);
Assert.Equal (5, text.Length);
Assert.Equal (new (0, 0, 5, 1), label.Frame);
Assert.Equal (new (5, 1), label.TextFormatter.Size);
Assert.Equal (["Label"], label.TextFormatter.GetLines ());
Assert.Equal (new (0, 0, 10, 4), win.Frame);
Assert.Equal (new (0, 0, 10, 4), Application.Top.Frame);
var expected = @"
┌────────┐
│Label │
│ │
└────────┘
";
Rectangle pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Assert.Equal (new (0, 0, 10, 4), pos);
text = "0123456789";
Assert.Equal (10, text.Length);
//label.Width = Dim.Fill () - text.Length;
Application.Refresh ();
Assert.Equal (new (0, 0, 5, 1), label.Frame);
Assert.Equal (new (5, 1), label.TextFormatter.Size);
Assert.Single (label.TextFormatter.GetLines ());
expected = @"
┌────────┐
│Label │
│ │
└────────┘
";
pos = TestHelpers.AssertDriverContentsWithFrameAre (expected, _output);
Assert.Equal (new (0, 0, 10, 4), pos);
}
[Fact]
public void Label_ResizeView_With_Dim_Absolute ()
{
var super = new View ()
{
Width = Dim.Fill (),
Height = Dim.Fill ()
};
var label = new Label ();
label.Text = "New text";
super.Add (label);
super.LayoutSubviews ();
Rectangle expectedLabelBounds = new (0, 0, 8, 1);
Assert.Equal (expectedLabelBounds, label.Viewport);
super.Dispose ();
}
}