Browse Source

Still trying to fix the Run_All_Scenarios unit test.

BDisp 3 years ago
parent
commit
c8db4ff88b
1 changed files with 53 additions and 51 deletions
  1. 53 51
      UnitTests/ScenarioTests.cs

+ 53 - 51
UnitTests/ScenarioTests.cs

@@ -26,15 +26,15 @@ namespace Terminal.Gui {
 		int CreateInput (string input)
 		int CreateInput (string input)
 		{
 		{
 			// Put a control-q in at the end
 			// Put a control-q in at the end
-			Console.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true));
+			FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo ('q', ConsoleKey.Q, shift: false, alt: false, control: true));
 			foreach (var c in input.Reverse ()) {
 			foreach (var c in input.Reverse ()) {
 				if (char.IsLetter (c)) {
 				if (char.IsLetter (c)) {
-					Console.MockKeyPresses.Push (new ConsoleKeyInfo (char.ToLower (c), (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
+					FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo (char.ToLower (c), (ConsoleKey)char.ToUpper (c), shift: char.IsUpper (c), alt: false, control: false));
 				} else {
 				} else {
-					Console.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
+					FakeConsole.MockKeyPresses.Push (new ConsoleKeyInfo (c, (ConsoleKey)c, shift: false, alt: false, control: false));
 				}
 				}
 			}
 			}
-			return Console.MockKeyPresses.Count;
+			return FakeConsole.MockKeyPresses.Count;
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
@@ -48,59 +48,61 @@ namespace Terminal.Gui {
 			List<Type> scenarioClasses = Scenario.GetDerivedClasses<Scenario> ();
 			List<Type> scenarioClasses = Scenario.GetDerivedClasses<Scenario> ();
 			Assert.NotEmpty (scenarioClasses);
 			Assert.NotEmpty (scenarioClasses);
 
 
-			foreach (var scenarioClass in scenarioClasses) {
+			lock (FakeConsole.MockKeyPresses) {
+				foreach (var scenarioClass in scenarioClasses) {
+
+					// Setup some fake keypresses 
+					// Passing empty string will cause just a ctrl-q to be fired
+					FakeConsole.MockKeyPresses.Clear ();
+					int stackSize = CreateInput ("");
+
+					Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+
+					int iterations = 0;
+					Application.Iteration = () => {
+						iterations++;
+						// Stop if we run out of control...
+						if (iterations > 10) {
+							Application.RequestStop ();
+						}
+					};
+
+					int ms;
+					if (scenarioClass.Name == "CharacterMap") {
+						ms = 2000;
+					} else {
+						ms = 1000;
+					}
+					var abortCount = 0;
+					Func<MainLoop, bool> abortCallback = (MainLoop loop) => {
+						abortCount++;
+						Application.RequestStop ();
+						return false;
+					};
+					var token = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (ms), abortCallback);
 
 
-				// Setup some fake keypresses 
-				// Passing empty string will cause just a ctrl-q to be fired
-				Console.MockKeyPresses.Clear ();
-				int stackSize = CreateInput ("");
+					var scenario = (Scenario)Activator.CreateInstance (scenarioClass);
+					scenario.Init (Application.Top, Colors.Base);
+					scenario.Setup ();
+					// There is no need to call Application.Begin because Init already creates the Application.Top
+					// If Application.RunState is used then the Application.RunLoop must also be used instead Application.Run.
+					//var rs = Application.Begin (Application.Top);
+					scenario.Run ();
 
 
-				Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
+					//Application.End (rs);
 
 
-				int iterations = 0;
-				Application.Iteration = () => {
-					iterations++;
-					// Stop if we run out of control...
-					if (iterations > 10) {
-						Application.RequestStop ();
+					// Shutdown must be called to safely clean up Application if Init has been called
+					Application.Shutdown ();
+
+					if (abortCount != 0) {
+						output.WriteLine ($"Scenario {scenarioClass} had abort count of {abortCount}");
 					}
 					}
-				};
 
 
-				int ms;
-				if (scenarioClass.Name == "CharacterMap") {
-					ms = 1500;
-				}else {
-					ms = 1000;
+					Assert.Equal (0, abortCount);
+					// # of key up events should match # of iterations
+					Assert.Equal (1, iterations);
+					Assert.Equal (stackSize, iterations);
 				}
 				}
-				var abortCount = 0;
-				Func<MainLoop, bool> abortCallback = (MainLoop loop) => {
-					abortCount++;
-					Application.RequestStop ();
-					return false;
-				};
-				var token = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (ms), abortCallback);
-
-				var scenario = (Scenario)Activator.CreateInstance (scenarioClass);
-				scenario.Init (Application.Top, Colors.Base);
-				scenario.Setup ();
-				// There is no need to call Application.Begin because Init already creates the Application.Top
-				// If Application.RunState is used then the Application.RunLoop must also be used instead Application.Run.
-				//var rs = Application.Begin (Application.Top);
-				scenario.Run ();
-
-				//Application.End (rs);
-
-				// Shutdown must be called to safely clean up Application if Init has been called
-				Application.Shutdown ();
-				
-				if(abortCount != 0) {
-					output.WriteLine ($"Scenario {scenarioClass} had abort count of {abortCount}");
-				}
-
-				Assert.Equal (0, abortCount);
-				// # of key up events should match # of iterations
-				Assert.Equal (1, iterations);
-				Assert.Equal (stackSize, iterations);
 			}
 			}
 #if DEBUG_IDISPOSABLE
 #if DEBUG_IDISPOSABLE
 			foreach (var inst in Responder.Instances) {
 			foreach (var inst in Responder.Instances) {