Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Terminal.Gui/Views/TableView/TreeTableSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ private string GetColumnZeroRepresentationFromTree (int row)
Branch<T> branch = RowToBranch (row);

// Everything on line before the expansion run and branch text
Rune [] prefix = branch.GetLinePrefix (Application.Driver).ToArray ();
Rune expansion = branch.GetExpandableSymbol (Application.Driver);
Rune [] prefix = branch.GetLinePrefix ().ToArray ();
Rune expansion = branch.GetExpandableSymbol ();
string lineBody = _tree.AspectGetter (branch.Model) ?? "";

var sb = new StringBuilder ();
Expand Down
180 changes: 91 additions & 89 deletions Terminal.Gui/Views/TreeView/Branch.cs
Comment thread
tig marked this conversation as resolved.

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Terminal.Gui/Views/TreeView/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ public IEnumerable<T> GetChildren (T o)
return new T [0];
}

return branch.ChildBranches?.Values?.Select (b => b.Model)?.ToArray () ?? new T [0];
return branch.ChildBranches?.Select (b => b.Model)?.ToArray () ?? new T [0];
}

/// <summary>Returns the maximum width line in the tree including prefix and expansion symbols.</summary>
Expand Down Expand Up @@ -879,10 +879,10 @@ public int GetContentWidth (bool visible)
return 0;
}

return map.Skip (ScrollOffsetVertical).Take (Viewport.Height).Max (b => b.GetWidth (Driver));
return map.Skip (ScrollOffsetVertical).Take (Viewport.Height).Max (b => b.GetWidth ());
}

return map.Max (b => b.GetWidth (Driver));
return map.Max (b => b.GetWidth ());
}

/// <summary>
Expand Down Expand Up @@ -1171,7 +1171,7 @@ protected override bool OnDrawingContent ()
if (idxToRender < map.Count)
{
// Render the line
map.ElementAt (idxToRender).Draw (Driver, ColorScheme, line, Viewport.Width);
map.ElementAt (idxToRender).Draw (line, Viewport.Width);
}
else
{
Expand Down Expand Up @@ -1488,7 +1488,7 @@ private IEnumerable<Branch<T>> AddToLineMap (Branch<T> currentBranch, bool paren

if (currentBranch.IsExpanded)
{
foreach (Branch<T> subBranch in currentBranch.ChildBranches.Values)
foreach (Branch<T> subBranch in currentBranch.ChildBranches)
{
foreach (Branch<T> sub in AddToLineMap (subBranch, weMatch, out bool childMatch))
{
Expand Down
6 changes: 6 additions & 0 deletions Terminal.sln
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests.Parallelizable",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TerminalGuiFluentTesting", "TerminalGuiFluentTesting\TerminalGuiFluentTesting.csproj", "{2DBA7BDC-17AE-474B-A507-00807D087607}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TerminalGuiFluentTesting.Xunit", "TerminalGuiFluentTesting.Xunit\TerminalGuiFluentTesting.Xunit.csproj", "{231B9723-10F3-46DB-8EAE-50C0C0375AD3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -123,6 +125,10 @@ Global
{2DBA7BDC-17AE-474B-A507-00807D087607}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DBA7BDC-17AE-474B-A507-00807D087607}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DBA7BDC-17AE-474B-A507-00807D087607}.Release|Any CPU.Build.0 = Release|Any CPU
{231B9723-10F3-46DB-8EAE-50C0C0375AD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{231B9723-10F3-46DB-8EAE-50C0C0375AD3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{231B9723-10F3-46DB-8EAE-50C0C0375AD3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{231B9723-10F3-46DB-8EAE-50C0C0375AD3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\TerminalGuiFluentTesting\TerminalGuiFluentTesting.csproj" />
<PackageReference Include="xunit" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions TerminalGuiFluentTesting.Xunit/XunitContextExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Xunit;

namespace TerminalGuiFluentTesting;

public static class XunitContextExtensions
{
public static GuiTestContext AssertTrue (this GuiTestContext context, bool? condition)
{
context.Then (
() =>
{
Assert.True (condition);
});
return context;
}
public static GuiTestContext AssertEqual (this GuiTestContext context, object? expected, object? actual)
{
context.Then (
() =>
{
Assert.Equal (expected,actual);
});
return context;
}
}
35 changes: 34 additions & 1 deletion TerminalGuiFluentTesting/GuiTestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,18 @@ public GuiTestContext WaitIteration (Action? a = null)
/// <returns></returns>
public GuiTestContext Then (Action doAction)
{
doAction ();
try
Comment thread
tig marked this conversation as resolved.
{
doAction ();
}
catch(Exception)
{
Stop ();
_hardStop.Cancel();

throw;

}

return this;
}
Expand Down Expand Up @@ -360,6 +371,7 @@ public GuiTestContext Right ()
{
SendNetKey (k);
}
WaitIteration ();
break;
default:
throw new ArgumentOutOfRangeException ();
Expand Down Expand Up @@ -550,4 +562,25 @@ private void SendWindowsKey (ConsoleKeyMapping.VK specialKey)

WaitIteration ();
}

/// <summary>
/// Sets the input focus to the given <see cref="View"/>.
/// Throws <see cref="ArgumentException"/> if focus did not change due to system
/// constraints e.g. <paramref name="toFocus"/>
/// <see cref="View.CanFocus"/> is <see langword="false"/>
/// </summary>
/// <param name="toFocus"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public GuiTestContext Focus (View toFocus)
Comment thread
tig marked this conversation as resolved.
{
toFocus.FocusDeepest (NavigationDirection.Forward, TabBehavior.TabStop);

if (!toFocus.HasFocus)
{
throw new ArgumentException ("Failed to set focus, FocusDeepest did not result in HasFocus becoming true. Ensure view is added and focusable");
}

return WaitIteration ();
}
}
14 changes: 1 addition & 13 deletions Tests/IntegrationTests/FluentTests/BasicFluentAssertionTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text;
using Terminal.Gui;
using Terminal.Gui;
using TerminalGuiFluentTesting;
using Xunit.Abstractions;

Expand All @@ -9,17 +8,6 @@ public class BasicFluentAssertionTests
{
private readonly TextWriter _out;

public class TestOutputWriter : TextWriter
{
private readonly ITestOutputHelper _output;

public TestOutputWriter (ITestOutputHelper output) { _output = output; }

public override void WriteLine (string? value) { _output.WriteLine (value ?? string.Empty); }

public override Encoding Encoding => Encoding.UTF8;
}

public BasicFluentAssertionTests (ITestOutputHelper outputHelper) { _out = new TestOutputWriter (outputHelper); }

[Theory]
Expand Down
15 changes: 15 additions & 0 deletions Tests/IntegrationTests/FluentTests/TestOutputWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text;
using Xunit.Abstractions;

namespace IntegrationTests.FluentTests;

public class TestOutputWriter : TextWriter
{
private readonly ITestOutputHelper _output;

public TestOutputWriter (ITestOutputHelper output) { _output = output; }

public override void WriteLine (string? value) { _output.WriteLine (value ?? string.Empty); }

public override Encoding Encoding => Encoding.UTF8;
}
162 changes: 162 additions & 0 deletions Tests/IntegrationTests/FluentTests/TreeViewFluentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
using Terminal.Gui;
using TerminalGuiFluentTesting;
using Xunit.Abstractions;

namespace IntegrationTests.FluentTests;

public class TreeViewFluentTests
{
private readonly TextWriter _out;

public TreeViewFluentTests (ITestOutputHelper outputHelper) { _out = new TestOutputWriter (outputHelper); }

[Theory]
[ClassData (typeof (V2TestDrivers))]
public void TreeView_AllowReOrdering (V2TestDriver d)
{
var tv = new TreeView
{
Width = Dim.Fill (),
Height = Dim.Fill ()
};

TreeNode car;
TreeNode lorry;
TreeNode bike;

var root = new TreeNode ("Root")
{
Children =
[
car = new ("Car"),
lorry = new ("Lorry"),
bike = new ("Bike")
]
};

tv.AddObject (root);

using GuiTestContext context =
With.A<Window> (40, 10, d)
.Add (tv)
.Focus (tv)
.WaitIteration ()
.ScreenShot ("Before expanding", _out)
.AssertEqual (root, tv.GetObjectOnRow (0))
.Then (() => Assert.Null (tv.GetObjectOnRow (1)))
.Right ()
.ScreenShot ("After expanding", _out)
.AssertEqual (root, tv.GetObjectOnRow (0))
.AssertEqual (car, tv.GetObjectOnRow (1))
.AssertEqual (lorry, tv.GetObjectOnRow (2))
.AssertEqual (bike, tv.GetObjectOnRow (3))
.Then (
() =>
{
// Re order
root.Children = [bike, car, lorry];
tv.RefreshObject (root);
})
.WaitIteration ()
.ScreenShot ("After re-order", _out)
.AssertEqual (root, tv.GetObjectOnRow (0))
.AssertEqual (bike, tv.GetObjectOnRow (1))
.AssertEqual (car, tv.GetObjectOnRow (2))
.AssertEqual (lorry, tv.GetObjectOnRow (3))
.WriteOutLogs (_out);

context.Stop ();
}

[Theory]
[ClassData (typeof (V2TestDrivers))]
public void TreeViewReOrder_PreservesExpansion (V2TestDriver d)
{
var tv = new TreeView
{
Width = Dim.Fill (),
Height = Dim.Fill ()
};

TreeNode car;
TreeNode lorry;
TreeNode bike;

TreeNode mrA;
TreeNode mrB;

TreeNode mrC;

TreeNode mrD;
TreeNode mrE;

var root = new TreeNode ("Root")
{
Children =
[
car = new ("Car")
{
Children =
[
mrA = new ("Mr A"),
mrB = new ("Mr B")
]
},
lorry = new ("Lorry")
{
Children =
[
mrC = new ("Mr C")
]
},
bike = new ("Bike")
{
Children =
[
mrD = new ("Mr D"),
mrE = new ("Mr E")
]
}
]
};

tv.AddObject (root);
tv.ExpandAll ();

using GuiTestContext context =
With.A<Window> (40, 13, d)
.Add (tv)
.WaitIteration ()
.ScreenShot ("Initial State", _out)
.AssertEqual (root, tv.GetObjectOnRow (0))
.AssertEqual (car, tv.GetObjectOnRow (1))
.AssertEqual (mrA, tv.GetObjectOnRow (2))
.AssertEqual (mrB, tv.GetObjectOnRow (3))
.AssertEqual (lorry, tv.GetObjectOnRow (4))
.AssertEqual (mrC, tv.GetObjectOnRow (5))
.AssertEqual (bike, tv.GetObjectOnRow (6))
.AssertEqual (mrD, tv.GetObjectOnRow (7))
.AssertEqual (mrE, tv.GetObjectOnRow (8))
.Then (
() =>
{
// Re order
root.Children = [bike, car, lorry];
tv.RefreshObject (root);
})
.WaitIteration ()
.ScreenShot ("After re-order", _out)
.AssertEqual (root, tv.GetObjectOnRow (0))
.AssertEqual (bike, tv.GetObjectOnRow (1))
.AssertEqual (mrD, tv.GetObjectOnRow (2))
.AssertEqual (mrE, tv.GetObjectOnRow (3))
.AssertEqual (car, tv.GetObjectOnRow (4))
.AssertEqual (mrA, tv.GetObjectOnRow (5))
.AssertEqual (mrB, tv.GetObjectOnRow (6))
.AssertEqual (lorry, tv.GetObjectOnRow (7))
.AssertEqual (mrC, tv.GetObjectOnRow (8))
.WriteOutLogs (_out);

context.Stop ();
}
}
1 change: 1 addition & 0 deletions Tests/IntegrationTests/IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Terminal.Gui\Terminal.Gui.csproj" />
<ProjectReference Include="..\..\TerminalGuiFluentTesting.Xunit\TerminalGuiFluentTesting.Xunit.csproj" />
<ProjectReference Include="..\..\TerminalGuiFluentTesting\TerminalGuiFluentTesting.csproj" />
<ProjectReference Include="..\..\UICatalog\UICatalog.csproj" />
<ProjectReference Include="..\UnitTests\UnitTests.csproj" />
Expand Down