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

WIP: Add programmatic constraint examples & surface in docs #813

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"ms-dotnettools.csharp",
"github.vscode-github-actions",
"ms-azuretools.vscode-docker",
"ms-dotnettools.csdevkit",
"stkb.rewrap"
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,10 @@ AllItemsConstraint(Constraint itemConstraint)

## Syntax

```csharp
Is.All...
Has.All...
```
You can use `Is.All` or `Has.All` to refer to all items in an `IEnumerable`.

## Examples of Use

```csharp
int[] iarray = new int[] { 1, 2, 3 };
string[] sarray = new string[] { "a", "b", "c" };
Assert.That(iarray, Is.All.Not.Null);
Assert.That(sarray, Is.All.InstanceOf<string>());
Assert.That(iarray, Is.All.GreaterThan(0));
Assert.That(iarray, Has.All.GreaterThan(0));
```
[!code-csharp[IsBasedFormat](~/snippets/Snippets.NUnit/Constraints/ConstraintExamples.cs#AllItemsIsExample)]

[!code-csharp[HasBasedFormat](~/snippets/Snippets.NUnit/Constraints/ConstraintExamples.cs#AllItemsHasExample)]
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ AndConstraint(Constraint left, Constraint right)

## Examples of Use

```csharp
Assert.That(2.3, Is.GreaterThan(2.0).And.LessThan(3.0));
```
[!code-csharp[AndExample](~/snippets/Snippets.NUnit/Constraints/ConstraintExamples.cs#AndExample)]

## Evaluation Order and Precedence

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ Is.AnyOf(object[] expected)

## Examples of Use

```csharp
Assert.That(42, Is.AnyOf(0, -1, 42, 100));
[!code-csharp[AnyOfExample](~/snippets/Snippets.NUnit/Constraints/ConstraintExamples.cs#AnyOfExample)]

// You can use a custom comparer as well
Assert.That(myOwnObject, Is.AnyOf(myArray).Using(myComparer));
```
[!code-csharp[AnyOfWithCustomComparison](~/snippets/Snippets.NUnit/Constraints/ConstraintExamples.cs#AnyOfWithCustomComparison)]
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,4 @@ Is.AssignableFrom<T>()

## Examples of Use

```csharp
Assert.That("Hello", Is.AssignableFrom(typeof(string)));
Assert.That(5, Is.Not.AssignableFrom(typeof(string)));
```
[!code-csharp[AssignableFromExample](~/snippets/Snippets.NUnit/Constraints/ConstraintExamples.cs#AssignableFromExample)]
230 changes: 116 additions & 114 deletions docs/articles/nunit/writing-tests/constraints/Constraints.md

Large diffs are not rendered by default.

71 changes: 71 additions & 0 deletions docs/snippets/Snippets.NUnit/Constraints/ConstraintExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections;
using NUnit.Framework;

namespace Snippets.NUnit.Attributes
{
public class ConstraintExamples
{
#region AllItemsIsExample
[Test]
public void CanUseIsToTalkAboutAllItems()
{
int[] arrayOfIntegers = new int[] { 1, 2, 3 };
string[] arrayOfStrings = new string[] { "a", "b", "c" };

Assert.That(arrayOfIntegers, Is.All.Not.Null);
Assert.That(arrayOfIntegers, Is.All.GreaterThan(0));

Assert.That(arrayOfStrings, Is.All.InstanceOf<string>());
}
#endregion

#region AllItemsHasExample
[Test]
public void CanUseHasToTalkAboutAllItems()
{
int[] arrayOfIntegers = new int[] { 1, 2, 3 };

Assert.That(arrayOfIntegers, Has.All.GreaterThan(0));
}
#endregion
#region AssignableFromExample
[Test]
public void AssignableFromExample()
{
var theString = "Hello";
Assert.That(theString, Is.AssignableFrom(typeof(string)));

var theInt = 5;
Assert.That(theInt, Is.Not.AssignableFrom<string>());
}
#endregion
#region AndExample
[Test]
public void UsingAndToCombineConstraints()
{
var testValue = 2.3;
Assert.That(testValue, Is.GreaterThan(2.0).And.LessThan(3.0));
}
#endregion
#region AnyOfExample
[Test]
public void AnyOfCanDetectIfAnItemMatchesAnArrayOfParams()
{
var testValue = 42;
Assert.That(testValue, Is.AnyOf(0, -1, 42, 100));
}
#endregion
#region AnyOfWithCustomComparison
[Test]
public void AnyOfCanUseCustomComparisons()
{
var testValue = "NUnit";

// NOTE: Here we cast our comparison to IComparer, because StringComparer implements
// multiple interfaces that NUnit custom comparisons supports.
Assert.That(testValue, Is.AnyOf("hello", "world", "nunit").Using((IComparer)StringComparer.InvariantCultureIgnoreCase));
}
#endregion
}

}