Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
feat: make CommandRunner a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-wff committed Dec 2, 2023
1 parent 9ce2523 commit dcbcfee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 21 additions & 0 deletions EasyCLI/CommandRunner/CommandRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@ namespace EasyCLI.CommandRunner;
/// </summary>
public sealed class CommandRunner
{
/// <summary>
/// Contains the singleton instance of the CommandRunner.
/// </summary>
private static CommandRunner? _instance;

/// <summary>
/// Makes the constructor private to prevent instantiation outside of the class.
/// </summary>
private CommandRunner()
{
}

/// <summary>
/// Gets the collection of commands registered with the CommandRunner.
/// </summary>
public List<Command> Commands { get; } = new();

/// <summary>
/// Gets the singleton instance of the CommandRunner. If the instance does not exist, it is created.
/// </summary>
/// <returns>CommandRunner instance</returns>
public static CommandRunner GetInstance()
{
return _instance ??= new CommandRunner();
}

/// <summary>
/// Registers a command with the CommandRunner.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ public class CommandRunnerTests
public void RegisterCommand_ShouldAddCommandToCommands()
{
// Arrange
var commandRunner = new CommandRunner();
var commandRunner = CommandRunner.GetInstance();
var command = new Mock<MockCommand>();

// Act
commandRunner.RegisterCommand(command.Object);

// Assert
commandRunner.Commands.Should().Contain(command.Object);
CommandRunner.GetInstance().Commands.Should().Contain(command.Object);
}

[Fact]
public void RunWithArgs_ShouldReturnFalse_WhenNoCommandMatches()
{
// Arrange
var commandRunner = new CommandRunner();
var commandRunner = CommandRunner.GetInstance();
var args = new List<string> { "command" };

// Act
Expand Down Expand Up @@ -55,7 +55,7 @@ public void RunWithArgs_ShouldCallRun_WhenCommandMatches()
public void GetCommandFromArgs_ShouldReturnNull_WhenNoCommandMatches()
{
// Arrange
var commandRunner = new CommandRunner();
var commandRunner = CommandRunner.GetInstance();
var args = new List<string> { "command" };

// Act
Expand Down

0 comments on commit dcbcfee

Please sign in to comment.