Преглед изворни кода

Add ObjectDisposedException testing for View.Title and fixes unit test.

BDisp пре 1 година
родитељ
комит
c471cf1aeb
2 измењених фајлова са 38 додато и 9 уклоњено
  1. 16 1
      Terminal.Gui/View/View.cs
  2. 22 8
      UnitTests/Application/ApplicationTests.cs

+ 16 - 1
Terminal.Gui/View/View.cs

@@ -427,9 +427,24 @@ public partial class View : Responder, ISupportInitializeNotification
     /// <value>The title.</value>
     /// <value>The title.</value>
     public string Title
     public string Title
     {
     {
-        get => _title;
+        get
+        {
+#if DEBUG_IDISPOSABLE
+            if (WasDisposed)
+            {
+                throw new ObjectDisposedException (GetType ().FullName);
+            }
+#endif
+            return _title;
+        }
         set
         set
         {
         {
+#if DEBUG_IDISPOSABLE
+            if (WasDisposed)
+            {
+                throw new ObjectDisposedException (GetType ().FullName);
+            }
+#endif
             if (value == _title)
             if (value == _title)
             {
             {
                 return;
                 return;

+ 22 - 8
UnitTests/Application/ApplicationTests.cs

@@ -879,37 +879,51 @@ public class ApplicationTests
                                               );
                                               );
 
 
         Application.End (rs);
         Application.End (rs);
+        w.Dispose ();
         Application.Shutdown ();
         Application.Shutdown ();
     }
     }
 
 
     [Fact]
     [Fact]
-    public void End_Disposing_Correctly ()
+    public void End_Does_Not_Dispose ()
     {
     {
         Init ();
         Init ();
 
 
-        var top = Application.Top;
+        var top = new Toplevel ();
 
 
         Window w = new ();
         Window w = new ();
         w.Ready += (s, e) => Application.RequestStop (); // Causes `End` to be called
         w.Ready += (s, e) => Application.RequestStop (); // Causes `End` to be called
         Application.Run(w);
         Application.Run(w);
 
 
 #if DEBUG_IDISPOSABLE
 #if DEBUG_IDISPOSABLE
-        Assert.True (w.WasDisposed);
+        Assert.False (w.WasDisposed);
 #endif
 #endif
 
 
         Assert.NotNull (w);
         Assert.NotNull (w);
-        Assert.Equal (string.Empty, w.Title); // Invalid - w has been disposed -> Valid - w isn't Application.Top but the original created by Init
+        Assert.Equal (string.Empty, w.Title); // Valid - w has not been disposed. The user may want to run it again
         Assert.NotNull (Application.Top);
         Assert.NotNull (Application.Top);
-        Assert.NotEqual(w, Application.Top);
-        Assert.Equal(top, Application.Top);
+        Assert.Equal(w, Application.Top);
+        Assert.NotEqual(top, Application.Top);
         Assert.Null (Application.Current);
         Assert.Null (Application.Current);
 
 
-        var exception = Record.Exception (()  => Application.Run(w)); // Invalid - w has been disposed. Run it in debug mode will throw, otherwise the user may want to run it again
+        Application.Run(w); // Valid - w has not been disposed.
+
+#if DEBUG_IDISPOSABLE
+        Assert.False (w.WasDisposed);
+        var exception = Record.Exception (() => Application.Shutdown()); // Invalid - w has not been disposed.
         Assert.NotNull (exception);
         Assert.NotNull (exception);
 
 
+        w.Dispose ();
+        Assert.True (w.WasDisposed);
+        exception = Record.Exception (() => Application.Run (w)); // Invalid - w has been disposed. Run it in debug mode will throw, otherwise the user may want to run it again
+        Assert.NotNull (exception);
+
+        exception = Record.Exception (() => Assert.Equal (string.Empty, w.Title)); // Invalid - w has been disposed and cannot be accessed
+        Assert.NotNull (exception);
+        exception = Record.Exception (() => w.Title = "NewTitle"); // Invalid - w has been disposed and cannot be accessed
+        Assert.NotNull (exception);
+#endif
         Application.Shutdown ();
         Application.Shutdown ();
         Assert.NotNull (w);
         Assert.NotNull (w);
-        Assert.Equal (string.Empty, w.Title); // Invalid - w has been disposed -> Valid - w isn't Application.Top but the original created by Init
         Assert.Null (Application.Current);
         Assert.Null (Application.Current);
         Assert.NotNull (top);
         Assert.NotNull (top);
         Assert.Null (Application.Top);
         Assert.Null (Application.Top);