Skip to content

Commit ab93f9b

Browse files
authored
refactor: rename relay to action (#94)
changes: - added unit testing for async actions - renamed and refactored the interfaces and implementations closes #92
2 parents 1f9bc00 + 34d65a1 commit ab93f9b

13 files changed

+204
-81
lines changed

src/BB84.Notifications/RelayCommand.cs src/BB84.Notifications/Commands/ActionCommand.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
using BB84.Notifications.Interfaces;
1+
using BB84.Notifications.Interfaces.Commands;
22

3-
namespace BB84.Notifications;
3+
namespace BB84.Notifications.Commands;
44

55
/// <summary>
6-
/// The relay command class.
6+
/// The action command class.
77
/// </summary>
88
/// <param name="execute">The action to execute.</param>
99
/// <param name="canExecute">The condition to execute.</param>
10-
public sealed class RelayCommand(Action execute, Func<bool>? canExecute) : IRelayCommand
10+
public sealed class ActionCommand(Action execute, Func<bool>? canExecute) : IActionCommand
1111
{
1212
/// <summary>
13-
/// Initializes a new instance of the <see cref="RelayCommand"/> class that can always execute.
13+
/// Initializes a new instance of the <see cref="ActionCommand"/> class that can always execute.
1414
/// </summary>
1515
/// <param name="execute">The action to execute.</param>
16-
public RelayCommand(Action execute) : this(execute, null)
16+
public ActionCommand(Action execute) : this(execute, null)
1717
{ }
1818

1919
/// <inheritdoc/>
@@ -47,21 +47,21 @@ public void RaiseCanExecuteChanged()
4747
}
4848

4949
/// <summary>
50-
/// The relay command class.
50+
/// The action command class.
5151
/// </summary>
5252
/// <remarks>
5353
/// For all commands that need a parameter.
5454
/// </remarks>
5555
/// <typeparam name="T">The generic type to work with.</typeparam>
5656
/// <param name="execute">The action to execute.</param>
5757
/// <param name="canExecute">The condition to execute.</param>
58-
public sealed class RelayCommand<T>(Action<T> execute, Func<T, bool>? canExecute) : IRelayCommand<T>
58+
public sealed class ActionCommand<T>(Action<T> execute, Func<T, bool>? canExecute) : IActionCommand<T>
5959
{
6060
/// <summary>
61-
/// Initializes a new instance of <see cref="RelayCommand{T}"/> class that can always execute.
61+
/// Initializes a new instance of <see cref="ActionCommand{T}"/> class that can always execute.
6262
/// </summary>
6363
/// <param name="execute">The action to execute.</param>
64-
public RelayCommand(Action<T> execute) : this(execute, null)
64+
public ActionCommand(Action<T> execute) : this(execute, null)
6565
{ }
6666

6767
/// <inheritdoc/>

src/BB84.Notifications/AsyncRelayCommand.cs src/BB84.Notifications/Commands/AsyncActionCommand.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
using BB84.Notifications.Interfaces;
1+
using BB84.Notifications.Interfaces.Commands;
22

3-
namespace BB84.Notifications;
3+
namespace BB84.Notifications.Commands;
44

55
/// <summary>
6-
/// The async relay command class.
6+
/// The async action command class.
77
/// </summary>
88
/// <param name="execute">The task to execute.</param>
99
/// <param name="canExecute">The condition to execute.</param>
10-
public sealed class AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute) : IAsyncRelayCommand
10+
public sealed class AsyncActionCommand(Func<Task> execute, Func<bool>? canExecute) : IAsyncActionCommand
1111
{
1212
private bool _isExecuting;
1313

1414
/// <summary>
15-
/// Initializes a new instance of the <see cref="AsyncRelayCommand"/> class that can always execute.
15+
/// Initializes a new instance of the <see cref="AsyncActionCommand"/> class that can always execute.
1616
/// </summary>
1717
/// <param name="execute">The task to execute.</param>
18-
public AsyncRelayCommand(Func<Task> execute) : this(execute, null)
18+
public AsyncActionCommand(Func<Task> execute) : this(execute, null)
1919
{ }
2020

2121
/// <inheritdoc/>
@@ -58,23 +58,23 @@ public void RaiseCanExecuteChanged()
5858
}
5959

6060
/// <summary>
61-
/// The async relay command class.
61+
/// The async action command class.
6262
/// </summary>
6363
/// <remarks>
6464
/// For all commands that need a parameter.
6565
/// </remarks>
6666
/// <typeparam name="T">The generic type to work with.</typeparam>
6767
/// <param name="execute">The task to execute.</param>
6868
/// <param name="canExecute">The condition to execute.</param>
69-
public sealed class AsyncRelayCommand<T>(Func<T, Task> execute, Func<T, bool>? canExecute) : IAsyncRelayCommand<T>
69+
public sealed class AsyncActionCommand<T>(Func<T, Task> execute, Func<T, bool>? canExecute) : IAsyncActionCommand<T>
7070
{
7171
private bool _isExecuting;
7272

7373
/// <summary>
74-
/// Initializes a new instance of <see cref="AsyncRelayCommand{T}"/> class that can always execute.
74+
/// Initializes a new instance of <see cref="AsyncActionCommand{T}"/> class that can always execute.
7575
/// </summary>
7676
/// <param name="execute">The task to execute.</param>
77-
public AsyncRelayCommand(Func<T, Task> execute) : this(execute, null)
77+
public AsyncActionCommand(Func<T, Task> execute) : this(execute, null)
7878
{ }
7979

8080
/// <inheritdoc/>

src/BB84.Notifications/Interfaces/IRelayCommand.cs src/BB84.Notifications/Interfaces/Commands/IActionCommand.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System.Windows.Input;
22

3-
namespace BB84.Notifications.Interfaces;
3+
namespace BB84.Notifications.Interfaces.Commands;
44

55
/// <summary>
6-
/// The relay command interface.
6+
/// The action command interface.
77
/// </summary>
8-
public interface IRelayCommand : ICommand
8+
public interface IActionCommand : ICommand
99
{
1010
/// <summary>
1111
/// Defines the method that determines whether the command can execute in its current state.
@@ -25,10 +25,10 @@ public interface IRelayCommand : ICommand
2525
}
2626

2727
/// <summary>
28-
/// The relay command interface.
28+
/// The action command interface.
2929
/// </summary>
30-
/// <typeparam name="T">The type to work with.</typeparam>
31-
public interface IRelayCommand<T> : ICommand
30+
/// <typeparam name="T">The generic type to work with.</typeparam>
31+
public interface IActionCommand<T> : ICommand
3232
{
3333
/// <summary>
3434
/// Defines the method that determines whether the command can execute in its current state.

src/BB84.Notifications/Interfaces/IAsyncRelayCommand.cs src/BB84.Notifications/Interfaces/Commands/IAsyncActionCommand.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System.Windows.Input;
22

3-
namespace BB84.Notifications.Interfaces;
3+
namespace BB84.Notifications.Interfaces.Commands;
44

55
/// <summary>
6-
/// The async relay command interface.
6+
/// The async action command interface.
77
/// </summary>
8-
public interface IAsyncRelayCommand : ICommand
8+
public interface IAsyncActionCommand : ICommand
99
{
1010
/// <summary>
1111
/// Defines the method to be called when the command is invoked.
@@ -26,10 +26,10 @@ public interface IAsyncRelayCommand : ICommand
2626
}
2727

2828
/// <summary>
29-
/// The async relay command interface.
29+
/// The async action command interface.
3030
/// </summary>
31-
/// <typeparam name="T">The generic type to wor with.</typeparam>
32-
public interface IAsyncRelayCommand<T> : ICommand
31+
/// <typeparam name="T">The generic type to work with.</typeparam>
32+
public interface IAsyncActionCommand<T> : ICommand
3333
{
3434
/// <summary>
3535
/// Defines the method to be called when the command is invoked.

tests/BB84.NotificationsTests/RelayCommandTests.CanExecute.cs tests/BB84.NotificationsTests/Commands/ActionCommandTests.CanExecute.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace BB84.NotificationsTests;
22

3-
public sealed partial class RelayCommandTests
3+
public sealed partial class ActionCommandTests
44
{
55
[TestMethod]
66
public void CanExecute()
@@ -9,8 +9,10 @@ public void CanExecute()
99

1010
test = new TestClass();
1111

12-
Assert.IsTrue(test.Command.CanExecute(this));
13-
Assert.IsFalse(test.CondCommand.CanExecute(this));
12+
Assert.IsTrue(test.Command.CanExecute());
13+
Assert.IsTrue(test.Command.CanExecute(null));
14+
Assert.IsFalse(test.CondCommand.CanExecute());
15+
Assert.IsFalse(test.CondCommand.CanExecute(null));
1416
}
1517

1618
[TestMethod]
@@ -23,5 +25,6 @@ public void CanExecuteWithParam()
2325
Assert.IsTrue(test.IntCommand.CanExecute(1));
2426
Assert.IsTrue(test.IntCommand.CanExecute((object)1));
2527
Assert.IsFalse(test.CondIntCommand.CanExecute(1));
28+
Assert.IsFalse(test.CondIntCommand.CanExecute((object)1));
2629
}
2730
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
namespace BB84.NotificationsTests;
22

3-
public sealed partial class RelayCommandTests
3+
public sealed partial class ActionCommandTests
44
{
55
[TestMethod]
66
public void Execute()
77
{
88
TestClass test = new();
99

10-
test.Command.Execute(this);
11-
12-
Assert.AreEqual(1, test.CommandExecution);
10+
test.Command.Execute();
11+
test.Command.Execute(null);
12+
Assert.AreEqual(2, test.CommandExecution);
1313
}
1414

1515
[TestMethod]
1616
public void ExecuteWithParam()
1717
{
1818
TestClass test = new();
1919

20-
test.IntCommand.Execute((object)1);
21-
test.IntCommand.Execute(2);
22-
20+
test.IntCommand.Execute(1);
21+
test.IntCommand.Execute((object)2);
2322
Assert.AreEqual(2, test.IntegerCommandExecution);
2423
}
2524
}

tests/BB84.NotificationsTests/RelayCommandTests.RaiseCanExecuteChanged.cs tests/BB84.NotificationsTests/Commands/ActionCommandTests.RaiseCanExecuteChanged.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace BB84.NotificationsTests;
22

3-
public sealed partial class RelayCommandTests
3+
public sealed partial class ActionCommandTests
44
{
55
[TestMethod]
66
public void RaiseCanExecuteChanged()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using BB84.Notifications.Commands;
2+
using BB84.Notifications.Interfaces.Commands;
3+
4+
namespace BB84.NotificationsTests;
5+
6+
[TestClass]
7+
public sealed partial class ActionCommandTests
8+
{
9+
private sealed class TestClass
10+
{
11+
private IActionCommand? _command;
12+
private IActionCommand? _condCommand;
13+
private IActionCommand<int>? _intCommand;
14+
private IActionCommand<int>? _condIntCommand;
15+
16+
public TestClass()
17+
{
18+
CommandExecution = default;
19+
IntegerCommandExecution = default;
20+
}
21+
22+
public IActionCommand Command
23+
=> _command ??= new ActionCommand(() => CommandExecution++);
24+
25+
public IActionCommand CondCommand
26+
=> _condCommand ??= new ActionCommand(() => CommandExecution++, () => false);
27+
28+
public IActionCommand<int> IntCommand
29+
=> _intCommand ??= new ActionCommand<int>(x => IntegerCommandExecution = x);
30+
31+
public IActionCommand<int> CondIntCommand
32+
=> _condIntCommand ??= new ActionCommand<int>(x => IntegerCommandExecution = x, x => false);
33+
34+
public int CommandExecution { get; private set; }
35+
36+
public int IntegerCommandExecution { get; private set; }
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace BB84.NotificationsTests.Commands;
2+
3+
public sealed partial class AsyncActionCommandTests
4+
{
5+
[TestMethod]
6+
public void CanExecute()
7+
{
8+
TestClass? test;
9+
10+
test = new();
11+
12+
Assert.IsTrue(test.Command.CanExecute());
13+
Assert.IsTrue(test.Command.CanExecute(null));
14+
Assert.IsFalse(test.CondCommand.CanExecute());
15+
Assert.IsFalse(test.CondCommand.CanExecute(null));
16+
}
17+
18+
[TestMethod]
19+
public void CanExecuteWithParams()
20+
{
21+
TestClass? test;
22+
23+
test = new();
24+
25+
Assert.IsTrue(test.IntCommand.CanExecute(1));
26+
Assert.IsTrue(test.IntCommand.CanExecute((object)1));
27+
Assert.IsFalse(test.CondIntCommand.CanExecute(1));
28+
Assert.IsFalse(test.CondIntCommand.CanExecute((object)1));
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace BB84.NotificationsTests.Commands;
2+
3+
public sealed partial class AsyncActionCommandTests
4+
{
5+
[TestMethod]
6+
public async Task Execute()
7+
{
8+
TestClass test = new();
9+
10+
await test.Command.ExecuteAsync();
11+
test.Command.Execute(null);
12+
13+
Assert.AreEqual(2, test.Execution);
14+
}
15+
16+
[TestMethod]
17+
public async Task ExecuteWithParams()
18+
{
19+
TestClass test = new();
20+
21+
await test.IntCommand.ExecuteAsync(1);
22+
test.IntCommand.Execute(2);
23+
24+
Assert.AreEqual(2, test.IntegerExecution);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace BB84.NotificationsTests.Commands;
2+
public sealed partial class AsyncActionCommandTests
3+
{
4+
[TestMethod]
5+
public void RaiseCanExecuteChanged()
6+
{
7+
bool commandChanged = default;
8+
bool intCommandChanged = default;
9+
TestClass test = new();
10+
test.Command.CanExecuteChanged += (s, e) => commandChanged = true;
11+
test.IntCommand.CanExecuteChanged += (s, e) => intCommandChanged = true;
12+
13+
test.Command.RaiseCanExecuteChanged();
14+
test.CondCommand.RaiseCanExecuteChanged();
15+
test.IntCommand.RaiseCanExecuteChanged();
16+
test.CondIntCommand.RaiseCanExecuteChanged();
17+
18+
Assert.IsTrue(commandChanged);
19+
Assert.IsTrue(intCommandChanged);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using BB84.Notifications.Commands;
2+
using BB84.Notifications.Interfaces.Commands;
3+
4+
namespace BB84.NotificationsTests.Commands;
5+
6+
[TestClass]
7+
public sealed partial class AsyncActionCommandTests
8+
{
9+
private sealed class TestClass
10+
{
11+
private IAsyncActionCommand? _command;
12+
private IAsyncActionCommand? _condCommand;
13+
private IAsyncActionCommand<int>? _intCommand;
14+
private IAsyncActionCommand<int>? _condIntCommand;
15+
16+
public IAsyncActionCommand Command
17+
=> _command ??= new AsyncActionCommand(CommandExecution);
18+
19+
public IAsyncActionCommand CondCommand
20+
=> _condCommand ??= new AsyncActionCommand(CommandExecution, () => false);
21+
22+
public IAsyncActionCommand<int> IntCommand
23+
=> _intCommand ??= new AsyncActionCommand<int>(IntegerCommandExecution);
24+
25+
public IAsyncActionCommand<int> CondIntCommand
26+
=> _condIntCommand ??= new AsyncActionCommand<int>(IntegerCommandExecution, x => false);
27+
28+
public int Execution { get; private set; }
29+
30+
public int IntegerExecution { get; private set; }
31+
32+
private Task CommandExecution()
33+
{
34+
Execution++;
35+
return Task.CompletedTask;
36+
}
37+
38+
private Task IntegerCommandExecution(int x)
39+
{
40+
IntegerExecution = x;
41+
return Task.CompletedTask;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)