mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-01 16:59:35 +01:00
first unit tests
This commit is contained in:
195
UnitTests/ApplicationTests.cs
Normal file
195
UnitTests/ApplicationTests.cs
Normal file
@@ -0,0 +1,195 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Terminal.Gui;
|
||||
using Xunit;
|
||||
|
||||
// Alais Console to MockConsole so we don't accidentally use Console
|
||||
using Console = Terminal.Gui.MockConsole;
|
||||
|
||||
// Since Application is a singleton we can't run tests in parallel
|
||||
[assembly: CollectionBehavior (DisableTestParallelization = true)]
|
||||
|
||||
namespace Terminal.Gui {
|
||||
public class ApplicationTests {
|
||||
[Fact]
|
||||
public void TestInitShutdown ()
|
||||
{
|
||||
Assert.Null (Application.Current);
|
||||
Assert.Null (Application.CurrentView);
|
||||
Assert.Null (Application.Top);
|
||||
Assert.Null (Application.MainLoop);
|
||||
Assert.Null (Application.Driver);
|
||||
|
||||
Application.Init (new MockDriver ());
|
||||
Assert.NotNull (Application.Current);
|
||||
Assert.NotNull (Application.CurrentView);
|
||||
Assert.NotNull (Application.Top);
|
||||
Assert.NotNull (Application.MainLoop);
|
||||
Assert.NotNull (Application.Driver);
|
||||
|
||||
// MockDriver is always 80x25
|
||||
Assert.Equal (80, Application.Driver.Cols);
|
||||
Assert.Equal (25, Application.Driver.Rows);
|
||||
|
||||
Application.Shutdown (true);
|
||||
Assert.Null (Application.Current);
|
||||
Assert.Null (Application.CurrentView);
|
||||
Assert.Null (Application.Top);
|
||||
Assert.Null (Application.MainLoop);
|
||||
Assert.Null (Application.Driver);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestNewRunState ()
|
||||
{
|
||||
var rs = new Application.RunState (null);
|
||||
Assert.NotNull (rs);
|
||||
|
||||
// Should not throw because Toplevel was null
|
||||
rs.Dispose ();
|
||||
|
||||
var top = new Toplevel ();
|
||||
rs = new Application.RunState (top);
|
||||
Assert.NotNull (rs);
|
||||
|
||||
// Should throw because there's no stack
|
||||
Assert.Throws<InvalidOperationException> (() => rs.Dispose ());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestBeginEnd ()
|
||||
{
|
||||
// Setup Mock driver
|
||||
Application.Init (new MockDriver ());
|
||||
Assert.NotNull (Application.Driver);
|
||||
|
||||
// Test null Toplevel
|
||||
Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
|
||||
|
||||
var top = new Toplevel ();
|
||||
var rs = Application.Begin (top);
|
||||
Assert.NotNull (rs);
|
||||
Assert.Equal (top, Application.Current);
|
||||
Application.End (rs, true);
|
||||
|
||||
Assert.Null (Application.Current);
|
||||
Assert.Null (Application.CurrentView);
|
||||
Assert.Null (Application.Top);
|
||||
Assert.Null (Application.MainLoop);
|
||||
Assert.Null (Application.Driver);
|
||||
|
||||
Application.Shutdown (true);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestRequestStop ()
|
||||
{
|
||||
// Setup Mock driver
|
||||
Application.Init (new MockDriver ());
|
||||
Assert.NotNull (Application.Driver);
|
||||
|
||||
var top = new Toplevel ();
|
||||
var rs = Application.Begin (top);
|
||||
Assert.NotNull (rs);
|
||||
Assert.Equal (top, Application.Current);
|
||||
|
||||
Application.Iteration = () => {
|
||||
Application.RequestStop ();
|
||||
};
|
||||
|
||||
Application.Run (top, true);
|
||||
|
||||
Application.Shutdown (true);
|
||||
Assert.Null (Application.Current);
|
||||
Assert.Null (Application.CurrentView);
|
||||
Assert.Null (Application.Top);
|
||||
Assert.Null (Application.MainLoop);
|
||||
Assert.Null (Application.Driver);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestRunningFalse ()
|
||||
{
|
||||
// Setup Mock driver
|
||||
Application.Init (new MockDriver ());
|
||||
Assert.NotNull (Application.Driver);
|
||||
|
||||
var top = new Toplevel ();
|
||||
var rs = Application.Begin (top);
|
||||
Assert.NotNull (rs);
|
||||
Assert.Equal (top, Application.Current);
|
||||
|
||||
Application.Iteration = () => {
|
||||
top.Running = false;
|
||||
};
|
||||
|
||||
Application.Run (top, true);
|
||||
|
||||
Application.Shutdown (true);
|
||||
Assert.Null (Application.Current);
|
||||
Assert.Null (Application.CurrentView);
|
||||
Assert.Null (Application.Top);
|
||||
Assert.Null (Application.MainLoop);
|
||||
Assert.Null (Application.Driver);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void TestKeyUp ()
|
||||
{
|
||||
// Setup Mock driver
|
||||
Application.Init (new MockDriver ());
|
||||
Assert.NotNull (Application.Driver);
|
||||
|
||||
// Setup some fake kepresses (This)
|
||||
var input = "Tests";
|
||||
|
||||
// Put a control-q in at the end
|
||||
Console.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true));
|
||||
foreach (var c in input.Reverse()) {
|
||||
if (char.IsLetter (c)) {
|
||||
Console.MockKeyPresses.Push (new ConsoleKeyInfo (char.ToLower (c), (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
|
||||
}
|
||||
}
|
||||
|
||||
int stackSize = Console.MockKeyPresses.Count;
|
||||
|
||||
int iterations = 0;
|
||||
Application.Iteration = () => {
|
||||
iterations++;
|
||||
};
|
||||
|
||||
int keyUps = 0;
|
||||
var output = string.Empty;
|
||||
Application.Top.KeyUp += (View.KeyEventEventArgs args) => {
|
||||
if (args.KeyEvent.Key != Key.ControlQ) {
|
||||
output += (char)args.KeyEvent.KeyValue;
|
||||
}
|
||||
keyUps++;
|
||||
};
|
||||
|
||||
Application.Run (Application.Top, true);
|
||||
|
||||
// Input string should match output
|
||||
Assert.Equal (input, output);
|
||||
|
||||
// # of key up events should match stack size
|
||||
Assert.Equal (stackSize, keyUps);
|
||||
|
||||
// # of key up events should match # of iterations
|
||||
Assert.Equal (stackSize, iterations);
|
||||
|
||||
Application.Shutdown (true);
|
||||
Assert.Null (Application.Current);
|
||||
Assert.Null (Application.CurrentView);
|
||||
Assert.Null (Application.Top);
|
||||
Assert.Null (Application.MainLoop);
|
||||
Assert.Null (Application.Driver);
|
||||
}
|
||||
}
|
||||
}
|
||||
67
UnitTests/ConsoleDriverTests.cs
Normal file
67
UnitTests/ConsoleDriverTests.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using Terminal.Gui;
|
||||
using Xunit;
|
||||
|
||||
// Alais Console to MockConsole so we don't accidentally use Console
|
||||
using Console = Terminal.Gui.MockConsole;
|
||||
|
||||
namespace Terminal.Gui {
|
||||
public class ConsoleDriverTests {
|
||||
[Fact]
|
||||
public void TestInit ()
|
||||
{
|
||||
var driver = new MockDriver ();
|
||||
driver.Init (() => { });
|
||||
|
||||
Assert.Equal (80, Console.BufferWidth);
|
||||
Assert.Equal (25, Console.BufferHeight);
|
||||
|
||||
// MockDriver is always 80x25
|
||||
Assert.Equal (Console.BufferWidth, driver.Cols);
|
||||
Assert.Equal (Console.BufferHeight, driver.Rows);
|
||||
driver.End ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestEnd ()
|
||||
{
|
||||
var driver = new MockDriver ();
|
||||
driver.Init (() => { });
|
||||
|
||||
MockConsole.ForegroundColor = ConsoleColor.Red;
|
||||
Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
|
||||
|
||||
MockConsole.BackgroundColor = ConsoleColor.Green;
|
||||
Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
|
||||
driver.Move (2, 3);
|
||||
Assert.Equal (2, Console.CursorLeft);
|
||||
Assert.Equal (3, Console.CursorTop);
|
||||
|
||||
driver.End ();
|
||||
Assert.Equal (0, Console.CursorLeft);
|
||||
Assert.Equal (0, Console.CursorTop);
|
||||
Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
|
||||
Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestSetColors ()
|
||||
{
|
||||
var driver = new MockDriver ();
|
||||
driver.Init (() => { });
|
||||
Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
|
||||
Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Assert.Equal (ConsoleColor.Red, Console.ForegroundColor);
|
||||
|
||||
Console.BackgroundColor = ConsoleColor.Green;
|
||||
Assert.Equal (ConsoleColor.Green, Console.BackgroundColor);
|
||||
|
||||
Console.ResetColor ();
|
||||
Assert.Equal (ConsoleColor.Gray, Console.ForegroundColor);
|
||||
Assert.Equal (ConsoleColor.Black, Console.BackgroundColor);
|
||||
driver.End ();
|
||||
}
|
||||
}
|
||||
}
|
||||
20
UnitTests/UnitTests.csproj
Normal file
20
UnitTests/UnitTests.csproj
Normal file
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.0" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="1.2.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Terminal.Gui\Terminal.Gui.csproj" />
|
||||
<ProjectReference Include="..\UICatalog\UICatalog.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
5
UnitTests/xunit.runner.json
Normal file
5
UnitTests/xunit.runner.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
|
||||
"parallelizeTestCollections": false,
|
||||
"parallelizeAssembly": false
|
||||
}
|
||||
Reference in New Issue
Block a user