2
0

AttributeTests.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.DriverTests {
  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_Creates ()
  68. {
  69. var driver = new FakeDriver ();
  70. Application.Init (driver);
  71. driver.Init (() => { });
  72. var fg = new Color ();
  73. fg = Color.Red;
  74. var bg = new Color ();
  75. bg = Color.Blue;
  76. var attr = Attribute.Make (fg, bg);
  77. Assert.Equal (fg, attr.Foreground);
  78. Assert.Equal (bg, attr.Background);
  79. driver.End ();
  80. Application.Shutdown ();
  81. }
  82. [Fact]
  83. public void Get_Asserts_IfNotInit ()
  84. {
  85. Assert.Throws<InvalidOperationException> (() => Attribute.Get ());
  86. }
  87. [Fact]
  88. public void Get_Gets ()
  89. {
  90. var driver = new FakeDriver ();
  91. Application.Init (driver);
  92. driver.Init (() => { });
  93. var value = 42;
  94. var fg = new Color ();
  95. fg = Color.Red;
  96. var bg = new Color ();
  97. bg = Color.Blue;
  98. var attr = new Attribute (value, fg, bg);
  99. driver.SetAttribute (attr);
  100. var ret_attr = Attribute.Get ();
  101. Assert.Equal (value, ret_attr.Value);
  102. Assert.Equal (fg, ret_attr.Foreground);
  103. Assert.Equal (bg, ret_attr.Background);
  104. driver.End ();
  105. Application.Shutdown ();
  106. }
  107. [Fact]
  108. [AutoInitShutdown]
  109. public void GetColors_Based_On_Value ()
  110. {
  111. var driver = Application.Driver;
  112. var attrValue = new Attribute (Color.Red, Color.Green).Value;
  113. driver.GetColors (attrValue, out Color fg, out Color bg);
  114. Assert.Equal (Color.Red, fg);
  115. Assert.Equal (Color.Green, bg);
  116. }
  117. }
  118. }