Skip to main content

Enum

Describes the possible expectations for enum values.

ExpectationNegatedSummary
IsEqualToIsNotEqualToequal to the expected value
IsOneOfIsNotOneOfequal to one of the expected values
HasValuenegated comparisonhas the expected underlying numeric value
IsDefinedIsNotDefineda named member or combination of flags
HasFlagDoesNotHaveFlaghas the expected flag set

The samples on this page use the following enum:

enum Genre { Rock = 1, Pop = 2, Jazz = 3, Blues = 4 }

Equality​

You can verify that the enum is equal to another one or not:

await That(Genre.Rock).IsEqualTo(Genre.Rock);
await That(Genre.Rock).IsNotEqualTo(Genre.Jazz);

One of​

You can verify that the enum is one of many alternatives:

await That(Genre.Rock).IsOneOf(Genre.Rock, Genre.Pop, Genre.Jazz);
await That(Genre.Blues).IsNotOneOf(Genre.Rock, Genre.Pop, Genre.Jazz);

Value​

You can verify that the enum has a given underlying numeric value or not:

await That(Genre.Rock).HasValue(1);
await That(Genre.Rock).HasValue().NotEqualTo(2);
await That(Genre.Jazz).HasValue().GreaterThan(2);

The Has… expectations whose value is a number or a TimeSpan, e.g. HasLength(), HasCount(), HasYear() or HasMajor(), continue with one of the following comparisons, each with a negated counterpart:

ComparisonNegatedSucceeds when the value is
EqualTo(x)NotEqualTo(x)equal to x
GreaterThan(x)NotGreaterThan(x)greater than x
GreaterThanOrEqualTo(x)NotGreaterThanOrEqualTo(x)greater than or equal to x
LessThan(x)NotLessThan(x)less than x
LessThanOrEqualTo(x)NotLessThanOrEqualTo(x)less than or equal to x
Between(min).And(max)NotBetween(min).And(max)between min and max, both bounds included

Passing the value directly, e.g. HasValue(1), is a shorthand for EqualTo.

Comparing with null

The expected value is nullable, e.g. to pass a value mapped from a property. As the actual value is never null, EqualTo(null) fails and NotEqualTo(null) succeeds, while a comparison of order against null, such as GreaterThan(null) or Between(null).And(3), fails even when negated.

Defined​

You can verify that the enum has a defined value or not:

await That((Genre)3).IsDefined()
.Because("3 corresponds to 'Jazz'");
await That((Genre)5).IsNotDefined()
.Because("5 is no valid genre");

For an enum with the [Flags] attribute, a combination of the bits of its named members is defined as well, while a value with any other bit set is not:

using System.Text.RegularExpressions;

await That(RegexOptions.Multiline | RegexOptions.IgnoreCase).IsDefined();
await That((RegexOptions)(1 << 20)).IsNotDefined();

Zero is only defined if a member names it, like RegexOptions.None does.

Flags​

You can verify that the enum has a specific flag or not:

using System.Text.RegularExpressions;

RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;

await That(options).HasFlag(RegexOptions.IgnoreCase);
await That(options).DoesNotHaveFlag(RegexOptions.ExplicitCapture);