AttributeTests.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  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. driver.End ();
  36. Application.Shutdown ();
  37. }
  38. [Fact]
  39. public void Implicit_Assign ()
  40. {
  41. var driver = new FakeDriver ();
  42. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  43. driver.Init (() => { });
  44. var attr = new Attribute ();
  45. var value = 42;
  46. var fg = new Color ();
  47. fg = Color.Red;
  48. var bg = new Color ();
  49. bg = Color.Blue;
  50. // Test converstion to int
  51. attr = new Attribute (value, fg, bg);
  52. int value_implicit = (int)attr.Value;
  53. Assert.Equal (value, value_implicit);
  54. // Test converstion from int
  55. attr = value;
  56. Assert.Equal (value, attr.Value);
  57. driver.End ();
  58. Application.Shutdown ();
  59. }
  60. [Fact]
  61. public void Make_Asserts_IfNotInit ()
  62. {
  63. var fg = new Color ();
  64. fg = Color.Red;
  65. var bg = new Color ();
  66. bg = Color.Blue;
  67. Assert.Throws<InvalidOperationException> (() => Attribute.Make (fg, bg));
  68. }
  69. [Fact]
  70. public void Make_Creates ()
  71. {
  72. var driver = new FakeDriver ();
  73. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  74. driver.Init (() => { });
  75. var fg = new Color ();
  76. fg = Color.Red;
  77. var bg = new Color ();
  78. bg = Color.Blue;
  79. var attr = Attribute.Make (fg, bg);
  80. Assert.Equal (fg, attr.Foreground);
  81. Assert.Equal (bg, attr.Background);
  82. driver.End ();
  83. Application.Shutdown ();
  84. }
  85. [Fact]
  86. public void Get_Asserts_IfNotInit ()
  87. {
  88. Assert.Throws<InvalidOperationException> (() => Attribute.Get ());
  89. }
  90. [Fact]
  91. public void Get_Gets ()
  92. {
  93. var driver = new FakeDriver ();
  94. Application.Init (driver, new FakeMainLoop (() => FakeConsole.ReadKey (true)));
  95. driver.Init (() => { });
  96. var value = 42;
  97. var fg = new Color ();
  98. fg = Color.Red;
  99. var bg = new Color ();
  100. bg = Color.Blue;
  101. var attr = new Attribute (value, fg, bg);
  102. driver.SetAttribute (attr);
  103. var ret_attr = Attribute.Get ();
  104. Assert.Equal (value, ret_attr.Value);
  105. Assert.Equal (fg, ret_attr.Foreground);
  106. Assert.Equal (bg, ret_attr.Background);
  107. driver.End ();
  108. Application.Shutdown ();
  109. }
  110. }
  111. }