Dim.FuncTests.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #nullable disable
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.ViewBase.Dim;
  4. namespace ViewBaseTests.Layout;
  5. public class DimFuncTests (ITestOutputHelper output)
  6. {
  7. private readonly ITestOutputHelper _output = output;
  8. [Fact]
  9. public void DimFunc_Equal ()
  10. {
  11. Func<View, int> f1 = _ => 0;
  12. Func<View, int> f2 = _ => 0;
  13. Dim dim1 = Func (f1);
  14. Dim dim2 = Func (f1);
  15. Assert.Equal (dim1, dim2);
  16. dim2 = Func (f2);
  17. Assert.NotEqual (dim1, dim2);
  18. f2 = _ => 1;
  19. dim2 = Func (f2);
  20. Assert.NotEqual (dim1, dim2);
  21. }
  22. [Fact]
  23. public void DimFunc_SetsValue ()
  24. {
  25. var text = "Test";
  26. Dim dim = Func (_ => text.Length);
  27. Assert.Equal ("DimFunc(4)", dim.ToString ());
  28. text = "New Test";
  29. Assert.Equal ("DimFunc(8)", dim.ToString ());
  30. text = "";
  31. Assert.Equal ("DimFunc(0)", dim.ToString ());
  32. }
  33. [Fact]
  34. public void DimFunc_Calculate_ReturnsCorrectValue ()
  35. {
  36. var dim = new DimFunc (_ => 10);
  37. int result = dim.Calculate (0, 100, null, Dimension.None);
  38. Assert.Equal (10, result);
  39. }
  40. [Fact]
  41. public void DimFunc_View_Equal ()
  42. {
  43. Func<View, int> f1 = v => v.Frame.Width;
  44. Func<View, int> f2 = v => v.Frame.Width;
  45. View view1 = new ();
  46. View view2 = new ();
  47. Dim dim1 = Func (f1, view1);
  48. Dim dim2 = Func (f1, view1);
  49. Assert.Equal (dim1, dim2);
  50. dim2 = Func (f2, view2);
  51. Assert.NotEqual (dim1, dim2);
  52. view2.Width = 1;
  53. Assert.NotEqual (dim1, dim2);
  54. Assert.Equal (1, f2 (view2));
  55. }
  56. [Fact]
  57. public void DimFunc_View_SetsValue ()
  58. {
  59. View view = new () { Text = "Test" };
  60. Dim dim = Func (v => v.Text.Length, view);
  61. Assert.Equal ("DimFunc(4)", dim.ToString ());
  62. view.Text = "New Test";
  63. Assert.Equal ("DimFunc(8)", dim.ToString ());
  64. view.Text = "";
  65. Assert.Equal ("DimFunc(0)", dim.ToString ());
  66. }
  67. [Fact]
  68. public void DimFunc_View_Calculate_ReturnsCorrectValue ()
  69. {
  70. View view = new () { Width = 10 };
  71. var dim = new DimFunc (v => v.Frame.Width, view);
  72. int result = dim.Calculate (0, 100, view, Dimension.None);
  73. Assert.Equal (10, result);
  74. }
  75. }