Browse Source

Attempting to figure out unit test fails and why tests are now slow. 2

Tig 10 months ago
parent
commit
8e4ddfab82

+ 7 - 0
UnitTests/Application/ApplicationTests.cs

@@ -10,6 +10,7 @@ public class ApplicationTests
     {
         _output = output;
         ConsoleDriver.RunningUnitTests = true;
+        ConfigurationManager.Locations = ConfigurationManager.ConfigLocations.None;
 
 #if DEBUG_IDISPOSABLE
         Responder.Instances.Clear ();
@@ -397,6 +398,12 @@ public class ApplicationTests
 #endif
     }
 
+    [Fact]
+    public void Shutdown_Alone_Does_Nothing ()
+    {
+        Application.Shutdown ();
+    }
+
     [Theory]
     [InlineData (typeof (FakeDriver))]
     [InlineData (typeof (NetDriver))]

+ 11 - 10
UnitTests/Application/Mouse/ApplicationMouseTests.cs

@@ -138,7 +138,7 @@ public class ApplicationMouseTests
     ///     Tests that the mouse coordinates passed to the focused view are correct when the mouse is clicked. With
     ///     Frames; Frame != Viewport
     /// </summary>
-    [AutoInitShutdown]
+    //[AutoInitShutdown]
     [Theory]
 
     // click on border
@@ -200,12 +200,12 @@ public class ApplicationMouseTests
 
         var clicked = false;
 
-        var top = new Toplevel ();
-        top.X = 0;
-        top.Y = 0;
-        top.Width = size.Width * 2;
-        top.Height = size.Height * 2;
-        top.BorderStyle = LineStyle.None;
+        Application.Top = new Toplevel ();
+        Application.Top.X = 0;
+        Application.Top.Y = 0;
+        Application.Top.Width = size.Width * 2;
+        Application.Top.Height = size.Height * 2;
+        Application.Top.BorderStyle = LineStyle.None;
 
         var view = new View { X = pos.X, Y = pos.Y, Width = size.Width, Height = size.Height };
 
@@ -213,8 +213,8 @@ public class ApplicationMouseTests
         view.BorderStyle = LineStyle.Single;
         view.CanFocus = true;
 
-        top.Add (view);
-        Application.Begin (top);
+        Application.Top.Add (view);
+
         var mouseEvent = new MouseEvent { Position = new (clickX, clickY), Flags = MouseFlags.Button1Clicked };
 
         view.MouseClick += (s, e) =>
@@ -226,7 +226,8 @@ public class ApplicationMouseTests
 
         Application.OnMouseEvent (mouseEvent);
         Assert.Equal (expectedClicked, clicked);
-        top.Dispose ();
+        Application.Top.Dispose ();
+        Application.ResetState (ignoreDisposed: true);
     }
 
     #endregion mouse coordinate tests

+ 1 - 1
UnitTests/Configuration/AppScopeTests.cs

@@ -15,7 +15,7 @@ public class AppScopeTests
     };
 
     [Fact]
-    [AutoInitShutdown]
+    [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
     public void Apply_ShouldApplyUpdatedProperties ()
     {
         Reset ();

+ 13 - 0
UnitTests/Configuration/ConfigurationMangerTests.cs

@@ -149,6 +149,8 @@ public class ConfigurationManagerTests
     [Fact]
     public void Load_FiresUpdated ()
     {
+        ConfigLocations savedLocations = Locations;
+        Locations = ConfigLocations.All;
         Reset ();
 
         Settings! ["Application.QuitKey"].PropertyValue = Key.Q;
@@ -183,6 +185,7 @@ public class ConfigurationManagerTests
 
         Updated -= ConfigurationManager_Updated;
         Reset ();
+        Locations = savedLocations;
     }
 
     [Fact]
@@ -414,6 +417,9 @@ public class ConfigurationManagerTests
     [Fact]
     public void TestConfigPropertyOmitClassName ()
     {
+        ConfigLocations savedLocations = Locations;
+        Locations = ConfigLocations.All;
+
         // Color.ColorSchemes is serialized as "ColorSchemes", not "Colors.ColorSchemes"
         PropertyInfo pi = typeof (Colors).GetProperty ("ColorSchemes");
         var scp = (SerializableConfigurationProperty)pi!.GetCustomAttribute (typeof (SerializableConfigurationProperty));
@@ -422,6 +428,8 @@ public class ConfigurationManagerTests
 
         Reset ();
         Assert.Equal (pi, Themes! ["Default"] ["ColorSchemes"].PropertyInfo);
+
+        Locations = savedLocations;
     }
 
     [Fact]
@@ -652,6 +660,9 @@ public class ConfigurationManagerTests
     [Fact]
     public void TestConfigurationManagerUpdateFromJson ()
     {
+        ConfigLocations savedLocations = Locations;
+        Locations = ConfigLocations.All;
+
         // Arrange
         var json = @"
 {
@@ -816,6 +827,8 @@ public class ConfigurationManagerTests
         Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
         Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
         Reset ();
+
+        Locations = savedLocations;
     }
 
     [Fact]

+ 5 - 1
UnitTests/Configuration/SettingsScopeTests.cs

@@ -5,7 +5,7 @@ namespace Terminal.Gui.ConfigurationTests;
 public class SettingsScopeTests
 {
     [Fact]
-    [AutoInitShutdown]
+    [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
     public void Apply_ShouldApplyProperties ()
     {
         // arrange
@@ -55,6 +55,8 @@ public class SettingsScopeTests
     [Fact]
     public void GetHardCodedDefaults_ShouldSetProperties ()
     {
+        ConfigLocations savedLocations = Locations;
+        Locations = ConfigLocations.DefaultOnly;
         Reset ();
 
         Assert.Equal (5, ((Dictionary<string, ThemeScope>)Settings ["Themes"].PropertyValue).Count);
@@ -72,5 +74,7 @@ public class SettingsScopeTests
 
         Assert.True (Settings ["Themes"].PropertyValue is Dictionary<string, ThemeScope>);
         Assert.Single ((Dictionary<string, ThemeScope>)Settings ["Themes"].PropertyValue);
+
+        Locations = savedLocations;
     }
 }

+ 4 - 1
UnitTests/Configuration/ThemeScopeTests.cs

@@ -15,6 +15,7 @@ public class ThemeScopeTests
     };
 
     [Fact]
+    [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
     public void AllThemesPresent ()
     {
         Reset ();
@@ -24,7 +25,7 @@ public class ThemeScopeTests
     }
 
     [Fact]
-    [AutoInitShutdown]
+    [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
     public void Apply_ShouldApplyUpdatedProperties ()
     {
         Reset ();
@@ -53,6 +54,7 @@ public class ThemeScopeTests
     }
 
     [Fact]
+    [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
     public void TestSerialize_RoundTrip ()
     {
         Reset ();
@@ -69,6 +71,7 @@ public class ThemeScopeTests
     }
 
     [Fact]
+    [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
     public void ThemeManager_ClassMethodsWork ()
     {
         Reset ();

+ 2 - 1
UnitTests/Configuration/ThemeTests.cs

@@ -10,7 +10,7 @@ public class ThemeTests
         Converters = { new AttributeJsonConverter (), new ColorJsonConverter () }
     };
 
-    [Fact]
+    [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
     public void TestApply ()
     {
         Reset ();
@@ -32,6 +32,7 @@ public class ThemeTests
     }
 
     [Fact]
+    [AutoInitShutdown (configLocation: ConfigLocations.DefaultOnly)]
     public void TestApply_UpdatesColors ()
     {
         // Arrange

+ 9 - 74
UnitTests/TestHelpers.cs

@@ -5,6 +5,7 @@ using System.Text;
 using System.Text.RegularExpressions;
 using Xunit.Abstractions;
 using Xunit.Sdk;
+using static Terminal.Gui.ConfigurationManager;
 
 namespace Terminal.Gui;
 
@@ -99,11 +100,7 @@ public class AutoInitShutdownAttribute : BeforeAfterTestAttribute
                 Application.ResetState (ignoreDisposed: true);
 #endif
                 ConfigurationManager.Reset ();
-
-                if (CM.Locations != CM.ConfigLocations.None)
-                {
-                    SetCurrentConfig (_savedValues);
-                }
+                ConfigurationManager.Locations = CM.ConfigLocations.None;
             }
 
 
@@ -131,11 +128,6 @@ public class AutoInitShutdownAttribute : BeforeAfterTestAttribute
             }
 #endif
             Application.Init ((ConsoleDriver)Activator.CreateInstance (_driverType));
-
-            if (CM.Locations != CM.ConfigLocations.None)
-            {
-                _savedValues = GetCurrentConfig ();
-            }
         }
     }
 
@@ -143,65 +135,7 @@ public class AutoInitShutdownAttribute : BeforeAfterTestAttribute
 
     private List<object> _savedValues;
 
-    private List<object> GetCurrentConfig ()
-    {
-        CM.Reset ();
-
-        List<object> savedValues =
-        [
-            Dialog.DefaultButtonAlignment,
-            Dialog.DefaultButtonAlignmentModes,
-            MessageBox.DefaultBorderStyle
-        ];
-        CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue = Alignment.End;
-        CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignmentModes"].PropertyValue = AlignmentModes.AddSpaceBetweenItems;
-        CM.Themes! ["Default"] ["MessageBox.DefaultBorderStyle"].PropertyValue = LineStyle.Double;
-        ThemeManager.Themes! [ThemeManager.SelectedTheme]!.Apply ();
-
-        return savedValues;
-    }
-
-    private void SetCurrentConfig (List<object> values)
-    {
-        CM.Reset ();
-        bool needApply = false;
-
-        foreach (object value in values)
-        {
-            switch (value)
-            {
-                case Alignment alignment:
-                    if ((Alignment)CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue! != alignment)
-                    {
-                        needApply = true;
-                        CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue = alignment;
-                    }
-
-                    break;
-                case AlignmentModes alignmentModes:
-                    if ((AlignmentModes)CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignmentModes"].PropertyValue! != alignmentModes)
-                    {
-                        needApply = true;
-                        CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignmentModes"].PropertyValue = alignmentModes;
-                    }
-
-                    break;
-                case LineStyle lineStyle:
-                    if ((LineStyle)CM.Themes! ["Default"] ["Dialog.DefaultButtonAlignment"].PropertyValue! != lineStyle)
-                    {
-                        needApply = true;
-                        CM.Themes! ["Default"] ["MessageBox.DefaultBorderStyle"].PropertyValue = lineStyle;
-                    }
-
-                    break;
-            }
-        }
-
-        if (needApply)
-        {
-            ThemeManager.Themes! [ThemeManager.SelectedTheme]!.Apply ();
-        }
-    }
+   
 }
 
 [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
@@ -259,6 +193,7 @@ public class SetupFakeDriverAttribute : BeforeAfterTestAttribute
         Application.ResetState ();
         Assert.Null (Application.Driver);
         Application.Driver = new FakeDriver { Rows = 25, Cols = 25 };
+
         base.Before (methodUnderTest);
     }
 }
@@ -766,11 +701,11 @@ internal partial class TestHelpers
         string replaced = toReplace;
 
         replaced = Environment.NewLine.Length switch
-                   {
-                       2 when !replaced.Contains ("\r\n") => replaced.Replace ("\n", Environment.NewLine),
-                       1 => replaced.Replace ("\r\n", Environment.NewLine),
-                       var _ => replaced
-                   };
+        {
+            2 when !replaced.Contains ("\r\n") => replaced.Replace ("\n", Environment.NewLine),
+            1 => replaced.Replace ("\r\n", Environment.NewLine),
+            var _ => replaced
+        };
 
         return replaced;
     }