DimTests.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. using Xunit;
  8. // Alais Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui {
  11. public class DimTests {
  12. [Fact]
  13. public void New_Works ()
  14. {
  15. var dim = new Dim ();
  16. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  17. }
  18. [Fact]
  19. public void Sized_SetsValue ()
  20. {
  21. var dim = Dim.Sized (0);
  22. Assert.Equal ("Dim.Absolute(0)", dim.ToString ());
  23. int testVal = 5;
  24. dim = Dim.Sized (testVal);
  25. Assert.Equal ($"Dim.Absolute({testVal})", dim.ToString ());
  26. }
  27. // TODO: Other Dim.Sized tests (e.g. Equal?)
  28. [Fact]
  29. public void Width_SetsValue ()
  30. {
  31. var dim = Dim.Width (null);
  32. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  33. var testVal = Rect.Empty;
  34. testVal = Rect.Empty;
  35. dim = Dim.Width (new View (testVal));
  36. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  37. testVal = new Rect (1, 2, 3, 4);
  38. dim = Dim.Width (new View (testVal));
  39. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  40. }
  41. // TODO: Other Dim.Width tests (e.g. Equal?)
  42. [Fact]
  43. public void Height_SetsValue ()
  44. {
  45. var dim = Dim.Height (null);
  46. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  47. var testVal = Rect.Empty;
  48. testVal = Rect.Empty;
  49. dim = Dim.Height (new View (testVal));
  50. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  51. testVal = new Rect (1, 2, 3, 4);
  52. dim = Dim.Height (new View (testVal));
  53. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  54. }
  55. // TODO: Other Dim.Height tests (e.g. Equal?)
  56. [Fact]
  57. public void Fill_SetsValue ()
  58. {
  59. var testMargin = 0;
  60. var dim = Dim.Fill ();
  61. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString());
  62. testMargin = 0;
  63. dim = Dim.Fill (testMargin);
  64. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  65. testMargin = 5;
  66. dim = Dim.Fill (testMargin);
  67. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  68. }
  69. [Fact]
  70. public void Fill_Equal()
  71. {
  72. var margin1 = 0;
  73. var margin2 = 0;
  74. var dim1 = Dim.Fill (margin1);
  75. var dim2 = Dim.Fill (margin2);
  76. Assert.Equal (dim1, dim2);
  77. }
  78. [Fact]
  79. public void Percent_SetsValue ()
  80. {
  81. float f = 0;
  82. var dim = Dim.Percent (f);
  83. Assert.Equal ($"Dim.Factor({f/100:0.###})", dim.ToString ());
  84. f = 0.5F;
  85. dim = Dim.Percent (f);
  86. Assert.Equal ($"Dim.Factor({f/100:0.###})", dim.ToString ());
  87. f = 100;
  88. dim = Dim.Percent (f);
  89. Assert.Equal ($"Dim.Factor({f/100:0.###})", dim.ToString ());
  90. }
  91. // TODO: Other Dim.Percent tests (e.g. Equal?)
  92. [Fact]
  93. public void Percent_ThrowsOnIvalid()
  94. {
  95. var dim = Dim.Percent (0);
  96. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  97. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  98. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  99. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  100. }
  101. // TODO: Test operators
  102. }
  103. }