Fixes #4057 - MASSIVE! Fully implements ColorScheme->Scheme + VisualRole + Colors.->SchemeManager. (#4062)

* touching publish.yml

* ColorScheme->Scheme

* ColorScheme->Scheme 2

* Prototype of GetAttributeForRole

* Badly broke CM

* Further Badly broke CM

* Refactored CM big-time. View still broken

* All unit test pass again. Tons added. CM is still WIP, but Schemes is not mostly refactored and working.

* Actually:
All unit test pass again.
Tons added.
CM is still WIP, but Schemes is not mostly refactored and working.

* Bug fixes.
DeepMemberWiseClone cleanup

* Further cleanup of Scope<T>, ConfigProperty, etc.

* Made ConfigManager thread safe.

* WIP: Broken

* WIP: new deep clone impl

* WIP: new deep clone impl is done. Now fixing CM

* WIP:
- config.md
- Working on AOT clean up
- Core CM is broken; but known.

* WIP

* Merged.
Removed CM from Application.Init

* WIP

* More WIP; Less broke

* All CM unit tests pass... Not sure if it actually works though

* All unit tests pass... Themes are broken though in UI Cat

* CM Ready for review?

* Fixed failures due to TextStyles PR

* Working on Scheme/Attribute

* Working on Scheme/Attribute 2

* Working on Scheme/Attribute 3

* Working on Scheme/Attribute 4

* Working on Scheme/Attribute 5

* Working on Scheme/Attribute 6

* Added test to show how awful memory usage is

* Improved schema. Updated config.json

* Nade Scope<T> concurrentdictionary and added test to prove

* Made Themes ConcrurrentDictionary. Added bunches of tests

* Code cleanup

* Code cleanup 2

* Code cleanup 3

* Tweaking Scheme

* ClearJsonErrors

* ClearJsonErrors2

* Updated Attribute API

* It all (mostly) works!

* Skip odd unit test

* Messed with Themes

* Theme tweaks

* Code reorg. New .md stuff

* Fixed Enabled. Added mock driver

* Fixed a bunch of View.Enabled related issues

* Scheme -> Get/SetScheme()

* Cleanup

* Cleanup2

* Broke something

* Fixed everything

* Made CM.Enable better

* Text Style Scenario

* Added comments

* Fixed UI Catalog Theme Changing

* Fixed more dynamic CM update stuff

* Warning cleanup

* New Default Theme

* fixed unit test

* Refactoring Scheme and Attribute to fix inheritance

* more unit tests

* ConfigProperty is not updating schemes correctly

* All unit tests pass.
Code cleanup

* All unit tests pass.
Code cleanup2

* Fixed unit tests

* Upgraded TextField and TextView

* Fixed TextView !Enabled bug

* More updates to TextView. More unit tests for SchemeManager

* Upgraded CharMap

* API docs

* Fixe HexView API

* upgrade HexView

* Fixed shortcut KeyView

* Fixed more bugs. Added new themes

* updated themes

* upgraded Border

* Fixed themes memory usage...mostly

* Fixed themes memory usage...mostly2

* Fixed themes memory usage...2

* Fixed themes memory usage...3

* Added new colors

* Fixed GetHardCodedConfig bug

* Added Themes Scenario - WIP

* Added Themes Scenario

* Tweaked Themes Scenario

* Code cleanup

* Fixed json schmea

* updated deepdives

* updated deepdives

* Tweaked Themes Scenario

* Made Schemes a concurrent dict

* Test cleanup

* Thread safe ConfigProperty tests

* trying to make things more thread safe

* more trying to make things more thread safe

* Fixing bugs in shadowview

* Fixing bugs in shadowview 2

* Refactored GetViewsUnderMouse to GetViewsUnderLocation etc...

* Fixed dupe unit tests?

* Added better description of layout and coordiantes to deep dive

* Added better description of layout and coordiantes to deep dive

* Modified tests that call v2.AddTimeout; they were returning true which means restart the timer!
This was causing mac/linux unit test failures.
I think

* Fixed auto scheme.
Broke TextView/TextField selection

* Realized Attribute.IsExplicitlySet is stupid; just use nullable

* Fixed Attribute. Simplified. MOre theme testing

* Updated themes again

* GetViewsUnderMouse to GetViewsUnderLocation broke TransparentMouse.

* Fixing mouseunder bugs

* rewriting...

* All working again.
Shadows are now slick as snot.
GetViewsUnderLocation is rewritten to actually work and be readable.
Tons more low-level unit tests.
Margin is now actually ViewportSettings.Transparent.

* Code cleanup

* Code cleanup

* Code cleanup of color apis

* Fixed Hover/Highlight

* Update Examples/UICatalog/Scenarios/AllViewsTester.cs

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

* Update Examples/UICatalog/Scenarios/CharacterMap/CharacterMap.cs

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

* Update Examples/UICatalog/Scenarios/Clipping.cs

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

* Fixed race condition?

* reverted

* Simplified Attribute API by removing events from SetAttributeForRole

* Removed recursion from GetViewsAtLocation

* Removed unneeded code

* Code clean up.
Fixed Scheme bug.

* reverted temporary disable

* Adjusted scheme algo

* Upgraded TextValidateField

* Fixed TextValidate bugs

* Tweaks

* Frameview rounded border by default

* API doc cleanup

* Readme fix

* Addressed tznind feeback

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true 2

* cleanup

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Tig
2025-05-29 13:55:54 -06:00
parent f6f052ac33
commit 3e2eebfd2c
385 changed files with 23471 additions and 13662 deletions

View File

@@ -1,14 +1,53 @@
// Alias Console to MockConsole so we don't accidentally use Console
// Alias Console to MockConsole so we don't accidentally use Console
namespace Terminal.Gui.DrawingTests;
public class AttributeTests
{
[Fact]
public void Attribute_Is_Value_Type ()
public void Constructor_ParsesNamedColorsAndStyle ()
{
// prove that Color is a value type
Assert.True (typeof (Attribute).IsValueType);
var attr = new Attribute ("Red", "Black", "Bold,Underline");
Assert.Equal (Color.Parse ("Red"), attr.Foreground);
Assert.Equal (Color.Parse ("Black"), attr.Background);
Assert.True (attr.Style.HasFlag (TextStyle.Bold));
Assert.True (attr.Style.HasFlag (TextStyle.Underline));
}
[Fact]
public void Constructor_ParsesHexColors ()
{
var attr = new Attribute ("#FF0000", "#000000", "Italic");
Assert.Equal (Color.Parse ("#FF0000"), attr.Foreground);
Assert.Equal (Color.Parse ("#000000"), attr.Background);
Assert.Equal (TextStyle.Italic, attr.Style);
}
[Fact]
public void Constructor_ParsesRgbColors ()
{
var attr = new Attribute ("rgb(0,255,0)", "rgb(0,0,255)", "Faint");
Assert.Equal (Color.Parse ("rgb(0,255,0)"), attr.Foreground);
Assert.Equal (Color.Parse ("rgb(0,0,255)"), attr.Background);
Assert.Equal (TextStyle.Faint, attr.Style);
}
[Fact]
public void Constructor_DefaultsToNoneStyle_WhenStyleIsNullOrEmpty ()
{
var attr1 = new Attribute ("White", "Black");
var attr2 = new Attribute ("White", "Black", null);
var attr3 = new Attribute ("White", "Black", "");
Assert.Equal (TextStyle.None, attr1.Style);
Assert.Equal (TextStyle.None, attr2.Style);
Assert.Equal (TextStyle.None, attr3.Style);
}
[Fact]
public void Constructor_DefaultsToNoneStyle_WhenStyleIsInvalid ()
{
var attr = new Attribute ("White", "Black", "NotAStyle");
Assert.Equal (TextStyle.None, attr.Style);
}
[Fact]
@@ -21,7 +60,7 @@ public class AttributeTests
// Assert
Assert.Equal (foregroundColor, attribute.Foreground);
Assert.Equal (new Color (backgroundColorName), attribute.Background);
Assert.Equal (new (backgroundColorName), attribute.Background);
}
[Fact]
@@ -46,7 +85,7 @@ public class AttributeTests
var attribute = new Attribute (foregroundColorName, backgroundColor);
// Assert
Assert.Equal (new Color (foregroundColorName), attribute.Foreground);
Assert.Equal (new (foregroundColorName), attribute.Foreground);
Assert.Equal (backgroundColor, attribute.Background);
}
@@ -57,8 +96,8 @@ public class AttributeTests
var attribute = new Attribute (ColorName16.Blue);
// Assert
Assert.Equal (new Color (Color.Blue), attribute.Foreground);
Assert.Equal (new Color (Color.Blue), attribute.Background);
Assert.Equal (new (Color.Blue), attribute.Foreground);
Assert.Equal (new (Color.Blue), attribute.Background);
}
[Fact]
@@ -71,31 +110,31 @@ public class AttributeTests
var attr = new Attribute ();
Assert.Equal (-1, attr.PlatformColor);
Assert.Equal (new Color (Color.White), attr.Foreground);
Assert.Equal (new Color (Color.Black), attr.Background);
Assert.Equal (new (Color.White), attr.Foreground);
Assert.Equal (new (Color.Black), attr.Background);
// Test foreground, background
var fg = new Color ();
fg = new Color (Color.Red);
fg = new (Color.Red);
var bg = new Color ();
bg = new Color (Color.Blue);
bg = new (Color.Blue);
attr = new Attribute (fg, bg);
attr = new (fg, bg);
//Assert.True (attr.Initialized);
//Assert.True (attr.HasValidColors);
Assert.Equal (fg, attr.Foreground);
Assert.Equal (bg, attr.Background);
attr = new Attribute (fg);
attr = new (fg);
//Assert.True (attr.Initialized);
//Assert.True (attr.HasValidColors);
Assert.Equal (fg, attr.Foreground);
Assert.Equal (fg, attr.Background);
attr = new Attribute (bg);
attr = new (bg);
//Assert.True (attr.Initialized);
//Assert.True (attr.HasValidColors);
@@ -114,8 +153,19 @@ public class AttributeTests
// Assert
//Assert.False (attribute.Initialized);
Assert.Equal (-1, attribute.PlatformColor);
Assert.Equal (new Color (Color.White), attribute.Foreground);
Assert.Equal (new Color (Color.Black), attribute.Background);
Assert.Equal (new (Color.White), attribute.Foreground);
Assert.Equal (new (Color.Black), attribute.Background);
}
[Fact]
public void Equality_IncludesStyle ()
{
var attr1 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
var attr2 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
var attr3 = new Attribute (Color.Red, Color.Black, TextStyle.Underline);
Assert.Equal (attr1, attr2);
Assert.NotEqual (attr1, attr3);
}
[Fact]
@@ -160,6 +210,16 @@ public class AttributeTests
Assert.True (attr2.Equals (attr1));
}
[Fact]
public void GetHashCode_ConsistentWithEquals ()
{
var attr1 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
var attr2 = new Attribute (Color.Red, Color.Black, TextStyle.Bold);
Assert.Equal (attr1, attr2);
Assert.Equal (attr1.GetHashCode (), attr2.GetHashCode ());
}
[Fact]
public void Implicit_Assign ()
{
@@ -170,13 +230,13 @@ public class AttributeTests
var value = 42;
var fg = new Color ();
fg = new Color (Color.Red);
fg = new (Color.Red);
var bg = new Color ();
bg = new Color (Color.Blue);
bg = new (Color.Blue);
// Test conversion to int
attr = new Attribute (value, fg, bg);
attr = new (value, fg, bg);
int value_implicit = attr.PlatformColor;
Assert.Equal (value, value_implicit);
@@ -207,6 +267,22 @@ public class AttributeTests
Assert.True (attribute1 != attribute2);
}
[Fact]
public void Is_Value_Type ()
{
// prove that Color is a value type
Assert.True (typeof (Attribute).IsValueType);
}
[Fact]
public void List_RoundTrip_EqualityHolds ()
{
List<Attribute> list1 = [new (Color.Red, Color.Black, TextStyle.Bold)];
List<Attribute> list2 = new (list1);
Assert.Equal (list1, list2);
}
[Fact]
public void Make_Creates ()
{
@@ -214,10 +290,10 @@ public class AttributeTests
driver.Init ();
var fg = new Color ();
fg = new Color (Color.Red);
fg = new (Color.Red);
var bg = new Color ();
bg = new Color (Color.Blue);
bg = new (Color.Blue);
var attr = new Attribute (fg, bg);
@@ -232,10 +308,10 @@ public class AttributeTests
public void Make_Creates_NoDriver ()
{
var fg = new Color ();
fg = new Color (Color.Red);
fg = new (Color.Red);
var bg = new Color ();
bg = new Color (Color.Blue);
bg = new (Color.Blue);
var attr = new Attribute (fg, bg);
@@ -248,10 +324,10 @@ public class AttributeTests
public void Make_SetsNotInitialized_NoDriver ()
{
var fg = new Color ();
fg = new Color (Color.Red);
fg = new (Color.Red);
var bg = new Color ();
bg = new Color (Color.Blue);
bg = new (Color.Blue);
var a = new Attribute (fg, bg);
@@ -285,7 +361,7 @@ public class AttributeTests
// Assert
Assert.Equal (foregroundColor, attribute.Foreground);
Assert.Equal (new Color (backgroundColorName), attribute.Background);
Assert.Equal (new (backgroundColorName), attribute.Background);
}
[Fact]
@@ -299,7 +375,7 @@ public class AttributeTests
var attribute = new Attribute (foregroundColorName, backgroundColor);
// Assert
Assert.Equal (new Color (foregroundColorName), attribute.Foreground);
Assert.Equal (new (foregroundColorName), attribute.Foreground);
Assert.Equal (backgroundColor, attribute.Background);
}
@@ -314,8 +390,8 @@ public class AttributeTests
var attribute = new Attribute (foregroundColorName, backgroundColorName);
// Assert
Assert.Equal (new Color (foregroundColorName), attribute.Foreground);
Assert.Equal (new Color (backgroundColorName), attribute.Background);
Assert.Equal (new (foregroundColorName), attribute.Foreground);
Assert.Equal (new (backgroundColorName), attribute.Background);
}
[Fact]
@@ -339,12 +415,12 @@ public class AttributeTests
}
[Fact]
public void ToString_ShouldReturnFormattedStringWithForegroundAndBackground ()
public void ToString_Formats_Correctly ()
{
// Arrange
var foregroundColor = new Color (0, 0, 255);
var backgroundColor = new Color (255, 255, 255);
var expectedString = $"[{foregroundColor},{backgroundColor}]";
var expectedString = $"[{foregroundColor},{backgroundColor},None]";
// Act
var attribute = new Attribute (foregroundColor, backgroundColor);
@@ -353,4 +429,29 @@ public class AttributeTests
// Assert
Assert.Equal (expectedString, attributeString);
}
[Theory]
[InlineData (TextStyle.Bold, "Bold")]
[InlineData (TextStyle.Bold | TextStyle.Underline, "Bold, Underline")]
[InlineData (TextStyle.None, "None")]
public void ToString_IncludesStyle (TextStyle style, string expectedStyleString)
{
var attr = new Attribute (Color.Red, Color.Black, style);
var result = attr.ToString ();
Assert.Contains (expectedStyleString, result);
}
[Fact]
public void ToString_ShouldFailComparison_IfDifferentInstancesSameContent ()
{
var original = new Attribute (Color.White, Color.White);
var clone = new Attribute (Color.White, Color.White);
// These print the same
Assert.Equal (original.ToString (), clone.ToString ());
// But this will fail if anything differs under the hood
Assert.Equal (original, clone); // Should pass — record struct
}
}

View File

@@ -42,10 +42,10 @@ public class CellTests ()
{
Rune = new ('a'), Attribute = new (Color.Red)
};
Assert.Equal ("[\0, ]", c1.ToString ());
Assert.Equal ("['\0':]", c1.ToString ());
Assert.Equal (
"[a, [Red,Red]]",
"['a':[Red,Red,None]]",
c2.ToString ()
);
}

View File

@@ -4,14 +4,58 @@ namespace Terminal.Gui.DrawingTests;
public class AnsiColorNameResolverTests
{
private readonly AnsiColorNameResolver _candidate = new();
private readonly AnsiColorNameResolver _candidate = new ();
[Fact]
public void TryNameColor_Resolves_All_ColorName16 ()
{
var resolver = new AnsiColorNameResolver ();
foreach (ColorName16 name in Enum.GetValues<ColorName16> ())
{
var color = new Color (name);
bool success = resolver.TryNameColor (color, out string? resultName);
Assert.True (success, $"Expected TryNameColor to succeed for {name}");
Assert.Equal (name.ToString (), resultName);
}
}
[Fact]
public void TryParseColor_Resolves_All_ColorName16_Names ()
{
var resolver = new AnsiColorNameResolver ();
foreach (ColorName16 name in Enum.GetValues<ColorName16> ())
{
bool success = resolver.TryParseColor (name.ToString (), out Color parsed);
Assert.True (success, $"Expected TryParseColor to succeed for {name}");
Assert.Equal (new Color (name), parsed);
}
}
public static IEnumerable<object []> AnsiColorName16NumericValues =>
Enum.GetValues<ColorName16> ()
.Select (e => new object [] { ((int)e).ToString () });
[Theory]
[MemberData (nameof (AnsiColorName16NumericValues))]
public void TryParseColor_Accepts_Enum_UnderlyingNumbers (string numeric)
{
var resolver = new AnsiColorNameResolver ();
bool success = resolver.TryParseColor (numeric, out _);
Assert.True (success, $"Expected numeric enum value '{numeric}' to resolve successfully.");
}
[Fact]
public void GetNames_Returns16ColorNames ()
{
string[] expected = Enum.GetNames<ColorName16>();
string [] expected = Enum.GetNames<ColorName16> ();
string[] actual = _candidate.GetColorNames ().ToArray();
string [] actual = _candidate.GetColorNames ().ToArray ();
Assert.Equal (expected, actual);
}
@@ -37,7 +81,7 @@ public class AnsiColorNameResolverTests
{
var expected = (true, expectedName);
bool actualSuccess = _candidate.TryNameColor(new Color(r, g, b), out string? actualName);
bool actualSuccess = _candidate.TryNameColor (new Color (r, g, b), out string? actualName);
var actual = (actualSuccess, actualName);
Assert.Equal (expected, actual);
@@ -76,7 +120,7 @@ public class AnsiColorNameResolverTests
[InlineData ("brightblue", 59, 120, 255)]
public void TryParseColor_ReturnsExpectedColor (string inputName, byte r, byte g, byte b)
{
var expected = (true, new Color(r, g, b));
var expected = (true, new Color (r, g, b));
bool actualSuccess = _candidate.TryParseColor (inputName, out Color actualColor);
var actual = (actualSuccess, actualColor);
@@ -88,7 +132,7 @@ public class AnsiColorNameResolverTests
[InlineData ("12", 231, 72, 86)] // ColorName16.BrightRed
public void TryParseColor_ResolvesValidEnumNumber (string inputName, byte r, byte g, byte b)
{
var expected = (true, new Color(r, g, b));
var expected = (true, new Color (r, g, b));
bool actualSuccess = _candidate.TryParseColor (inputName, out Color actualColor);
var actual = (actualSuccess, actualColor);
@@ -102,7 +146,7 @@ public class AnsiColorNameResolverTests
[InlineData ("brightlight")]
public void TryParseColor_FailsOnInvalidColorName (string? invalidName)
{
var expected = (false, default(Color));
var expected = (false, default (Color));
bool actualSuccess = _candidate.TryParseColor (invalidName, out Color actualColor);
var actual = (actualSuccess, actualColor);
@@ -114,7 +158,7 @@ public class AnsiColorNameResolverTests
[InlineData ("-12")]
public void TryParseColor_FailsOnInvalidEnumNumber (string invalidName)
{
var expected = (false, default(Color));
var expected = (false, default (Color));
bool actualSuccess = _candidate.TryParseColor (invalidName, out Color actualColor);
var actual = (actualSuccess, actualColor);

View File

@@ -1,13 +1,80 @@
#nullable enable
using System.Collections.Generic;
using Xunit.Abstractions;
using static Terminal.Gui.StandardColors;
namespace Terminal.Gui.DrawingTests;
public class MultiStandardColorNameResolverTests
public class MultiStandardColorNameResolverTests (ITestOutputHelper output)
{
private readonly MultiStandardColorNameResolver _candidate = new();
private readonly MultiStandardColorNameResolver _candidate = new ();
public static IEnumerable<object []> StandardColors =>
Enum.GetValues<StandardColor> ().Select (sc => new object [] { sc });
[Theory]
[MemberData (nameof (StandardColors))]
public void TryParseColor_ResolvesAllStandardColorNames (StandardColor standardColor)
{
string name = standardColor.ToString ();
bool parsed = _candidate.TryParseColor (name, out Color actualColor);
Assert.True (parsed, $"TryParseColor should succeed for {name}");
Color expectedColor = new (GetArgb (standardColor));
Assert.Equal (expectedColor.R, actualColor.R);
Assert.Equal (expectedColor.G, actualColor.G);
Assert.Equal (expectedColor.B, actualColor.B);
}
[Theory]
[MemberData (nameof (StandardColors))]
public void TryNameColor_ResolvesAllStandardColors (StandardColor standardColor)
{
Color color = new (GetArgb (standardColor));
bool success = _candidate.TryNameColor (color, out string? resolvedName);
if (!success)
{
output.WriteLine ($"Unmapped: {standardColor} → {color}");
}
Assert.True (success, $"TryNameColor should succeed for {standardColor}");
List<string> expectedNames = Enum.GetNames<StandardColor> ()
.Where (name => GetArgb (Enum.Parse<StandardColor> (name)) == color.Argb)
.ToList ();
Assert.Contains (resolvedName, expectedNames);
}
[Fact]
public void TryNameColor_Logs_Unmapped_StandardColors ()
{
List<StandardColor> unmapped = new ();
foreach (StandardColor sc in Enum.GetValues<StandardColor> ())
{
Color color = new (GetArgb (sc));
if (!_candidate.TryNameColor (color, out _))
{
unmapped.Add (sc);
}
}
output.WriteLine ("Unmapped StandardColor entries:");
foreach (StandardColor sc in unmapped.Distinct ())
{
output.WriteLine ($"- {sc}");
}
Assert.True (unmapped.Count < 10, $"Too many StandardColor values are not name-resolvable. Got {unmapped.Count}.");
}
[Theory]
// ANSI color names.
[InlineData (nameof (ColorName16.Black))]
[InlineData (nameof (ColorName16.White))]
[InlineData (nameof (ColorName16.Red))]
@@ -18,215 +85,72 @@ public class MultiStandardColorNameResolverTests
[InlineData (nameof (ColorName16.DarkGray))]
[InlineData (nameof (ColorName16.BrightGreen))]
[InlineData (nameof (ColorName16.BrightMagenta))]
// Regular W3C color.
[InlineData (nameof (W3cColor.AliceBlue))]
[InlineData (nameof (W3cColor.BlanchedAlmond))]
[InlineData (nameof (W3cColor.CadetBlue))]
[InlineData (nameof (W3cColor.DarkBlue))]
[InlineData (nameof (W3cColor.FireBrick))]
[InlineData (nameof (W3cColor.Gainsboro))]
[InlineData (nameof (W3cColor.HoneyDew))]
[InlineData (nameof (W3cColor.Indigo))]
[InlineData (nameof (W3cColor.Khaki))]
[InlineData (nameof (W3cColor.Lavender))]
[InlineData (nameof (W3cColor.Maroon))]
[InlineData (nameof (W3cColor.Navy))]
[InlineData (nameof (W3cColor.Olive))]
[InlineData (nameof (W3cColor.Plum))]
[InlineData (nameof (W3cColor.RoyalBlue))]
[InlineData (nameof (W3cColor.Silver))]
[InlineData (nameof (W3cColor.Tomato))]
[InlineData (nameof (W3cColor.Violet))]
[InlineData (nameof (W3cColor.WhiteSmoke))]
[InlineData (nameof (W3cColor.YellowGreen))]
// W3C alternatives.
[InlineData (nameof (W3cColor.Grey))]
[InlineData (nameof (W3cColor.DarkGrey))]
[InlineData (nameof (W3cColor.Aqua))]
[InlineData (nameof (W3cColor.Fuchsia))]
[InlineData (nameof (W3cColor.DarkSlateGray))]
[InlineData (nameof (W3cColor.DarkSlateGrey))]
[InlineData (nameof (W3cColor.DimGray))]
[InlineData (nameof (W3cColor.DimGrey))]
[InlineData (nameof (W3cColor.LightGray))]
[InlineData (nameof (W3cColor.LightGrey))]
[InlineData (nameof (W3cColor.SlateGray))]
[InlineData (nameof (W3cColor.SlateGrey))]
public void GetNames_ContainsCombinationOfAnsiAndW3cNames (string name)
[InlineData (nameof (StandardColor.AliceBlue))]
[InlineData (nameof (StandardColor.BlanchedAlmond))]
public void GetNames_ContainsKnownNames (string name)
{
string[] names = _candidate.GetColorNames ().ToArray();
string [] names = _candidate.GetColorNames ().ToArray ();
Assert.Contains (name, names);
}
[Theory]
// ANSI color names
[InlineData (0, 0, 0, nameof (ColorName16.Black))]
[InlineData (0, 0, 255, nameof (ColorName16.Blue))]
[InlineData (59, 120, 255, nameof (ColorName16.BrightBlue))]
[InlineData (97, 214, 214, nameof (ColorName16.BrightCyan))]
[InlineData (22, 198, 12, nameof (ColorName16.BrightGreen))]
[InlineData (180, 0, 158, nameof (ColorName16.BrightMagenta))]
[InlineData (231, 72, 86, nameof (ColorName16.BrightRed))]
[InlineData (249, 241, 165, nameof (ColorName16.BrightYellow))]
[InlineData (0, 255, 255, nameof (ColorName16.Cyan))]
[InlineData (118, 118, 118, nameof (ColorName16.DarkGray))]
[InlineData (128, 128, 128, nameof (ColorName16.Gray))]
[InlineData (0, 128, 0, nameof (ColorName16.Green))]
[InlineData (255, 0, 255, nameof (ColorName16.Magenta))]
[InlineData (255, 0, 0, nameof (ColorName16.Red))]
[InlineData (255, 255, 255, nameof (ColorName16.White))]
[InlineData (255, 255, 0, nameof (ColorName16.Yellow))]
// W3C color names
[InlineData (240, 248, 255, nameof (W3cColor.AliceBlue))]
[InlineData (255, 235, 205, nameof (W3cColor.BlanchedAlmond))]
[InlineData (95, 158, 160, nameof (W3cColor.CadetBlue))]
[InlineData (0, 0, 139, nameof (W3cColor.DarkBlue))]
[InlineData (178, 34, 34, nameof (W3cColor.FireBrick))]
[InlineData (220, 220, 220, nameof (W3cColor.Gainsboro))]
[InlineData (240, 255, 240, nameof (W3cColor.HoneyDew))]
[InlineData (75, 0, 130, nameof (W3cColor.Indigo))]
[InlineData (240, 230, 140, nameof (W3cColor.Khaki))]
[InlineData (230, 230, 250, nameof (W3cColor.Lavender))]
[InlineData (128, 0, 0, nameof (W3cColor.Maroon))]
[InlineData (0, 0, 128, nameof (W3cColor.Navy))]
[InlineData (128, 128, 0, nameof (W3cColor.Olive))]
[InlineData (221, 160, 221, nameof (W3cColor.Plum))]
[InlineData (65, 105, 225, nameof (W3cColor.RoyalBlue))]
[InlineData (192, 192, 192, nameof (W3cColor.Silver))]
[InlineData (255, 99, 71, nameof (W3cColor.Tomato))]
[InlineData (238, 130, 238, nameof (W3cColor.Violet))]
[InlineData (245, 245, 245, nameof (W3cColor.WhiteSmoke))]
[InlineData (154, 205, 50, nameof (W3cColor.YellowGreen))]
[InlineData (240, 248, 255, nameof (StandardColor.AliceBlue))]
[InlineData (178, 34, 34, nameof (StandardColor.FireBrick))]
[InlineData (245, 245, 245, nameof (StandardColor.WhiteSmoke))]
public void TryNameColor_ReturnsExpectedColorNames (byte r, byte g, byte b, string expectedName)
{
var expected = (true, expectedName);
Color color = new (r, g, b);
bool actualSuccess = _candidate.TryNameColor (color, out string? actualName);
bool actualSuccess = _candidate.TryNameColor(new Color(r, g, b), out string? actualName);
var actual = (actualSuccess, actualName);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData (169, 169, 169)] // W3cColor.DarkGr(a|e)y
public void TryNameColor_OmitsBlockedW3cColors (byte r, byte g, byte b)
{
(bool, string?) expected = (false, null);
bool actualSuccess = _candidate.TryNameColor (new Color (r, g, b), out string? actualName);
var actual = (actualSuccess, actualName);
Assert.Equal (expected, actual);
Assert.True (actualSuccess);
Assert.Equal (expectedName, actualName);
}
[Fact]
public void TryNameColor_NoMatchFails ()
{
(bool, string?) expected = (false, null);
bool actualSuccess = _candidate.TryNameColor (new Color (1, 2, 3), out string? actualName);
var actual = (actualSuccess, actualName);
Assert.Equal (expected, actual);
}
[Theory]
// ANSI colors
[InlineData (nameof (ColorName16.Black), 0, 0, 0)]
[InlineData (nameof (ColorName16.Blue), 0, 0, 255)]
[InlineData (nameof (ColorName16.BrightBlue), 59, 120, 255)]
[InlineData (nameof (ColorName16.BrightCyan), 97, 214, 214)]
[InlineData (nameof (ColorName16.BrightGreen), 22, 198, 12)]
[InlineData (nameof (ColorName16.BrightMagenta), 180, 0, 158)]
[InlineData (nameof (ColorName16.BrightRed), 231, 72, 86)]
[InlineData (nameof (ColorName16.BrightYellow), 249, 241, 165)]
[InlineData (nameof (ColorName16.Cyan), 0, 255, 255)]
[InlineData (nameof (ColorName16.DarkGray), 118, 118, 118)]
[InlineData (nameof (ColorName16.Gray), 128, 128, 128)]
[InlineData (nameof (ColorName16.Green), 0, 128, 0)]
[InlineData (nameof (ColorName16.Magenta), 255, 0, 255)]
[InlineData (nameof (ColorName16.Red), 255, 0, 0)]
[InlineData (nameof (ColorName16.White), 255, 255, 255)]
[InlineData (nameof (ColorName16.Yellow), 255, 255, 0)]
// W3C color name => substituted ANSI color
[InlineData (nameof (W3cColor.Fuchsia), 255, 0, 255)] // ANSI Magenta
[InlineData (nameof (W3cColor.DarkGrey), 118, 118, 118)] // ANSI Dark Gray
[InlineData (nameof (W3cColor.Grey), 128, 128, 128)] // ANSI Gray
[InlineData (nameof (W3cColor.Aqua), 0, 255, 255)] // ANSI Cyan
// W3C colors
[InlineData (nameof (W3cColor.AliceBlue), 240, 248, 255)]
[InlineData (nameof (W3cColor.BlanchedAlmond), 255, 235, 205)]
[InlineData (nameof (W3cColor.CadetBlue), 95, 158, 160)]
[InlineData (nameof (W3cColor.DarkBlue), 0, 0, 139)]
[InlineData (nameof (W3cColor.FireBrick), 178, 34, 34)]
[InlineData (nameof (W3cColor.Gainsboro), 220, 220, 220)]
[InlineData (nameof (W3cColor.HoneyDew), 240, 255, 240)]
[InlineData (nameof (W3cColor.Indigo), 75, 0, 130)]
[InlineData (nameof (W3cColor.Khaki), 240, 230, 140)]
[InlineData (nameof (W3cColor.Lavender), 230, 230, 250)]
[InlineData (nameof (W3cColor.Maroon), 128, 0, 0)]
[InlineData (nameof (W3cColor.Navy), 0, 0, 128)]
[InlineData (nameof (W3cColor.Olive), 128, 128, 0)]
[InlineData (nameof (W3cColor.Plum), 221, 160, 221)]
[InlineData (nameof (W3cColor.RoyalBlue), 65, 105, 225)]
[InlineData (nameof (W3cColor.Silver), 192, 192, 192)]
[InlineData (nameof (W3cColor.Tomato), 255, 99, 71)]
[InlineData (nameof (W3cColor.Violet), 238, 130, 238)]
[InlineData (nameof (W3cColor.WhiteSmoke), 245, 245, 245)]
[InlineData (nameof (W3cColor.YellowGreen), 154, 205, 50)]
// Case-insensitive
[InlineData ("BRIGHTBLUE", 59, 120, 255)]
[InlineData ("brightblue", 59, 120, 255)]
[InlineData ("TOMATO", 255, 99, 71)]
[InlineData ("tomato", 255, 99, 71)]
public void TryParseColor_ResolvesCombinationOfAnsiAndW3cColors (string inputName, byte r, byte g, byte b)
{
var expected = (true, new Color(r, g, b));
bool actualSuccess = _candidate.TryParseColor (inputName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
Color input = new (1, 2, 3);
bool success = _candidate.TryNameColor (input, out string? actualName);
Assert.False (success);
Assert.Null (actualName);
}
[Theory]
[InlineData ("12", 231, 72, 86)] // ColorName16.BrightRed
[InlineData ("16737095", 255, 99, 71)] // W3cColor.Tomato
[InlineData ("16737095", 255, 99, 71)] // StandardColor.Tomato
[InlineData ("#FF0000", 255, 0, 0)] // Red
public void TryParseColor_ResolvesValidEnumNumber (string inputName, byte r, byte g, byte b)
{
var expected = (true, new Color(r, g, b));
bool actualSuccess = _candidate.TryParseColor (inputName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
bool success = _candidate.TryParseColor (inputName, out Color actualColor);
Assert.True (success);
Assert.Equal (r, actualColor.R);
Assert.Equal (g, actualColor.G);
Assert.Equal (b, actualColor.B);
}
[Theory]
[InlineData (null)]
[InlineData ("")]
[InlineData ("brightlight")]
public void TryParseColor_FailsOnInvalidColorName (string? invalidName)
public void TryParseColor_FailsOnInvalidColorName (string? input)
{
var expected = (false, default(Color));
bool actualSuccess = _candidate.TryParseColor (invalidName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
bool success = _candidate.TryParseColor (input, out Color actualColor);
Assert.False (success);
Assert.Equal (default, actualColor);
}
[Theory]
[InlineData ("-12")]
[InlineData ("-16737095")]
public void TryParseColor_FailsOnInvalidEnumNumber (string invalidName)
public void TryParseColor_FailsOnInvalidEnumNumber (string input)
{
var expected = (false, default(Color));
bool actualSuccess = _candidate.TryParseColor (invalidName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
bool success = _candidate.TryParseColor (input, out Color actualColor);
Assert.False (success);
Assert.Equal (default, actualColor);
}
}

View File

@@ -0,0 +1,224 @@
#nullable enable
using Xunit.Abstractions;
namespace Terminal.Gui.DrawingTests;
public class StandardColorNameResolverTests (ITestOutputHelper output)
{
private readonly StandardColorsNameResolver _candidate = new();
[Fact]
public void GetColorNames_NamesAreInAlphabeticalOrder ()
{
string[] alphabeticallyOrderedNames = Enum.GetNames<StandardColor> ().Order ().ToArray ();
Assert.Equal (alphabeticallyOrderedNames, _candidate.GetColorNames ());
}
[Fact]
public void TryParseColor_Resolves_All_StandardColor_Enum_Values ()
{
foreach (StandardColor sc in Enum.GetValues<StandardColor> ())
{
bool success = _candidate.TryParseColor (sc.ToString (), out Color actual);
Assert.True (success, $"Expected to parse StandardColor.{sc}");
Color expected = new ((int)sc);
Assert.Equal (expected.R, actual.R);
Assert.Equal (expected.G, actual.G);
Assert.Equal (expected.B, actual.B);
}
}
[Fact]
public void TryNameColor_Resolves_FirstName_For_UniqueArgbValues ()
{
Dictionary<uint, string> seen = new ();
foreach (StandardColor sc in Enum.GetValues<StandardColor> ())
{
uint argb = StandardColors.GetArgb (sc);
if (seen.ContainsKey (argb))
{
continue;
}
Color color = new (argb);
bool success = _candidate.TryNameColor (color, out string? resolved);
Assert.True (success, $"Expected name resolution for {sc} -> ARGB #{argb:X8}");
Assert.NotNull (resolved);
List<string> expectedNames = new ();
foreach (string name in Enum.GetNames<StandardColor> ())
{
StandardColor parsed = Enum.Parse<StandardColor> (name);
if (StandardColors.GetArgb (parsed) == argb)
{
expectedNames.Add (name);
}
}
Assert.Contains (resolved, expectedNames);
seen [argb] = resolved!;
}
}
[Fact]
public void TryNameColor_Logs_StandardColor_Aliases ()
{
var map = new Dictionary<uint, List<string>> ();
foreach (StandardColor sc in Enum.GetValues<StandardColor> ())
{
var color = new Color ((int)sc);
if (!map.TryGetValue (color.Argb, out var list))
{
list = new List<string> ();
map [color.Argb] = list;
}
list.Add (sc.ToString ());
}
foreach (var kvp in map.Where (kvp => kvp.Value.Count > 1))
{
output.WriteLine ($"ARGB #{kvp.Key:X8} maps to: {string.Join (", ", kvp.Value)}");
}
}
[Theory]
[InlineData (nameof (StandardColor.Aqua))]
[InlineData (nameof (StandardColor.Cyan))]
[InlineData (nameof (StandardColor.DarkGray))]
[InlineData (nameof (StandardColor.DarkGrey))]
[InlineData (nameof (StandardColor.DarkSlateGray))]
[InlineData (nameof (StandardColor.DarkSlateGrey))]
[InlineData (nameof (StandardColor.DimGray))]
[InlineData (nameof (StandardColor.DimGrey))]
[InlineData (nameof (StandardColor.Fuchsia))]
[InlineData (nameof (StandardColor.LightGray))]
[InlineData (nameof (StandardColor.LightGrey))]
[InlineData (nameof (StandardColor.LightSlateGray))]
[InlineData (nameof (StandardColor.LightSlateGrey))]
[InlineData (nameof (StandardColor.Magenta))]
[InlineData (nameof (StandardColor.SlateGray))]
[InlineData (nameof (StandardColor.SlateGrey))]
public void GetColorNames_IncludesNamesWithSameValues (string name)
{
string[] names = _candidate.GetColorNames ().ToArray();
Assert.True (names.Contains (name), $"W3C color names is missing '{name}'.");
}
[Theory]
[InlineData (240, 248, 255, nameof (StandardColor.AliceBlue))]
[InlineData (0, 255, 255, nameof (StandardColor.Aqua))]
[InlineData (255, 0, 0, nameof (StandardColor.Red))]
[InlineData (0, 128, 0, nameof (StandardColor.Green))]
[InlineData (0, 0, 255, nameof (StandardColor.Blue))]
[InlineData (0, 255, 0, nameof (StandardColor.Lime))]
[InlineData (0, 0, 0, nameof (StandardColor.Black))]
[InlineData (255, 255, 255, nameof (StandardColor.White))]
[InlineData (154, 205, 50, nameof (StandardColor.YellowGreen))]
public void TryNameColor_ReturnsExpectedColorName (int r, int g, int b, string expectedName)
{
var expected = (true, expectedName);
Color inputColor = new(r, g, b);
bool actualSuccess = _candidate.TryNameColor (inputColor, out string? actualName);
var actual = (actualSuccess, actualName);
Assert.Equal (expected, actual);
}
[Fact]
public void TryNameColor_NoMatchFails ()
{
(bool, string?) expected = (false, null);
bool actualSuccess = _candidate.TryNameColor (new Color (1, 2, 3), out string? actualName);
var actual = (actualSuccess, actualName);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData (nameof (StandardColor.AliceBlue), 240, 248, 255)]
[InlineData (nameof (StandardColor.BlanchedAlmond), 255, 235, 205)]
[InlineData (nameof (StandardColor.CadetBlue), 95, 158, 160)]
[InlineData (nameof (StandardColor.DarkBlue), 0, 0, 139)]
[InlineData (nameof (StandardColor.FireBrick), 178, 34, 34)]
[InlineData (nameof (StandardColor.Gainsboro), 220, 220, 220)]
[InlineData (nameof (StandardColor.HoneyDew), 240, 255, 240)]
[InlineData (nameof (StandardColor.Indigo), 75, 0, 130)]
[InlineData (nameof (StandardColor.Khaki), 240, 230, 140)]
[InlineData (nameof (StandardColor.Lavender), 230, 230, 250)]
[InlineData (nameof (StandardColor.Maroon), 128, 0, 0)]
[InlineData (nameof (StandardColor.Navy), 0, 0, 128)]
[InlineData (nameof (StandardColor.Olive), 128, 128, 0)]
[InlineData (nameof (StandardColor.Plum), 221, 160, 221)]
[InlineData (nameof (StandardColor.RoyalBlue), 65, 105, 225)]
[InlineData (nameof (StandardColor.Silver), 192, 192, 192)]
[InlineData (nameof (StandardColor.Tomato), 255, 99, 71)]
[InlineData (nameof (StandardColor.Violet), 238, 130, 238)]
[InlineData (nameof (StandardColor.WhiteSmoke), 245, 245, 245)]
[InlineData (nameof (StandardColor.YellowGreen), 154, 205, 50)]
// Aliases also work
[InlineData (nameof (StandardColor.Aqua), 0, 255, 255)]
[InlineData (nameof (StandardColor.Cyan), 0, 255, 255)]
[InlineData (nameof (StandardColor.DarkGray), 169, 169, 169)]
[InlineData (nameof (StandardColor.DarkGrey), 169, 169, 169)]
// Case-insensitive
[InlineData ("Red", 255, 0, 0)]
[InlineData ("red", 255, 0, 0)]
[InlineData ("RED", 255, 0, 0)]
public void TryParseColor_ReturnsExpectedColor (string inputName, int r, int g, int b)
{
var expected = (true, new Color(r, g, b));
bool actualSuccess = _candidate.TryParseColor (inputName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData ("16737095", 255, 99, 71)] // W3cColor.Tomato
public void TryParseColor_ResolvesValidEnumNumber (string inputName, byte r, byte g, byte b)
{
var expected = (true, new Color(r, g, b));
bool actualSuccess = _candidate.TryParseColor (inputName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData (null)]
[InlineData ("")]
[InlineData ("brightlight")]
public void TryParseColor_FailsOnInvalidColorName (string? invalidName)
{
var expected = (false, default(Color));
bool actualSuccess = _candidate.TryParseColor (invalidName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData ("-16737095")]
public void TryParseColor_FailsOnInvalidEnumNumber (string invalidName)
{
var expected = (false, default(Color));
bool actualSuccess = _candidate.TryParseColor (invalidName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
}
}

View File

@@ -1,150 +0,0 @@
#nullable enable
namespace Terminal.Gui.DrawingTests;
public class W3cColorNameResolverTests
{
private readonly W3cColorNameResolver _candidate = new();
[Fact]
public void GetColorNames_NamesAreInAlphabeticalOrder ()
{
string[] alphabeticallyOrderedNames = Enum.GetNames<W3cColor> ().Order ().ToArray ();
Assert.Equal (alphabeticallyOrderedNames, _candidate.GetColorNames ());
}
[Theory]
[InlineData (nameof (W3cColor.Aqua))]
[InlineData (nameof (W3cColor.Cyan))]
[InlineData (nameof (W3cColor.DarkGray))]
[InlineData (nameof (W3cColor.DarkGrey))]
[InlineData (nameof (W3cColor.DarkSlateGray))]
[InlineData (nameof (W3cColor.DarkSlateGrey))]
[InlineData (nameof (W3cColor.DimGray))]
[InlineData (nameof (W3cColor.DimGrey))]
[InlineData (nameof (W3cColor.Fuchsia))]
[InlineData (nameof (W3cColor.LightGray))]
[InlineData (nameof (W3cColor.LightGrey))]
[InlineData (nameof (W3cColor.LightSlateGray))]
[InlineData (nameof (W3cColor.LightSlateGrey))]
[InlineData (nameof (W3cColor.Magenta))]
[InlineData (nameof (W3cColor.SlateGray))]
[InlineData (nameof (W3cColor.SlateGrey))]
public void GetColorNames_IncludesNamesWithSameValues (string name)
{
string[] names = _candidate.GetColorNames ().ToArray();
Assert.True (names.Contains (name), $"W3C color names is missing '{name}'.");
}
[Theory]
[InlineData (240, 248, 255, nameof (W3cColor.AliceBlue))]
[InlineData (0, 255, 255, nameof (W3cColor.Aqua))]
[InlineData (255, 0, 0, nameof (W3cColor.Red))]
[InlineData (0, 128, 0, nameof (W3cColor.Green))]
[InlineData (0, 0, 255, nameof (W3cColor.Blue))]
[InlineData (0, 255, 0, nameof (W3cColor.Lime))]
[InlineData (0, 0, 0, nameof (W3cColor.Black))]
[InlineData (255, 255, 255, nameof (W3cColor.White))]
[InlineData (154, 205, 50, nameof (W3cColor.YellowGreen))]
public void TryNameColor_ReturnsExpectedColorName (int r, int g, int b, string expectedName)
{
var expected = (true, expectedName);
Color inputColor = new(r, g, b);
bool actualSuccess = _candidate.TryNameColor (inputColor, out string? actualName);
var actual = (actualSuccess, actualName);
Assert.Equal (expected, actual);
}
[Fact]
public void TryNameColor_NoMatchFails ()
{
(bool, string?) expected = (false, null);
bool actualSuccess = _candidate.TryNameColor (new Color (1, 2, 3), out string? actualName);
var actual = (actualSuccess, actualName);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData (nameof (W3cColor.AliceBlue), 240, 248, 255)]
[InlineData (nameof (W3cColor.BlanchedAlmond), 255, 235, 205)]
[InlineData (nameof (W3cColor.CadetBlue), 95, 158, 160)]
[InlineData (nameof (W3cColor.DarkBlue), 0, 0, 139)]
[InlineData (nameof (W3cColor.FireBrick), 178, 34, 34)]
[InlineData (nameof (W3cColor.Gainsboro), 220, 220, 220)]
[InlineData (nameof (W3cColor.HoneyDew), 240, 255, 240)]
[InlineData (nameof (W3cColor.Indigo), 75, 0, 130)]
[InlineData (nameof (W3cColor.Khaki), 240, 230, 140)]
[InlineData (nameof (W3cColor.Lavender), 230, 230, 250)]
[InlineData (nameof (W3cColor.Maroon), 128, 0, 0)]
[InlineData (nameof (W3cColor.Navy), 0, 0, 128)]
[InlineData (nameof (W3cColor.Olive), 128, 128, 0)]
[InlineData (nameof (W3cColor.Plum), 221, 160, 221)]
[InlineData (nameof (W3cColor.RoyalBlue), 65, 105, 225)]
[InlineData (nameof (W3cColor.Silver), 192, 192, 192)]
[InlineData (nameof (W3cColor.Tomato), 255, 99, 71)]
[InlineData (nameof (W3cColor.Violet), 238, 130, 238)]
[InlineData (nameof (W3cColor.WhiteSmoke), 245, 245, 245)]
[InlineData (nameof (W3cColor.YellowGreen), 154, 205, 50)]
// Aliases also work
[InlineData (nameof (W3cColor.Aqua), 0, 255, 255)]
[InlineData (nameof (W3cColor.Cyan), 0, 255, 255)]
[InlineData (nameof (W3cColor.DarkGray), 169, 169, 169)]
[InlineData (nameof (W3cColor.DarkGrey), 169, 169, 169)]
// Case-insensitive
[InlineData ("Red", 255, 0, 0)]
[InlineData ("red", 255, 0, 0)]
[InlineData ("RED", 255, 0, 0)]
public void TryParseColor_ReturnsExpectedColor (string inputName, int r, int g, int b)
{
var expected = (true, new Color(r, g, b));
bool actualSuccess = _candidate.TryParseColor (inputName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData ("16737095", 255, 99, 71)] // W3cColor.Tomato
public void TryParseColor_ResolvesValidEnumNumber (string inputName, byte r, byte g, byte b)
{
var expected = (true, new Color(r, g, b));
bool actualSuccess = _candidate.TryParseColor (inputName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData (null)]
[InlineData ("")]
[InlineData ("brightlight")]
public void TryParseColor_FailsOnInvalidColorName (string? invalidName)
{
var expected = (false, default(Color));
bool actualSuccess = _candidate.TryParseColor (invalidName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
}
[Theory]
[InlineData ("-16737095")]
public void TryParseColor_FailsOnInvalidEnumNumber (string invalidName)
{
var expected = (false, default(Color));
bool actualSuccess = _candidate.TryParseColor (invalidName, out Color actualColor);
var actual = (actualSuccess, actualColor);
Assert.Equal (expected, actual);
}
}

View File

@@ -1,64 +0,0 @@
using System.Reflection;
namespace Terminal.Gui.DrawingTests;
public class ColorSchemeTests
{
[Fact]
public void Colors_ColorSchemes_Built_Ins ()
{
Colors.Reset ();
Dictionary<string, ColorScheme> schemes = Colors.ColorSchemes;
Assert.NotNull (schemes);
Assert.Equal (5, schemes.Count);
Assert.True (schemes.ContainsKey ("TopLevel"));
Assert.True (schemes.ContainsKey ("Base"));
Assert.True (schemes.ContainsKey ("Dialog"));
Assert.True (schemes.ContainsKey ("Menu"));
Assert.True (schemes.ContainsKey ("Error"));
}
[Fact]
public void Colors_ColorSchemes_Property_Has_Private_Setter ()
{
// Resharper Code Cleanup likes to remove the `private set; `
// from the ColorSchemes property. This test will fail if
// that happens.
PropertyInfo property = typeof (Colors).GetProperty ("ColorSchemes");
Assert.NotNull (property);
Assert.NotNull (property.SetMethod);
Assert.True (property.GetSetMethod (true).IsPrivate);
}
[Fact]
public void ColorScheme_New ()
{
var scheme = new ColorScheme ();
var lbl = new Label ();
lbl.ColorScheme = scheme;
lbl.Draw ();
}
[Fact]
public void ColorScheme_BigConstructor ()
{
var a = new Attribute (1);
var b = new Attribute (2);
var c = new Attribute (3);
var d = new Attribute (4);
var e = new Attribute (5);
var cs = new ColorScheme (
normal: a,
focus: b,
hotNormal: c,
disabled: d,
hotFocus: e);
Assert.Equal (a, cs.Normal);
Assert.Equal (b, cs.Focus);
Assert.Equal (c, cs.HotNormal);
Assert.Equal (d, cs.Disabled);
Assert.Equal (e, cs.HotFocus);
}
}

View File

@@ -0,0 +1,128 @@
using Xunit;
namespace Terminal.Gui.DrawingTests;
public class SchemeGetAttributeForRoleAlgorithmTests
{
[Fact]
public void Normal_Is_Always_Explicit ()
{
Attribute normal = new ("Red", "Blue");
Scheme scheme = new (normal);
Assert.NotNull (scheme.Normal);
Assert.Equal (normal, scheme.GetAttributeForRole (VisualRole.Normal));
}
[Fact]
public void Focus_Derived_From_Normal_Swaps_FgBg ()
{
Attribute normal = new ("Red", "Blue");
Scheme scheme = new (normal);
Attribute focus = scheme.GetAttributeForRole (VisualRole.Focus);
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Focus, out _));
Assert.Equal (normal.Background, focus.Foreground);
Assert.Equal (normal.Foreground, focus.Background);
}
//[Fact]
//public void Highlight_Derived_From_Normal_HighlightColor ()
//{
// Attribute normal = new ("Red", "Blue");
// Scheme scheme = new (normal);
// Attribute highlight = scheme.GetAttributeForRole (VisualRole.Highlight);
// Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Highlight, out _));
// Assert.Equal (normal.Background.GetHighlightColor (), highlight.Background);
//}
//[Fact]
//public void Editable_Derived_From_Normal_LightYellow_Fg ()
//{
// Attribute normal = new ("Red", "Blue");
// Scheme scheme = new (normal);
// Attribute editable = scheme.GetAttributeForRole (VisualRole.Editable);
// Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Editable, out _));
// Assert.Equal (new Color ("LightYellow"), editable.Foreground);
//}
//[Fact]
//public void ReadOnly_Derived_From_Editable_Italic ()
//{
// Attribute normal = new ("Red", "Blue");
// Scheme scheme = new (normal);
// Attribute readOnly = scheme.GetAttributeForRole (VisualRole.ReadOnly);
// Attribute editable = scheme.GetAttributeForRole (VisualRole.Editable);
// Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.ReadOnly, out _));
// Assert.Equal (editable.Foreground, readOnly.Foreground);
// Assert.True (readOnly.Style.HasFlag (TextStyle.Italic));
//}
//[Fact]
//public void Disabled_Derived_From_Normal_Faint ()
//{
// Attribute normal = new ("Red", "Blue");
// Scheme scheme = new (normal);
// Attribute disabled = scheme.GetAttributeForRole (VisualRole.Disabled);
// Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Disabled, out _));
// Assert.True (disabled.Style.HasFlag (TextStyle.Faint));
//}
[Fact]
public void Active_Derived_Correctly ()
{
Attribute normal = new ("Red", "Blue");
Scheme scheme = new (normal);
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Active, out _));
Attribute active = scheme.GetAttributeForRole (VisualRole.Active);
Attribute focus = scheme.GetAttributeForRole (VisualRole.Focus);
Assert.True (active.Style.HasFlag (TextStyle.Bold));
//Assert.Equal (active.Foreground, focus.Foreground);
//Assert.Equal (active.Background, active.Background);
}
[Fact]
public void HotNormal_Derived_From_Normal_Underline ()
{
Attribute normal = new ("Red", "Blue");
Scheme scheme = new (normal);
Attribute hotNormal = scheme.GetAttributeForRole (VisualRole.HotNormal);
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotNormal, out _));
Assert.True (hotNormal.Style.HasFlag (TextStyle.Underline));
}
[Fact]
public void HotFocus_Derived_From_Focus_Underline ()
{
Attribute normal = new ("Red", "Blue");
Scheme scheme = new (normal);
Attribute hotFocus = scheme.GetAttributeForRole (VisualRole.HotFocus);
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotFocus, out _));
Attribute focus = scheme.GetAttributeForRole (VisualRole.Focus);
Assert.True (hotFocus.Style.HasFlag (TextStyle.Underline));
Assert.Equal (focus.Foreground, hotFocus.Foreground);
Assert.Equal (focus.Background, hotFocus.Background);
}
[Fact]
public void HotActive_Derived_From_Active_Underline ()
{
Attribute normal = new ("Red", "Blue");
Scheme scheme = new (normal);
Attribute hotActive = scheme.GetAttributeForRole (VisualRole.HotActive);
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotActive, out _));
Attribute active = scheme.GetAttributeForRole (VisualRole.Active);
Assert.True (hotActive.Style.HasFlag (TextStyle.Underline));
Assert.Equal (active.Foreground, hotActive.Foreground);
Assert.Equal (active.Background, hotActive.Background);
}
}

View File

@@ -0,0 +1,165 @@
#nullable enable
using System.Reflection;
namespace Terminal.Gui.DrawingTests;
public class SchemeTests
{
[Fact]
public void Colors_Schemes_Property_Has_Private_Setter ()
{
// Resharper Code Cleanup likes to remove the `private set; `
// from the Schemes property. This test will fail if
// that happens.
PropertyInfo? property = typeof (SchemeManager).GetProperty ("Schemes");
Assert.NotNull (property);
Assert.NotNull (property.SetMethod);
Assert.True (property.GetSetMethod (true)!.IsPrivate);
}
[Fact]
public void New ()
{
var scheme = new Scheme ();
var lbl = new Label ();
lbl.SetScheme (scheme);
lbl.Draw ();
}
[Fact]
public void Built_Ins ()
{
Dictionary<string, Scheme?> schemes = SchemeManager.GetSchemes ();
Assert.NotNull (schemes);
Assert.Equal (5, schemes.Count);
Assert.True (schemes.ContainsKey ("Base"));
Assert.True (schemes.ContainsKey ("Dialog"));
Assert.True (schemes.ContainsKey ("Error"));
Assert.True (schemes.ContainsKey ("Menu"));
Assert.True (schemes.ContainsKey ("TopLevel"));
}
[Fact]
public void Built_Ins_Are_Implicit ()
{
Dictionary<string, Scheme?> schemes = SchemeManager.GetSchemes ();
Assert.True (schemes ["Base"]!.TryGetExplicitlySetAttributeForRole (VisualRole.Normal, out _));
Assert.False (schemes ["Base"]!.TryGetExplicitlySetAttributeForRole (VisualRole.HotNormal, out _));
}
[Fact]
public void With_Same_Attributes_AreEqual ()
{
Attribute attr = new (Color.Red, Color.Blue, TextStyle.Bold);
Scheme s1 = new (attr);
Scheme s2 = new (attr);
Assert.Equal (s1, s2);
Assert.Equal (s1.GetHashCode (), s2.GetHashCode ());
}
[Fact]
public void Scheme_Properties_Are_Immutable ()
{
Scheme scheme = new (new Attribute ("Red", "Blue"));
// The following line should not compile if uncommented:
// scheme.Normal = new Attribute("Green", "Yellow");
// Immutability is enforced by the C# compiler for init-only properties.
Assert.True (true); // This test is a placeholder for documentation purposes.
}
[Fact]
public void ObjectInitializer_Sets_Properties ()
{
Scheme scheme = new ()
{
Normal = new Attribute ("Red", "Blue"),
Focus = new Attribute ("Green", "Yellow"),
HotNormal = new Attribute ("White", "Black"),
HotFocus = new Attribute ("Black", "White"),
Active = new Attribute ("Cyan", "Magenta"),
HotActive = new Attribute ("Magenta", "Cyan"),
Highlight = new Attribute ("Yellow", "Red"),
Editable = new Attribute ("Blue", "Yellow"),
ReadOnly = new Attribute ("Gray", "Black"),
Disabled = new Attribute ("DarkGray", "White")
};
Assert.Equal (new Attribute ("Red", "Blue"), scheme.Normal);
Assert.Equal (new Attribute ("Green", "Yellow"), scheme.Focus);
Assert.Equal (new Attribute ("White", "Black"), scheme.HotNormal);
Assert.Equal (new Attribute ("Black", "White"), scheme.HotFocus);
Assert.Equal (new Attribute ("Cyan", "Magenta"), scheme.Active);
Assert.Equal (new Attribute ("Magenta", "Cyan"), scheme.HotActive);
Assert.Equal (new Attribute ("Yellow", "Red"), scheme.Highlight);
Assert.Equal (new Attribute ("Blue", "Yellow"), scheme.Editable);
Assert.Equal (new Attribute ("Gray", "Black"), scheme.ReadOnly);
Assert.Equal (new Attribute ("DarkGray", "White"), scheme.Disabled);
}
[Fact]
public void With_Different_Attributes_AreNotEqual ()
{
Scheme s1 = new (new Attribute ("Red", "Blue"));
Scheme s2 = new (new Attribute ("Green", "Yellow"));
Assert.NotEqual (s1, s2);
Assert.NotEqual (s1.GetHashCode (), s2.GetHashCode ());
}
[Fact]
public void Default_Constructor_Has_Default_Values ()
{
Scheme scheme = new ();
Assert.True (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Normal, out _));
// All other roles should be implicit and derived from Normal
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Active, out _));
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotNormal, out _));
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Focus, out _));
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotFocus, out _));
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Active, out _));
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.HotActive, out _));
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Highlight, out _));
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Editable, out _));
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.ReadOnly, out _));
Assert.False (scheme.TryGetExplicitlySetAttributeForRole (VisualRole.Disabled, out _));
}
[Fact]
public void ToString_Outputs_All_Properties ()
{
Scheme scheme = new (new Attribute ("Red", "Blue"));
string str = scheme.ToString ();
Assert.Contains ("Normal", str, StringComparison.OrdinalIgnoreCase);
Assert.Contains ("HotNormal", str, StringComparison.OrdinalIgnoreCase);
Assert.Contains ("Focus", str, StringComparison.OrdinalIgnoreCase);
Assert.Contains ("HotFocus", str, StringComparison.OrdinalIgnoreCase);
Assert.Contains ("Active", str, StringComparison.OrdinalIgnoreCase);
Assert.Contains ("HotActive", str, StringComparison.OrdinalIgnoreCase);
Assert.Contains ("Highlight", str, StringComparison.OrdinalIgnoreCase);
Assert.Contains ("Editable", str, StringComparison.OrdinalIgnoreCase);
Assert.Contains ("ReadOnly", str, StringComparison.OrdinalIgnoreCase);
Assert.Contains ("Disabled", str, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void CopyConstructor_Null_Throws ()
{
Assert.Throws<ArgumentNullException> (() => new Scheme (null));
}
[Fact]
public void Is_Thread_Safe_For_Concurrent_Reads ()
{
Scheme scheme = new (new Attribute ("Red", "Blue"));
Parallel.For (0, 1000, i =>
{
// All threads can safely read properties
_ = scheme.Normal;
_ = scheme.GetAttributeForRole (VisualRole.Focus);
_ = scheme.ToString ();
});
}
}