-
Notifications
You must be signed in to change notification settings - Fork 8
Overview of All Assertions
Kenny Pflug edited this page May 27, 2020
·
16 revisions
This page contains all assertions that Light.GuardClauses provides to you. Please note the following:
- every assertion that starts with Must throws an exception if one of the internal checks fails. You can customize the exception by providing
parameterName
ormessage
, or by using the overload that uses theexceptionFactory
to create your custom exception instance. - If you think that some exception types sound unfamiliar to you, then they are probably part of the
Light.GuardClauses.Exceptions
namespace and derive fromArgumentException
(directly or indirectly). - all other assertions return a Boolean value and should usually throw no exceptions (except for
ArgumentNullException
s where necessary).
See How to Structure Your Precondition Checks for details.
Name | Description |
---|---|
MustNotBeNull | Ensures that the specified object reference is not null, or otherwise throws an ArgumentNullException . |
MustNotBeDefault | Ensures that the specified parameter is not the default value, or otherwise throws an ArgumentNullException for reference types, or an ArgumentDefaultException for value types. |
MustNotBeNullReference | Ensures that the specified parameter is not null when T is a reference type, or otherwise throws an ArgumentNullException . PLEASE NOTICE: you should only use this assertion in generic contexts, use MustNotBeNull by default. |
MustBeOfType | Ensures that parameter can be cast to T and returns the cast value, or otherwise throws a TypeCastException . |
IsValidEnumValue | Checks if the specified value is a valid enum value of its type. This is true when the specified value is one of the constants defined in the enum, or a valid flags combination when the enum type is marked with the FlagsAttribute . |
MustBeValidEnumValue | Ensures that the specified enum value is valid, or otherwise throws an EnumValueNotDefinedException . An enum value is valid when the specified value is one of the constants defined in the enum, or a valid flags combination when the enum type is marked with the FlagsAttribute . |
IsEmpty (Guid) | Checks if the specified GUID is an empty one. |
MustNotBeEmpty (Guid) | Ensures that the specified GUID is not empty, or otherwise throws an EmptyGuidException . |
InvalidOperation | Checks if the specified condition is true and throws an InvalidOperationException in this case. |
InvalidState | Checks if the specified condition is true and throws an InvalidStateException in this case. |
MustHaveValue | Ensures that the specified nullable has a value and returns it, or otherwise throws a NullableHasNoValueException . |
IsSameAs | Checks if parameter and other point to the same object. |
MustNotBeSameAs | Ensures that parameter and other do not point to the same object instance, or otherwise throws a SameObjectReferenceException . |
MustBe | Ensures that parameter is equal to other , or otherwise throws a ValuesNotEqualException . There are overloads that support IEqualityComparer<T> . |
MustNotBe | Ensures that parameter is not equal to other , or otherwise throws a ValuesEqualException . There are overloads that support IEqualityComparer<T> . |
InvalidArgument | Checks if the specified is true and throws an in this case. |
IsApproximately | Checks if the specified double or float value is approximately the same as the other value, using the given tolerance. |
Name | Description |
---|---|
MustNotBeLessThan | Ensures that the specified parameter is not less than the given other value, or otherwise throws an ArgumentOutOfRangeException . |
MustBeGreaterThanOrEqualTo | Ensures that the specified parameter is not less than the given other value, or otherwise throws an ArgumentOutOfRangeException . |
MustBeLessThan | Ensures that the specified parameter is less than the given other value, or otherwise throws an ArgumentOutOfRangeException . |
MustNotBeGreaterThanOrEqualTo | Ensures that the specified parameter is less than the given other value, or otherwise throws an ArgumentOutOfRangeException . |
MustBeGreaterThan | Ensures that the specified parameter is greater than the given other value, or otherwise throws an ArgumentOutOfRangeException . |
MustNotBeLessThanOrEqualTo | Ensures that the specified parameter is greater than the given other value, or otherwise throws an ArgumentOutOfRangeException . |
MustNotBeGreaterThan | Ensures that the specified parameter is not greater than the given other value, or otherwise throws an ArgumentOutOfRangeException . |
MustBeLessThanOrEqualTo | Ensures that the specified parameter is not greater than the given other value, or otherwise throws an ArgumentOutOfRangeException . |
IsIn | Checks if the value is within the specified range. |
IsNotIn | Checks if the value is not within the specified range. |
MustBeIn | Ensures that parameter is within the specified range, or otherwise throws an ArgumentOutOfRangeException . |
MustNotBeIn | Ensures that parameter is not within the specified range, or otherwise throws an ArgumentOutOfRangeException . |
The last four assertions of this group use Range<T>
instances. These are best created via the fluent API they provide, e.g.:
starRating.MustBeIn(Range.FromExclusive(0).ToInclusive(5));
Name | Description |
---|---|
MustBeUtc | Ensures that the specified parameter uses DateTimeKind.Utc , or otherwise throws an InvalidDateTimeException . |
MustBeLocal | Ensures that the specified parameter uses DateTimeKind.Local , or otherwise throws an InvalidDateTimeException . |
MustBeUnspecified | Ensures that the specified parameter uses DateTimeKind.Unspecified , or otherwise throws an InvalidDateTimeException . |
Name | Description |
---|---|
MustHaveCount | Ensures that the collection has the specified number of items, or otherwise throws an InvalidCollectionCountException . |
IsNullOrEmpty | Checks if the specified collection is null or empty. |
MustNotBeNullOrEmpty | Ensures that the collection is not null or empty, or otherwise throws an ArgumentNullException or EmptyCollectionException . |
MustContain | Ensures that the collection contains the specified item, or otherwise throws a MissingItemException . |
MustNotContain | Ensures that the collection does not contain the specified item, or otherwise throws an ExistingItemException . |
IsOneOf | Checks if the given item is one of the specified items . |
MustBeOneOf | Ensures that the value is one of the specified items, or otherwise throws a ValueIsNotOneOfException . |
MustNotBeOneOf | Ensures that the value is not one of the specified items, or otherwise throws a ValueIsOneOfException . |
MustHaveMinimumCount | Ensures that the collection has at least the specified number of items, or otherwise throws an InvalidCollectionCountException . |
MustHaveMaximumCount | Ensures that the collection has at most the specified number of items, or otherwise throws an InvalidCollectionCountException . |
The assertions in this category are only available in .NET Standard 2.0 and .NET 4.5.
Name | Description |
---|---|
MustHaveLength | Ensures that the span has the specified length, or otherwise throws an InvalidCollectionCountException . |
MustBeLongerThan | Ensures that the span is longer than the specified length, or otherwise throws an InvalidCollectionCountException . |
MustBeLongerThanOrEqualTo | Ensures that the span is longer than or equal to the specified length, or otherwise throws an InvalidCollectionCountException . |
MustBeShorterThan | Ensures that the span is shorter than the specified length, or otherwise throws an InvalidCollectionCountException . |
MustBeShorterThanOrEqualTo | Ensures that the span is shorter than or equal to the specified length, or otherwise throws an InvalidCollectionCountException . |
Name | Description |
---|---|
IsNullOrEmpty | Checks if the specified string is null or empty. |
MustNotBeNullOrEmpty | Ensures that the specified string is not null or empty, or otherwise throws an ArgumentNullException or EmptyStringException . |
IsNullOrWhiteSpace | Checks if the specified string is null, empty, or contains only white space. |
MustNotBeNullOrWhiteSpace | Ensures that the specified string is not null, empty, or contains only white space, or otherwise throws an ArgumentNullException , an EmptyStringException , or a WhiteSpaceStringException . |
IsWhiteSpace (char) | Checks if the specified character is a white space character. |
IsLetter (char) | Checks if the specified character is a letter. |
IsLetterOrDigit (char) | Checks if the specified character is a letter or digit. |
IsDigit (char) | Checks if the specified character is a digit. |
MustBe | Ensures that the two strings are equal using the specified comparisonType , or otherwise throws a ValuesNotEqualException . |
MustNotBe | Ensures that the two strings are not equal using the specified comparisonType , or otherwise throws a ValuesEqualException . |
MustMatch | Ensures that the string matches the specified regular expression, or otherwise throws a StringDoesNotMatchException . |
Equals | Checks if the specified strings are equal, using the given comparison rules. This is an overload of the regular string.Equals method that allows you to use additional comparison types, namely StringComparisonType.OrdinalIgnoreWhiteSpace and StringComparisonType.OrdinalIgnoreCaseIgnoreWhiteSpace . |
MustContain | Ensures that the string contains the specified substring, or otherwise throws a SubstringException . |
MustNotContain | Ensures that the string does not contain the specified value, or otherwise throws a SubstringException . |
Contains | Checks if the string contains the specified value using the given comparison type. |
IsSubstringOf | Checks if the string is a substring of the other string. |
MustBeSubstringOf | Ensures that the string is a substring of the specified other string, or otherwise throws a SubstringException . |
MustNotBeSubstringOf | Ensures that the string is not a substring of the specified other string, or otherwise throws a SubstringException . |
IsEmailAddress | Checks if the specified string is an email address using the default email regular expression defined in RegularExpressions.EmailRegex . There is also an overload to provide a custom regular expression. |
MustBeEmailAddress | Ensures that the string is a valid email address using the default email regular expression defined in RegularExpressions.EmailRegex , or otherwise throws an InvalidEmailAddressException . There is also an overload to provide a custom regular expression. |
MustBeShorterThan | Ensures that the string is shorter than the specified length, or otherwise throws a StringLengthException . |
MustBeShorterThanOrEqualTo | Ensures that the string is shorter than or equal to the specified length, or otherwise throws a StringLengthException . |
MustBeLongerThan | Ensures that the string is longer than the specified length, or otherwise throws a StringLengthException . |
MustBeLongerThanOrEqualTo | Ensures that the string is longer than or equal to the specified length, or otherwise throws a StringLengthException . |
MustHaveLengthIn | Ensures that the string's length is within the specified range, or otherwise throws a StringLengthException . |
IsNewLine | Checks if the string is either "\n" or "\r\n". This is done independently of the current value of Environment.NewLine . |
MustBeNewLine | Ensures that the string is either "\n" or "\r\n", or otherwise throws a StringException . This is done independently of the current value of Environment.NewLine . |
Name | Description |
---|---|
IsEquivalentTypeTo | Checks if the two specified types are equivalent. This is true when both types are equal or when one type is a constructed generic type and the other type is the corresponding generic type definition. |
IsConstructedGenericType | Gets a value that indicates whether the specified type is a constructed generic type. This is true when the type is a generic type, but not a generic type definition. Constructed generic types resolve at least one generic parameter of a generic type definition. They may either be open (when not all generic parameters are resolved) or closed (when all generic parameters of the generic type definition are resolved). This assertion is only available for .NET 3.5, .NET 3.5 Compact Framework, .NET 4.0, and Silverlight. Newer versions of .NET and .NET Core already provide the Type.IsConstructedGenericType property. |
Implements | Checks if the type implements the specified interface type. Internally, this method uses IsEquivalentTypeTo by default so that constructed generic types and their corresponding generic type definitions are regarded as equal. There is an overload where you can provide a custom IEqualityComparer<Type> . |
IsOrImplements | Checks if the given type is equal to the specified otherType or if it implements it. Internally, this method uses IsEquivalentTypeTo by default so that constructed generic types and their corresponding generic type definitions are regarded as equal. There is an overload where you can provide a custom IEqualityComparer<Type> . |
DerivesFrom | Checks if the specified type derives from the other type. Internally, this method uses IsEquivalentTypeTo by default so that constructed generic types and their corresponding generic type definitions are regarded as equal. There is an overload where you can provide a custom IEqualityComparer<Type> . |
IsOrDerivesFrom | Checks if the given type is equal to the specified otherType or if it derives from it. Internally, this method uses IsEquivalentTypeTo by default so that constructed generic types and their corresponding generic type definitions are regarded as equal. There is an overload where you can provide a custom IEqualityComparer<Type> . |
InheritsFrom | Checks if the given type derives from the specified base class or interface type. Internally, this method uses IsEquivalentTypeTo so that constructed generic types and their corresponding generic type definitions are regarded as equal. There is an overload where you can provide a custom IEqualityComparer<Type> . |
IsOrInheritsFrom | Checks if the given type is equal to the specified otherType or if it derives from it or implements it. Internally, this method uses IsEquivalentTypeTo so that constructed generic types and their corresponding generic type definitions are regarded as equal. There is an overload where you can provide a custom IEqualityComparer<Type> . |
IsOpenConstructedGenericType | Checks if the given type is a generic type that has open generic parameters, but is no generic type definition. |
Name | Description |
---|---|
MustBeAbsoluteUri | Ensures that the specified URI is an absolute one, or otherwise throws a RelativeUriException . |
MustBeRelativeUri | Ensures that the specified URI is a relative one, or otherwise throws an AbsoluteUriException . |
MustHaveScheme | Ensures that the parameter has the specified scheme, or otherwise throws an InvalidUriSchemeException . |
MustBeHttpsUrl | Ensures that the specified URI has the "https" scheme, or otherwise throws an InvalidUriSchemeException . |
MustBeHttpUrl | Ensures that the specified URI has the "http" scheme, or otherwise throws an InvalidUriSchemeException . |
MustBeHttpOrHttpsUrl | Ensures that the specified URI has the "http" or "https" scheme, or otherwise throws an InvalidUriSchemeException . |
MustHaveOneSchemeOf | Ensures that the URI has one of the specified schemes, or otherwise throws an InvalidUriSchemeException . |