Pārlūkot izejas kodu

Fixed KeyBindingTests

Tig 1 gadu atpakaļ
vecāks
revīzija
6e48b07527
2 mainītis faili ar 66 papildinājumiem un 16 dzēšanām
  1. 26 10
      Terminal.Gui/Input/KeyBindings.cs
  2. 40 6
      UnitTests/Input/KeyBindingTests.cs

+ 26 - 10
Terminal.Gui/Input/KeyBindings.cs

@@ -1,4 +1,6 @@
-namespace Terminal.Gui;
+using static System.Formats.Asn1.AsnWriter;
+
+namespace Terminal.Gui;
 
 
 /// <summary>Provides a collection of <see cref="KeyBinding"/> objects bound to a <see cref="Key"/>.</summary>
 /// <summary>Provides a collection of <see cref="KeyBinding"/> objects bound to a <see cref="Key"/>.</summary>
 public class KeyBindings
 public class KeyBindings
@@ -27,7 +29,18 @@ public class KeyBindings
     /// <param name="binding"></param>
     /// <param name="binding"></param>
     public void Add (Key key, KeyBinding binding)
     public void Add (Key key, KeyBinding binding)
     {
     {
-        Bindings.Add (key, binding);
+        if (TryGet (key, out KeyBinding _))
+        {
+            Bindings [key] = binding;
+        }
+        else
+        {
+            Bindings.Add (key, binding);
+            if (binding.Scope.HasFlag (KeyBindingScope.Application))
+            {
+                Application.AddKeyBinding (key, BoundView);
+            }
+        }
     }
     }
 
 
     /// <summary>
     /// <summary>
@@ -99,7 +112,10 @@ public class KeyBindings
     ///     multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
     ///     multiple commands are provided,they will be applied in sequence. The bound <paramref name="key"/> strike will be
     ///     consumed if any took effect.
     ///     consumed if any took effect.
     /// </param>
     /// </param>
-    public void Add (Key key, params Command [] commands) { Add (key, KeyBindingScope.Focused, commands); }
+    public void Add (Key key, params Command [] commands)
+    {
+        Add (key, KeyBindingScope.Focused, commands);
+    }
 
 
     /// <summary>Removes all <see cref="KeyBinding"/> objects from the collection.</summary>
     /// <summary>Removes all <see cref="KeyBinding"/> objects from the collection.</summary>
     public void Clear ()
     public void Clear ()
@@ -170,18 +186,18 @@ public class KeyBindings
 
 
     /// <summary>Replaces a key combination already bound to a set of <see cref="Command"/>s.</summary>
     /// <summary>Replaces a key combination already bound to a set of <see cref="Command"/>s.</summary>
     /// <remarks></remarks>
     /// <remarks></remarks>
-    /// <param name="fromKey">The key to be replaced.</param>
-    /// <param name="toKey">The new key to be used.</param>
-    public void Replace (Key fromKey, Key toKey)
+    /// <param name="oldKey">The key to be replaced.</param>
+    /// <param name="newKey">The new key to be used.</param>
+    public void Replace (Key oldKey, Key newKey)
     {
     {
-        if (!TryGet (fromKey, out KeyBinding _))
+        if (!TryGet (oldKey, out KeyBinding _))
         {
         {
             return;
             return;
         }
         }
 
 
-        KeyBinding value = Bindings [fromKey];
-        Remove (fromKey);
-        Add (toKey, value);
+        KeyBinding value = Bindings [oldKey];
+        Remove (oldKey);
+        Add (newKey, value);
     }
     }
 
 
     /// <summary>Gets the commands bound with the specified Key.</summary>
     /// <summary>Gets the commands bound with the specified Key.</summary>

+ 40 - 6
UnitTests/Input/KeyBindingTests.cs

@@ -160,6 +160,40 @@ public class KeyBindingTests
         Assert.Equal (Key.A, resultKey);
         Assert.Equal (Key.A, resultKey);
     }
     }
 
 
+    // Add should not allow duplicates
+    [Fact]
+    public void Add_Replaces_If_Exists ()
+    {
+        var keyBindings = new KeyBindings ();
+        keyBindings.Add (Key.A, Command.HotKey);
+        keyBindings.Add (Key.A, Command.Accept);
+
+        Command [] resultCommands = keyBindings.GetCommands (Key.A);
+        Assert.DoesNotContain (Command.HotKey, resultCommands);
+
+        keyBindings = new KeyBindings ();
+        keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.HotKey);
+        keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept);
+
+        resultCommands = keyBindings.GetCommands (Key.A);
+        Assert.DoesNotContain (Command.HotKey, resultCommands);
+        
+        keyBindings = new KeyBindings ();
+        keyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.HotKey);
+        keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept);
+
+        resultCommands = keyBindings.GetCommands (Key.A);
+        Assert.DoesNotContain (Command.HotKey, resultCommands);
+
+        keyBindings = new KeyBindings ();
+        keyBindings.Add (Key.A, new KeyBinding (new [] { Command.HotKey }, KeyBindingScope.HotKey));
+        keyBindings.Add (Key.A, new KeyBinding (new [] { Command.Accept }, KeyBindingScope.HotKey));
+
+        resultCommands = keyBindings.GetCommands (Key.A);
+        Assert.DoesNotContain (Command.HotKey, resultCommands);
+
+    }
+
     [Fact]
     [Fact]
     public void Replace_Key ()
     public void Replace_Key ()
     {
     {
@@ -173,17 +207,17 @@ public class KeyBindingTests
         Assert.Empty (keyBindings.GetCommands (Key.A));
         Assert.Empty (keyBindings.GetCommands (Key.A));
         Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
         Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
 
 
-        keyBindings.Replace (Key.B, Key.E);
+        keyBindings.Replace (Key.B, Key.F);
         Assert.Empty (keyBindings.GetCommands (Key.B));
         Assert.Empty (keyBindings.GetCommands (Key.B));
-        Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
+        Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.F));
 
 
-        keyBindings.Replace (Key.C, Key.E);
+        keyBindings.Replace (Key.C, Key.G);
         Assert.Empty (keyBindings.GetCommands (Key.C));
         Assert.Empty (keyBindings.GetCommands (Key.C));
-        Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
+        Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.G));
 
 
-        keyBindings.Replace (Key.D, Key.E);
+        keyBindings.Replace (Key.D, Key.H);
         Assert.Empty (keyBindings.GetCommands (Key.D));
         Assert.Empty (keyBindings.GetCommands (Key.D));
-        Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
+        Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.H));
     }
     }
 
 
     // Add with scope does the right things
     // Add with scope does the right things