Pos.FuncTests.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #nullable disable
  2. using Xunit.Abstractions;
  3. namespace ViewBaseTests.Layout;
  4. public class PosFuncTests (ITestOutputHelper output)
  5. {
  6. private readonly ITestOutputHelper _output = output;
  7. [Fact]
  8. public void PosFunc_Equal ()
  9. {
  10. Func<View, int> f1 = _ => 0;
  11. Func<View, int> f2 = _ => 0;
  12. Pos pos1 = Pos.Func (f1);
  13. Pos pos2 = Pos.Func (f1);
  14. Assert.Equal (pos1, pos2);
  15. f2 = _ => 1;
  16. pos2 = Pos.Func (f2);
  17. Assert.NotEqual (pos1, pos2);
  18. }
  19. [Fact]
  20. public void PosFunc_SetsValue ()
  21. {
  22. var text = "Test";
  23. Pos pos = Pos.Func (_ => text.Length);
  24. Assert.Equal ("PosFunc(4)", pos.ToString ());
  25. text = "New Test";
  26. Assert.Equal ("PosFunc(8)", pos.ToString ());
  27. text = "";
  28. Assert.Equal ("PosFunc(0)", pos.ToString ());
  29. }
  30. [Fact]
  31. public void PosFunc_Calculate_ReturnsCorrectValue ()
  32. {
  33. var pos = new PosFunc (_ => 10);
  34. int result = pos.Calculate (0, 100, null, Dimension.None);
  35. Assert.Equal (10, result);
  36. }
  37. [Fact]
  38. public void PosFunc_View_Equal ()
  39. {
  40. Func<View, int> f1 = v => v.Frame.X;
  41. Func<View, int> f2 = v => v.Frame.X;
  42. View view1 = new ();
  43. View view2 = new ();
  44. Pos pos1 = Pos.Func (f1, view1);
  45. Pos pos2 = Pos.Func (f1, view1);
  46. Assert.Equal (pos1, pos2);
  47. f2 = _ => 1;
  48. pos2 = Pos.Func (f2, view2);
  49. Assert.NotEqual (pos1, pos2);
  50. view2.X = 1;
  51. Assert.NotEqual (pos1, pos2);
  52. Assert.Equal (1, f2 (view2));
  53. }
  54. [Fact]
  55. public void PosFunc_View_SetsValue ()
  56. {
  57. View view = new () { Text = "Test" };
  58. Pos pos = Pos.Func (v => v.Text.Length, view);
  59. Assert.Equal ("PosFunc(4)", pos.ToString ());
  60. view.Text = "New Test";
  61. Assert.Equal ("PosFunc(8)", pos.ToString ());
  62. view.Text = "";
  63. Assert.Equal ("PosFunc(0)", pos.ToString ());
  64. }
  65. [Fact]
  66. public void PosFunc_View_Calculate_ReturnsCorrectValue ()
  67. {
  68. View view = new () { X = 10 };
  69. var pos = new PosFunc (v => v.Frame.X, view);
  70. int result = pos.Calculate (0, 100, view, Dimension.None);
  71. Assert.Equal (10, result);
  72. }
  73. }