Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename relay to action #94

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using BB84.Notifications.Interfaces;
using BB84.Notifications.Interfaces.Commands;

namespace BB84.Notifications;
namespace BB84.Notifications.Commands;

/// <summary>
/// The relay command class.
/// The action command class.
/// </summary>
/// <param name="execute">The action to execute.</param>
/// <param name="canExecute">The condition to execute.</param>
public sealed class RelayCommand(Action execute, Func<bool>? canExecute) : IRelayCommand
public sealed class ActionCommand(Action execute, Func<bool>? canExecute) : IActionCommand
{
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand"/> class that can always execute.
/// Initializes a new instance of the <see cref="ActionCommand"/> class that can always execute.
/// </summary>
/// <param name="execute">The action to execute.</param>
public RelayCommand(Action execute) : this(execute, null)
public ActionCommand(Action execute) : this(execute, null)
{ }

/// <inheritdoc/>
Expand Down Expand Up @@ -47,21 +47,21 @@ public void RaiseCanExecuteChanged()
}

/// <summary>
/// The relay command class.
/// The action command class.
/// </summary>
/// <remarks>
/// For all commands that need a parameter.
/// </remarks>
/// <typeparam name="T">The generic type to work with.</typeparam>
/// <param name="execute">The action to execute.</param>
/// <param name="canExecute">The condition to execute.</param>
public sealed class RelayCommand<T>(Action<T> execute, Func<T, bool>? canExecute) : IRelayCommand<T>
public sealed class ActionCommand<T>(Action<T> execute, Func<T, bool>? canExecute) : IActionCommand<T>
{
/// <summary>
/// Initializes a new instance of <see cref="RelayCommand{T}"/> class that can always execute.
/// Initializes a new instance of <see cref="ActionCommand{T}"/> class that can always execute.
/// </summary>
/// <param name="execute">The action to execute.</param>
public RelayCommand(Action<T> execute) : this(execute, null)
public ActionCommand(Action<T> execute) : this(execute, null)
{ }

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using BB84.Notifications.Interfaces;
using BB84.Notifications.Interfaces.Commands;

namespace BB84.Notifications;
namespace BB84.Notifications.Commands;

/// <summary>
/// The async relay command class.
/// The async action command class.
/// </summary>
/// <param name="execute">The task to execute.</param>
/// <param name="canExecute">The condition to execute.</param>
public sealed class AsyncRelayCommand(Func<Task> execute, Func<bool>? canExecute) : IAsyncRelayCommand
public sealed class AsyncActionCommand(Func<Task> execute, Func<bool>? canExecute) : IAsyncActionCommand
{
private bool _isExecuting;

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

/// <inheritdoc/>
Expand Down Expand Up @@ -58,23 +58,23 @@ public void RaiseCanExecuteChanged()
}

/// <summary>
/// The async relay command class.
/// The async action command class.
/// </summary>
/// <remarks>
/// For all commands that need a parameter.
/// </remarks>
/// <typeparam name="T">The generic type to work with.</typeparam>
/// <param name="execute">The task to execute.</param>
/// <param name="canExecute">The condition to execute.</param>
public sealed class AsyncRelayCommand<T>(Func<T, Task> execute, Func<T, bool>? canExecute) : IAsyncRelayCommand<T>
public sealed class AsyncActionCommand<T>(Func<T, Task> execute, Func<T, bool>? canExecute) : IAsyncActionCommand<T>
{
private bool _isExecuting;

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

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Windows.Input;

namespace BB84.Notifications.Interfaces;
namespace BB84.Notifications.Interfaces.Commands;

/// <summary>
/// The relay command interface.
/// The action command interface.
/// </summary>
public interface IRelayCommand : ICommand
public interface IActionCommand : ICommand
{
/// <summary>
/// Defines the method that determines whether the command can execute in its current state.
Expand All @@ -25,10 +25,10 @@ public interface IRelayCommand : ICommand
}

/// <summary>
/// The relay command interface.
/// The action command interface.
/// </summary>
/// <typeparam name="T">The type to work with.</typeparam>
public interface IRelayCommand<T> : ICommand
/// <typeparam name="T">The generic type to work with.</typeparam>
public interface IActionCommand<T> : ICommand
{
/// <summary>
/// Defines the method that determines whether the command can execute in its current state.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Windows.Input;

namespace BB84.Notifications.Interfaces;
namespace BB84.Notifications.Interfaces.Commands;

/// <summary>
/// The async relay command interface.
/// The async action command interface.
/// </summary>
public interface IAsyncRelayCommand : ICommand
public interface IAsyncActionCommand : ICommand
{
/// <summary>
/// Defines the method to be called when the command is invoked.
Expand All @@ -26,10 +26,10 @@ public interface IAsyncRelayCommand : ICommand
}

/// <summary>
/// The async relay command interface.
/// The async action command interface.
/// </summary>
/// <typeparam name="T">The generic type to wor with.</typeparam>
public interface IAsyncRelayCommand<T> : ICommand
/// <typeparam name="T">The generic type to work with.</typeparam>
public interface IAsyncActionCommand<T> : ICommand
{
/// <summary>
/// Defines the method to be called when the command is invoked.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BB84.NotificationsTests;

public sealed partial class RelayCommandTests
public sealed partial class ActionCommandTests
{
[TestMethod]
public void CanExecute()
Expand All @@ -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]
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
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]
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace BB84.NotificationsTests;

public sealed partial class RelayCommandTests
public sealed partial class ActionCommandTests
{
[TestMethod]
public void RaiseCanExecuteChanged()
Expand Down
38 changes: 38 additions & 0 deletions tests/BB84.NotificationsTests/Commands/ActionCommandTests.cs
Original file line number Diff line number Diff line change
@@ -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<int>? _intCommand;
private IActionCommand<int>? _condIntCommand;

public TestClass()
{
CommandExecution = default;
IntegerCommandExecution = default;
}

public IActionCommand Command
=> _command ??= new ActionCommand(() => CommandExecution++);

public IActionCommand CondCommand
=> _condCommand ??= new ActionCommand(() => CommandExecution++, () => false);

public IActionCommand<int> IntCommand
=> _intCommand ??= new ActionCommand<int>(x => IntegerCommandExecution = x);

public IActionCommand<int> CondIntCommand
=> _condIntCommand ??= new ActionCommand<int>(x => IntegerCommandExecution = x, x => false);

public int CommandExecution { get; private set; }

public int IntegerCommandExecution { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
44 changes: 44 additions & 0 deletions tests/BB84.NotificationsTests/Commands/AsyncActionCommandTests.cs
Original file line number Diff line number Diff line change
@@ -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<int>? _intCommand;
private IAsyncActionCommand<int>? _condIntCommand;

public IAsyncActionCommand Command
=> _command ??= new AsyncActionCommand(CommandExecution);

public IAsyncActionCommand CondCommand
=> _condCommand ??= new AsyncActionCommand(CommandExecution, () => false);

public IAsyncActionCommand<int> IntCommand
=> _intCommand ??= new AsyncActionCommand<int>(IntegerCommandExecution);

public IAsyncActionCommand<int> CondIntCommand
=> _condIntCommand ??= new AsyncActionCommand<int>(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;
}
}
}
Loading