Browse Source

Add TestLineCanvas_PositiveLocation

tznind 2 years ago
parent
commit
319fb8a3d4
2 changed files with 45 additions and 5 deletions
  1. 6 3
      Terminal.Gui/Core/Graphs/LineCanvas.cs
  2. 39 2
      UnitTests/Core/LineCanvasTests.cs

+ 6 - 3
Terminal.Gui/Core/Graphs/LineCanvas.cs

@@ -89,12 +89,15 @@ namespace Terminal.Gui.Graphs {
 		{
 			var runes = GenerateImage (bounds);
 
-			for (int y = bounds.Y; y < bounds.Height; y++) {
-				for (int x = bounds.X; x < bounds.Width; x++) {
+			var runeRows = runes.GetLength (0);
+			var runeCols = runes.GetLength (1);
+
+			for (int y = 0; y < runeRows; y++) {
+				for (int x = 0; x < runeCols; x++) {
 					var rune = runes [y, x];
 
 					if (rune.HasValue) {
-						view.AddRune (x, y, rune.Value);
+						view.AddRune (bounds.X + x, bounds.Y + y, rune.Value);
 					}
 				}
 			}

+ 39 - 2
UnitTests/Core/LineCanvasTests.cs

@@ -284,6 +284,35 @@ namespace Terminal.Gui.CoreTests {
 			TestHelpers.AssertDriverContentsAre (looksLike, output);
 		}
 
+		[Fact, AutoInitShutdown]
+		public void TestLineCanvas_PositiveLocation ()
+		{
+			var x = 1;
+			var y = 1;
+			var v = GetCanvas (out var canvas, x, y);
+
+			// outer box
+			canvas.AddLine (new Point (x, y), 9, Orientation.Horizontal, BorderStyle.Single);
+			canvas.AddLine (new Point (x + 9, y + 0), 4, Orientation.Vertical, BorderStyle.Single);
+			canvas.AddLine (new Point (x + 9, y + 4), -9, Orientation.Horizontal, BorderStyle.Single);
+			canvas.AddLine (new Point (x + 0, y + 4), -4, Orientation.Vertical, BorderStyle.Single);
+
+
+			canvas.AddLine (new Point (x + 5, y + 0), 4, Orientation.Vertical, BorderStyle.Single);
+			canvas.AddLine (new Point (x + 0, y + 2), 9, Orientation.Horizontal, BorderStyle.Single);
+
+			v.Redraw (v.Bounds);
+
+			string looksLike =
+@"    
+
+ ┌────┬───┐
+ │    │   │
+ ├────┼───┤
+ │    │   │
+ └────┴───┘";
+			TestHelpers.AssertDriverContentsAre (looksLike, output);
+		}
 
 		[Theory, AutoInitShutdown]
 		[InlineData (0, 0, @"
@@ -330,12 +359,20 @@ namespace Terminal.Gui.CoreTests {
 		}
 
 
-		private View GetCanvas (out LineCanvas canvas)
+		/// <summary>
+		/// Creates a new <see cref="View"/> into which a <see cref="LineCanvas"/> is rendered
+		/// at <see cref="View.DrawContentComplete"/> time.
+		/// </summary>
+		/// <param name="canvas">The <see cref="LineCanvas"/> you can draw into.</param>
+		/// <param name="offsetX">How far to offset the <see cref="View.Bounds"/> in X</param>
+		/// <param name="offsetX">How far to offset the <see cref="View.Bounds"/> in Y</param>
+		/// <returns></returns>
+		private View GetCanvas (out LineCanvas canvas, int offsetX = 0, int offsetY = 0)
 		{
 			var v = new View {
 				Width = 10,
 				Height = 5,
-				Bounds = new Rect (0, 0, 10, 5)
+				Bounds = new Rect (offsetX, offsetY, 10, 5)
 			};
 
 			var canvasCopy = canvas = new LineCanvas ();