Skip to content

Commit e483a03

Browse files
committed
Investigate fluent assertions for testing v2
1 parent f5ec131 commit e483a03

File tree

7 files changed

+212
-1
lines changed

7 files changed

+212
-1
lines changed

Terminal.Gui/Terminal.Gui.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
<InternalsVisibleTo Include="StressTests" />
8787
<InternalsVisibleTo Include="IntegrationTests" />
8888
<InternalsVisibleTo Include="TerminalGuiDesigner" />
89+
<InternalsVisibleTo Include="TerminalGuiFluentAssertions" />
8990
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
9091
</ItemGroup>
9192
<!-- =================================================================== -->

Terminal.sln

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StressTests", "Tests\Stress
6262
EndProject
6363
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests.Parallelizable", "Tests\UnitTestsParallelizable\UnitTests.Parallelizable.csproj", "{DE780834-190A-8277-51FD-750CC666E82D}"
6464
EndProject
65+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TerminalGuiFluentAssertions", "TerminalGuiFluentAssertions\TerminalGuiFluentAssertions.csproj", "{7C610F03-9E38-409F-9B21-A02D5569E16A}"
66+
EndProject
6567
Global
6668
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6769
Debug|Any CPU = Debug|Any CPU
@@ -116,6 +118,10 @@ Global
116118
{DE780834-190A-8277-51FD-750CC666E82D}.Debug|Any CPU.Build.0 = Debug|Any CPU
117119
{DE780834-190A-8277-51FD-750CC666E82D}.Release|Any CPU.ActiveCfg = Release|Any CPU
118120
{DE780834-190A-8277-51FD-750CC666E82D}.Release|Any CPU.Build.0 = Release|Any CPU
121+
{7C610F03-9E38-409F-9B21-A02D5569E16A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
122+
{7C610F03-9E38-409F-9B21-A02D5569E16A}.Debug|Any CPU.Build.0 = Debug|Any CPU
123+
{7C610F03-9E38-409F-9B21-A02D5569E16A}.Release|Any CPU.ActiveCfg = Release|Any CPU
124+
{7C610F03-9E38-409F-9B21-A02D5569E16A}.Release|Any CPU.Build.0 = Release|Any CPU
119125
EndGlobalSection
120126
GlobalSection(SolutionProperties) = preSolution
121127
HideSolutionNode = FALSE

TerminalGuiFluentAssertions/Class1.cs

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using System.Collections.Concurrent;
2+
using System.Drawing;
3+
using Terminal.Gui;
4+
5+
namespace TerminalGuiFluentAssertions;
6+
7+
class FakeInput<T> : IConsoleInput<T>
8+
{
9+
/// <inheritdoc />
10+
public void Dispose () { }
11+
12+
/// <inheritdoc />
13+
public void Initialize (ConcurrentQueue<T> inputBuffer) { }
14+
15+
/// <inheritdoc />
16+
public void Run (CancellationToken token)
17+
{
18+
// Simulate an infinite loop that checks for cancellation
19+
token.WaitHandle.WaitOne (); // Blocks until the token is cancelled
20+
}
21+
}
22+
23+
class FakeNetInput : FakeInput<ConsoleKeyInfo>, INetInput
24+
{
25+
26+
}
27+
28+
class FakeWindowsInput : FakeInput<WindowsConsole.InputRecord>, IWindowsInput
29+
{
30+
31+
}
32+
33+
class FakeOutput : IConsoleOutput
34+
{
35+
public Size Size { get; set; }
36+
37+
/// <inheritdoc />
38+
public void Dispose ()
39+
{
40+
41+
}
42+
43+
/// <inheritdoc />
44+
public void Write (ReadOnlySpan<char> text)
45+
{
46+
47+
}
48+
49+
/// <inheritdoc />
50+
public void Write (IOutputBuffer buffer)
51+
{
52+
53+
}
54+
55+
/// <inheritdoc />
56+
public Size GetWindowSize ()
57+
{
58+
return Size;
59+
}
60+
61+
/// <inheritdoc />
62+
public void SetCursorVisibility (CursorVisibility visibility)
63+
{
64+
65+
}
66+
67+
/// <inheritdoc />
68+
public void SetCursorPosition (int col, int row)
69+
{
70+
71+
}
72+
73+
}
74+
/// <summary>
75+
/// Entry point to fluent assertions.
76+
/// </summary>
77+
public static class With
78+
{
79+
/// <summary>
80+
/// Entrypoint to fluent assertions
81+
/// </summary>
82+
/// <param name="width"></param>
83+
/// <param name="height"></param>
84+
/// <returns></returns>
85+
public static GuiTestContext<T> A<T> (int width, int height) where T : Toplevel, new ()
86+
{
87+
return new GuiTestContext<T> (width,height);
88+
}
89+
}
90+
public class GuiTestContext<T> where T : Toplevel, new()
91+
{
92+
private readonly CancellationTokenSource _cts;
93+
private readonly Task _runTask;
94+
95+
internal GuiTestContext (int width, int height)
96+
{
97+
IApplication origApp = ApplicationImpl.Instance;
98+
99+
var netInput = new FakeNetInput ();
100+
var winInput = new FakeWindowsInput ();
101+
var output = new FakeOutput ();
102+
103+
output.Size = new (width, height);
104+
105+
var v2 = new ApplicationV2(
106+
() => netInput,
107+
()=>output,
108+
() => winInput,
109+
() => output);
110+
111+
// Create a cancellation token
112+
_cts = new ();
113+
114+
// Start the application in a background thread
115+
_runTask = Task.Run (() =>
116+
{
117+
try
118+
{
119+
ApplicationImpl.ChangeInstance (v2);
120+
121+
v2.Init ();
122+
123+
Application.Run<T> (); // This will block, but it's on a background thread now
124+
125+
Application.Shutdown ();
126+
}
127+
catch (OperationCanceledException)
128+
{
129+
130+
}
131+
finally
132+
{
133+
ApplicationImpl.ChangeInstance (origApp);
134+
}
135+
}, _cts.Token);
136+
137+
Application.Shutdown ();
138+
}
139+
140+
/// <summary>
141+
/// Stops the application and waits for the background thread to exit.
142+
/// </summary>
143+
public void Stop ()
144+
{
145+
_cts.Cancel ();
146+
Application.Invoke (()=>Application.RequestStop());
147+
_runTask.Wait (); // Ensure the background thread exits
148+
}
149+
150+
// Cleanup to avoid state bleed between tests
151+
public void Dispose ()
152+
{
153+
}
154+
}
155+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\Terminal.Gui\Terminal.Gui.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

Tests/UnitTests/ConsoleDrivers/V2/ApplicationV2Tests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace UnitTests.ConsoleDrivers.V2;
77
public class ApplicationV2Tests
88
{
9-
9+
1010
private ApplicationV2 NewApplicationV2 ()
1111
{
1212
var netInput = new Mock<INetInput> ();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using TerminalGuiFluentAssertions;
7+
8+
namespace UnitTests.FluentTests;
9+
public class BasicFluentAssertionTests
10+
{
11+
[Fact]
12+
public void GuiTestContext_StartsAndStopsWithoutError ()
13+
{
14+
var context = With.A<Window> (40, 10);
15+
16+
// No actual assertions are needed — if no exceptions are thrown, it's working
17+
context.Stop ();
18+
}
19+
20+
[Fact]
21+
public void Test ()
22+
{
23+
var myView = new TextField () { Width = 10 };
24+
25+
26+
27+
/*
28+
using var ctx = With.A<Window> (20, 10)
29+
.Add (myView, 3, 2)
30+
.Focus (myView)
31+
.Type ("Hello");
32+
33+
Assert.Equal ("Hello", myView.Text);*/
34+
}
35+
}

Tests/UnitTests/UnitTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
</ItemGroup>
4646
<ItemGroup>
4747
<ProjectReference Include="..\..\Terminal.Gui\Terminal.Gui.csproj" />
48+
<ProjectReference Include="..\..\TerminalGuiFluentAssertions\TerminalGuiFluentAssertions.csproj" />
4849
<ProjectReference Include="..\..\UICatalog\UICatalog.csproj" />
4950
</ItemGroup>
5051
<ItemGroup>

0 commit comments

Comments
 (0)