Sfoglia il codice sorgente

Refactored Run_All_Scenarios to use theory data

Tig 1 anno fa
parent
commit
248bbe0afc
2 ha cambiato i file con 85 aggiunte e 82 eliminazioni
  1. 15 0
      UnitTests/TestHelpers.cs
  2. 70 82
      UnitTests/UICatalog/ScenarioTests.cs

+ 15 - 0
UnitTests/TestHelpers.cs

@@ -3,6 +3,7 @@ using System.Globalization;
 using System.Reflection;
 using System.Reflection;
 using System.Text;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Text.RegularExpressions;
+using UICatalog;
 using Xunit.Abstractions;
 using Xunit.Abstractions;
 using Xunit.Sdk;
 using Xunit.Sdk;
 
 
@@ -565,6 +566,20 @@ internal partial class TestHelpers
     }
     }
 
 
 
 
+    public static TheoryData<Scenario, string> GetAllScenarioTheoryData ()
+    {
+        // TODO: Figure out how to simplify this. I couldn't figure out how to not have to iterate over ret.
+        var scenarios = Scenario.GetScenarios ();
+        (Scenario scenario, string name) [] ret = scenarios.Select (s => (scenario: s, name: s.GetName ())).ToArray();
+        TheoryData<Scenario, string> td = new ();
+        foreach ((Scenario scenario, string name) in ret)
+        {
+            td.Add (scenario, name);
+        }
+
+        return td;
+    }
+
     /// <summary>
     /// <summary>
     ///     Verifies the console used all the <paramref name="expectedColors"/> when rendering. If one or more of the
     ///     Verifies the console used all the <paramref name="expectedColors"/> when rendering. If one or more of the
     ///     expected colors are not used then the failure will output both the colors that were found to be used and which of
     ///     expected colors are not used then the failure will output both the colors that were found to be used and which of

+ 70 - 82
UnitTests/UICatalog/ScenarioTests.cs

@@ -15,85 +15,73 @@ public class ScenarioTests
         _output = output;
         _output = output;
     }
     }
 
 
+    public static TheoryData<Scenario, string> AllScenarios => TestHelpers.GetAllScenarioTheoryData ();
+
     /// <summary>
     /// <summary>
     ///     <para>This runs through all Scenarios defined in UI Catalog, calling Init, Setup, and Run.</para>
     ///     <para>This runs through all Scenarios defined in UI Catalog, calling Init, Setup, and Run.</para>
     ///     <para>Should find any Scenarios which crash on load or do not respond to <see cref="Application.RequestStop()"/>.</para>
     ///     <para>Should find any Scenarios which crash on load or do not respond to <see cref="Application.RequestStop()"/>.</para>
     /// </summary>
     /// </summary>
-    [Fact]
-    public void Run_All_Scenarios ()
+    [Theory]
+    [MemberData (nameof (AllScenarios))]
+    public void Run_All_Scenarios (Scenario scenario, string viewName)
     {
     {
-        List<Scenario> scenarios = Scenario.GetScenarios ();
-        Assert.NotEmpty (scenarios);
+        _output.WriteLine ($"Running Scenario '{scenario.GetName ()}'");
 
 
-        foreach (Scenario scenario in scenarios)
-        {
-            _output.WriteLine ($"Running Scenario '{scenario.GetName ()}'");
-
-            Application.Init (new FakeDriver ());
+        Application.Init (new FakeDriver ());
 
 
-            // Press QuitKey 
-            Assert.Empty (FakeConsole.MockKeyPresses);
+        // Press QuitKey 
+        Assert.Empty (FakeConsole.MockKeyPresses);
 
 
-            // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
-            // by adding this Space it seems to work.
-            //FakeConsole.PushMockKeyPress (Key.Space);
-            FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
+        // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
+        // by adding this Space it seems to work.
+        //FakeConsole.PushMockKeyPress (Key.Space);
+        FakeConsole.PushMockKeyPress ((KeyCode)Application.QuitKey);
 
 
-            // The only key we care about is the QuitKey
-            Application.KeyDown += (sender, args) =>
-                                       {
-                                           _output.WriteLine ($"  Keypress: {args.KeyCode}");
+        // The only key we care about is the QuitKey
+        Application.KeyDown += (sender, args) =>
+                               {
+                                   _output.WriteLine ($"  Keypress: {args.KeyCode}");
 
 
-                                           // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
-                                           // by adding this Space it seems to work.
-                                           // See #2474 for why this is commented out
-                                           Assert.Equal (Application.QuitKey.KeyCode, args.KeyCode);
-                                       };
+                                   // BUGBUG: (#2474) For some reason ReadKey is not returning the QuitKey for some Scenarios
+                                   // by adding this Space it seems to work.
+                                   // See #2474 for why this is commented out
+                                   Assert.Equal (Application.QuitKey.KeyCode, args.KeyCode);
+                               };
 
 
-            uint abortTime = 500;
+        uint abortTime = 500;
 
 
-            // If the scenario doesn't close within 500ms, this will force it to quit
-            bool ForceCloseCallback ()
+        // If the scenario doesn't close within 500ms, this will force it to quit
+        bool ForceCloseCallback ()
+        {
+            if (Application.Top.Running && FakeConsole.MockKeyPresses.Count == 0)
             {
             {
-                if (Application.Top.Running && FakeConsole.MockKeyPresses.Count == 0)
-                {
-                    Application.RequestStop ();
-
-                    // See #2474 for why this is commented out
-                    Assert.Fail (
-                                 $"'{
-                                     scenario.GetName ()
-                                 }' failed to Quit with {
-                                     Application.QuitKey
-                                 } after {
-                                     abortTime
-                                 }ms. Force quit.");
-                }
+                Application.RequestStop ();
 
 
-                return false;
+                // See #2474 for why this is commented out
+                Assert.Fail (
+                             $"'{scenario.GetName ()}' failed to Quit with {Application.QuitKey} after {abortTime}ms. Force quit.");
             }
             }
 
 
-            //output.WriteLine ($"  Add timeout to force quit after {abortTime}ms");
-            _ = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
+            return false;
+        }
+
+        //output.WriteLine ($"  Add timeout to force quit after {abortTime}ms");
+        _ = Application.AddTimeout (TimeSpan.FromMilliseconds (abortTime), ForceCloseCallback);
 
 
-            Application.Iteration += (s, a) =>
+        Application.Iteration += (s, a) =>
+                                 {
+                                     //output.WriteLine ($"  iteration {++iterations}");
+                                     if (Application.Top.Running && FakeConsole.MockKeyPresses.Count == 0)
                                      {
                                      {
-                                         //output.WriteLine ($"  iteration {++iterations}");
-                                         if (Application.Top.Running && FakeConsole.MockKeyPresses.Count == 0)
-                                         {
-                                             Application.RequestStop ();
-                                             Assert.Fail ($"'{scenario.GetName ()}' failed to Quit with {Application.QuitKey}. Force quit.");
-                                         }
-                                     };
+                                         Application.RequestStop ();
+                                         Assert.Fail ($"'{scenario.GetName ()}' failed to Quit with {Application.QuitKey}. Force quit.");
+                                     }
+                                 };
 
 
-            scenario.Main ();
-            scenario.Dispose ();
+        scenario.Main ();
+        scenario.Dispose ();
 
 
-            Application.Shutdown ();
-#if DEBUG_IDISPOSABLE
-            Assert.Empty (Responder.Instances);
-#endif
-        }
+        Application.Shutdown ();
 #if DEBUG_IDISPOSABLE
 #if DEBUG_IDISPOSABLE
         Assert.Empty (Responder.Instances);
         Assert.Empty (Responder.Instances);
 #endif
 #endif
@@ -132,14 +120,14 @@ public class ScenarioTests
 
 
         Application.Init (new FakeDriver ());
         Application.Init (new FakeDriver ());
 
 
-        Toplevel top = new Toplevel ();
+        var top = new Toplevel ();
 
 
         _viewClasses = GetAllViewClassesCollection ()
         _viewClasses = GetAllViewClassesCollection ()
                        .OrderBy (t => t.Name)
                        .OrderBy (t => t.Name)
                        .Select (t => new KeyValuePair<string, Type> (t.Name, t))
                        .Select (t => new KeyValuePair<string, Type> (t.Name, t))
                        .ToDictionary (t => t.Key, t => t.Value);
                        .ToDictionary (t => t.Key, t => t.Value);
 
 
-        _leftPane = new Window
+        _leftPane = new()
         {
         {
             Title = "Classes",
             Title = "Classes",
             X = 0,
             X = 0,
@@ -150,7 +138,7 @@ public class ScenarioTests
             ColorScheme = Colors.ColorSchemes ["TopLevel"]
             ColorScheme = Colors.ColorSchemes ["TopLevel"]
         };
         };
 
 
-        _classListView = new ListView
+        _classListView = new()
         {
         {
             X = 0,
             X = 0,
             Y = 0,
             Y = 0,
@@ -162,7 +150,7 @@ public class ScenarioTests
         };
         };
         _leftPane.Add (_classListView);
         _leftPane.Add (_classListView);
 
 
-        _settingsPane = new FrameView
+        _settingsPane = new()
         {
         {
             X = Pos.Right (_leftPane),
             X = Pos.Right (_leftPane),
             Y = 0, // for menu
             Y = 0, // for menu
@@ -172,12 +160,12 @@ public class ScenarioTests
             ColorScheme = Colors.ColorSchemes ["TopLevel"],
             ColorScheme = Colors.ColorSchemes ["TopLevel"],
             Title = "Settings"
             Title = "Settings"
         };
         };
-        _computedCheckBox = new CheckBox { X = 0, Y = 0, Text = "Computed Layout", Checked = true };
+        _computedCheckBox = new() { X = 0, Y = 0, Text = "Computed Layout", Checked = true };
         _settingsPane.Add (_computedCheckBox);
         _settingsPane.Add (_computedCheckBox);
 
 
         var radioItems = new [] { "Percent(x)", "AnchorEnd(x)", "Center", "At(x)" };
         var radioItems = new [] { "Percent(x)", "AnchorEnd(x)", "Center", "At(x)" };
 
 
-        _locationFrame = new FrameView
+        _locationFrame = new()
         {
         {
             X = Pos.Left (_computedCheckBox),
             X = Pos.Left (_computedCheckBox),
             Y = Pos.Bottom (_computedCheckBox),
             Y = Pos.Bottom (_computedCheckBox),
@@ -189,21 +177,21 @@ public class ScenarioTests
 
 
         var label = new Label { X = 0, Y = 0, Text = "x:" };
         var label = new Label { X = 0, Y = 0, Text = "x:" };
         _locationFrame.Add (label);
         _locationFrame.Add (label);
-        _xRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
-        _xText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
+        _xRadioGroup = new() { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
+        _xText = new() { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_xVal}" };
         _locationFrame.Add (_xText);
         _locationFrame.Add (_xText);
 
 
         _locationFrame.Add (_xRadioGroup);
         _locationFrame.Add (_xRadioGroup);
 
 
         radioItems = new [] { "Percent(y)", "AnchorEnd(y)", "Center", "At(y)" };
         radioItems = new [] { "Percent(y)", "AnchorEnd(y)", "Center", "At(y)" };
-        label = new Label { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "y:" };
+        label = new() { X = Pos.Right (_xRadioGroup) + 1, Y = 0, Text = "y:" };
         _locationFrame.Add (label);
         _locationFrame.Add (label);
-        _yText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
+        _yText = new() { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_yVal}" };
         _locationFrame.Add (_yText);
         _locationFrame.Add (_yText);
-        _yRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
+        _yRadioGroup = new() { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
         _locationFrame.Add (_yRadioGroup);
         _locationFrame.Add (_yRadioGroup);
 
 
-        _sizeFrame = new FrameView
+        _sizeFrame = new()
         {
         {
             X = Pos.Right (_locationFrame),
             X = Pos.Right (_locationFrame),
             Y = Pos.Y (_locationFrame),
             Y = Pos.Y (_locationFrame),
@@ -213,25 +201,25 @@ public class ScenarioTests
         };
         };
 
 
         radioItems = new [] { "Percent(width)", "Fill(width)", "Sized(width)" };
         radioItems = new [] { "Percent(width)", "Fill(width)", "Sized(width)" };
-        label = new Label { X = 0, Y = 0, Text = "width:" };
+        label = new() { X = 0, Y = 0, Text = "width:" };
         _sizeFrame.Add (label);
         _sizeFrame.Add (label);
-        _wRadioGroup = new RadioGroup { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
-        _wText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
+        _wRadioGroup = new() { X = 0, Y = Pos.Bottom (label), RadioLabels = radioItems };
+        _wText = new() { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_wVal}" };
         _sizeFrame.Add (_wText);
         _sizeFrame.Add (_wText);
         _sizeFrame.Add (_wRadioGroup);
         _sizeFrame.Add (_wRadioGroup);
 
 
         radioItems = new [] { "Percent(height)", "Fill(height)", "Sized(height)" };
         radioItems = new [] { "Percent(height)", "Fill(height)", "Sized(height)" };
-        label = new Label { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "height:" };
+        label = new() { X = Pos.Right (_wRadioGroup) + 1, Y = 0, Text = "height:" };
         _sizeFrame.Add (label);
         _sizeFrame.Add (label);
-        _hText = new TextField { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
+        _hText = new() { X = Pos.Right (label) + 1, Y = 0, Width = 4, Text = $"{_hVal}" };
         _sizeFrame.Add (_hText);
         _sizeFrame.Add (_hText);
 
 
-        _hRadioGroup = new RadioGroup { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
+        _hRadioGroup = new() { X = Pos.X (label), Y = Pos.Bottom (label), RadioLabels = radioItems };
         _sizeFrame.Add (_hRadioGroup);
         _sizeFrame.Add (_hRadioGroup);
 
 
         _settingsPane.Add (_sizeFrame);
         _settingsPane.Add (_sizeFrame);
 
 
-        _hostPane = new FrameView
+        _hostPane = new()
         {
         {
             X = Pos.Right (_leftPane),
             X = Pos.Right (_leftPane),
             Y = Pos.Bottom (_settingsPane),
             Y = Pos.Bottom (_settingsPane),
@@ -623,10 +611,10 @@ public class ScenarioTests
                                  };
                                  };
 
 
         Application.KeyDown += (sender, args) =>
         Application.KeyDown += (sender, args) =>
-                                   {
-                                       // See #2474 for why this is commented out
-                                       Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, args.KeyCode);
-                                   };
+                               {
+                                   // See #2474 for why this is commented out
+                                   Assert.Equal (KeyCode.CtrlMask | KeyCode.Q, args.KeyCode);
+                               };
 
 
         generic.Main ();
         generic.Main ();