Skip to content

Add DefaultConstraint #3457

Closed
Closed
@Dreamescaper

Description

@Dreamescaper

Sometimes it is needed to verify value type against default value.
Common use case - verify that value in rest response is not empty, e.g. Guid (not empty) or enums (not Undefined value).

Current alternative - using EqualConstraint (e.g. Is.Not.EqualTo(Guid.Empty), Is.Not.EqualTo(default(MyEnumTypeName))), which is not always convenient.

Details:

  1. If actual is non-nullable value type - should pass if actual == default.

  2. If actual is nullable value type - should pass if actual is equal to default value of underlying type, so the following code could be possible:

MyValueType? actual = ...;
Assert.That(actual, Is.Not.Null & Is.Not.Default);

Open question - what the expected behavior if nullable actual is null? Technically it is default value for nullable value type, but I think would make sense to fail it in this case, i.e. only account for underlying type default value.

  1. Not sure about the behavior for reference types. Not sure it should be allowed at all. Another option - allow (passing when null), and provide roslyn analyzer with warning if used against reference type.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions