Skip to content

Commit

Permalink
Finished documentation, ready for review
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankRay78 committed Sep 17, 2024
1 parent 272d22b commit 5b613c0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
6 changes: 2 additions & 4 deletions docs/input/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ on the main thread.
### Unit Testing Best Practices

For testing of console output, Spectre.Console has [`IAnsiConsole`](xref:T:Spectre.Console.IAnsiConsole) that can be
injected into your application.
The [Spectre.Console.Test](https://www.nuget.org/packages/Spectre.Console.Testing/) contains a set of utilities for
capturing the output for verification, either manually or via a tool such
as [Verify](https://github.com/VerifyTests/Verify).
injected into your application. The [Spectre.Console.Test](https://www.nuget.org/packages/Spectre.Console.Testing/)
NuGet package contains utilities for capturing the console output for verification. See the [Unit Testing](cli/unit-testing) page for further guidance.

### Analyzer for Best Practices

Expand Down
60 changes: 58 additions & 2 deletions docs/input/cli/unit-testing.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Title: Unit Testing
Order: 14
Description: Instructions for unit testing a Spectre.Console application.
Reference: T:Spectre.Console.Testing.CommandAppTester
Reference:
- T:Spectre.Console.Testing.CommandAppTester
- T:Spectre.Console.Testing.TestConsole
- T:Spectre.Console.Testing.TestConsoleInput
---

`Spectre.Console` has a separate project that contains test harnesses for unit testing your own console applications.
Expand All @@ -14,6 +17,8 @@ The fastest way of getting started is to install the `Spectre.Console.Testing` N

`Spectre.Console.Testing` is also the namespace containing the test classes.

## Testing a CommandApp

The `CommandAppTester` is a test implementation of `CommandApp` that's configured in a similar manner but designed for unit testing.

The following example validates the exit code and terminal output of a `Spectre.Console` command:
Expand Down Expand Up @@ -56,4 +61,55 @@ The following example validates the exit code and terminal output of a `Spectre.
Assert.AreEqual(result.ExitCode, 0);
Assert.AreEqual(result.Output, "Hello world.");
}
```
```

## Testing console behaviour

`TestConsole` and `TestConsoleInput` are testable implementations of `IAnsiConsole` and `IAnsiConsoleInput`, allowing you fine-grain control over testing console output and interactivity.

The following example renders some widgets before then validating the console output:

```csharp
[TestMethod]
public void Should_Render_Panel()
{
// Given
var console = new TestConsole();

// When
console.Write(new Panel(new Text("Hello World")));

// Then
Assert.AreEqual(console.Output, """"
┌─────────────┐
│ Hello World │
└─────────────┘
"""");
}
```

While `Assert` is fine for validating simple output, more complex output may benefit from a tool like [Verify](https://github.com/VerifyTests/Verify).

The following example prompts the user for input before then validating the expected choice was made:

```csharp
[TestMethod]
public void Should_Select_Orange()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("Orange");

// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.AddChoice("Banana")
.AddChoice("Orange"));

// Then
Assert.AreEqual(console.Output, "Favorite fruit? [Banana/Orange]: Orange\n");
}
```

`CommandAppTester` uses `TestConsole` internally, which in turn uses `TestConsoleInput`, offering a fully testable harness for `Spectre.Console` widgets, prompts and commands.
3 changes: 2 additions & 1 deletion docs/input/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Spectre.Console.AnsiConsole has been heavily inspired by the excellent [Rich](ht

## Spectre.Console.Testing

* Spectre.Console has been developed with unit testing in mind. The Spectre.Console library itself is covered by an extensive test suite, project maintainers require test coverage for all new commits, and the same extension points and test harnesses used internally are made available to you.
* Spectre.Console has been developed with unit testing in mind. The Spectre.Console library itself is covered by an extensive test suite, project maintainers require test coverage for all new commits, and the same extension points and test harnesses used internally for testing are available to you.

* The [Unit Testing](cli/unit-testing) page provides instructions for testing a Spectre.Console application.

Expand All @@ -39,3 +39,4 @@ Spectre.Console.AnsiConsole has been heavily inspired by the excellent [Rich](ht
Sorry, your browser doesn't support embedded videos.
</video>

The Spectre.Console [examples repository](https://github.com/spectreconsole/examples) contains many other examples.

0 comments on commit 5b613c0

Please sign in to comment.