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 #810

Merged
merged 19 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,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/AllItemsConstraintExamples.cs#AllItemsIsExample)]

[!code-csharp[HasBasedFormat](~/snippets/Snippets.NUnit/Constraints/AllItemsConstraintExamples.cs#AllItemsHasExample)]
226 changes: 113 additions & 113 deletions docs/articles/nunit/writing-tests/constraints/Constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,141 +11,141 @@ This page lists all the constraints available in NUnit.

## Alphabetical List of Constraints

Constraint Name |
-------------------------------------|
[AllItemsConstraint](AllItemsConstraint.md) |
[AndConstraint](AndConstraint.md) |
[AssignableFromConstraint](AssignableFromConstraint.md) |
[AssignableToConstraint](AssignableToConstraint.md) |
[AttributeConstraint](AttributeConstraint.md) |
[AttributeExistsConstraint](AttributeExistsConstraint.md) |
[AnyOfConstraint](AnyOfConstraint.md) |
[BinarySerializableConstraint](BinarySerializableConstraint.md) |
[CollectionContainsConstraint](CollectionContainsConstraint.md) |
[CollectionEquivalentConstraint](CollectionEquivalentConstraint.md) |
[CollectionOrderedConstraint](CollectionOrderedConstraint.md) |
[CollectionSubsetConstraint](CollectionSubsetConstraint.md) |
[CollectionSupersetConstraint](CollectionSupersetConstraint.md) |
[DelayedConstraint](DelayedConstraint.md) |
[DictionaryContainsKeyConstraint](DictionaryContainsKeyConstraint.md) |
[DictionaryContainsKeyValuePairConstraint](DictionaryContainsKeyValuePairConstraint.md) |
[DictionaryContainsValueConstraint](DictionaryContainsValueConstraint.md)|
[EmptyCollectionConstraint](EmptyCollectionConstraint.md) |
[EmptyConstraint](EmptyConstraint.md) |
[EmptyDirectoryConstraint](EmptyDirectoryConstraint.md) |
[EmptyStringConstraint](EmptyStringConstraint.md) |
[EndsWithConstraint](EndsWithConstraint.md) |
[EqualConstraint](EqualConstraint.md) |
[ExactCountConstraint](ExactCountConstraint.md) |
[ExactTypeConstraint](ExactTypeConstraint.md) |
[FalseConstraint](FalseConstraint.md) |
[FileOrDirectoryExistsConstraint](FileOrDirectoryExistsConstraint.md) |
[GreaterThanConstraint](GreaterThanConstraint.md) |
[GreaterThanOrEqualConstraint](GreaterThanOrEqualConstraint.md) |
[InstanceOfTypeConstraint](InstanceOfTypeConstraint.md) |
[LessThanConstraint](LessThanConstraint.md) |
[LessThanOrEqualConstraint](LessThanOrEqualConstraint.md) |
[NaNConstraint](NaNConstraint.md) |
[NoItemConstraint](NoItemConstraint.md) |
[NotConstraint](NotConstraint.md) |
[NullConstraint](NullConstraint.md) |
[OrConstraint](OrConstraint.md) |
[PropertyConstraint](PropertyConstraint.md) |
[PropertyExistsConstraint](PropertyExistsConstraint.md) |
[RangeConstraint](RangeConstraint.md) |
[RegexConstraint](RegexConstraint.md) |
[ReusableConstraint](ReusableConstraint.md) |
[SameAsConstraint](SameAsConstraint.md) |
[SamePathConstraint](SamePathConstraint.md) |
[SamePathOrUnderConstraint](SamePathOrUnderConstraint.md) |
[SomeItemsConstraint](SomeItemsConstraint.md) |
[StartsWithConstraint](StartsWithConstraint.md) |
[SubPathConstraint](SubPathConstraint.md) |
[SubstringConstraint](SubstringConstraint.md) |
[ThrowsConstraint](ThrowsConstraint.md) |
[ThrowsNothingConstraint](ThrowsNothingConstraint.md) |
[TrueConstraint](TrueConstraint.md) |
[UniqueItemsConstraint](UniqueItemsConstraint.md) |
[XmlSerializableConstraint](XmlSerializableConstraint.md) |
| Constraint Name | Quick Example |
| ------------------------------------- | ------------- |
| [AllItemsConstraint](AllItemsConstraint.md) | |
| [AndConstraint](AndConstraint.md) | |
| [AssignableFromConstraint](AssignableFromConstraint.md) | |
| [AssignableToConstraint](AssignableToConstraint.md) | |
| [AttributeConstraint](AttributeConstraint.md) | |
| [AttributeExistsConstraint](AttributeExistsConstraint.md) | |
| [AnyOfConstraint](AnyOfConstraint.md) | |
| [BinarySerializableConstraint](BinarySerializableConstraint.md) | |
| [CollectionContainsConstraint](CollectionContainsConstraint.md) | |
| [CollectionEquivalentConstraint](CollectionEquivalentConstraint.md) | |
| [CollectionOrderedConstraint](CollectionOrderedConstraint.md) | |
| [CollectionSubsetConstraint](CollectionSubsetConstraint.md) | |
| [CollectionSupersetConstraint](CollectionSupersetConstraint.md) | |
| [DelayedConstraint](DelayedConstraint.md) | |
| [DictionaryContainsKeyConstraint](DictionaryContainsKeyConstraint.md) | |
| [DictionaryContainsKeyValuePairConstraint](DictionaryContainsKeyValuePairConstraint.md) | |
| [DictionaryContainsValueConstraint](DictionaryContainsValueConstraint.md)| |
| [EmptyCollectionConstraint](EmptyCollectionConstraint.md) | |
| [EmptyConstraint](EmptyConstraint.md) | |
| [EmptyDirectoryConstraint](EmptyDirectoryConstraint.md) | |
| [EmptyStringConstraint](EmptyStringConstraint.md) | |
| [EndsWithConstraint](EndsWithConstraint.md) | |
| [EqualConstraint](EqualConstraint.md) | |
| [ExactCountConstraint](ExactCountConstraint.md) | |
| [ExactTypeConstraint](ExactTypeConstraint.md) | |
| [FalseConstraint](FalseConstraint.md) | |
| [FileOrDirectoryExistsConstraint](FileOrDirectoryExistsConstraint.md) | |
| [GreaterThanConstraint](GreaterThanConstraint.md) | |
| [GreaterThanOrEqualConstraint](GreaterThanOrEqualConstraint.md) | |
| [InstanceOfTypeConstraint](InstanceOfTypeConstraint.md) | |
| [LessThanConstraint](LessThanConstraint.md) | |
| [LessThanOrEqualConstraint](LessThanOrEqualConstraint.md) | |
| [NaNConstraint](NaNConstraint.md) | |
| [NoItemConstraint](NoItemConstraint.md) | |
| [NotConstraint](NotConstraint.md) | |
| [NullConstraint](NullConstraint.md) | |
| [OrConstraint](OrConstraint.md) | |
| [PropertyConstraint](PropertyConstraint.md) | |
| [PropertyExistsConstraint](PropertyExistsConstraint.md) | |
| [RangeConstraint](RangeConstraint.md) | |
| [RegexConstraint](RegexConstraint.md) | |
| [ReusableConstraint](ReusableConstraint.md) | |
| [SameAsConstraint](SameAsConstraint.md) | |
| [SamePathConstraint](SamePathConstraint.md) | |
| [SamePathOrUnderConstraint](SamePathOrUnderConstraint.md) | |
| [SomeItemsConstraint](SomeItemsConstraint.md) | |
| [StartsWithConstraint](StartsWithConstraint.md) | |
| [SubPathConstraint](SubPathConstraint.md) | |
| [SubstringConstraint](SubstringConstraint.md) | |
| [ThrowsConstraint](ThrowsConstraint.md) | |
| [ThrowsNothingConstraint](ThrowsNothingConstraint.md) | |
| [TrueConstraint](TrueConstraint.md) | |
| [UniqueItemsConstraint](UniqueItemsConstraint.md) | |
| [XmlSerializableConstraint](XmlSerializableConstraint.md) | |

## Constraints by Category

### Collection Constraints
Constraint Name |
-------------------------------------|
[AllItemsConstraint](AllItemsConstraint.md) |
[AnyOfConstraint](AnyOfConstraint.md) |
[CollectionContainsConstraint](CollectionContainsConstraint.md) |
[CollectionEquivalentConstraint](CollectionEquivalentConstraint.md) |
[CollectionOrderedConstraint](CollectionOrderedConstraint.md) |
[CollectionSubsetConstraint](CollectionSubsetConstraint.md) |
[CollectionSupersetConstraint](CollectionSupersetConstraint.md) |
[DictionaryContainsKeyConstraint](DictionaryContainsKeyConstraint.md) |
[DictionaryContainsKeyValuePairConstraint](DictionaryContainsKeyValuePairConstraint.md) |
[DictionaryContainsValueConstraint](DictionaryContainsValueConstraint.md)|
[EmptyCollectionConstraint](EmptyCollectionConstraint.md) |
[ExactCountConstraint](ExactCountConstraint.md) |
[NoItemConstraint](NoItemConstraint.md) |
[SomeItemsConstraint](SomeItemsConstraint.md) |
[UniqueItemsConstraint](UniqueItemsConstraint.md) |

| Constraint Name | Quick Example |
| -------------------------------------| ------------- |
| [AllItemsConstraint](AllItemsConstraint.md) | |
| [AnyOfConstraint](AnyOfConstraint.md) | |
| [CollectionContainsConstraint](CollectionContainsConstraint.md) | |
| [CollectionEquivalentConstraint](CollectionEquivalentConstraint.md) | |
| [CollectionOrderedConstraint](CollectionOrderedConstraint.md) | |
| [CollectionSubsetConstraint](CollectionSubsetConstraint.md) | |
| [CollectionSupersetConstraint](CollectionSupersetConstraint.md) | |
| [DictionaryContainsKeyConstraint](DictionaryContainsKeyConstraint.md) | |
| [DictionaryContainsKeyValuePairConstraint](DictionaryContainsKeyValuePairConstraint.md) | |
| [DictionaryContainsValueConstraint](DictionaryContainsValueConstraint.md)| |
| [EmptyCollectionConstraint](EmptyCollectionConstraint.md) | |
| [ExactCountConstraint](ExactCountConstraint.md) | |
| [NoItemConstraint](NoItemConstraint.md) | |
| [SomeItemsConstraint](SomeItemsConstraint.md) | |
| [UniqueItemsConstraint](UniqueItemsConstraint.md) | |

### Comparison Constraints

Constraint Name |
-------------------------------------|
[GreaterThanConstraint](GreaterThanConstraint.md) |
[GreaterThanOrEqualConstraint](GreaterThanOrEqualConstraint.md) |
[LessThanConstraint](LessThanConstraint.md) |
[LessThanOrEqualConstraint](LessThanOrEqualConstraint.md) |
[RangeConstraint](RangeConstraint.md) |
| Constraint Name | Quick Example |
| -------------------------------------| ------------- |
| [GreaterThanConstraint](GreaterThanConstraint.md) | |
| [GreaterThanOrEqualConstraint](GreaterThanOrEqualConstraint.md) | |
| [LessThanConstraint](LessThanConstraint.md) | |
| [LessThanOrEqualConstraint](LessThanOrEqualConstraint.md) | |
| [RangeConstraint](RangeConstraint.md) | |

### Compound Constraints

Constraint Name |
-------------------------------------|
[AndConstraint](AndConstraint.md) |
[NotConstraint](NotConstraint.md) |
[OrConstraint](OrConstraint.md) |
| Constraint Name | Quick Example |
| -------------------------------------| ------------- |
| [AndConstraint](AndConstraint.md) | |
| [NotConstraint](NotConstraint.md) | |
| [OrConstraint](OrConstraint.md) | |

### Condition Constraints

Constraint Name |
-------------------------------------|
[EmptyConstraint](EmptyConstraint.md) |
[FalseConstraint](FalseConstraint.md) |
[NaNConstraint](NaNConstraint.md) |
[NullConstraint](NullConstraint.md) |
[TrueConstraint](TrueConstraint.md) |
| Constraint Name | Quick Example |
| -------------------------------------| ------------- |
| [EmptyConstraint](EmptyConstraint.md) | |
| [FalseConstraint](FalseConstraint.md) | |
| [NaNConstraint](NaNConstraint.md) | |
| [NullConstraint](NullConstraint.md) | |
| [TrueConstraint](TrueConstraint.md) | |

### File and Directory Constraints

Constraint Name |
-------------------------------------|
[EmptyDirectoryConstraint](EmptyDirectoryConstraint.md) |
[FileOrDirectoryExistsConstraint](FileOrDirectoryExistsConstraint.md) |
[SamePathConstraint](SamePathConstraint.md) |
[SamePathOrUnderConstraint](SamePathOrUnderConstraint.md) |
[SubPathConstraint](SubPathConstraint.md) |
| Constraint Name | Quick Example |
| -------------------------------------| ------------- |
| [EmptyDirectoryConstraint](EmptyDirectoryConstraint.md) | |
| [FileOrDirectoryExistsConstraint](FileOrDirectoryExistsConstraint.md) | |
| [SamePathConstraint](SamePathConstraint.md) | |
| [SamePathOrUnderConstraint](SamePathOrUnderConstraint.md) | |
| [SubPathConstraint](SubPathConstraint.md) | |

### String Constraints

Constraint Name |
-------------------------------------|
[EmptyStringConstraint](EmptyStringConstraint.md) |
[EndsWithConstraint](EndsWithConstraint.md) |
[RegexConstraint](RegexConstraint.md) |
[StartsWithConstraint](StartsWithConstraint.md) |
[SubstringConstraint](SubstringConstraint.md) |
| Constraint Name | Quick Example |
| -------------------------------------| ------------- |
| [EmptyStringConstraint](EmptyStringConstraint.md) | |
| [EndsWithConstraint](EndsWithConstraint.md) | |
| [RegexConstraint](RegexConstraint.md) | |
| [StartsWithConstraint](StartsWithConstraint.md) | |
| [SubstringConstraint](SubstringConstraint.md) | |

### Type Constraints

Constraint Name |
-------------------------------------|
[AssignableFromConstraint](AssignableFromConstraint.md) |
[AssignableToConstraint](AssignableToConstraint.md) |
[ExactTypeConstraint](ExactTypeConstraint.md) |
[InstanceOfTypeConstraint](InstanceOfTypeConstraint.md) |
| Constraint Name | Quick Example |
| -------------------------------------| ------------- |
| [AssignableFromConstraint](AssignableFromConstraint.md) | |
| [AssignableToConstraint](AssignableToConstraint.md) | |
| [ExactTypeConstraint](ExactTypeConstraint.md) | |
| [InstanceOfTypeConstraint](InstanceOfTypeConstraint.md) | |

## See also

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using NUnit.Framework;

namespace Snippets.NUnit.Attributes
{
public class AllItemsConstraintExamples
{
#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
}

}