ClipRegionTests.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #nullable enable
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace DriverTests;
  5. public class ClipRegionTests (ITestOutputHelper output) : FakeDriverBase
  6. {
  7. private readonly ITestOutputHelper _output = output;
  8. [Fact]
  9. public void AddRune_Is_Clipped ()
  10. {
  11. IDriver? driver = CreateFakeDriver ();
  12. driver.Move (0, 0);
  13. driver.AddRune ('x');
  14. Assert.Equal ("x", driver.Contents! [0, 0].Grapheme);
  15. driver.Move (5, 5);
  16. driver.AddRune ('x');
  17. Assert.Equal ("x", driver.Contents [5, 5].Grapheme);
  18. // Clear the contents
  19. driver.FillRect (new Rectangle (0, 0, driver.Rows, driver.Cols), ' ');
  20. Assert.Equal (" ", driver.Contents [0, 0].Grapheme);
  21. // Setup the region with a single rectangle, fill screen with 'x'
  22. driver.Clip = new (new Rectangle (5, 5, 5, 5));
  23. driver.FillRect (new Rectangle (0, 0, driver.Rows, driver.Cols), 'x');
  24. Assert.Equal (" ", driver.Contents [0, 0].Grapheme);
  25. Assert.Equal (" ", driver.Contents [4, 9].Grapheme);
  26. Assert.Equal ("x", driver.Contents [5, 5].Grapheme);
  27. Assert.Equal ("x", driver.Contents [9, 9].Grapheme);
  28. Assert.Equal (" ", driver.Contents [10, 10].Grapheme);
  29. }
  30. [Fact]
  31. public void Clip_Set_To_Empty_AllInvalid ()
  32. {
  33. IDriver? driver = CreateFakeDriver ();
  34. // Define a clip rectangle
  35. driver.Clip = new (Rectangle.Empty);
  36. // negative
  37. Assert.False (driver.IsValidLocation (null!, 4, 5));
  38. Assert.False (driver.IsValidLocation (null!, 5, 4));
  39. Assert.False (driver.IsValidLocation (null!, 10, 9));
  40. Assert.False (driver.IsValidLocation (null!, 9, 10));
  41. Assert.False (driver.IsValidLocation (null!, -1, 0));
  42. Assert.False (driver.IsValidLocation (null!, 0, -1));
  43. Assert.False (driver.IsValidLocation (null!, -1, -1));
  44. Assert.False (driver.IsValidLocation (null!, driver.Cols, driver.Rows - 1));
  45. Assert.False (driver.IsValidLocation (null!, driver.Cols, driver.Rows - 1));
  46. Assert.False (driver.IsValidLocation (null!, driver.Cols, driver.Rows));
  47. }
  48. [Fact]
  49. public void IsValidLocation ()
  50. {
  51. IDriver? driver = CreateFakeDriver ();
  52. driver.Rows = 10;
  53. driver.Cols = 10;
  54. // positive
  55. Assert.True (driver.IsValidLocation (null!, 0, 0));
  56. Assert.True (driver.IsValidLocation (null!, 1, 1));
  57. Assert.True (driver.IsValidLocation (null!, driver.Cols - 1, driver.Rows - 1));
  58. // negative
  59. Assert.False (driver.IsValidLocation (null!, -1, 0));
  60. Assert.False (driver.IsValidLocation (null!, 0, -1));
  61. Assert.False (driver.IsValidLocation (null!, -1, -1));
  62. Assert.False (driver.IsValidLocation (null!, driver.Cols, driver.Rows - 1));
  63. Assert.False (driver.IsValidLocation (null!, driver.Cols, driver.Rows - 1));
  64. Assert.False (driver.IsValidLocation (null!, driver.Cols, driver.Rows));
  65. // Define a clip rectangle
  66. driver.Clip = new (new Rectangle (5, 5, 5, 5));
  67. // positive
  68. Assert.True (driver.IsValidLocation (null!, 5, 5));
  69. Assert.True (driver.IsValidLocation (null!, 9, 9));
  70. // negative
  71. Assert.False (driver.IsValidLocation (null!, 4, 5));
  72. Assert.False (driver.IsValidLocation (null!, 5, 4));
  73. Assert.False (driver.IsValidLocation (null!, 10, 9));
  74. Assert.False (driver.IsValidLocation (null!, 9, 10));
  75. Assert.False (driver.IsValidLocation (null!, -1, 0));
  76. Assert.False (driver.IsValidLocation (null!, 0, -1));
  77. Assert.False (driver.IsValidLocation (null!, -1, -1));
  78. Assert.False (driver.IsValidLocation (null!, driver.Cols, driver.Rows - 1));
  79. Assert.False (driver.IsValidLocation (null!, driver.Cols, driver.Rows - 1));
  80. Assert.False (driver.IsValidLocation (null!, driver.Cols, driver.Rows));
  81. }
  82. }