AttributeTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. using Xunit;
  6. // Alias Console to MockConsole so we don't accidentally use Console
  7. using Console = Terminal.Gui.FakeConsole;
  8. namespace Terminal.Gui.ConsoleDrivers {
  9. public class AttributeTests {
  10. [Fact]
  11. public void Constuctors_Constuct ()
  12. {
  13. var driver = new FakeDriver ();
  14. Application.Init (driver);
  15. driver.Init (() => { });
  16. // Test parameterless constructor
  17. var attr = new Attribute ();
  18. Assert.Equal (default (int), attr.Value);
  19. Assert.Equal (default (Color), attr.Foreground);
  20. Assert.Equal (default (Color), attr.Background);
  21. // Test value, foreground, background
  22. var value = 42;
  23. var fg = new Color ();
  24. fg = Color.Red;
  25. var bg = new Color ();
  26. bg = Color.Blue;
  27. attr = new Attribute (value, fg, bg);
  28. Assert.Equal (value, attr.Value);
  29. Assert.Equal (fg, attr.Foreground);
  30. Assert.Equal (bg, attr.Background);
  31. // value, foreground, background
  32. attr = new Attribute (fg, bg);
  33. Assert.Equal (fg, attr.Foreground);
  34. Assert.Equal (bg, attr.Background);
  35. attr = new Attribute (fg);
  36. Assert.Equal (fg, attr.Foreground);
  37. Assert.Equal (fg, attr.Background);
  38. attr = new Attribute (bg);
  39. Assert.Equal (bg, attr.Foreground);
  40. Assert.Equal (bg, attr.Background);
  41. driver.End ();
  42. Application.Shutdown ();
  43. }
  44. [Fact]
  45. public void Implicit_Assign ()
  46. {
  47. var driver = new FakeDriver ();
  48. Application.Init (driver);
  49. driver.Init (() => { });
  50. var attr = new Attribute ();
  51. var value = 42;
  52. var fg = new Color ();
  53. fg = Color.Red;
  54. var bg = new Color ();
  55. bg = Color.Blue;
  56. // Test conversion to int
  57. attr = new Attribute (value, fg, bg);
  58. int value_implicit = (int)attr.Value;
  59. Assert.Equal (value, value_implicit);
  60. // Test conversion from int
  61. attr = value;
  62. Assert.Equal (value, attr.Value);
  63. driver.End ();
  64. Application.Shutdown ();
  65. }
  66. [Fact]
  67. public void Make_Asserts_IfNotInit ()
  68. {
  69. var fg = new Color ();
  70. fg = Color.Red;
  71. var bg = new Color ();
  72. bg = Color.Blue;
  73. Assert.Throws<InvalidOperationException> (() => Attribute.Make (fg, bg));
  74. }
  75. [Fact]
  76. public void Make_Creates ()
  77. {
  78. var driver = new FakeDriver ();
  79. Application.Init (driver);
  80. driver.Init (() => { });
  81. var fg = new Color ();
  82. fg = Color.Red;
  83. var bg = new Color ();
  84. bg = Color.Blue;
  85. var attr = Attribute.Make (fg, bg);
  86. Assert.Equal (fg, attr.Foreground);
  87. Assert.Equal (bg, attr.Background);
  88. driver.End ();
  89. Application.Shutdown ();
  90. }
  91. [Fact]
  92. public void Get_Asserts_IfNotInit ()
  93. {
  94. Assert.Throws<InvalidOperationException> (() => Attribute.Get ());
  95. }
  96. [Fact]
  97. public void Get_Gets ()
  98. {
  99. var driver = new FakeDriver ();
  100. Application.Init (driver);
  101. driver.Init (() => { });
  102. var value = 42;
  103. var fg = new Color ();
  104. fg = Color.Red;
  105. var bg = new Color ();
  106. bg = Color.Blue;
  107. var attr = new Attribute (value, fg, bg);
  108. driver.SetAttribute (attr);
  109. var ret_attr = Attribute.Get ();
  110. Assert.Equal (value, ret_attr.Value);
  111. Assert.Equal (fg, ret_attr.Foreground);
  112. Assert.Equal (bg, ret_attr.Background);
  113. driver.End ();
  114. Application.Shutdown ();
  115. }
  116. [Fact]
  117. [AutoInitShutdown]
  118. public void GetColors_Based_On_Value ()
  119. {
  120. var driver = Application.Driver;
  121. var attrValue = new Attribute (Color.Red, Color.Green).Value;
  122. driver.GetColors (attrValue, out Color fg, out Color bg);
  123. Assert.Equal (Color.Red, fg);
  124. Assert.Equal (Color.Green, bg);
  125. }
  126. }
  127. }