浏览代码

Fixes #1314. TextView now exposes file exceptions from callers.

BDisp 4 年之前
父节点
当前提交
3da689ff31
共有 2 个文件被更改,包括 32 次插入21 次删除
  1. 9 20
      Terminal.Gui/Views/TextView.cs
  2. 23 1
      UnitTests/TextViewTests.cs

+ 9 - 20
Terminal.Gui/Views/TextView.cs

@@ -38,14 +38,8 @@ namespace Terminal.Gui {
 
 		public bool LoadFile (string file)
 		{
-			if (file == null)
-				throw new ArgumentNullException (nameof (file));
-			try {
-				FilePath = file;
-				var stream = File.OpenRead (file);
-			} catch {
-				return false;
-			}
+			FilePath = file ?? throw new ArgumentNullException (nameof (file));
+
 			LoadStream (File.OpenRead (file));
 			return true;
 		}
@@ -54,12 +48,9 @@ namespace Terminal.Gui {
 		{
 			if (FilePath == null)
 				throw new ArgumentNullException (nameof (FilePath));
-			try {
-				FilePath = null;
-				lines = new List<List<Rune>> ();
-			} catch {
-				return false;
-			}
+
+			FilePath = null;
+			lines = new List<List<Rune>> ();
 			return true;
 		}
 
@@ -1241,10 +1232,8 @@ namespace Terminal.Gui {
 		/// <param name="path">Path to the file to load.</param>
 		public bool LoadFile (string path)
 		{
-			if (path == null)
-				throw new ArgumentNullException (nameof (path));
-			ResetPosition ();
 			var res = model.LoadFile (path);
+			ResetPosition ();
 			SetNeedsDisplay ();
 			return res;
 		}
@@ -1269,8 +1258,8 @@ namespace Terminal.Gui {
 		/// <returns><c>true</c>, if stream was closed, <c>false</c> otherwise.</returns>
 		public bool CloseFile ()
 		{
-			ResetPosition ();
 			var res = model.CloseFile ();
+			ResetPosition ();
 			SetNeedsDisplay ();
 			return res;
 		}
@@ -1701,7 +1690,7 @@ namespace Terminal.Gui {
 						&& HasFocus && idxCol < lineRuneCount) {
 						ColorUsed (line, idxCol);
 					} else {
-						ColorNormal (line,idxCol);
+						ColorNormal (line, idxCol);
 					}
 
 					if (rune == '\t' && TabWidth > 0) {
@@ -1974,7 +1963,7 @@ namespace Terminal.Gui {
 			List<Rune> rest;
 
 			// if the user presses Left (without any control keys) and they are at the start of the text
-			if(kb.Key == Key.CursorLeft && currentColumn == 0 && currentRow == 0) {
+			if (kb.Key == Key.CursorLeft && currentColumn == 0 && currentRow == 0) {
 				// do not respond (this lets the key press fall through to navigation system - which usually changes focus backward)
 				return false;
 			}

+ 23 - 1
UnitTests/TextViewTests.cs

@@ -7,7 +7,7 @@ namespace Terminal.Gui.Views {
 	public class TextViewTests {
 		private static TextView _textView;
 
-		// This class enables test functions annoated with the [InitShutdown] attribute
+		// This class enables test functions annotated with the [InitShutdown] attribute
 		// to have a function called before the test function is called and after.
 		// 
 		// This is necessary because a) Application is a singleton and Init/Shutdown must be called
@@ -1751,5 +1751,27 @@ namespace Terminal.Gui.Views {
 
 			return col;
 		}
+
+		[Fact]
+		public void LoadFile_Throws_If_File_Is_Null ()
+		{
+			var tv = new TextView ();
+			Assert.Throws<ArgumentNullException> (() => tv.LoadFile (null));
+		}
+
+		[Fact]
+		public void LoadFile_Throws_If_File_Is_Empty ()
+		{
+			var tv = new TextView ();
+			Assert.Throws<ArgumentException> (() => tv.LoadFile (""));
+		}
+
+		[Fact]
+		public void CloseFile_Throws_If_FilePath_Is_Null ()
+		{
+			var tv = new TextView ();
+			Assert.Throws<ArgumentNullException> (() => tv.CloseFile ());
+		}
+
 	}
 }