mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-01 08:50:25 +01:00
Fixed KeyBindingTests
This commit is contained in:
@@ -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>
|
||||
public class KeyBindings
|
||||
@@ -27,7 +29,18 @@ public class KeyBindings
|
||||
/// <param name="binding"></param>
|
||||
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>
|
||||
@@ -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
|
||||
/// consumed if any took effect.
|
||||
/// </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>
|
||||
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>
|
||||
/// <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;
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
@@ -160,6 +160,40 @@ public class KeyBindingTests
|
||||
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]
|
||||
public void Replace_Key ()
|
||||
{
|
||||
@@ -173,17 +207,17 @@ public class KeyBindingTests
|
||||
Assert.Empty (keyBindings.GetCommands (Key.A));
|
||||
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.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.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.Contains (Command.HotKey, keyBindings.GetCommands (Key.E));
|
||||
Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.H));
|
||||
}
|
||||
|
||||
// Add with scope does the right things
|
||||
|
||||
Reference in New Issue
Block a user