Merged with v2_develop.

Code cleanup
This commit is contained in:
Tig
2024-08-05 09:14:33 -06:00
14 changed files with 1090 additions and 1014 deletions

View File

@@ -4,17 +4,8 @@ namespace Terminal.Gui.InputTests;
public class KeyBindingTests
{
private readonly ITestOutputHelper _output;
public KeyBindingTests (ITestOutputHelper output) { _output = output; }
[Fact]
public void Add_No_Commands_Throws ()
{
var keyBindings = new KeyBindings ();
List<Command> commands = new ();
Assert.Throws<ArgumentException> (() => keyBindings.Add (Key.A, commands.ToArray ()));
}
private readonly ITestOutputHelper _output;
[Fact]
public void Add_Invalid_Key_Throws ()
@@ -41,6 +32,14 @@ public class KeyBindingTests
Assert.Contains (Command.Left, resultCommands);
}
[Fact]
public void Add_No_Commands_Throws ()
{
var keyBindings = new KeyBindings ();
List<Command> commands = new ();
Assert.Throws<ArgumentException> (() => keyBindings.Add (Key.A, commands.ToArray ()));
}
[Fact]
public void Add_Single_Adds ()
{
@@ -54,6 +53,39 @@ public class KeyBindingTests
Assert.Contains (Command.HotKey, resultCommands);
}
// Add should not allow duplicates
[Fact]
public void Add_Throws_If_Exists ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, KeyBindingScope.Application, Command.HotKey);
Assert.Throws<InvalidOperationException> (() => keyBindings.Add (Key.A, KeyBindingScope.Application, Command.Accept));
Command [] resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
keyBindings = new ();
keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.HotKey);
Assert.Throws<InvalidOperationException> (() => keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept));
resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
keyBindings = new ();
keyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.HotKey);
Assert.Throws<InvalidOperationException> (() => keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept));
resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
keyBindings = new ();
keyBindings.Add (Key.A, new KeyBinding (new [] { Command.HotKey }, KeyBindingScope.HotKey));
Assert.Throws<InvalidOperationException> (() => keyBindings.Add (Key.A, new KeyBinding (new [] { Command.Accept }, KeyBindingScope.HotKey)));
resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
}
// Clear
[Fact]
public void Clear_Clears ()
@@ -74,6 +106,14 @@ public class KeyBindingTests
Assert.Throws<InvalidOperationException> (() => keyBindings.GetKeyFromCommands (Command.Accept));
}
[Fact]
public void Get_Binding_Not_Found_Throws ()
{
var keyBindings = new KeyBindings ();
Assert.Throws<InvalidOperationException> (() => keyBindings.Get (Key.A));
Assert.Throws<InvalidOperationException> (() => keyBindings.Get (Key.B, KeyBindingScope.Application));
}
// GetCommands
[Fact]
public void GetCommands_Unknown_ReturnsEmpty ()
@@ -168,39 +208,6 @@ public class KeyBindingTests
Assert.Equal (Key.A, resultKey);
}
// Add should not allow duplicates
[Fact]
public void Add_Throws_If_Exists ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, KeyBindingScope.Application, Command.HotKey);
Assert.Throws<InvalidOperationException> (() => keyBindings.Add (Key.A, KeyBindingScope.Application, Command.Accept));
Command [] resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
keyBindings = new ();
keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.HotKey);
Assert.Throws<InvalidOperationException> (() => keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept));
resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
keyBindings = new ();
keyBindings.Add (Key.A, KeyBindingScope.HotKey, Command.HotKey);
Assert.Throws<InvalidOperationException> (() => keyBindings.Add (Key.A, KeyBindingScope.Focused, Command.Accept));
resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
keyBindings = new ();
keyBindings.Add (Key.A, new KeyBinding (new [] { Command.HotKey }, KeyBindingScope.HotKey));
Assert.Throws<InvalidOperationException> (() => keyBindings.Add (Key.A, new KeyBinding (new [] { Command.Accept }, KeyBindingScope.HotKey)));
resultCommands = keyBindings.GetCommands (Key.A);
Assert.Contains (Command.HotKey, resultCommands);
}
[Fact]
public void ReplaceKey_Replaces ()
{
@@ -227,6 +234,18 @@ public class KeyBindingTests
Assert.Contains (Command.HotKey, keyBindings.GetCommands (Key.H));
}
[Fact]
public void ReplaceKey_Replaces_Leaves_Old_Binding ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, KeyBindingScope.Application, Command.Accept);
keyBindings.Add (Key.B, KeyBindingScope.Application, Command.HotKey);
keyBindings.ReplaceKey (keyBindings.GetKeyFromCommands (Command.Accept), Key.C);
Assert.Empty (keyBindings.GetCommands (Key.A));
Assert.Contains (Command.Accept, keyBindings.GetCommands (Key.C));
}
[Fact]
public void ReplaceKey_Throws_If_DoesNotContain_Old ()
{
@@ -242,20 +261,6 @@ public class KeyBindingTests
Assert.Throws<InvalidOperationException> (() => keyBindings.ReplaceKey (Key.A, Key.Empty));
}
[Fact]
public void ReplaceKey_Replaces_Leaves_Old_Binding ()
{
var keyBindings = new KeyBindings ();
keyBindings.Add (Key.A, KeyBindingScope.Application, Command.Accept);
keyBindings.Add (Key.B, KeyBindingScope.Application, Command.HotKey);
keyBindings.ReplaceKey (keyBindings.GetKeyFromCommands(Command.Accept), Key.C);
Assert.Empty (keyBindings.GetCommands (Key.A));
Assert.Contains (Command.Accept, keyBindings.GetCommands (Key.C));
}
// Add with scope does the right things
[Theory]
[InlineData (KeyBindingScope.Focused)]
@@ -301,14 +306,6 @@ public class KeyBindingTests
Assert.Contains (Command.Left, binding.Commands);
}
[Fact]
public void Get_Binding_Not_Found_Throws ()
{
var keyBindings = new KeyBindings ();
Assert.Throws<InvalidOperationException> (() => keyBindings.Get (Key.A));
Assert.Throws<InvalidOperationException> (() => keyBindings.Get (Key.B, KeyBindingScope.Application));
}
[Theory]
[InlineData (KeyBindingScope.Focused)]
[InlineData (KeyBindingScope.HotKey)]
@@ -355,6 +352,4 @@ public class KeyBindingTests
Assert.True (result);
Assert.Contains (Command.HotKey, bindings.Commands);
}
}