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

Add testing documentation #1416

Closed
wants to merge 2 commits into from
Closed
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
60 changes: 60 additions & 0 deletions docs/input/testing/cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Title: Testing Spectre CLI Apps
Description: "Test Spectre CLI features,functionality, output, and performance."
---

The `CommandAppTester` class is used to test a Spectre CLI application.
It should be used to test a `CommandApp` `Command`s and `CommandSetting`s.

## CommandAppTester

The `CommandAppTester` class is a wrapper around the `CommandApp` object configured for testing.

### Example

```csharp
public class Program
{
public static int Main(string[] args)
{
var app = new CommandApp();
app.Configure(appConfig);
var result = app.Run(args);
return result;
}

public static void appConfig(IConfigurator config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this to something like ConfigureCommandApp?

{
config.AddCommand<HelloCommand>("hello");
}
}

public class HelloCommand : Command
{
public override int Execute(CommandContext context)
{
AnsiConsole.WriteLine("Hello world");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Command should not use AnsiConsole directly, since this doesn't play well with testing. The command should inject a IAnsiConsole and use that.

return 0;
}
}

public class ProgramTests
{
[Fact]
public void Should_Return_Hello_World()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great example! 👍

{
// Given
var app = new CommandAppTester();
app.Configure(Program.appConfig);

// When
var result = app.Run("hello");

// Then
Assert.Multiple(() =>
{
Assert.Equal(0, result.ExitCode);
Assert.Equal("Hello world", result.Output);
});
}
}
```
27 changes: 27 additions & 0 deletions docs/input/testing/console.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Title: Testing Spectre Console
Description: "Test Spectre Console features,functionality, output, and performance."
---

The `TestConsole` class is used to test the output of `AnsiConsole`.

## TestConsole

The `TestConsole` class is a wrapper around the `AnsiConsole` object configured for testing, with no (current) support for input.

## Example

```csharp
public class ProgramTests
{
[Fact]
public void Should_Return_Hello_World()
{
// Given
var console = new TestConsole();
// When
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add line break

console.WriteLine("Hello world");
// Then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Add line break

Assert.Equal("Hello world" + Environment.NewLine, console.Output);
}
}
```
15 changes: 15 additions & 0 deletions docs/input/testing/index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Title: Spectre.Console.Testing
Order: 90
Description: "The testing suite for the command line interface and spectre console...."
---

<p>Spectre.Console.Testing is the namespace for testing suite of Spectre Console and CLI.
The <a href="https://github.com/VerifyTests/Verify">Verify</a> package can also be used to verify console output.
</p>

<ul>
@foreach(IDocument child in OutputPages.GetChildrenOf(Document))
{
<li>@Html.DocumentLink(child)</li>
}
</ul>
Loading