mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-04 10:35:27 +08:00 
			
		
		
		
	Async command unit tests
This commit is contained in:
		@@ -0,0 +1,28 @@
 | 
			
		||||
namespace Spectre.Console.Tests.Data;
 | 
			
		||||
 | 
			
		||||
public sealed class AsynchronousCommand : AsyncCommand<AsynchronousCommandSettings>
 | 
			
		||||
{
 | 
			
		||||
    private readonly IAnsiConsole _console;
 | 
			
		||||
 | 
			
		||||
    public AsynchronousCommand(IAnsiConsole console)
 | 
			
		||||
    {
 | 
			
		||||
        _console = console;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async override Task<int> ExecuteAsync(CommandContext context, AsynchronousCommandSettings settings)
 | 
			
		||||
    {
 | 
			
		||||
        // Simulate a long running asynchronous task
 | 
			
		||||
        await Task.Delay(200);
 | 
			
		||||
 | 
			
		||||
        if (settings.ThrowException)
 | 
			
		||||
        {
 | 
			
		||||
            throw new Exception($"Throwing exception asynchronously");
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            _console.WriteLine($"Finished executing asynchronously");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,8 +6,4 @@ public sealed class ThrowingCommand : Command<ThrowingCommandSettings>
 | 
			
		||||
    {
 | 
			
		||||
        throw new InvalidOperationException("W00t?");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
public sealed class ThrowingCommandSettings : CommandSettings
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,8 @@
 | 
			
		||||
namespace Spectre.Console.Tests.Data;
 | 
			
		||||
 | 
			
		||||
public sealed class AsynchronousCommandSettings : CommandSettings
 | 
			
		||||
{
 | 
			
		||||
    [CommandOption("--ThrowException")]
 | 
			
		||||
    [DefaultValue(false)]
 | 
			
		||||
    public bool ThrowException { get; set; }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,5 @@
 | 
			
		||||
namespace Spectre.Console.Tests.Data;
 | 
			
		||||
 | 
			
		||||
public sealed class ThrowingCommandSettings : CommandSettings
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user