From 27cf8f8fbbcfd7dc7f0b8b2391ff56e289694251 Mon Sep 17 00:00:00 2001 From: Tigger Kindel Date: Tue, 10 Oct 2023 00:29:01 -0600 Subject: [PATCH] Test clean up --- UnitTests/ConsoleDrivers/AttributeTests.cs | 242 ------------------ .../{ColorTests.cs => DriverColorTests.cs} | 42 +-- UnitTests/Drawing/AttributeTests.cs | 241 +++++++++++++++++ UnitTests/Drawing/ColorTests.cs | 40 +++ 4 files changed, 283 insertions(+), 282 deletions(-) delete mode 100644 UnitTests/ConsoleDrivers/AttributeTests.cs rename UnitTests/ConsoleDrivers/{ColorTests.cs => DriverColorTests.cs} (56%) create mode 100644 UnitTests/Drawing/AttributeTests.cs diff --git a/UnitTests/ConsoleDrivers/AttributeTests.cs b/UnitTests/ConsoleDrivers/AttributeTests.cs deleted file mode 100644 index 91dae739b..000000000 --- a/UnitTests/ConsoleDrivers/AttributeTests.cs +++ /dev/null @@ -1,242 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Terminal.Gui; -using Xunit; - -// Alias Console to MockConsole so we don't accidentally use Console -using Console = Terminal.Gui.FakeConsole; - -namespace Terminal.Gui.DriverTests { - public class AttributeTests { - [Fact] - public void Constuctors_Constuct () - { - var driver = new FakeDriver (); - Application.Init (driver); - driver.Init (() => { }); - - // Test parameterless constructor - var attr = new Attribute (); - - Assert.Equal (-1, attr.Value); - Assert.Equal ((Color)Color.White, attr.Foreground); - Assert.Equal ((Color)Color.Black, attr.Background); - - // Test foreground, background - var fg = new Color (); - fg = (Color)Color.Red; - - var bg = new Color (); - bg = (Color)Color.Blue; - - attr = new Attribute (fg, bg); - - Assert.True (attr.Initialized); - Assert.True (attr.HasValidColors); - Assert.Equal (fg, attr.Foreground); - Assert.Equal (bg, attr.Background); - - attr = new Attribute (fg); - Assert.True (attr.Initialized); - Assert.True (attr.HasValidColors); - Assert.Equal (fg, attr.Foreground); - Assert.Equal (fg, attr.Background); - - attr = new Attribute (bg); - Assert.True (attr.Initialized); - Assert.True (attr.HasValidColors); - Assert.Equal (bg, attr.Foreground); - Assert.Equal (bg, attr.Background); - - driver.End (); - Application.Shutdown (); - } - - [Fact] - public void Implicit_Assign () - { - var driver = new FakeDriver (); - Application.Init (driver); - driver.Init (() => { }); - - var attr = new Attribute (); - - var value = 42; - var fg = new Color (); - fg = (Color)Color.Red; - - var bg = new Color (); - bg = (Color)Color.Blue; - - // Test conversion to int - attr = new Attribute (value, fg, bg); - int value_implicit = attr.Value; - Assert.Equal (value, value_implicit); - - Assert.Equal (value, attr.Value); - - driver.End (); - Application.Shutdown (); - } - - [Fact] - public void Make_SetsNotInitialized_NoDriver () - { - var fg = new Color (); - fg = (Color)Color.Red; - - var bg = new Color (); - bg = (Color)Color.Blue; - - var a = Attribute.Make (fg, bg); - - Assert.False (a.Initialized); - } - - [Fact] - public void Make_Creates () - { - var driver = new FakeDriver (); - Application.Init (driver); - driver.Init (() => { }); - - var fg = new Color (); - fg = (Color)Color.Red; - - var bg = new Color (); - bg = (Color)Color.Blue; - - var attr = Attribute.Make (fg, bg); - Assert.True (attr.Initialized); - Assert.Equal (fg, attr.Foreground); - Assert.Equal (bg, attr.Background); - - driver.End (); - Application.Shutdown (); - } - - [Fact] - public void Make_Creates_NoDriver () - { - - var fg = new Color (); - fg = (Color)Color.Red; - - var bg = new Color (); - bg = (Color)Color.Blue; - - var attr = Attribute.Make (fg, bg); - Assert.False (attr.Initialized); - Assert.Equal (fg, attr.Foreground); - Assert.Equal (bg, attr.Background); - } - - [Fact] - public void Get_Asserts_NoDriver () - { - Assert.Throws (() => Attribute.Get ()); - } - - [Fact] - public void Get_Gets () - { - var driver = new FakeDriver (); - Application.Init (driver); - driver.Init (() => { }); - - var value = 42; - var fg = new Color (); - fg = (Color)Color.Red; - - var bg = new Color (); - bg = (Color)Color.Blue; - - var attr = new Attribute (value, fg, bg); - - driver.SetAttribute (attr); - - var ret_attr = Attribute.Get (); - - Assert.Equal (value, ret_attr.Value); - Assert.Equal (fg, ret_attr.Foreground); - Assert.Equal (bg, ret_attr.Background); - - driver.End (); - Application.Shutdown (); - } - - [Fact] - [AutoInitShutdown] - public void GetColors_Based_On_Value () - { - var driver = Application.Driver; - var attrValue = new Attribute (Color.Red, Color.Green).Value; - driver.GetColors (attrValue, out ColorNames fg, out ColorNames bg); - - Assert.Equal ((Color)Color.Red, (Color)fg); - Assert.Equal ((Color)Color.Green, (Color)bg); - } - - [Fact] - public void IsValid_Tests () - { - var attr = new Attribute (); - Assert.True (attr.HasValidColors); - - attr = new Attribute (Color.Red, Color.Green); - Assert.True (attr.HasValidColors); - - //attr = new Attribute (Color.Red, (Color)(-1)); - //Assert.False (attr.HasValidColors); - - //attr = new Attribute ((Color)(-1), Color.Green); - //Assert.False (attr.HasValidColors); - - //attr = new Attribute ((Color)(-1), (Color)(-1)); - //Assert.False (attr.HasValidColors); - } - - [Fact] - public void Equals_NotInitialized() - { - var attr1 = new Attribute (Color.Red, Color.Green); - var attr2 = new Attribute (Color.Red, Color.Green); - - Assert.True (attr1.Equals (attr2)); - Assert.True (attr2.Equals (attr1)); - } - - [Fact] - public void NotEquals_NotInitialized () - { - var attr1 = new Attribute (Color.Red, Color.Green); - var attr2 = new Attribute (Color.Green, Color.Red); - - Assert.False (attr1.Equals (attr2)); - Assert.False (attr2.Equals (attr1)); - } - - [Fact, AutoInitShutdown] - public void Equals_Initialized () - { - Assert.NotNull(Application.Driver); - - var attr1 = new Attribute (Color.Red, Color.Green); - var attr2 = new Attribute (Color.Red, Color.Green); - - Assert.True (attr1.Equals (attr2)); - Assert.True (attr2.Equals (attr1)); - } - - [Fact,AutoInitShutdown] - public void NotEquals_Initialized () - { - var attr1 = new Attribute (Color.Red, Color.Green); - var attr2 = new Attribute (Color.Green, Color.Red); - - Assert.False (attr1.Equals (attr2)); - Assert.False (attr2.Equals (attr1)); - } - } -} diff --git a/UnitTests/ConsoleDrivers/ColorTests.cs b/UnitTests/ConsoleDrivers/DriverColorTests.cs similarity index 56% rename from UnitTests/ConsoleDrivers/ColorTests.cs rename to UnitTests/ConsoleDrivers/DriverColorTests.cs index 1470a5fc0..d9de6973e 100644 --- a/UnitTests/ConsoleDrivers/ColorTests.cs +++ b/UnitTests/ConsoleDrivers/DriverColorTests.cs @@ -5,8 +5,8 @@ using Xunit; using Console = Terminal.Gui.FakeConsole; namespace Terminal.Gui.DriverTests { - public class ColorTests { - public ColorTests () + public class DriverColorTests { + public DriverColorTests () { ConsoleDriver.RunningUnitTests = true; } @@ -38,44 +38,6 @@ namespace Terminal.Gui.DriverTests { Application.Shutdown (); } - [Fact, AutoInitShutdown] - public void ColorScheme_New () - { - var scheme = new ColorScheme (); - var lbl = new Label (); - lbl.ColorScheme = scheme; - lbl.Draw (); - } - - [Fact] - public void TestAllColors () - { - var colorNames = Enum.GetValues (typeof (ColorNames)); - Attribute [] attrs = new Attribute [colorNames.Length]; - - int idx = 0; - foreach (ColorNames bg in colorNames) { - attrs [idx] = new Attribute (bg, colorNames.Length - 1 - bg); - idx++; - } - Assert.Equal (16, attrs.Length); - Assert.Equal (new Attribute (Color.Black, Color.White), attrs [0]); - Assert.Equal (new Attribute (Color.Blue, Color.BrightYellow), attrs [1]); - Assert.Equal (new Attribute (Color.Green, Color.BrightMagenta), attrs [2]); - Assert.Equal (new Attribute (Color.Cyan, Color.BrightRed), attrs [3]); - Assert.Equal (new Attribute (Color.Red, Color.BrightCyan), attrs [4]); - Assert.Equal (new Attribute (Color.Magenta, Color.BrightGreen), attrs [5]); - Assert.Equal (new Attribute (Color.Brown, Color.BrightBlue), attrs [6]); - Assert.Equal (new Attribute (Color.Gray, Color.DarkGray), attrs [7]); - Assert.Equal (new Attribute (Color.DarkGray, Color.Gray), attrs [8]); - Assert.Equal (new Attribute (Color.BrightBlue, Color.Brown), attrs [9]); - Assert.Equal (new Attribute (Color.BrightGreen, Color.Magenta), attrs [10]); - Assert.Equal (new Attribute (Color.BrightCyan, Color.Red), attrs [11]); - Assert.Equal (new Attribute (Color.BrightRed, Color.Cyan), attrs [12]); - Assert.Equal (new Attribute (Color.BrightMagenta, Color.Green), attrs [13]); - Assert.Equal (new Attribute (Color.BrightYellow, Color.Blue), attrs [14]); - Assert.Equal (new Attribute (Color.White, Color.Black), attrs [^1]); - } [Theory] [InlineData (typeof (FakeDriver), false)] diff --git a/UnitTests/Drawing/AttributeTests.cs b/UnitTests/Drawing/AttributeTests.cs new file mode 100644 index 000000000..de5cee490 --- /dev/null +++ b/UnitTests/Drawing/AttributeTests.cs @@ -0,0 +1,241 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Terminal.Gui; +using Xunit; + +// Alias Console to MockConsole so we don't accidentally use Console +using Console = Terminal.Gui.FakeConsole; + +namespace Terminal.Gui.DrawingTests; +public class AttributeTests { + [Fact] + public void Constuctors_Constuct () + { + var driver = new FakeDriver (); + Application.Init (driver); + driver.Init (() => { }); + + // Test parameterless constructor + var attr = new Attribute (); + + Assert.Equal (-1, attr.Value); + Assert.Equal ((Color)Color.White, attr.Foreground); + Assert.Equal ((Color)Color.Black, attr.Background); + + // Test foreground, background + var fg = new Color (); + fg = (Color)Color.Red; + + var bg = new Color (); + bg = (Color)Color.Blue; + + attr = new Attribute (fg, bg); + + Assert.True (attr.Initialized); + Assert.True (attr.HasValidColors); + Assert.Equal (fg, attr.Foreground); + Assert.Equal (bg, attr.Background); + + attr = new Attribute (fg); + Assert.True (attr.Initialized); + Assert.True (attr.HasValidColors); + Assert.Equal (fg, attr.Foreground); + Assert.Equal (fg, attr.Background); + + attr = new Attribute (bg); + Assert.True (attr.Initialized); + Assert.True (attr.HasValidColors); + Assert.Equal (bg, attr.Foreground); + Assert.Equal (bg, attr.Background); + + driver.End (); + Application.Shutdown (); + } + + [Fact] + public void Implicit_Assign () + { + var driver = new FakeDriver (); + Application.Init (driver); + driver.Init (() => { }); + + var attr = new Attribute (); + + var value = 42; + var fg = new Color (); + fg = (Color)Color.Red; + + var bg = new Color (); + bg = (Color)Color.Blue; + + // Test conversion to int + attr = new Attribute (value, fg, bg); + int value_implicit = attr.Value; + Assert.Equal (value, value_implicit); + + Assert.Equal (value, attr.Value); + + driver.End (); + Application.Shutdown (); + } + + [Fact] + public void Make_SetsNotInitialized_NoDriver () + { + var fg = new Color (); + fg = (Color)Color.Red; + + var bg = new Color (); + bg = (Color)Color.Blue; + + var a = Attribute.Make (fg, bg); + + Assert.False (a.Initialized); + } + + [Fact] + public void Make_Creates () + { + var driver = new FakeDriver (); + Application.Init (driver); + driver.Init (() => { }); + + var fg = new Color (); + fg = (Color)Color.Red; + + var bg = new Color (); + bg = (Color)Color.Blue; + + var attr = Attribute.Make (fg, bg); + Assert.True (attr.Initialized); + Assert.Equal (fg, attr.Foreground); + Assert.Equal (bg, attr.Background); + + driver.End (); + Application.Shutdown (); + } + + [Fact] + public void Make_Creates_NoDriver () + { + + var fg = new Color (); + fg = (Color)Color.Red; + + var bg = new Color (); + bg = (Color)Color.Blue; + + var attr = Attribute.Make (fg, bg); + Assert.False (attr.Initialized); + Assert.Equal (fg, attr.Foreground); + Assert.Equal (bg, attr.Background); + } + + [Fact] + public void Get_Asserts_NoDriver () + { + Assert.Throws (() => Attribute.Get ()); + } + + [Fact] + public void Get_Gets () + { + var driver = new FakeDriver (); + Application.Init (driver); + driver.Init (() => { }); + + var value = 42; + var fg = new Color (); + fg = (Color)Color.Red; + + var bg = new Color (); + bg = (Color)Color.Blue; + + var attr = new Attribute (value, fg, bg); + + driver.SetAttribute (attr); + + var ret_attr = Attribute.Get (); + + Assert.Equal (value, ret_attr.Value); + Assert.Equal (fg, ret_attr.Foreground); + Assert.Equal (bg, ret_attr.Background); + + driver.End (); + Application.Shutdown (); + } + + [Fact] + [AutoInitShutdown] + public void GetColors_Based_On_Value () + { + var driver = Application.Driver; + var attrValue = new Attribute (Color.Red, Color.Green).Value; + driver.GetColors (attrValue, out ColorNames fg, out ColorNames bg); + + Assert.Equal ((Color)Color.Red, (Color)fg); + Assert.Equal ((Color)Color.Green, (Color)bg); + } + + [Fact] + public void IsValid_Tests () + { + var attr = new Attribute (); + Assert.True (attr.HasValidColors); + + attr = new Attribute (Color.Red, Color.Green); + Assert.True (attr.HasValidColors); + + //attr = new Attribute (Color.Red, (Color)(-1)); + //Assert.False (attr.HasValidColors); + + //attr = new Attribute ((Color)(-1), Color.Green); + //Assert.False (attr.HasValidColors); + + //attr = new Attribute ((Color)(-1), (Color)(-1)); + //Assert.False (attr.HasValidColors); + } + + [Fact] + public void Equals_NotInitialized () + { + var attr1 = new Attribute (Color.Red, Color.Green); + var attr2 = new Attribute (Color.Red, Color.Green); + + Assert.True (attr1.Equals (attr2)); + Assert.True (attr2.Equals (attr1)); + } + + [Fact] + public void NotEquals_NotInitialized () + { + var attr1 = new Attribute (Color.Red, Color.Green); + var attr2 = new Attribute (Color.Green, Color.Red); + + Assert.False (attr1.Equals (attr2)); + Assert.False (attr2.Equals (attr1)); + } + + [Fact, AutoInitShutdown] + public void Equals_Initialized () + { + Assert.NotNull (Application.Driver); + + var attr1 = new Attribute (Color.Red, Color.Green); + var attr2 = new Attribute (Color.Red, Color.Green); + + Assert.True (attr1.Equals (attr2)); + Assert.True (attr2.Equals (attr1)); + } + + [Fact, AutoInitShutdown] + public void NotEquals_Initialized () + { + var attr1 = new Attribute (Color.Red, Color.Green); + var attr2 = new Attribute (Color.Green, Color.Red); + + Assert.False (attr1.Equals (attr2)); + Assert.False (attr2.Equals (attr1)); + } +} diff --git a/UnitTests/Drawing/ColorTests.cs b/UnitTests/Drawing/ColorTests.cs index 6be6a2805..41f74ce6f 100644 --- a/UnitTests/Drawing/ColorTests.cs +++ b/UnitTests/Drawing/ColorTests.cs @@ -13,6 +13,46 @@ using static Unix.Terminal.Curses; namespace Terminal.Gui.DrawingTests; public class ColorTests { + + [Fact, AutoInitShutdown] + public void ColorScheme_New () + { + var scheme = new ColorScheme (); + var lbl = new Label (); + lbl.ColorScheme = scheme; + lbl.Draw (); + } + + [Fact] + public void TestAllColors () + { + var colorNames = Enum.GetValues (typeof (ColorNames)); + Attribute [] attrs = new Attribute [colorNames.Length]; + + int idx = 0; + foreach (ColorNames bg in colorNames) { + attrs [idx] = new Attribute (bg, colorNames.Length - 1 - bg); + idx++; + } + Assert.Equal (16, attrs.Length); + Assert.Equal (new Attribute (Color.Black, Color.White), attrs [0]); + Assert.Equal (new Attribute (Color.Blue, Color.BrightYellow), attrs [1]); + Assert.Equal (new Attribute (Color.Green, Color.BrightMagenta), attrs [2]); + Assert.Equal (new Attribute (Color.Cyan, Color.BrightRed), attrs [3]); + Assert.Equal (new Attribute (Color.Red, Color.BrightCyan), attrs [4]); + Assert.Equal (new Attribute (Color.Magenta, Color.BrightGreen), attrs [5]); + Assert.Equal (new Attribute (Color.Brown, Color.BrightBlue), attrs [6]); + Assert.Equal (new Attribute (Color.Gray, Color.DarkGray), attrs [7]); + Assert.Equal (new Attribute (Color.DarkGray, Color.Gray), attrs [8]); + Assert.Equal (new Attribute (Color.BrightBlue, Color.Brown), attrs [9]); + Assert.Equal (new Attribute (Color.BrightGreen, Color.Magenta), attrs [10]); + Assert.Equal (new Attribute (Color.BrightCyan, Color.Red), attrs [11]); + Assert.Equal (new Attribute (Color.BrightRed, Color.Cyan), attrs [12]); + Assert.Equal (new Attribute (Color.BrightMagenta, Color.Green), attrs [13]); + Assert.Equal (new Attribute (Color.BrightYellow, Color.Blue), attrs [14]); + Assert.Equal (new Attribute (Color.White, Color.Black), attrs [^1]); + } + [Fact] public void ColorNames_Has16Elements () {