ClipRegionTests.cs 3.9 KB

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