Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 32 additions & 0 deletions docs/articles/nunit/writing-tests/constraints/DefaultConstraint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# DefaultConstraint

`DefaultConstraint` tests that the actual value is the default value for the type.

It is implemented equal to the C# keyword `default`.

## Constructor

```csharp
DefaultConstrint()
```

## Syntax

```csharp
Is.Default
Has.Length.Default
Has.Count.Default
Is.Not.Default
```

All resolvable properties of `Has` can be used with the `Default` property.
`Default` can be used with the `Not` operator.
`Default` can be used with the combinatorial operators.

## Examples of use

[!code-csharp[DefaultConstraintExample](~/snippets/Snippets.NUnit/DefaultConstraintExamples.cs#DefaultConstraintExample)]

## Version

From version 4.0.0
36 changes: 36 additions & 0 deletions docs/snippets/Snippets.NUnit/DefaultConstraintExamples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using NUnit.Framework;

namespace Snippets.NUnit;

public class DefaultConstraintExamples
{
#region DefaultConstraintExample
[Test]
public void DefaultConstraintExample()
{
string defaultLength = string.Empty;
var nonDefaultLength = "1";
var defaultList = new List<int>();
var nonDefaultList = new List<int> {1};
var defaultDate = default(DateTime);
var noNDefaultDate = DateTime.Now;

using (Assert.EnterMultipleScope())
{
Assert.That(defaultLength.Length, Is.Zero);
Assert.That(defaultLength.Length, Is.Default);
Assert.That(defaultLength, Has.Length.Default);
Assert.That(defaultLength, Has.Property("Length").Default);
Assert.That(defaultList,Has.Count.Default);
Assert.That(defaultDate, Is.Default);

Assert.That(nonDefaultLength.Length, Is.Not.Zero);
Assert.That(nonDefaultLength.Length, Is.Not.Default);
Assert.That(nonDefaultLength, Has.Length.Not.Default);
Assert.That(nonDefaultLength, Has.Property("Length").Not.Default);
Assert.That(nonDefaultList, Has.Count.Not.Default);
Assert.That(noNDefaultDate, Is.Not.Default);
}
}
#endregion
}
Loading