diff --git a/Terminal.Gui/Views/TextView.cs b/Terminal.Gui/Views/TextView.cs index b2444c75e..c16e28580 100644 --- a/Terminal.Gui/Views/TextView.cs +++ b/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> (); - } catch { - return false; - } + + FilePath = null; + lines = new List> (); return true; } @@ -1241,10 +1232,8 @@ namespace Terminal.Gui { /// Path to the file to load. 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 { /// true, if stream was closed, false otherwise. 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 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; } diff --git a/UnitTests/TextViewTests.cs b/UnitTests/TextViewTests.cs index abb3c609d..3b5b406e0 100644 --- a/UnitTests/TextViewTests.cs +++ b/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 (() => tv.LoadFile (null)); + } + + [Fact] + public void LoadFile_Throws_If_File_Is_Empty () + { + var tv = new TextView (); + Assert.Throws (() => tv.LoadFile ("")); + } + + [Fact] + public void CloseFile_Throws_If_FilePath_Is_Null () + { + var tv = new TextView (); + Assert.Throws (() => tv.CloseFile ()); + } + } }