123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- 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 DefaultConstructor ()
- {
- // Arrange & Act
- var attribute = new Attribute ();
- // Assert
- Assert.False (attribute.Initialized);
- Assert.Equal (-1, attribute.Value);
- Assert.Equal ((Color)Color.White, attribute.Foreground);
- Assert.Equal ((Color)Color.Black, attribute.Background);
- }
- [Fact]
- public void PlatformColorConstructor ()
- {
- // Arrange & Act
- var attribute = new Attribute (42);
- // Assert
- Assert.True (attribute.Initialized);
- Assert.Equal (42, attribute.Value);
- Assert.Equal ((Color)Color.White, attribute.Foreground);
- Assert.Equal ((Color)Color.Black, attribute.Background);
- }
- [Fact]
- public void ColorNamesConstructor ()
- {
- // Arrange & Act
- var attribute = new Attribute (ColorNames.Blue);
- // Assert
- Assert.Equal ((Color)Color.Blue, attribute.Foreground);
- Assert.Equal ((Color)Color.Blue, attribute.Background);
- }
- [Fact, AutoInitShutdown]
- public void ColorConstructor ()
- {
- // Arrange & Act
- var foregroundColor = new Color (0, 0, 255);
- var backgroundColor = new Color (255, 255, 255);
- var attribute = new Attribute (foregroundColor, backgroundColor);
- // Assert
- Assert.Equal (foregroundColor, attribute.Foreground);
- Assert.Equal (backgroundColor, attribute.Background);
- }
- [Fact, AutoInitShutdown]
- public void ColorAndColorNamesConstructor ()
- {
- // Arrange & Act
- var foregroundColor = new Color (0, 0, 255);
- var backgroundColorName = ColorNames.Black;
- var attribute = new Attribute (foregroundColor, backgroundColorName);
- // Assert
- Assert.Equal (foregroundColor, attribute.Foreground);
- Assert.Equal ((Color)backgroundColorName, attribute.Background);
- }
- [Fact]
- public void ColorNamesAndColorConstructor ()
- {
- // Arrange & Act
- var foregroundColorName = ColorNames.BrightYellow;
- var backgroundColor = new Color (128, 128, 128);
- var attribute = new Attribute (foregroundColorName, backgroundColor);
- // Assert
- Assert.Equal ((Color)foregroundColorName, attribute.Foreground);
- Assert.Equal (backgroundColor, attribute.Background);
- }
- [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 MakeColorAndColor_ForegroundAndBackgroundShouldMatchInput ()
- {
- // Arrange
- var foregroundColor = new Color (0, 0, 255);
- var backgroundColor = new Color (255, 255, 255);
- // Act
- var attribute = new Attribute (foregroundColor, backgroundColor);
- // Assert
- Assert.Equal (foregroundColor, attribute.Foreground);
- Assert.Equal (backgroundColor, attribute.Background);
- }
- [Fact]
- public void MakeColorNamesAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
- {
- // Arrange
- var foregroundColorName = ColorNames.BrightYellow;
- var backgroundColorName = ColorNames.Black;
- // Act
- var attribute = new Attribute (foregroundColorName, backgroundColorName);
- // Assert
- Assert.Equal ((Color)foregroundColorName, attribute.Foreground);
- Assert.Equal ((Color)backgroundColorName, attribute.Background);
- }
- [Fact]
- public void MakeColorNamesAndColor_ForegroundAndBackgroundShouldMatchInput ()
- {
- // Arrange
- var foregroundColorName = ColorNames.Green;
- var backgroundColor = new Color (128, 128, 128);
- // Act
- var attribute = new Attribute (foregroundColorName, backgroundColor);
- // Assert
- Assert.Equal ((Color)foregroundColorName, attribute.Foreground);
- Assert.Equal (backgroundColor, attribute.Background);
- }
- [Fact]
- public void MakeColorAndColorNames_ForegroundAndBackgroundShouldMatchInput ()
- {
- // Arrange
- var foregroundColor = new Color (255, 0, 0);
- var backgroundColorName = ColorNames.White;
- // Act
- var attribute = new Attribute (foregroundColor, backgroundColorName);
- // Assert
- Assert.Equal (foregroundColor, attribute.Foreground);
- Assert.Equal ((Color)backgroundColorName, attribute.Background);
- }
- [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 = new Attribute (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 = new Attribute (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 = new Attribute (fg, bg);
- Assert.False (attr.Initialized);
- Assert.Equal (fg, attr.Foreground);
- Assert.Equal (bg, attr.Background);
- }
- [Fact]
- public void Get_Asserts_NoDriver ()
- {
- Assert.Throws<InvalidOperationException> (() => 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));
- }
- [Fact]
- public void EqualityOperator_ShouldReturnTrueForEqualAttributes ()
- {
- // Arrange
- var attribute1 = new Attribute (Color.Red, Color.Black);
- var attribute2 = new Attribute (Color.Red, Color.Black);
- // Act & Assert
- Assert.True (attribute1 == attribute2);
- }
- [Fact]
- public void EqualityOperator_ShouldReturnFalseForDifferentAttributes ()
- {
- // Arrange
- var attribute1 = new Attribute (Color.Red, Color.Black);
- var attribute2 = new Attribute (Color.Blue, Color.Black);
- // Act & Assert
- Assert.False (attribute1 == attribute2);
- }
- [Fact]
- public void InequalityOperator_ShouldReturnTrueForDifferentAttributes ()
- {
- // Arrange
- var attribute1 = new Attribute (Color.Red, Color.Black);
- var attribute2 = new Attribute (Color.Blue, Color.Black);
- // Act & Assert
- Assert.True (attribute1 != attribute2);
- }
- [Fact]
- public void InequalityOperator_ShouldReturnFalseForEqualAttributes ()
- {
- // Arrange
- var attribute1 = new Attribute (Color.Red, Color.Black);
- var attribute2 = new Attribute (Color.Red, Color.Black);
- // Act & Assert
- Assert.False (attribute1 != attribute2);
- }
- [Fact]
- public void ToString_ShouldReturnFormattedStringWithForegroundAndBackground ()
- {
- // Arrange
- var foregroundColor = new Color (0, 0, 255);
- var backgroundColor = new Color (255, 255, 255);
- var expectedString = $"{foregroundColor},{backgroundColor}";
- // Act
- var attribute = new Attribute (foregroundColor, backgroundColor);
- var attributeString = attribute.ToString ();
- // Assert
- Assert.Equal (expectedString, attributeString);
- }
- }
|