浏览代码

Cleanup code

tznind 4 月之前
父节点
当前提交
56d3835c37

+ 31 - 40
TerminalGuiFluentTesting/GuiTestContext.cs

@@ -1,27 +1,10 @@
-
-using System.Text;
+using System.Text;
 using Microsoft.Extensions.Logging;
 using Microsoft.Extensions.Logging;
 using Terminal.Gui;
 using Terminal.Gui;
 using Terminal.Gui.ConsoleDrivers;
 using Terminal.Gui.ConsoleDrivers;
 
 
 namespace TerminalGuiFluentTesting;
 namespace TerminalGuiFluentTesting;
 
 
-
-class TextWriterLoggerProvider (TextWriter writer) : ILoggerProvider
-{
-    public ILogger CreateLogger (string category) => new TextWriterLogger (writer);
-    public void Dispose () => writer.Dispose ();
-}
-
-class TextWriterLogger (TextWriter writer) : ILogger
-{
-    public IDisposable? BeginScope<TState> (TState state) => null;
-    public bool IsEnabled (LogLevel logLevel) => true;
-    public void Log<TState> (LogLevel logLevel, EventId eventId, TState state,
-                             Exception? ex, Func<TState, Exception?, string> formatter) =>
-        writer.WriteLine (formatter (state, ex));
-}
-
 public class GuiTestContext : IDisposable
 public class GuiTestContext : IDisposable
 {
 {
     private readonly CancellationTokenSource _cts = new ();
     private readonly CancellationTokenSource _cts = new ();
@@ -34,11 +17,11 @@ public class GuiTestContext : IDisposable
     private View? _lastView;
     private View? _lastView;
     private readonly StringBuilder _logsSb;
     private readonly StringBuilder _logsSb;
 
 
-    internal GuiTestContext(Func<Toplevel> topLevelBuilder, int width, int height)
+    internal GuiTestContext (Func<Toplevel> topLevelBuilder, int width, int height)
     {
     {
         IApplication origApp = ApplicationImpl.Instance;
         IApplication origApp = ApplicationImpl.Instance;
-        var origLogger = Logging.Logger;
-        _logsSb = new StringBuilder ();
+        ILogger? origLogger = Logging.Logger;
+        _logsSb = new ();
 
 
         _netInput = new (_cts.Token);
         _netInput = new (_cts.Token);
         _winInput = new (_cts.Token);
         _winInput = new (_cts.Token);
@@ -61,19 +44,20 @@ public class GuiTestContext : IDisposable
                                  {
                                  {
                                      ApplicationImpl.ChangeInstance (v2);
                                      ApplicationImpl.ChangeInstance (v2);
 
 
-                                     var logger = LoggerFactory.Create (builder =>
-                                                                            builder.SetMinimumLevel (LogLevel.Trace)
-                                                                                   .AddProvider (new TextWriterLoggerProvider (new StringWriter (_logsSb))))
-                                                               .CreateLogger ("Test Logging");
+                                     ILogger logger = LoggerFactory.Create (
+                                                                            builder =>
+                                                                                builder.SetMinimumLevel (LogLevel.Trace)
+                                                                                       .AddProvider (new TextWriterLoggerProvider (new StringWriter (_logsSb))))
+                                                                   .CreateLogger ("Test Logging");
                                      Logging.Logger = logger;
                                      Logging.Logger = logger;
 
 
                                      v2.Init (null, "v2win");
                                      v2.Init (null, "v2win");
 
 
                                      booting.Release ();
                                      booting.Release ();
 
 
-                                     var t = topLevelBuilder ();
+                                     Toplevel t = topLevelBuilder ();
 
 
-                                     Application.Run(t); // This will block, but it's on a background thread now
+                                     Application.Run (t); // This will block, but it's on a background thread now
 
 
                                      Application.Shutdown ();
                                      Application.Shutdown ();
                                  }
                                  }
@@ -186,7 +170,8 @@ public class GuiTestContext : IDisposable
 
 
     public GuiTestContext WriteOutLogs (TextWriter writer)
     public GuiTestContext WriteOutLogs (TextWriter writer)
     {
     {
-        writer.WriteLine (_logsSb.ToString());
+        writer.WriteLine (_logsSb.ToString ());
+
         return WaitIteration ();
         return WaitIteration ();
     }
     }
 
 
@@ -217,6 +202,7 @@ public class GuiTestContext : IDisposable
     public GuiTestContext Then (Action doAction)
     public GuiTestContext Then (Action doAction)
     {
     {
         doAction ();
         doAction ();
+
         return this;
         return this;
     }
     }
 
 
@@ -227,10 +213,10 @@ public class GuiTestContext : IDisposable
     private GuiTestContext Click (WindowsConsole.ButtonState btn, int screenX, int screenY)
     private GuiTestContext Click (WindowsConsole.ButtonState btn, int screenX, int screenY)
     {
     {
         _winInput.InputBuffer.Enqueue (
         _winInput.InputBuffer.Enqueue (
-                                       new()
+                                       new ()
                                        {
                                        {
                                            EventType = WindowsConsole.EventType.Mouse,
                                            EventType = WindowsConsole.EventType.Mouse,
-                                           MouseEvent = new()
+                                           MouseEvent = new ()
                                            {
                                            {
                                                ButtonState = btn,
                                                ButtonState = btn,
                                                MousePosition = new ((short)screenX, (short)screenY)
                                                MousePosition = new ((short)screenX, (short)screenY)
@@ -238,10 +224,10 @@ public class GuiTestContext : IDisposable
                                        });
                                        });
 
 
         _winInput.InputBuffer.Enqueue (
         _winInput.InputBuffer.Enqueue (
-                                       new()
+                                       new ()
                                        {
                                        {
                                            EventType = WindowsConsole.EventType.Mouse,
                                            EventType = WindowsConsole.EventType.Mouse,
-                                           MouseEvent = new()
+                                           MouseEvent = new ()
                                            {
                                            {
                                                ButtonState = WindowsConsole.ButtonState.NoButtonPressed,
                                                ButtonState = WindowsConsole.ButtonState.NoButtonPressed,
                                                MousePosition = new ((short)screenX, (short)screenY)
                                                MousePosition = new ((short)screenX, (short)screenY)
@@ -256,27 +242,35 @@ public class GuiTestContext : IDisposable
     public GuiTestContext Down ()
     public GuiTestContext Down ()
     {
     {
         SendWindowsKey (ConsoleKeyMapping.VK.DOWN);
         SendWindowsKey (ConsoleKeyMapping.VK.DOWN);
+
         return this;
         return this;
     }
     }
+
     public GuiTestContext Right ()
     public GuiTestContext Right ()
     {
     {
         SendWindowsKey (ConsoleKeyMapping.VK.RIGHT);
         SendWindowsKey (ConsoleKeyMapping.VK.RIGHT);
+
         return this;
         return this;
     }
     }
+
     public GuiTestContext Left ()
     public GuiTestContext Left ()
     {
     {
         SendWindowsKey (ConsoleKeyMapping.VK.LEFT);
         SendWindowsKey (ConsoleKeyMapping.VK.LEFT);
+
         return this;
         return this;
     }
     }
+
     public GuiTestContext Up ()
     public GuiTestContext Up ()
     {
     {
         SendWindowsKey (ConsoleKeyMapping.VK.UP);
         SendWindowsKey (ConsoleKeyMapping.VK.UP);
+
         return this;
         return this;
     }
     }
+
     public GuiTestContext Enter ()
     public GuiTestContext Enter ()
     {
     {
         SendWindowsKey (
         SendWindowsKey (
-                        new WindowsConsole.KeyEventRecord ()
+                        new WindowsConsole.KeyEventRecord
                         {
                         {
                             UnicodeChar = '\r',
                             UnicodeChar = '\r',
                             dwControlKeyState = WindowsConsole.ControlKeyState.NoControlKeyPressed,
                             dwControlKeyState = WindowsConsole.ControlKeyState.NoControlKeyPressed,
@@ -289,18 +283,17 @@ public class GuiTestContext : IDisposable
     }
     }
 
 
     /// <summary>
     /// <summary>
-    /// Send a full windows OS key including both down and up.
+    ///     Send a full windows OS key including both down and up.
     /// </summary>
     /// </summary>
     /// <param name="fullKey"></param>
     /// <param name="fullKey"></param>
     private void SendWindowsKey (WindowsConsole.KeyEventRecord fullKey)
     private void SendWindowsKey (WindowsConsole.KeyEventRecord fullKey)
     {
     {
-        var down = fullKey;
-        var up = fullKey; // because struct this is new copy
+        WindowsConsole.KeyEventRecord down = fullKey;
+        WindowsConsole.KeyEventRecord up = fullKey; // because struct this is new copy
 
 
         down.bKeyDown = true;
         down.bKeyDown = true;
         up.bKeyDown = false;
         up.bKeyDown = false;
 
 
-
         _winInput.InputBuffer.Enqueue (
         _winInput.InputBuffer.Enqueue (
                                        new ()
                                        new ()
                                        {
                                        {
@@ -319,12 +312,11 @@ public class GuiTestContext : IDisposable
     }
     }
 
 
     /// <summary>
     /// <summary>
-    /// Sends a special key e.g. cursor key that does not map to a specific character
+    ///     Sends a special key e.g. cursor key that does not map to a specific character
     /// </summary>
     /// </summary>
     /// <param name="specialKey"></param>
     /// <param name="specialKey"></param>
     private void SendWindowsKey (ConsoleKeyMapping.VK specialKey)
     private void SendWindowsKey (ConsoleKeyMapping.VK specialKey)
     {
     {
-
         _winInput.InputBuffer.Enqueue (
         _winInput.InputBuffer.Enqueue (
                                        new ()
                                        new ()
                                        {
                                        {
@@ -358,7 +350,6 @@ public class GuiTestContext : IDisposable
         WaitIteration ();
         WaitIteration ();
     }
     }
 
 
-
     public GuiTestContext WithContextMenu (ContextMenu ctx, MenuBarItem menuItems)
     public GuiTestContext WithContextMenu (ContextMenu ctx, MenuBarItem menuItems)
     {
     {
         LastView.MouseEvent += (s, e) =>
         LastView.MouseEvent += (s, e) =>

+ 9 - 9
TerminalGuiFluentTesting/TerminalGuiFluentTesting.csproj

@@ -1,13 +1,13 @@
 <Project Sdk="Microsoft.NET.Sdk">
 <Project Sdk="Microsoft.NET.Sdk">
 
 
-  <PropertyGroup>
-    <TargetFramework>net8.0</TargetFramework>
-    <ImplicitUsings>enable</ImplicitUsings>
-    <Nullable>enable</Nullable>
-  </PropertyGroup>
+	<PropertyGroup>
+		<TargetFramework>net8.0</TargetFramework>
+		<ImplicitUsings>enable</ImplicitUsings>
+		<Nullable>enable</Nullable>
+	</PropertyGroup>
 
 
-  <ItemGroup>
-    <ProjectReference Include="..\Terminal.Gui\Terminal.Gui.csproj" />
-  </ItemGroup>
+	<ItemGroup>
+		<ProjectReference Include="..\Terminal.Gui\Terminal.Gui.csproj" />
+	</ItemGroup>
 
 
-</Project>
+</Project>

+ 21 - 0
TerminalGuiFluentTesting/TextWriterLogger.cs

@@ -0,0 +1,21 @@
+using Microsoft.Extensions.Logging;
+
+namespace TerminalGuiFluentTesting;
+
+internal class TextWriterLogger (TextWriter writer) : ILogger
+{
+    public IDisposable? BeginScope<TState> (TState state) { return null; }
+
+    public bool IsEnabled (LogLevel logLevel) { return true; }
+
+    public void Log<TState> (
+        LogLevel logLevel,
+        EventId eventId,
+        TState state,
+        Exception? ex,
+        Func<TState, Exception?, string> formatter
+    )
+    {
+        writer.WriteLine (formatter (state, ex));
+    }
+}

+ 10 - 0
TerminalGuiFluentTesting/TextWriterLoggerProvider.cs

@@ -0,0 +1,10 @@
+using Microsoft.Extensions.Logging;
+
+namespace TerminalGuiFluentTesting;
+
+internal class TextWriterLoggerProvider (TextWriter writer) : ILoggerProvider
+{
+    public ILogger CreateLogger (string category) { return new TextWriterLogger (writer); }
+
+    public void Dispose () { writer.Dispose (); }
+}

+ 1 - 1
TerminalGuiFluentTesting/With.cs

@@ -13,7 +13,7 @@ public static class With
     /// <param name="width"></param>
     /// <param name="width"></param>
     /// <param name="height"></param>
     /// <param name="height"></param>
     /// <returns></returns>
     /// <returns></returns>
-    public static GuiTestContext A<T> (int width, int height) where T : Toplevel, new () { return new (()=>new T(), width, height); }
+    public static GuiTestContext A<T> (int width, int height) where T : Toplevel, new () { return new (() => new T (), width, height); }
 
 
     /// <summary>
     /// <summary>
     ///     The global timeout to allow for any given application to run for before shutting down.
     ///     The global timeout to allow for any given application to run for before shutting down.