Dim.FuncTests.cs 2.3 KB

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