Files
Terminal.Gui/Tests/UnitTestsParallelizable/Drawing/ThicknessTests.cs
Copilot e6a0ec64ca Fixes #4361 - Consolidate FakeDriver into library and refactor driver architecture (#4362)
* Initial plan

* Add ScreenChanged event, SetScreenSize method, and fix FakeDriver buffer initialization

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add comprehensive tests for ScreenChanged event and buffer integrity

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Replace obsolete SizeChanged usage with ScreenChanged in core and tests

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Refactor terminal size event handling

Replaced `ScreenChanged` with `SizeChanged` across the codebase to standardize naming and improve clarity. Updated event handling logic, including subscriptions, unsubscriptions, and raising methods. Removed deprecated `ScreenChanged` event and backward compatibility code.

Refactored driver initialization to handle legacy `IConsoleDriver` types separately. Updated tests and mock implementations to align with the new `SizeChanged` event. Improved documentation and comments to reflect these changes.

These updates enhance maintainability, consistency, and modernize the architecture.

* Refactor & Code Cleanup: Replace IWindowSizeMonitor with IConsoleSizeMonitor

Renamed `IWindowSizeMonitor` to `IConsoleSizeMonitor` across the codebase, updating all references, method signatures, and event handlers. Replaced the `WindowSizeMonitor` class with the new `ConsoleSizeMonitor` implementation, which includes improved terminal size change handling via the `Poll` method.

Enabled nullable reference types in several files to enhance code safety. Updated test cases to reflect the new `IConsoleSizeMonitor` interface. Removed redundant code, simplified null checks, and corrected minor typos in comments. Streamlined the codebase by removing the obsolete `WindowSizeMonitor` class and its interface.

* Code cleanup - Refactor and enhance ShadowView and FakeDriverTests

Updated ShadowView.cs to use null-conditional operators and added null checks for safer access to `ScreenContents`. Refined XML documentation in View.Layout.cs for clarity and consistency.

Refactored FakeDriverTests.cs to leverage modern C# features, including shorthand object instantiation, inline lambdas, and tuple-like syntax for `Size` and `Rectangle`. Removed redundant tests and improved test readability and reliability.

Enhanced error handling with null checks and ensured backward compatibility for deprecated events. Improved test coverage for resizing, clipboard operations, and invalid coordinates. Verified buffer integrity and screen updates after resizing.

General improvements include replacing explicit type declarations with `var`, removing unused imports, and aligning code formatting for better readability.

Refactor and improve code quality and test coverage

Updated `ShadowView` for null safety using null-conditional operators. Simplified object initializations and modernized syntax across the codebase, including shorthand initializations and inline lambdas. Enhanced event handling logic and ensured compatibility with obsolete members.

Refactored `FakeDriverTests` by removing redundant code, standardizing formatting, and improving test setup. Suppressed obsolete warnings where necessary. Improved XML documentation in `View.Layout.cs` for clarity and removed outdated references.

Performed general cleanup, including removing unused namespaces, redundant comments, and ensuring consistent formatting. These changes enhance readability, maintainability, and runtime safety.

* Code cleanup

Refactor TimedEventsTests for readability and consistency

Improved code readability and maintainability:
- Enabled nullable reference types with `#nullable enable`.
- Removed unused `using System.Diagnostics;`.
- Updated namespace to `UnitTests.ApplicationTests`.
- Replaced `Terminal.Gui.App.TimedEvents` with `TimedEvents`.
- Reformatted XML documentation comments for alignment.
- Used `var` and target-typed new expressions for consistency.
- Reformatted `Parallel.For` loops and lambdas for clarity.
- Added `Thread.Sleep(10)` to prevent excessive CPU usage in tests.
- Improved assertions and event handler formatting in tests.

Aligned with modern C# coding practices.

* Code Cleanup - No more driver warnings.

Refactor codebase and introduce FakeClipboard

- Adjusted `.editorconfig` to change severity levels for CS0612, CS0618, and CS0672 diagnostics.
- Replaced `FakeDriver.FakeClipboard` with a new `FakeClipboard` class for testing purposes, supporting exception handling and clipboard data manipulation.
- Removed redundant methods (`MakeColor`, `MapKey`) and unused classes (`MockConsoleDriver`) to streamline the codebase.
- Refactored `ConsoleDriverFacade` and `FakeDriver` to simplify logic and improve maintainability.
- Updated tests to use `CreateFakeDriver` and removed or commented out obsolete tests.
- Reformatted and cleaned up code for readability across multiple files.

* Refactor FakeDriver - Code Cleanup

Standardized console size management by replacing `WindowSizeMonitor` with `ConsoleSizeMonitor` across the codebase. Updated methods `GetWindowSize` and `SetWindowSize` to `GetSize` and `SetSize` for consistency.

Refactored `FakeDriver` to use `SetScreenSize` and removed redundant methods. Simplified driver initialization by removing legacy `InternalInit` logic.

Standardized ANSI escape sequences by replacing `CSI_ReportTerminalSizeInChars` with `CSI_ReportWindowSizeInChars`.

Updated test cases to align with the new `ConsoleSizeMonitor` and `SetScreenSize` methods. Removed obsolete test utilities like `FakeSizeMonitor` and `FakeWindowSizeMonitor`.

Performed general code cleanup, including removing unused classes, redundant code, and improving formatting. Fixed resizing logic issues and improved exception handling in driver methods.

* Update Terminal.Gui/Drivers/OutputBuffer.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Terminal.Gui/Drivers/MouseButtonStateEx.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Terminal.Gui/App/MainLoop/IApplicationMainLoop.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Tests/UnitTests/Views/ToplevelTests.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Terminal.Gui/ViewBase/View.Layout.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Moved all Drawing tests to Paralleizable - proving Fakedriver works

Enhanced `Ruler` and `Thickness` classes by adding an optional `IConsoleDriver? driver` parameter to decouple rendering from the default `Application.Driver`, improving flexibility and testability. Updated `DriverAssert` to support nullable drivers and added defensive checks.

Refactored and expanded test cases for `Ruler`, `Thickness`, `LineCanvas`, and `StraightLineExtensions` to ensure comprehensive coverage, including zero-length intersections, line rendering, and exclusion logic. Migrated rendering-dependent tests to a parallelizable namespace.

Removed redundant tests, improved naming conventions, and updated documentation for better maintainability and clarity.

* Fixed Run<T> startup hang.

Refactor: Simplify driver logic and update SetSize methods

Removed FakeDriver safeguard in unit tests to simplify
CreateDriver logic. Updated SetSize methods in NetOutput,
UnixOutput, and WindowsOutput to do nothing instead of
throwing NotImplementedException. Modified SizeChanged
event in ConsoleDriverFacade to call SetScreenSize directly.
Commented out unnecessary debug validation in DimAuto.
These changes improve maintainability and reduce complexity.

* Fixed intermittent unit test bug.

Refactored `_cachedRunStateToplevel` to `CachedRunStateToplevel` as an internal static property, delegating its management to `ApplicationImpl` for improved encapsulation. Updated all references to use the new property and centralized its handling in `ApplicationImpl`.

Removed the `MouseGrabHandler` property from `ApplicationImpl` and simplified driver-related assignments by replacing `Application.ForceDriver` and `Application.Screen` with direct references.

Reset `CachedRunStateToplevel` during cleanup to ensure proper state management. Updated the `Invoke` method to use `Top` directly, aligning with the refactored design. Improved debug assertions and performed general cleanup to enhance code readability and maintainability.

* Fixed intermittent bug an massive code cleanup of warnings.

Refactor and enhance codebase for maintainability

- Applied null-conditional operator (`!`) to improve null-safety.
- Refactored tests for clarity, added new cases, and removed redundancies.
- Introduced `FakeApplicationFactory` and `FakeSizeMonitor` for testing.
- Removed unused code, including legacy `DecodeEscSeq` logic.
- Reformatted code for readability and consistency.
- Updated assertions for more accurate validation in tests.
- Fixed potential null reference issues across multiple files.
- Improved event handling with proper null checks.
- Enhanced documentation for new classes and methods.
- Modernized code with C# features like `record struct` and `nullable enable`.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tig <585482+tig@users.noreply.github.com>
Co-authored-by: Tig <tig@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-27 23:34:14 -06:00

884 lines
32 KiB
C#

using System.Text;
using UnitTests;
using UnitTests.Parallelizable;
using Xunit.Abstractions;
namespace UnitTests_Parallelizable.DrawingTests;
public class ThicknessTests (ITestOutputHelper output) : ParallelizableBase
{
[Fact]
public void Constructor_Defaults ()
{
var t = new Thickness ();
Assert.Equal (0, t.Left);
Assert.Equal (0, t.Top);
Assert.Equal (0, t.Right);
Assert.Equal (0, t.Bottom);
}
[Fact]
public void Constructor_params ()
{
var t = new Thickness (1, 2, 3, 4);
Assert.Equal (1, t.Left);
Assert.Equal (2, t.Top);
Assert.Equal (3, t.Right);
Assert.Equal (4, t.Bottom);
t = new (0, 0, 0, 0);
Assert.Equal (0, t.Left);
Assert.Equal (0, t.Top);
Assert.Equal (0, t.Right);
Assert.Equal (0, t.Bottom);
t = new (-1, 0, 0, 0);
Assert.Equal (-1, t.Left);
Assert.Equal (0, t.Top);
Assert.Equal (0, t.Right);
Assert.Equal (0, t.Bottom);
}
[Fact]
public void Constructor_Width ()
{
var t = new Thickness (1);
Assert.Equal (1, t.Left);
Assert.Equal (1, t.Top);
Assert.Equal (1, t.Right);
Assert.Equal (1, t.Bottom);
}
[Fact]
public void Empty_Is_empty ()
{
var t = Thickness.Empty;
Assert.Equal (0, t.Left);
Assert.Equal (0, t.Top);
Assert.Equal (0, t.Right);
Assert.Equal (0, t.Bottom);
}
// TODO: Add more test cases:
// - Negative thickness values
[Fact]
public void EqualsTest ()
{
var t = new Thickness (1, 2, 3, 4);
var t2 = new Thickness (1, 2, 3, 4);
Assert.True (t.Equals (t2));
Assert.True (t == t2);
Assert.False (t != t2);
}
[Fact]
public void GetHashCodeTest ()
{
var t = new Thickness (1, 2, 3, 4);
Assert.Equal (t.GetHashCode (), t.GetHashCode ());
}
// Test Thickness.GetInside(Rectangle)
[Theory]
[InlineData (0, 0, 10, 10, 1, 1, 8, 8)]
[InlineData (1, 0, 10, 10, 2, 1, 8, 8)]
[InlineData (0, 1, 10, 10, 1, 2, 8, 8)]
public void GetInside_Uniform (int x, int y, int width, int height, int expectedX, int expectedY, int expectedWidth, int expectedHeight)
{
var t = new Thickness (1, 1, 1, 1); // Uniform thickness for simplicity
var r = new Rectangle (x, y, width, height);
Rectangle inside = t.GetInside (r);
Assert.Equal (expectedX, inside.X);
Assert.Equal (expectedY, inside.Y);
Assert.Equal (expectedWidth, inside.Width);
Assert.Equal (expectedHeight, inside.Height);
}
[Fact]
public void GetInsideTests_Mixed_Pos_Neg_Thickness_Non_Empty_Size ()
{
var t = new Thickness (-1, 1, -1, 1);
var r = new Rectangle (0, 0, 3, 3);
Rectangle inside = t.GetInside (r);
Assert.Equal (-1, inside.X);
Assert.Equal (1, inside.Y);
Assert.Equal (5, inside.Width);
Assert.Equal (1, inside.Height);
t = new (-1, 1, -1, 1);
r = new (-1, -1, 3, 3);
inside = t.GetInside (r);
Assert.Equal (-2, inside.X);
Assert.Equal (0, inside.Y);
Assert.Equal (5, inside.Width);
Assert.Equal (1, inside.Height);
t = new (-1, 1, -1, 1);
r = new (1, 1, 3, 3);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (2, inside.Y);
Assert.Equal (5, inside.Width);
Assert.Equal (1, inside.Height);
t = new (-2, -1, 0, 1);
r = new (-1, 0, 50, 60);
inside = t.GetInside (r);
Assert.Equal (-3, inside.X);
Assert.Equal (-1, inside.Y);
Assert.Equal (52, inside.Width);
Assert.Equal (60, inside.Height);
}
[Fact]
public void GetInsideTests_Negative_Thickness_Non_Empty_Size ()
{
var t = new Thickness (-1, -1, -1, -1);
var r = new Rectangle (0, 0, 3, 3);
Rectangle inside = t.GetInside (r);
Assert.Equal (-1, inside.X);
Assert.Equal (-1, inside.Y);
Assert.Equal (5, inside.Width);
Assert.Equal (5, inside.Height);
t = new (-1, -1, -1, -1);
r = new (-1, -1, 3, 3);
inside = t.GetInside (r);
Assert.Equal (-2, inside.X);
Assert.Equal (-2, inside.Y);
Assert.Equal (5, inside.Width);
Assert.Equal (5, inside.Height);
t = new (-1, -1, -1, -1);
r = new (1, 1, 3, 3);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (0, inside.Y);
Assert.Equal (5, inside.Width);
Assert.Equal (5, inside.Height);
t = new (-1, -2, -3, -4);
r = new (-1, 0, 50, 60);
inside = t.GetInside (r);
Assert.Equal (-2, inside.X);
Assert.Equal (-2, inside.Y);
Assert.Equal (54, inside.Width);
Assert.Equal (66, inside.Height);
}
[Fact]
public void GetInsideTests_Positive_Thickness_Non_Empty_Size ()
{
var t = new Thickness (1, 1, 1, 1);
var r = new Rectangle (0, 0, 3, 3);
Rectangle inside = t.GetInside (r);
Assert.Equal (1, inside.X);
Assert.Equal (1, inside.Y);
Assert.Equal (1, inside.Width);
Assert.Equal (1, inside.Height);
t = new (1, 1, 1, 1);
r = new (-1, -1, 3, 3);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (0, inside.Y);
Assert.Equal (1, inside.Width);
Assert.Equal (1, inside.Height);
t = new (1, 1, 1, 1);
r = new (1, 1, 3, 3);
inside = t.GetInside (r);
Assert.Equal (2, inside.X);
Assert.Equal (2, inside.Y);
Assert.Equal (1, inside.Width);
Assert.Equal (1, inside.Height);
t = new (1, 2, 3, 4);
r = new (-1, 0, 50, 60);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (2, inside.Y);
Assert.Equal (46, inside.Width);
Assert.Equal (54, inside.Height);
}
[Fact]
public void GetInsideTests_Positive_Thickness_Too_Small_Rect_Means_Empty_Size ()
{
var t = new Thickness (1, 1, 1, 1);
var r = Rectangle.Empty;
Rectangle inside = t.GetInside (r);
Assert.Equal (1, inside.X);
Assert.Equal (1, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (1, 1, 1, 1);
r = new (0, 0, 1, 1);
inside = t.GetInside (r);
Assert.Equal (1, inside.X);
Assert.Equal (1, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (1, 1, 1, 1);
r = new (1, 1, 1, 1);
inside = t.GetInside (r);
Assert.Equal (2, inside.X);
Assert.Equal (2, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (1, 1, 1, 1);
r = new (0, 0, 1, 0);
inside = t.GetInside (r);
Assert.Equal (1, inside.X);
Assert.Equal (1, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (1, 1, 1, 1);
r = new (0, 0, 0, 1);
inside = t.GetInside (r);
Assert.Equal (1, inside.X);
Assert.Equal (1, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (1, 1, 1, 1);
r = new (-1, -1, 0, 1);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (0, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (1, 1, 1, 1);
r = new (0, 0, 2, 2);
inside = t.GetInside (r);
Assert.Equal (1, inside.X);
Assert.Equal (1, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (1, 1, 1, 1);
r = new (-1, -1, 2, 2);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (0, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (1, 1, 1, 1);
r = new (1, 1, 2, 2);
inside = t.GetInside (r);
Assert.Equal (2, inside.X);
Assert.Equal (2, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (1, 2, 3, 4);
r = new (-1, 0, 4, 6);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (2, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
}
[Fact]
public void GetInsideTests_Zero_Thickness ()
{
var t = new Thickness (0, 0, 0, 0);
var r = Rectangle.Empty;
Rectangle inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (0, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (0, inside.Height);
t = new (0, 0, 0, 0);
r = new (0, 0, 1, 1);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (0, inside.Y);
Assert.Equal (1, inside.Width);
Assert.Equal (1, inside.Height);
t = new (0, 0, 0, 0);
r = new (1, 1, 1, 1);
inside = t.GetInside (r);
Assert.Equal (1, inside.X);
Assert.Equal (1, inside.Y);
Assert.Equal (1, inside.Width);
Assert.Equal (1, inside.Height);
t = new (0, 0, 0, 0);
r = new (0, 0, 1, 0);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (0, inside.Y);
Assert.Equal (1, inside.Width);
Assert.Equal (0, inside.Height);
t = new (0, 0, 0, 0);
r = new (0, 0, 0, 1);
inside = t.GetInside (r);
Assert.Equal (0, inside.X);
Assert.Equal (0, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (1, inside.Height);
t = new (0, 0, 0, 0);
r = new (-1, -1, 0, 1);
inside = t.GetInside (r);
Assert.Equal (-1, inside.X);
Assert.Equal (-1, inside.Y);
Assert.Equal (0, inside.Width);
Assert.Equal (1, inside.Height);
}
[Fact]
public void Horizontal_get ()
{
var t = new Thickness (1, 2, 3, 4);
Assert.Equal (4, t.Horizontal);
t = new (0);
Assert.Equal (0, t.Horizontal);
}
[Fact]
public void Horizontal_set ()
{
var t = new Thickness ();
t.Horizontal = 10;
Assert.Equal (10, t.Horizontal);
Assert.Equal (5, t.Left);
Assert.Equal (0, t.Top);
Assert.Equal (5, t.Right);
Assert.Equal (0, t.Bottom);
Assert.Equal (0, t.Vertical);
t.Horizontal = 11;
Assert.Equal (10, t.Horizontal);
Assert.Equal (5, t.Left);
Assert.Equal (0, t.Top);
Assert.Equal (5, t.Right);
Assert.Equal (0, t.Bottom);
Assert.Equal (0, t.Vertical);
t.Horizontal = 1;
Assert.Equal (0, t.Horizontal);
Assert.Equal (0, t.Left);
Assert.Equal (0, t.Top);
Assert.Equal (0, t.Right);
Assert.Equal (0, t.Bottom);
Assert.Equal (0, t.Vertical);
}
[Theory]
[InlineData (0, 0, 10, 10, 3, 3, false)] // Inside the inner rectangle
[InlineData (0, 0, 10, 10, 0, 0, true)] // On corner, in thickness
[InlineData (0, 0, 10, 10, 9, 9, true)] // On opposite corner, in thickness
[InlineData (0, 0, 10, 10, 5, 5, false)] // Inside the inner rectangle
[InlineData (0, 0, 10, 10, -1, -1, false)] // Outside the outer rectangle
[InlineData (0, 0, 0, 0, 3, 3, false)] // Inside the inner rectangle
[InlineData (0, 0, 0, 0, 0, 0, false)] // On corner, in thickness
[InlineData (0, 0, 0, 0, 9, 9, false)] // On opposite corner, in thickness
[InlineData (0, 0, 0, 0, 5, 5, false)] // Inside the inner rectangle
[InlineData (0, 0, 0, 0, -1, -1, false)] // Outside the outer rectangle
[InlineData (1, 1, 10, 10, 1, 1, true)] // On corner, in thickness
[InlineData (1, 1, 10, 10, 10, 10, true)] // On opposite corner, in thickness
[InlineData (1, 1, 10, 10, 6, 6, false)] // Inside the inner rectangle
[InlineData (1, 1, 10, 10, 0, 0, false)] // Outside the outer rectangle
[InlineData (-1, -1, 10, 10, -1, -1, true)] // On corner, in thickness
[InlineData (-1, -1, 10, 10, 8, 8, true)] // On opposite corner, in thickness
[InlineData (-1, -1, 10, 10, 4, 4, false)] // Inside the inner rectangle
[InlineData (-1, -1, 10, 10, -2, -2, false)] // Outside the outer rectangle
public void TestContains_Uniform1 (int x, int y, int width, int height, int pointX, int pointY, bool expected)
{
var rect = new Rectangle (x, y, width, height);
var thickness = new Thickness (1, 1, 1, 1); // Uniform thickness for simplicity
bool result = thickness.Contains (rect, new (pointX, pointY));
Assert.Equal (expected, result);
}
[Theory]
[InlineData (0, 0, 10, 10, 3, 3, false)] // Inside the inner rectangle
[InlineData (0, 0, 10, 10, 0, 0, true)] // On corner, in thickness
[InlineData (0, 0, 10, 10, 1, 1, true)] // On corner, in thickness
[InlineData (0, 0, 10, 10, 9, 9, true)] // On opposite corner, in thickness
[InlineData (0, 0, 10, 10, 5, 5, false)] // Inside the inner rectangle
[InlineData (0, 0, 10, 10, 8, 8, true)] // On opposite corner, in thickness
[InlineData (0, 0, 10, 10, -1, -1, false)] // Outside the outer rectangle
// Test with a rectangle that is same size as the thickness (inner is empty)
[InlineData (0, 0, 4, 4, 0, 0, true)] // in thickness
[InlineData (0, 0, 4, 4, 1, 1, true)] // in thickness
[InlineData (0, 0, 4, 4, 2, 2, true)] // in thickness
[InlineData (0, 0, 4, 4, 3, 3, true)] // in thickness
[InlineData (0, 0, 4, 4, 5, 5, false)] // outside outer rect
[InlineData (0, 0, 4, 4, 4, 4, false)] // outside outer rect
[InlineData (1, 1, 10, 10, 4, 4, false)] // Inside the inner rectangle
[InlineData (1, 1, 10, 10, 1, 1, true)] // On corner, in thickness
[InlineData (1, 1, 10, 10, 2, 2, true)] // On corner, in thickness
[InlineData (1, 1, 10, 10, 10, 10, true)] // On opposite corner, in thickness
[InlineData (1, 1, 10, 10, 6, 6, false)] // Inside the inner rectangle
[InlineData (1, 1, 10, 10, 9, 9, true)] // On opposite corner, in thickness
[InlineData (1, 1, 10, 10, 0, 0, false)] // Outside the outer rectangle
// Test with a rectangle that is same size as the thickness (inner is empty)
[InlineData (-1, -1, 4, 4, -1, -1, true)] // in thickness
[InlineData (-1, -1, 4, 4, 0, 0, true)] // in thickness
[InlineData (-1, -1, 4, 4, 1, 1, true)] // in thickness
[InlineData (-1, -1, 4, 4, 2, 2, true)] // in thickness
[InlineData (-1, -1, 4, 4, 4, 4, false)] // outside outer rect
[InlineData (-1, -1, 4, 4, 3, 3, false)] // outside outer rect
public void TestContains_Uniform2 (int x, int y, int width, int height, int pointX, int pointY, bool expected)
{
var rect = new Rectangle (x, y, width, height);
var thickness = new Thickness (2, 2, 2, 2); // Uniform thickness for simplicity
bool result = thickness.Contains (rect, new (pointX, pointY));
Assert.Equal (expected, result);
}
[Theory]
[InlineData (0, 0, 10, 10, 3, 3, false)] // Inside the inner rectangle
[InlineData (0, 0, 10, 10, 0, 0, false)] // On corner, in thickness
[InlineData (0, 0, 10, 10, 9, 9, false)] // On opposite corner, in thickness
[InlineData (0, 0, 10, 10, 5, 5, false)] // Inside the inner rectangle
[InlineData (0, 0, 10, 10, -1, -1, false)] // Outside the outer rectangle
[InlineData (0, 0, 0, 0, 3, 3, false)] // Inside the inner rectangle
[InlineData (0, 0, 0, 0, 0, 0, false)] // On corner, in thickness
[InlineData (0, 0, 0, 0, 9, 9, false)] // On opposite corner, in thickness
[InlineData (0, 0, 0, 0, 5, 5, false)] // Inside the inner rectangle
[InlineData (0, 0, 0, 0, -1, -1, false)] // Outside the outer rectangle
[InlineData (1, 1, 10, 10, 1, 1, false)] // On corner, in thickness
[InlineData (1, 1, 10, 10, 10, 10, false)] // On opposite corner, in thickness
[InlineData (1, 1, 10, 10, 6, 6, false)] // Inside the inner rectangle
[InlineData (1, 1, 10, 10, 0, 0, false)] // Outside the outer rectangle
[InlineData (-1, -1, 10, 10, -1, -1, false)] // On corner, in thickness
[InlineData (-1, -1, 10, 10, 8, 8, false)] // On opposite corner, in thickness
[InlineData (-1, -1, 10, 10, 4, 4, false)] // Inside the inner rectangle
[InlineData (-1, -1, 10, 10, -2, -2, false)] // Outside the outer rectangle
public void TestContains_ZeroThickness (
int x,
int y,
int width,
int height,
int pointX,
int pointY,
bool expected
)
{
var rect = new Rectangle (x, y, width, height);
var thickness = new Thickness (0, 0, 0, 0); // Uniform thickness for simplicity
bool result = thickness.Contains (rect, new (pointX, pointY));
Assert.Equal (expected, result);
}
[Theory]
[InlineData (0, 0, 0, 0, 0, 0, false)]
[InlineData (0, 0, 0, 1, 0, 0, false)]
[InlineData (0, 0, 1, 0, 0, 0, false)]
[InlineData (0, 0, 1, 1, 0, 0, true)]
[InlineData (1, 1, 0, 0, 0, 0, false)]
[InlineData (1, 1, 0, 1, 0, 0, false)]
[InlineData (1, 1, 1, 0, 0, 0, false)]
[InlineData (1, 1, 1, 0, 0, 1, false)]
[InlineData (1, 1, 1, 1, 1, 0, false)]
[InlineData (1, 1, 1, 1, 1, 1, true)]
public void TestContains_Left_Only (int x, int y, int width, int height, int pointX, int pointY, bool expected)
{
var outside = new Rectangle (x, y, width, height);
var thickness = new Thickness (1, 0, 0, 0);
bool result = thickness.Contains (outside, new (pointX, pointY));
Assert.Equal (expected, result);
}
[Fact]
public void ToStringTest ()
{
var t = new Thickness (1, 2, 3, 4);
Assert.Equal ("(Left=1,Top=2,Right=3,Bottom=4)", t.ToString ());
}
[Fact]
public void Vertical_get ()
{
var t = new Thickness (1, 2, 3, 4);
Assert.Equal (6, t.Vertical);
t = new (0);
Assert.Equal (0, t.Vertical);
}
[Fact]
public void Vertical_set ()
{
var t = new Thickness ();
t.Vertical = 10;
Assert.Equal (10, t.Vertical);
Assert.Equal (0, t.Left);
Assert.Equal (5, t.Top);
Assert.Equal (0, t.Right);
Assert.Equal (5, t.Bottom);
Assert.Equal (0, t.Horizontal);
t.Vertical = 11;
Assert.Equal (10, t.Vertical);
Assert.Equal (0, t.Left);
Assert.Equal (5, t.Top);
Assert.Equal (0, t.Right);
Assert.Equal (5, t.Bottom);
Assert.Equal (0, t.Horizontal);
t.Vertical = 1;
Assert.Equal (0, t.Vertical);
Assert.Equal (0, t.Left);
Assert.Equal (0, t.Top);
Assert.Equal (0, t.Right);
Assert.Equal (0, t.Bottom);
Assert.Equal (0, t.Horizontal);
}
// Test Thickness.Add
[Theory]
[InlineData (
1,
2,
3,
4,
1,
2,
3,
4,
2,
4,
6,
8)]
[InlineData (
1,
2,
3,
4,
0,
0,
0,
0,
1,
2,
3,
4)]
[InlineData (
1,
2,
3,
4,
-1,
-2,
-3,
-4,
0,
0,
0,
0)]
[InlineData (
1,
2,
3,
4,
1,
1,
1,
1,
2,
3,
4,
5)]
public void AddTest (
int left,
int top,
int right,
int bottom,
int left2,
int top2,
int right2,
int bottom2,
int expectedLeft,
int expectedTop,
int expectedRight,
int expectedBottom
)
{
var t = new Thickness (left, top, right, bottom);
var t2 = new Thickness (left2, top2, right2, bottom2);
Thickness result = t.Add (t2);
Assert.Equal (expectedLeft, result.Left);
Assert.Equal (expectedTop, result.Top);
Assert.Equal (expectedRight, result.Right);
Assert.Equal (expectedBottom, result.Bottom);
}
[Fact]
public void DrawTests ()
{
IConsoleDriver driver = new FakeDriver ();
driver.SetScreenSize (60, 40);
var t = new Thickness (0, 0, 0, 0);
var r = new Rectangle (5, 5, 40, 15);
driver.FillRect (
new (0, 0, driver!.Cols, driver!.Rows),
(Rune)' '
);
t.Draw (r, ViewDiagnosticFlags.Thickness, "Test", driver);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
Test (Left=0,Top=0,Right=0,Bottom=0)",
output,
driver
);
t = new (1, 1, 1, 1);
r = new (5, 5, 40, 15);
driver.FillRect (
new (0, 0, driver!.Cols, driver!.Rows),
(Rune)' '
);
t.Draw (r, ViewDiagnosticFlags.Thickness, "Test", driver);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
T T
T T
T T
T T
T T
T T
T T
T T
T T
T T
T T
T T
T T
TTTest (Left=1,Top=1,Right=1,Bottom=1)TT",
output,
driver
);
t = new (1, 2, 3, 4);
r = new (5, 5, 40, 15);
driver?.FillRect (
new (0, 0, driver!.Cols, driver!.Rows),
(Rune)' '
);
t.Draw (r, ViewDiagnosticFlags.Thickness, "Test", driver);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
T TTT
T TTT
T TTT
T TTT
T TTT
T TTT
T TTT
T TTT
T TTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
TTTest (Left=1,Top=2,Right=3,Bottom=4)TT",
output,
driver
);
t = new (-1, 1, 1, 1);
r = new (5, 5, 40, 15);
driver?.FillRect (
new (0, 0, driver!.Cols, driver!.Rows),
(Rune)' '
);
t.Draw (r, ViewDiagnosticFlags.Thickness, "Test", driver);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
T
T
T
T
T
T
T
T
T
T
T
T
T
TTest (Left=-1,Top=1,Right=1,Bottom=1)TT",
output,
driver
);
}
[Fact]
public void DrawTests_Ruler ()
{
IConsoleDriver driver = new FakeDriver ();
// Add a frame so we can see the ruler
var f = new FrameView { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill (), BorderStyle = LineStyle.Single };
f.Driver = driver;
driver.SetScreenSize (45, 20);
var top = new Toplevel () { Width = driver.Cols, Height = driver.Rows };
top.Driver = driver;
top.Add (f);
top.Layout ();
var t = new Thickness (0, 0, 0, 0);
var r = new Rectangle (2, 2, 40, 15);
top.Draw ();
t.Draw (r, ViewDiagnosticFlags.Ruler, "Test", driver);
DriverAssert.AssertDriverContentsAre (
@"
┌───────────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└───────────────────────────────────────────┘",
output,
driver
);
t = new (1, 1, 1, 1);
r = new (1, 1, 40, 15);
top.SetNeedsDraw ();
top.Draw ();
t.Draw (r, ViewDiagnosticFlags.Ruler, "Test", driver);
DriverAssert.AssertDriverContentsAre (
@"
┌───────────────────────────────────────────┐
│|123456789|123456789|123456789|123456789 │
│1 1 │
│2 2 │
│3 3 │
│4 4 │
│5 5 │
│6 6 │
│7 7 │
│8 8 │
│9 9 │
│- - │
│1 1 │
│2 2 │
│3 3 │
│|123456789|123456789|123456789|123456789 │
│ │
│ │
│ │
└───────────────────────────────────────────┘",
output,
driver
);
t = new (1, 2, 3, 4);
r = new (2, 2, 40, 15);
top.SetNeedsDraw ();
top.Draw ();
t.Draw (r, ViewDiagnosticFlags.Ruler, "Test", driver);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
┌───────────────────────────────────────────┐
│ │
│ |123456789|123456789|123456789|123456789 │
│ 1 1 │
│ 2 2 │
│ 3 3 │
│ 4 4 │
│ 5 5 │
│ 6 6 │
│ 7 7 │
│ 8 8 │
│ 9 9 │
│ - - │
│ 1 1 │
│ 2 2 │
│ 3 3 │
│ |123456789|123456789|123456789|123456789 │
│ │
│ │
└───────────────────────────────────────────┘",
output,
driver
);
t = new (-1, 1, 1, 1);
r = new (5, 5, 40, 15);
top.SetNeedsDraw ();
top.Draw ();
t.Draw (r, ViewDiagnosticFlags.Ruler, "Test", driver);
DriverAssert.AssertDriverContentsWithFrameAre (
@"
┌───────────────────────────────────────────┐
│ │
│ │
│ │
│ │
│ |123456789|123456789|123456789|123456789
│ 1
│ 2
│ 3
│ 4
│ 5
│ 6
│ 7
│ 8
│ 9
│ -
│ 1
│ 2
│ 3
└────|123456789|123456789|123456789|123456789",
output,
driver
);
top.Dispose ();
}
}