瀏覽代碼

Updated Rect tests

Tigger Kindel 2 年之前
父節點
當前提交
0c67ad99c5
共有 2 個文件被更改,包括 99 次插入16 次删除
  1. 15 16
      Terminal.Gui/Types/Rect.cs
  2. 84 0
      UnitTests/Types/RectTests.cs

+ 15 - 16
Terminal.Gui/Types/Rect.cs

@@ -1,5 +1,5 @@
 //
 //
-// System.Drawing.Rectangle.cs
+// Derived from System.Drawing.Rectangle.cs
 //
 //
 // Author:
 // Author:
 //   Mike Kestner ([email protected])
 //   Mike Kestner ([email protected])
@@ -9,7 +9,6 @@
 //
 //
 
 
 using System;
 using System;
-
 namespace Terminal.Gui
 namespace Terminal.Gui
 {
 {
 	/// <summary>
 	/// <summary>
@@ -80,12 +79,12 @@ namespace Terminal.Gui
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
-		///	Inflate Shared Method
+		///	Produces a new Rect by inflating an existing Rect by the specified coordinate values.
 		/// </summary>
 		/// </summary>
 		///
 		///
 		/// <remarks>
 		/// <remarks>
-		///	Produces a new Rectangle by inflating an existing 
-		///	Rectangle by the specified coordinate values.
+		///	Produces a new Rect by inflating an existing Rect by the specified coordinate values.
+		///     The rectangle is enlarged in both directions along an axis. 
 		/// </remarks>
 		/// </remarks>
 
 
 		public static Rect Inflate (Rect rect, int x, int y)
 		public static Rect Inflate (Rect rect, int x, int y)
@@ -96,32 +95,33 @@ namespace Terminal.Gui
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
-		///	Inflate Method
+		///	Inflates an existing Rect by the specified coordinate values.
 		/// </summary>
 		/// </summary>
 		///
 		///
 		/// <remarks>
 		/// <remarks>
-		///	Inflates the Rectangle by a specified width and height.
+		///	This method enlarges this rectangle, not a copy of it. The rectangle is enlarged in both directions along an axis. 
 		/// </remarks>
 		/// </remarks>
 
 
 		public void Inflate (int width, int height)
 		public void Inflate (int width, int height)
 		{
 		{
-			Inflate (new Size (width, height));
+			// Set dims first so we don't lose the original values on exception
+			Width += width * 2;
+			Height += height * 2;
+
+			X -= width;
+			Y -= height;
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
-		///	Inflate Method
+		///	Inflates an existing Rect by the specified Sizwe.
 		/// </summary>
 		/// </summary>
 		///
 		///
 		/// <remarks>
 		/// <remarks>
-		///	Inflates the Rectangle by a specified Size.
+		///	This method enlarges this rectangle, not a copy of it. The rectangle is enlarged in both directions along an axis. 
 		/// </remarks>
 		/// </remarks>
-
 		public void Inflate (Size size)
 		public void Inflate (Size size)
 		{
 		{
-			X -= size.Width;
-			Y -= size.Height;
-			Width += size.Width * 2;
-			Height += size.Height * 2;
+			Inflate (size.Width, size.Height);
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
@@ -492,6 +492,5 @@ namespace Terminal.Gui
 			return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
 			return String.Format ("{{X={0},Y={1},Width={2},Height={3}}}",
 						 X, Y, Width, Height);
 						 X, Y, Width, Height);
 		}
 		}
-
 	}
 	}
 }
 }

+ 84 - 0
UnitTests/Types/RectTests.cs

@@ -148,5 +148,89 @@ namespace Terminal.Gui.TypeTests {
 			Assert.Equal (xCount, rect.Width);
 			Assert.Equal (xCount, rect.Width);
 			Assert.Equal (yxCount, rect.Height * rect.Width);
 			Assert.Equal (yxCount, rect.Height * rect.Width);
 		}
 		}
+
+
+		[Theory]
+		// Empty
+		[InlineData (
+			0, 0, 0, 0,
+			0, 0,
+			0, 0, 0, 0)]
+		[InlineData (
+			0, 0, 0, 0,
+			1, 0,
+			-1, 0, 2, 0)]
+		[InlineData (
+			0, 0, 0, 0,
+			0, 1,
+			0, -1, 0, 2)]
+		[InlineData (
+			0, 0, 0, 0,
+			1, 1,
+			-1, -1, 2, 2)]
+		[InlineData (
+			0, 0, 0, 0,
+			-1, -1,        // Throws
+			0, 0, 0, 0)]
+		// Zero location, Size of 1
+		[InlineData (
+			0, 0, 1, 1,
+			0, 0,
+			0, 0, 1, 1)]
+		[InlineData (
+			0, 0, 1, 1,
+			1, 0,
+			-1, 0, 3, 1)]
+		[InlineData (
+			0, 0, 1, 1,
+			0, 1,
+			0, -1, 1, 3)]
+		[InlineData (
+			0, 0, 1, 1,
+			1, 1,
+			-1, -1, 3, 3)]
+		// Positive location, Size of 1
+		[InlineData (
+			1, 1, 1, 1,
+			0, 0,
+			1, 1, 1, 1)]
+		[InlineData (
+			1, 1, 1, 1,
+			1, 0,
+			0, 1, 3, 1)]
+		[InlineData (
+			1, 1, 1, 1,
+			0, 1,
+			1, 0, 1, 3)]
+		[InlineData (
+			1, 1, 1, 1,
+			1, 1,
+			0, 0, 3, 3)]
+		public void Inflate (int x, int y, int width, int height, int inflateWidth, int inflateHeight, int expectedX, int exptectedY, int expectedWidth, int expectedHeight)
+		{
+			var rect = new Rect (x, y, width, height);
+
+			if (rect.Width + inflateWidth < 0 || rect.Height + inflateHeight < 0) {
+				Assert.Throws<ArgumentException> (() => rect.Inflate (inflateWidth, inflateHeight));
+			} else {
+				rect.Inflate (inflateWidth, inflateHeight);
+			}
+			Assert.Equal (expectedWidth, rect.Width);
+			Assert.Equal (expectedHeight, rect.Height);
+			Assert.Equal (expectedX, rect.X);
+			Assert.Equal (exptectedY, rect.Y);
+
+			// Use the other overload (Size)
+			rect = new Rect (x, y, width, height);
+			if (rect.Width + inflateWidth < 0 || rect.Height + inflateHeight < 0) {
+				Assert.Throws<ArgumentException> (() => rect.Inflate (new Size (inflateWidth, inflateHeight)));
+			} else {
+				rect.Inflate (new Size (inflateWidth, inflateHeight));
+			}
+			Assert.Equal (expectedWidth, rect.Width);
+			Assert.Equal (expectedHeight, rect.Height);
+			Assert.Equal (expectedX, rect.X);
+			Assert.Equal (exptectedY, rect.Y);
+		}
 	}
 	}
 }
 }