浏览代码

Added tests for RemoveTile and made it return the removed Tile for convenience

tznind 2 年之前
父节点
当前提交
ecdc8e396c
共有 2 个文件被更改,包括 100 次插入2 次删除
  1. 13 2
      Terminal.Gui/Views/TileView.cs
  2. 87 0
      UnitTests/TileViewTests.cs

+ 13 - 2
Terminal.Gui/Views/TileView.cs

@@ -153,13 +153,22 @@ namespace Terminal.Gui {
 
 
 			return toReturn;
 			return toReturn;
 		}
 		}
-		public void RemoveTile (int idx)
+
+		/// <summary>
+		/// Removes a <see cref="Tiles"/> at the provided <paramref name="idx"/> from
+		/// the view.  Returns the removed tile or null if already empty.
+		/// </summary>
+		/// <param name="idx"></param>
+		/// <returns></returns>
+		public Tile RemoveTile (int idx)
 		{
 		{
 			var oldTiles = Tiles.ToArray ();
 			var oldTiles = Tiles.ToArray ();
 			
 			
 			if (idx < 0 || idx >= oldTiles.Length) {
 			if (idx < 0 || idx >= oldTiles.Length) {
-				return;
+				return null;
 			}
 			}
+			
+			var removed = Tiles.ElementAt (idx);
 
 
 			RebuildForTileCount (oldTiles.Length - 1);
 			RebuildForTileCount (oldTiles.Length - 1);
 
 
@@ -178,6 +187,8 @@ namespace Terminal.Gui {
 			}
 			}
 			SetNeedsDisplay ();
 			SetNeedsDisplay ();
 			LayoutSubviews ();
 			LayoutSubviews ();
+
+			return removed;
 		}
 		}
 
 
 		///<summary>
 		///<summary>

+ 87 - 0
UnitTests/TileViewTests.cs

@@ -714,6 +714,93 @@ namespace UnitTests {
 			Assert.IsType<TextView>(subSplit.Tiles.ElementAt(1).View.Subviews.Single());
 			Assert.IsType<TextView>(subSplit.Tiles.ElementAt(1).View.Subviews.Single());
 		}
 		}
 
 
+		[Fact, AutoInitShutdown]
+		public void TestNestedContainer3RightAnd1Down_WithBorder_RemovingTiles ()
+		{
+			var tileView = GetNestedContainer3Right1Down (true);
+
+			tileView.Redraw (tileView.Bounds);
+
+			string looksLike =
+@"
+┌─────┬──────┬─────┐
+│11111│222222│33333│
+│11111│222222│33333│
+│11111│222222│33333│
+│11111│222222│33333│
+│11111│222222├─────┤
+│11111│222222│44444│
+│11111│222222│44444│
+│11111│222222│44444│
+└─────┴──────┴─────┘";
+			TestHelpers.AssertDriverContentsAre (looksLike, output);
+
+			var toRemove = tileView.Tiles.ElementAt(1);
+			var removed = tileView.RemoveTile (1);
+			Assert.Same(toRemove, removed);
+			Assert.DoesNotContain(removed,tileView.Tiles);
+
+
+			tileView.Redraw (tileView.Bounds);
+
+			looksLike =
+@"
+┌─────────┬────────┐
+│111111111│33333333│
+│111111111│33333333│
+│111111111│33333333│
+│111111111│33333333│
+│111111111├────────┤
+│111111111│44444444│
+│111111111│44444444│
+│111111111│44444444│
+└─────────┴────────┘";
+			TestHelpers.AssertDriverContentsAre (looksLike, output);
+
+			// cannot remove at this index because there is only one horizontal tile left
+			Assert.Null (tileView.RemoveTile (2));
+			tileView.RemoveTile (0);
+
+
+
+			tileView.Redraw (tileView.Bounds);
+
+			looksLike =
+@"
+┌──────────────────┐
+│333333333333333333│
+│333333333333333333│
+│333333333333333333│
+│333333333333333333│
+├──────────────────┤
+│444444444444444444│
+│444444444444444444│
+│444444444444444444│
+└──────────────────┘";
+			TestHelpers.AssertDriverContentsAre (looksLike, output);
+
+			Assert.NotNull(tileView.RemoveTile (0));
+
+
+			tileView.Redraw (tileView.Bounds);
+
+			looksLike =
+@"
+┌──────────────────┐
+│                  │
+│                  │
+│                  │
+│                  │
+│                  │
+│                  │
+│                  │
+│                  │
+└──────────────────┘";
+			TestHelpers.AssertDriverContentsAre (looksLike, output);
+
+			// cannot remove
+			Assert.Null (tileView.RemoveTile (0));
+		}
 
 
 		/// <summary>
 		/// <summary>
 		/// Creates a vertical orientation root container with left pane split into
 		/// Creates a vertical orientation root container with left pane split into