Files
Terminal.Gui/Tests/UnitTestsParallelizable/Drawing/Region/MergeRectanglesTests.cs
Copilot 6d53276be2 Fixes #4289 - Simplify Drawing/Color: unify named color handling under StandardColor and remove layered resolvers (#4432)
* Initial plan

* Delete AnsiColorNameResolver and MultiStandardColorNameResolver, add legacy 16-color names to StandardColor

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

* Refactor and enhance tests for Color, Region, and Lines

Refactored `Color` struct by removing unused methods and simplifying logic. Updated namespaces for better organization. Enhanced test coverage for `Color`, `Region`, and `LineCanvas` with new test cases, parameterized tests, and edge case handling.

Added `StraightLineExtensionsTests`, `StraightLineTests`, and `RegionClassTests` to validate behavior under various scenarios. Improved `MergeRectangles` stability and addressed crash patterns. Removed legacy features and unused code. Enhanced documentation and optimized performance in key methods.

* Improve Color struct and StandardColors functionality

Enhanced the Color struct to fully support the alpha channel for rendering intent while maintaining semantic color identity. Updated TryNameColor to ignore alpha when matching colors, ensuring transparency does not affect color resolution. Expanded XML documentation to clarify alpha channel usage and future alpha blending support.

Improved drawing documentation to explain the lifecycle, deferred rendering, and color support, including 24-bit true color and legacy 16-color compatibility. Added a new section on transparency and its role in rendering.

Revised StandardColors implementation to use modern C# features and ensure consistent ARGB mapping. Added comprehensive tests for StandardColors and Color, covering alpha handling, color parsing, thread safety, and aliased color resolution. Removed outdated tests relying on legacy behavior.

Enhanced code readability, maintainability, and test coverage to ensure correctness and backward compatibility.

* Code cleanup

* Code cleanup

* Fix warnings. Code cleanup

* Add comprehensive unit tests for ColorStrings class

Introduced a new test class `ColorStringsTests` under the
`DrawingTests.ColorTests` namespace to validate the functionality
of the `ColorStrings` class.

Key changes include:
- Added tests for `GetColorName` to verify behavior for standard
  and non-standard colors, ignoring alpha channels, and handling
  known colors.
- Added tests for `GetStandardColorNames` to ensure the method
  returns a non-empty, alphabetically sorted collection containing
  all `StandardColor` enum values.
- Implemented tests for `TryParseStandardColorName` to validate
  case-insensitive parsing, hex color support, handling invalid
  input, and `ReadOnlySpan<char>` compatibility.
- Added tests for `TryParseNamedColor` to verify parsing of named
  and hex colors, handling of aliases, and `ReadOnlySpan<char>`
  support.
- Added round-trip tests to ensure consistency between
  `GetColorName`, `TryParseNamedColor`, `GetStandardColorNames`,
  and `TryParseStandardColorName`.

These tests ensure robust validation of color parsing and naming
functionality.

---------

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>
2025-12-03 11:09:02 -07:00

189 lines
5.3 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace DrawingTests.RegionTests;
public class MergeRectanglesTests
{
[Fact]
public void MergeRectangles_ComplexAdjacentRectangles_NoOverlap ()
{
/*
INPUT: Complex arrangement of four adjacent rectangles forming a hollow square ring.
Top-left origin (0,0), x→, y↓:
x=0 1 2 3 4
y=0 A A A
y=1 B C
y=2 B C
y=3 B C
y=4 D D D
Rectangles (width × height):
A: (1,0,3,1) // top edge
B: (0,1,1,3) // left edge
C: (4,1,1,3) // right edge
D: (1,4,3,1) // bottom edge
They only touch corners or edges, with no overlapping areas.
The expected result is exactly these four rectangles, unmerged.
*/
List<Rectangle> rectangles = new ()
{
new (1, 0, 3, 1), // A
new (0, 1, 1, 3), // B
new (4, 1, 1, 3), // C
new (1, 4, 3, 1) // D
};
List<Rectangle> merged = Region.MergeRectangles (rectangles, false);
// Because there's no overlapping area, the method shouldn't merge any of them.
Assert.Equal (4, merged.Count);
Assert.Contains (new (1, 0, 3, 1), merged);
Assert.Contains (new (0, 1, 1, 3), merged);
Assert.Contains (new (4, 1, 1, 3), merged);
Assert.Contains (new (1, 4, 3, 1), merged);
}
[Fact]
public void MergeRectangles_ComplexContainedRectangles_AllMergeIntoBoundingRect ()
{
/*
INPUT: (top-left origin, x→, y↓):
x=0 1 2 3 4 5
y=0 A A A A A A
y=1 A . . . . A
y=2 A . B B . A
y=3 A . B B . A
y=4 A . . . C C
y=5 A A A A C C
Where:
A = (0,0,6,6) // Large bounding rectangle
B = (2,2,2,2) // Fully contained inside A
C = (4,4,2,2) // Also fully contained inside A
*/
List<Rectangle> rectangles = new ()
{
new (0, 0, 6, 6), // A
new (2, 2, 2, 2), // B inside A
new (4, 4, 2, 2) // C inside A
};
List<Rectangle> merged = Region.MergeRectangles (rectangles, minimize: false);
/*
OUTPUT: The expected result should be a minimal set of non-overlapping rectangles
that cover the same area as the input rectangles.
x=0 1 2 3 4 5
y=0 a a b b c c
y=1 a a b b c c
y=2 a a b b c c
y=3 a a b b c c
y=4 a a b b c c
y=5 a a b b c c
*/
Assert.Equal (3, merged.Count);
Assert.Contains (new (0, 0, 2, 6), merged); // a
Assert.Contains (new (2, 0, 2, 6), merged); // b
Assert.Contains (new (4, 0, 2, 6), merged); // c
}
[Fact]
public void MergeRectangles_ComplexOverlap_ReturnsMergedRectangles ()
{
/*
INPUT: Visual diagram treating (0,0) as top-left, x increasing to the right, y increasing downward:
x=0 1 2 3 4 5 6 ...
y=0 A A
y=1 A B B
y=2 B B
y=3 C C
y=4 C D D
y=5 D D
A overlaps B slightly; C overlaps D slightly. The union of A & B forms one rectangle,
and the union of C & D forms another.
*/
List<Rectangle> rectangles = new ()
{
// A
new (0, 0, 2, 2),
// B
new (1, 1, 2, 2),
// C
new (3, 3, 2, 2),
// D
new (4, 4, 2, 2)
};
List<Rectangle> merged = Region.MergeRectangles (rectangles, false);
/*
OUTPUT: Merged fragments (top-left origin, x→, y↓).
Lowercase letters a..f show the six sub-rectangles:
x=0 1 2 3 4 5
y=0 a b
y=1 a b c
y=2 b c
y=3 d e
y=4 d e f
y=5 e f
*/
Assert.Equal (6, merged.Count);
Assert.Contains (new (0, 0, 1, 2), merged); // a
Assert.Contains (new (1, 0, 1, 3), merged); // b
Assert.Contains (new (2, 1, 1, 2), merged); // c
Assert.Contains (new (3, 3, 1, 2), merged); // d
Assert.Contains (new (4, 3, 1, 3), merged); // e
Assert.Contains (new (5, 4, 1, 2), merged); // f
}
[Fact]
public void MergeRectangles_NoOverlap_ReturnsSameRectangles ()
{
List<Rectangle> rectangles = new ()
{
new (0, 0, 10, 10),
new (20, 20, 10, 10),
new (40, 40, 10, 10)
};
List<Rectangle> result = Region.MergeRectangles (rectangles, false);
Assert.Equal (3, result.Count);
Assert.Contains (new (0, 0, 10, 10), result);
Assert.Contains (new (20, 20, 10, 10), result);
Assert.Contains (new (40, 40, 10, 10), result);
}
[Fact]
public void MergeRectangles_EmptyRectangles_ReturnsEmptyList ()
{
// Arrange: Create list of empty rectangles
var emptyRectangles = new List<Rectangle> { new (0, 0, 0, 0), new (0, 0, 0, 0) };
// Act: Call MergeRectangles with granular output
var result = Region.MergeRectangles (emptyRectangles, minimize: false);
// Assert: Result is empty
Assert.Empty (result);
}
}