diff --git a/Terminal.Gui/Input/KeyBinding.cs b/Terminal.Gui/Input/KeyBinding.cs index 84c60b30e..29d52f05d 100644 --- a/Terminal.Gui/Input/KeyBinding.cs +++ b/Terminal.Gui/Input/KeyBinding.cs @@ -1,10 +1,11 @@ -// These classes use a key binding system based on the design implemented in Scintilla.Net which is an +#nullable enable +// These classes use a key binding system based on the design implemented in Scintilla.Net which is an // MIT licensed open source project https://github.com/jacobslusser/ScintillaNET/blob/master/src/ScintillaNET/Command.cs namespace Terminal.Gui; /// Provides a collection of objects that are scoped to . -public class KeyBinding +public record struct KeyBinding { /// Initializes a new instance. /// diff --git a/Terminal.Gui/Input/KeyBindings.cs b/Terminal.Gui/Input/KeyBindings.cs index 2545d1a9a..5285e3c55 100644 --- a/Terminal.Gui/Input/KeyBindings.cs +++ b/Terminal.Gui/Input/KeyBindings.cs @@ -1,4 +1,4 @@ -using static System.Formats.Asn1.AsnWriter; +#nullable enable namespace Terminal.Gui; @@ -36,7 +36,7 @@ public class KeyBindings else { Bindings.Add (key, binding); - if (binding.Scope.HasFlag (KeyBindingScope.Application)) + if (binding.Scope.FastHasFlags (KeyBindingScope.Application)) { Application.AddKeyBinding (key, BoundView); } @@ -81,7 +81,7 @@ public class KeyBindings else { Add (key, new KeyBinding (commands, scope)); - if (scope.HasFlag (KeyBindingScope.Application)) + if (scope.FastHasFlags (KeyBindingScope.Application)) { Application.AddKeyBinding (key, BoundView); } @@ -145,13 +145,27 @@ public class KeyBindings /// Gets the for the specified . /// /// - public KeyBinding Get (Key key) { return TryGet (key, out KeyBinding binding) ? binding : null; } + public KeyBinding Get (Key key) + { + if (TryGet (key, out KeyBinding binding)) + { + return binding; + } + throw new InvalidOperationException ($"Key {key} is not bound."); + } /// Gets the for the specified . /// /// /// - public KeyBinding Get (Key key, KeyBindingScope scope) { return TryGet (key, scope, out KeyBinding binding) ? binding : null; } + public KeyBinding Get (Key key, KeyBindingScope scope) + { + if (TryGet (key, scope, out KeyBinding binding)) + { + return binding; + } + throw new InvalidOperationException ($"Key {key}/{scope} is not bound."); + } /// Gets the array of s bound to if it exists. /// The key to check. diff --git a/UnitTests/Input/KeyBindingTests.cs b/UnitTests/Input/KeyBindingTests.cs index c6980303a..73a1c6b00 100644 --- a/UnitTests/Input/KeyBindingTests.cs +++ b/UnitTests/Input/KeyBindingTests.cs @@ -261,14 +261,14 @@ public class KeyBindingTests binding = keyBindings.Get (key, scope); Assert.Contains (Command.Right, binding.Commands); Assert.Contains (Command.Left, binding.Commands); + } - // negative test - binding = keyBindings.Get (key, 0); - Assert.Null (binding); - - Command [] resultCommands = keyBindings.GetCommands (key); - Assert.Contains (Command.Right, resultCommands); - Assert.Contains (Command.Left, resultCommands); + [Fact] + public void Get_Binding_Not_Found_Throws () + { + var keyBindings = new KeyBindings (); + Assert.Throws (() => keyBindings.Get (Key.A)); + Assert.Throws (() => keyBindings.Get (Key.B, KeyBindingScope.Application)); } [Theory]