Skip to content

Commit ee62a00

Browse files
authored
Merge pull request #1072 from nunit/defaultConstraint
Defaultconstraint
2 parents fb945da + 436e14b commit ee62a00

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# DefaultConstraint
2+
3+
`DefaultConstraint` tests that the actual value is the default value for the type.
4+
5+
It is implemented equal to the C# keyword `default`.
6+
7+
## Constructor
8+
9+
```csharp
10+
DefaultConstraint()
11+
```
12+
13+
## Syntax
14+
15+
```csharp
16+
Is.Default
17+
Has.Length.Default
18+
Has.Count.Default
19+
Is.Not.Default
20+
```
21+
22+
All resolvable properties of `Has` can be used with the `Default` property.
23+
`Default` can be used with the `Not` operator.
24+
`Default` can be used with the combinatorial operators.
25+
26+
## Examples of use
27+
28+
[!code-csharp[DefaultConstraintExample](~/snippets/Snippets.NUnit/DefaultConstraintExamples.cs#DefaultConstraintExample)]
29+
30+
## Version
31+
32+
From version 4.0.0
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using NUnit.Framework;
2+
3+
namespace Snippets.NUnit;
4+
5+
public class DefaultConstraintExamples
6+
{
7+
#region DefaultConstraintExample
8+
[Test]
9+
public void DefaultConstraintExample()
10+
{
11+
string defaultLength = string.Empty;
12+
var nonDefaultLength = "1";
13+
var defaultList = new List<int>();
14+
var nonDefaultList = new List<int> {1};
15+
var defaultDate = default(DateTime);
16+
var nonDefaultDate = DateTime.Now;
17+
18+
using (Assert.EnterMultipleScope())
19+
{
20+
Assert.That(defaultLength.Length, Is.Zero);
21+
Assert.That(defaultLength.Length, Is.Default);
22+
Assert.That(defaultLength, Has.Length.Default);
23+
Assert.That(defaultLength, Has.Property("Length").Default);
24+
Assert.That(defaultList, Has.Count.Default);
25+
Assert.That(defaultDate, Is.Default);
26+
27+
Assert.That(nonDefaultLength.Length, Is.Not.Zero);
28+
Assert.That(nonDefaultLength.Length, Is.Not.Default);
29+
Assert.That(nonDefaultLength, Has.Length.Not.Default);
30+
Assert.That(nonDefaultLength, Has.Property("Length").Not.Default);
31+
Assert.That(nonDefaultList, Has.Count.Not.Default);
32+
Assert.That(nonDefaultDate, Is.Not.Default);
33+
}
34+
}
35+
#endregion
36+
}

0 commit comments

Comments
 (0)