From 34d65a168080cc9c178cdbecc007331d4e1e9967 Mon Sep 17 00:00:00 2001 From: BoBoBaSs84 <73112377+BoBoBaSs84@users.noreply.github.com> Date: Sun, 21 Apr 2024 10:38:58 +0200 Subject: [PATCH] refactor: rename relay to action changes: - added unit testing for async actions - renamed and refactored the interfaces and implementations closes #92 --- .../ActionCommand.cs} | 20 ++++----- .../AsyncActionCommand.cs} | 20 ++++----- .../IActionCommand.cs} | 12 ++--- .../IAsyncActionCommand.cs} | 12 ++--- .../ActionCommandTests.CanExecute.cs} | 9 ++-- .../ActionCommandTests.Execute.cs} | 13 +++--- ...ionCommandTests.RaiseCanExecuteChanged.cs} | 2 +- .../Commands/ActionCommandTests.cs | 38 ++++++++++++++++ .../AsyncActionCommandTests.CanExecute.cs | 30 +++++++++++++ .../AsyncActionCommandTests.Execute.cs | 26 +++++++++++ ...tionCommandTests.RaiseCanExecuteChanged.cs | 21 +++++++++ .../Commands/AsyncActionCommandTests.cs | 44 +++++++++++++++++++ .../RelayCommandTests.cs | 38 ---------------- 13 files changed, 204 insertions(+), 81 deletions(-) rename src/BB84.Notifications/{RelayCommand.cs => Commands/ActionCommand.cs} (75%) rename src/BB84.Notifications/{AsyncRelayCommand.cs => Commands/AsyncActionCommand.cs} (75%) rename src/BB84.Notifications/Interfaces/{IRelayCommand.cs => Commands/IActionCommand.cs} (82%) rename src/BB84.Notifications/Interfaces/{IAsyncRelayCommand.cs => Commands/IAsyncActionCommand.cs} (82%) rename tests/BB84.NotificationsTests/{RelayCommandTests.CanExecute.cs => Commands/ActionCommandTests.CanExecute.cs} (57%) rename tests/BB84.NotificationsTests/{RelayCommandTests.Execute.cs => Commands/ActionCommandTests.Execute.cs} (53%) rename tests/BB84.NotificationsTests/{RelayCommandTests.RaiseCanExecuteChanged.cs => Commands/ActionCommandTests.RaiseCanExecuteChanged.cs} (92%) create mode 100644 tests/BB84.NotificationsTests/Commands/ActionCommandTests.cs create mode 100644 tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.CanExecute.cs create mode 100644 tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.Execute.cs create mode 100644 tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.RaiseCanExecuteChanged.cs create mode 100644 tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.cs delete mode 100644 tests/BB84.NotificationsTests/RelayCommandTests.cs diff --git a/src/BB84.Notifications/RelayCommand.cs b/src/BB84.Notifications/Commands/ActionCommand.cs similarity index 75% rename from src/BB84.Notifications/RelayCommand.cs rename to src/BB84.Notifications/Commands/ActionCommand.cs index 7217a7b..d00afcc 100644 --- a/src/BB84.Notifications/RelayCommand.cs +++ b/src/BB84.Notifications/Commands/ActionCommand.cs @@ -1,19 +1,19 @@ -using BB84.Notifications.Interfaces; +using BB84.Notifications.Interfaces.Commands; -namespace BB84.Notifications; +namespace BB84.Notifications.Commands; /// -/// The relay command class. +/// The action command class. /// /// The action to execute. /// The condition to execute. -public sealed class RelayCommand(Action execute, Func? canExecute) : IRelayCommand +public sealed class ActionCommand(Action execute, Func? canExecute) : IActionCommand { /// - /// Initializes a new instance of the class that can always execute. + /// Initializes a new instance of the class that can always execute. /// /// The action to execute. - public RelayCommand(Action execute) : this(execute, null) + public ActionCommand(Action execute) : this(execute, null) { } /// @@ -47,7 +47,7 @@ public void RaiseCanExecuteChanged() } /// -/// The relay command class. +/// The action command class. /// /// /// For all commands that need a parameter. @@ -55,13 +55,13 @@ public void RaiseCanExecuteChanged() /// The generic type to work with. /// The action to execute. /// The condition to execute. -public sealed class RelayCommand(Action execute, Func? canExecute) : IRelayCommand +public sealed class ActionCommand(Action execute, Func? canExecute) : IActionCommand { /// - /// Initializes a new instance of class that can always execute. + /// Initializes a new instance of class that can always execute. /// /// The action to execute. - public RelayCommand(Action execute) : this(execute, null) + public ActionCommand(Action execute) : this(execute, null) { } /// diff --git a/src/BB84.Notifications/AsyncRelayCommand.cs b/src/BB84.Notifications/Commands/AsyncActionCommand.cs similarity index 75% rename from src/BB84.Notifications/AsyncRelayCommand.cs rename to src/BB84.Notifications/Commands/AsyncActionCommand.cs index a427704..b7c7d80 100644 --- a/src/BB84.Notifications/AsyncRelayCommand.cs +++ b/src/BB84.Notifications/Commands/AsyncActionCommand.cs @@ -1,21 +1,21 @@ -using BB84.Notifications.Interfaces; +using BB84.Notifications.Interfaces.Commands; -namespace BB84.Notifications; +namespace BB84.Notifications.Commands; /// -/// The async relay command class. +/// The async action command class. /// /// The task to execute. /// The condition to execute. -public sealed class AsyncRelayCommand(Func execute, Func? canExecute) : IAsyncRelayCommand +public sealed class AsyncActionCommand(Func execute, Func? canExecute) : IAsyncActionCommand { private bool _isExecuting; /// - /// Initializes a new instance of the class that can always execute. + /// Initializes a new instance of the class that can always execute. /// /// The task to execute. - public AsyncRelayCommand(Func execute) : this(execute, null) + public AsyncActionCommand(Func execute) : this(execute, null) { } /// @@ -58,7 +58,7 @@ public void RaiseCanExecuteChanged() } /// -/// The async relay command class. +/// The async action command class. /// /// /// For all commands that need a parameter. @@ -66,15 +66,15 @@ public void RaiseCanExecuteChanged() /// The generic type to work with. /// The task to execute. /// The condition to execute. -public sealed class AsyncRelayCommand(Func execute, Func? canExecute) : IAsyncRelayCommand +public sealed class AsyncActionCommand(Func execute, Func? canExecute) : IAsyncActionCommand { private bool _isExecuting; /// - /// Initializes a new instance of class that can always execute. + /// Initializes a new instance of class that can always execute. /// /// The task to execute. - public AsyncRelayCommand(Func execute) : this(execute, null) + public AsyncActionCommand(Func execute) : this(execute, null) { } /// diff --git a/src/BB84.Notifications/Interfaces/IRelayCommand.cs b/src/BB84.Notifications/Interfaces/Commands/IActionCommand.cs similarity index 82% rename from src/BB84.Notifications/Interfaces/IRelayCommand.cs rename to src/BB84.Notifications/Interfaces/Commands/IActionCommand.cs index 692e745..02ca099 100644 --- a/src/BB84.Notifications/Interfaces/IRelayCommand.cs +++ b/src/BB84.Notifications/Interfaces/Commands/IActionCommand.cs @@ -1,11 +1,11 @@ using System.Windows.Input; -namespace BB84.Notifications.Interfaces; +namespace BB84.Notifications.Interfaces.Commands; /// -/// The relay command interface. +/// The action command interface. /// -public interface IRelayCommand : ICommand +public interface IActionCommand : ICommand { /// /// Defines the method that determines whether the command can execute in its current state. @@ -25,10 +25,10 @@ public interface IRelayCommand : ICommand } /// -/// The relay command interface. +/// The action command interface. /// -/// The type to work with. -public interface IRelayCommand : ICommand +/// The generic type to work with. +public interface IActionCommand : ICommand { /// /// Defines the method that determines whether the command can execute in its current state. diff --git a/src/BB84.Notifications/Interfaces/IAsyncRelayCommand.cs b/src/BB84.Notifications/Interfaces/Commands/IAsyncActionCommand.cs similarity index 82% rename from src/BB84.Notifications/Interfaces/IAsyncRelayCommand.cs rename to src/BB84.Notifications/Interfaces/Commands/IAsyncActionCommand.cs index 8ac31f1..e92bb2f 100644 --- a/src/BB84.Notifications/Interfaces/IAsyncRelayCommand.cs +++ b/src/BB84.Notifications/Interfaces/Commands/IAsyncActionCommand.cs @@ -1,11 +1,11 @@ using System.Windows.Input; -namespace BB84.Notifications.Interfaces; +namespace BB84.Notifications.Interfaces.Commands; /// -/// The async relay command interface. +/// The async action command interface. /// -public interface IAsyncRelayCommand : ICommand +public interface IAsyncActionCommand : ICommand { /// /// Defines the method to be called when the command is invoked. @@ -26,10 +26,10 @@ public interface IAsyncRelayCommand : ICommand } /// -/// The async relay command interface. +/// The async action command interface. /// -/// The generic type to wor with. -public interface IAsyncRelayCommand : ICommand +/// The generic type to work with. +public interface IAsyncActionCommand : ICommand { /// /// Defines the method to be called when the command is invoked. diff --git a/tests/BB84.NotificationsTests/RelayCommandTests.CanExecute.cs b/tests/BB84.NotificationsTests/Commands/ActionCommandTests.CanExecute.cs similarity index 57% rename from tests/BB84.NotificationsTests/RelayCommandTests.CanExecute.cs rename to tests/BB84.NotificationsTests/Commands/ActionCommandTests.CanExecute.cs index bbeb26d..d54781a 100644 --- a/tests/BB84.NotificationsTests/RelayCommandTests.CanExecute.cs +++ b/tests/BB84.NotificationsTests/Commands/ActionCommandTests.CanExecute.cs @@ -1,6 +1,6 @@ namespace BB84.NotificationsTests; -public sealed partial class RelayCommandTests +public sealed partial class ActionCommandTests { [TestMethod] public void CanExecute() @@ -9,8 +9,10 @@ public void CanExecute() test = new TestClass(); - Assert.IsTrue(test.Command.CanExecute(this)); - Assert.IsFalse(test.CondCommand.CanExecute(this)); + Assert.IsTrue(test.Command.CanExecute()); + Assert.IsTrue(test.Command.CanExecute(null)); + Assert.IsFalse(test.CondCommand.CanExecute()); + Assert.IsFalse(test.CondCommand.CanExecute(null)); } [TestMethod] @@ -23,5 +25,6 @@ public void CanExecuteWithParam() Assert.IsTrue(test.IntCommand.CanExecute(1)); Assert.IsTrue(test.IntCommand.CanExecute((object)1)); Assert.IsFalse(test.CondIntCommand.CanExecute(1)); + Assert.IsFalse(test.CondIntCommand.CanExecute((object)1)); } } diff --git a/tests/BB84.NotificationsTests/RelayCommandTests.Execute.cs b/tests/BB84.NotificationsTests/Commands/ActionCommandTests.Execute.cs similarity index 53% rename from tests/BB84.NotificationsTests/RelayCommandTests.Execute.cs rename to tests/BB84.NotificationsTests/Commands/ActionCommandTests.Execute.cs index dd8decb..e47d664 100644 --- a/tests/BB84.NotificationsTests/RelayCommandTests.Execute.cs +++ b/tests/BB84.NotificationsTests/Commands/ActionCommandTests.Execute.cs @@ -1,15 +1,15 @@ namespace BB84.NotificationsTests; -public sealed partial class RelayCommandTests +public sealed partial class ActionCommandTests { [TestMethod] public void Execute() { TestClass test = new(); - test.Command.Execute(this); - - Assert.AreEqual(1, test.CommandExecution); + test.Command.Execute(); + test.Command.Execute(null); + Assert.AreEqual(2, test.CommandExecution); } [TestMethod] @@ -17,9 +17,8 @@ public void ExecuteWithParam() { TestClass test = new(); - test.IntCommand.Execute((object)1); - test.IntCommand.Execute(2); - + test.IntCommand.Execute(1); + test.IntCommand.Execute((object)2); Assert.AreEqual(2, test.IntegerCommandExecution); } } diff --git a/tests/BB84.NotificationsTests/RelayCommandTests.RaiseCanExecuteChanged.cs b/tests/BB84.NotificationsTests/Commands/ActionCommandTests.RaiseCanExecuteChanged.cs similarity index 92% rename from tests/BB84.NotificationsTests/RelayCommandTests.RaiseCanExecuteChanged.cs rename to tests/BB84.NotificationsTests/Commands/ActionCommandTests.RaiseCanExecuteChanged.cs index ec29670..661ef19 100644 --- a/tests/BB84.NotificationsTests/RelayCommandTests.RaiseCanExecuteChanged.cs +++ b/tests/BB84.NotificationsTests/Commands/ActionCommandTests.RaiseCanExecuteChanged.cs @@ -1,6 +1,6 @@ namespace BB84.NotificationsTests; -public sealed partial class RelayCommandTests +public sealed partial class ActionCommandTests { [TestMethod] public void RaiseCanExecuteChanged() diff --git a/tests/BB84.NotificationsTests/Commands/ActionCommandTests.cs b/tests/BB84.NotificationsTests/Commands/ActionCommandTests.cs new file mode 100644 index 0000000..3300a8e --- /dev/null +++ b/tests/BB84.NotificationsTests/Commands/ActionCommandTests.cs @@ -0,0 +1,38 @@ +using BB84.Notifications.Commands; +using BB84.Notifications.Interfaces.Commands; + +namespace BB84.NotificationsTests; + +[TestClass] +public sealed partial class ActionCommandTests +{ + private sealed class TestClass + { + private IActionCommand? _command; + private IActionCommand? _condCommand; + private IActionCommand? _intCommand; + private IActionCommand? _condIntCommand; + + public TestClass() + { + CommandExecution = default; + IntegerCommandExecution = default; + } + + public IActionCommand Command + => _command ??= new ActionCommand(() => CommandExecution++); + + public IActionCommand CondCommand + => _condCommand ??= new ActionCommand(() => CommandExecution++, () => false); + + public IActionCommand IntCommand + => _intCommand ??= new ActionCommand(x => IntegerCommandExecution = x); + + public IActionCommand CondIntCommand + => _condIntCommand ??= new ActionCommand(x => IntegerCommandExecution = x, x => false); + + public int CommandExecution { get; private set; } + + public int IntegerCommandExecution { get; private set; } + } +} diff --git a/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.CanExecute.cs b/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.CanExecute.cs new file mode 100644 index 0000000..b4c51d5 --- /dev/null +++ b/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.CanExecute.cs @@ -0,0 +1,30 @@ +namespace BB84.NotificationsTests.Commands; + +public sealed partial class AsyncActionCommandTests +{ + [TestMethod] + public void CanExecute() + { + TestClass? test; + + test = new(); + + Assert.IsTrue(test.Command.CanExecute()); + Assert.IsTrue(test.Command.CanExecute(null)); + Assert.IsFalse(test.CondCommand.CanExecute()); + Assert.IsFalse(test.CondCommand.CanExecute(null)); + } + + [TestMethod] + public void CanExecuteWithParams() + { + TestClass? test; + + test = new(); + + Assert.IsTrue(test.IntCommand.CanExecute(1)); + Assert.IsTrue(test.IntCommand.CanExecute((object)1)); + Assert.IsFalse(test.CondIntCommand.CanExecute(1)); + Assert.IsFalse(test.CondIntCommand.CanExecute((object)1)); + } +} diff --git a/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.Execute.cs b/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.Execute.cs new file mode 100644 index 0000000..ad314ab --- /dev/null +++ b/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.Execute.cs @@ -0,0 +1,26 @@ +namespace BB84.NotificationsTests.Commands; + +public sealed partial class AsyncActionCommandTests +{ + [TestMethod] + public async Task Execute() + { + TestClass test = new(); + + await test.Command.ExecuteAsync(); + test.Command.Execute(null); + + Assert.AreEqual(2, test.Execution); + } + + [TestMethod] + public async Task ExecuteWithParams() + { + TestClass test = new(); + + await test.IntCommand.ExecuteAsync(1); + test.IntCommand.Execute(2); + + Assert.AreEqual(2, test.IntegerExecution); + } +} diff --git a/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.RaiseCanExecuteChanged.cs b/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.RaiseCanExecuteChanged.cs new file mode 100644 index 0000000..5ed60e9 --- /dev/null +++ b/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.RaiseCanExecuteChanged.cs @@ -0,0 +1,21 @@ +namespace BB84.NotificationsTests.Commands; +public sealed partial class AsyncActionCommandTests +{ + [TestMethod] + public void RaiseCanExecuteChanged() + { + bool commandChanged = default; + bool intCommandChanged = default; + TestClass test = new(); + test.Command.CanExecuteChanged += (s, e) => commandChanged = true; + test.IntCommand.CanExecuteChanged += (s, e) => intCommandChanged = true; + + test.Command.RaiseCanExecuteChanged(); + test.CondCommand.RaiseCanExecuteChanged(); + test.IntCommand.RaiseCanExecuteChanged(); + test.CondIntCommand.RaiseCanExecuteChanged(); + + Assert.IsTrue(commandChanged); + Assert.IsTrue(intCommandChanged); + } +} diff --git a/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.cs b/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.cs new file mode 100644 index 0000000..73d1284 --- /dev/null +++ b/tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.cs @@ -0,0 +1,44 @@ +using BB84.Notifications.Commands; +using BB84.Notifications.Interfaces.Commands; + +namespace BB84.NotificationsTests.Commands; + +[TestClass] +public sealed partial class AsyncActionCommandTests +{ + private sealed class TestClass + { + private IAsyncActionCommand? _command; + private IAsyncActionCommand? _condCommand; + private IAsyncActionCommand? _intCommand; + private IAsyncActionCommand? _condIntCommand; + + public IAsyncActionCommand Command + => _command ??= new AsyncActionCommand(CommandExecution); + + public IAsyncActionCommand CondCommand + => _condCommand ??= new AsyncActionCommand(CommandExecution, () => false); + + public IAsyncActionCommand IntCommand + => _intCommand ??= new AsyncActionCommand(IntegerCommandExecution); + + public IAsyncActionCommand CondIntCommand + => _condIntCommand ??= new AsyncActionCommand(IntegerCommandExecution, x => false); + + public int Execution { get; private set; } + + public int IntegerExecution { get; private set; } + + private Task CommandExecution() + { + Execution++; + return Task.CompletedTask; + } + + private Task IntegerCommandExecution(int x) + { + IntegerExecution = x; + return Task.CompletedTask; + } + } +} diff --git a/tests/BB84.NotificationsTests/RelayCommandTests.cs b/tests/BB84.NotificationsTests/RelayCommandTests.cs deleted file mode 100644 index e10197e..0000000 --- a/tests/BB84.NotificationsTests/RelayCommandTests.cs +++ /dev/null @@ -1,38 +0,0 @@ -using BB84.Notifications; -using BB84.Notifications.Interfaces; - -namespace BB84.NotificationsTests; - -[TestClass] -public sealed partial class RelayCommandTests -{ - private sealed class TestClass - { - private IRelayCommand? _command; - private IRelayCommand? _condCommand; - private IRelayCommand? _intCommand; - private IRelayCommand? _condIntCommand; - - public TestClass() - { - CommandExecution = default; - IntegerCommandExecution = default; - } - - public IRelayCommand Command - => _command ??= new RelayCommand(() => CommandExecution++); - - public IRelayCommand CondCommand - => _condCommand ??= new RelayCommand(() => CommandExecution++, () => false); - - public IRelayCommand IntCommand - => _intCommand ??= new RelayCommand(x => IntegerCommandExecution = x); - - public IRelayCommand CondIntCommand - => _condIntCommand ??= new RelayCommand(x => IntegerCommandExecution = x, x => false); - - public int CommandExecution { get; private set; } - - public int IntegerCommandExecution { get; private set; } - } -}