public static void IfNull <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)] T value, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string name) { Fail.RequiresArgumentName(name); Fail.IfNull(value, Violation.WhenVariableIsNull(name)); }
public static void IfCollectionEmpty( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] IEnumerable collection, Violation message ) { collection.OrFailIfCollectionEmpty(message); }
/// <summary> /// Checks whether specified enum value is defined. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> public static void IfEnumNotDefined <T>(T value) where T : struct { // ReSharper disable once HeapView.BoxingAllocation if (Enum.IsDefined(typeof(T), value) == false) { throw Fail.Because(Violation.WhenEnumOutOfRange <T>(null, value)); } }
public static T FailIfNull <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)][NoEnumeration] this T value, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string name) { Fail.RequiresArgumentName(name); return(value.FailIfNull(Violation.WhenVariableIsNull(name))); }
public static void IfNotDate([CanBeNull] DateTime?date, string name) { if (date == null) { return; } Fail.IfNotDate(date, Violation.WhenDateTimeIsNotDate(name, date.Value)); }
public static void IfNotEqual <TExpected, TActual>( [CanBeNull] TExpected expected, [CanBeNull] TActual actual, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string name ) { Fail.RequiresArgumentName(name); Fail.IfNotEqual(expected, actual, Violation.WhenNotEqual(name, expected, actual)); }
public static void IfNull <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)] T value, Violation message) { if (value == null) { throw Fail.Because(message); } }
public static void IfArgumentEqual <TExpected, TActual>( [CanBeNull] TExpected unexpected, [CanBeNull] TActual argumentValue, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string argumentName ) { Fail.RequiresArgumentName(argumentName); Fail.IfEqual(unexpected, argumentValue, Violation.WhenArgumentEqual(argumentName, unexpected)); }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="value"></param> /// <returns>Violation message</returns> internal static Violation WhenEnumOutOfRange <T>([CanBeNull] string name, [JetBrains.Annotations.NotNull][System.Diagnostics.CodeAnalysis.NotNull] object value) { string enumType = typeof(T).Name; string enumValue = value.ToString(); name = name ?? "enum"; return(Violation.Of($"Unsupported {name} value: {enumValue} ({enumType})")); }
public static void IfTrue( [AssertionCondition(AssertionConditionType.IS_FALSE)] bool value, Violation message) { if (value) { throw Fail.Because(message); } }
public static void IfFalse( [AssertionCondition(AssertionConditionType.IS_TRUE)] bool value, Violation message) { if (value == false) { throw Fail.Because(message); } }
public static void IfEmpty( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string value, Violation message) { Fail.IfNull(value, message); if (value.Length == 0) { throw Fail.Because(message); } }
public static void IfWhitespace( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string value, Violation message) { Fail.IfNull(value, message); if (string.IsNullOrWhiteSpace(value)) { throw Fail.Because(message); } }
public static void IfNotDate([CanBeNull] DateTime?date, Violation message) { if (date == null) { return; } DateTime dateTime = date.Value; Fail.IfNotEqual(dateTime.Date, dateTime, message); }
public static void IfNotEqual <TExpected, TActual>( [CanBeNull] TExpected expected, [CanBeNull] TActual actual, Violation message ) { if (object.Equals(expected, actual) == false) { throw Fail.Because(message); } }
public static T OrFail <T>(this T?value, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string name) where T : struct { Fail.RequiresArgumentName(name); if (value == null) { throw Fail.Because(Violation.WhenVariableIsNull(name)); } return(value.Value); }
//[Obsolete("Use " + nameof(Fail) + "." + nameof(IfNull) + " instead.")] public static void IfArgumentNull <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)][NoEnumeration] T argumentValue, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string argumentName) { Fail.RequiresArgumentName(argumentName); if (argumentValue == null) { throw Fail.Because(Violation.WhenArgumentIsNull(argumentName)); } }
public static T FailIfNull <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)][NoEnumeration] this T value, Violation message) { if (value == null) { throw Fail.Because(message); } return(value); }
public static void IfArgumentEmpty( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string argumentValue, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string argumentName) { Fail.RequiresArgumentName(argumentName); Fail.IfArgumentNull(argumentValue, argumentName); if (argumentValue.Length == 0) { throw Fail.Because(Violation.WhenArgumentEmpty(argumentName)); } }
public static void IfArgumentWhiteSpace( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string argumentValue, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string argumentName) { Fail.RequiresArgumentName(argumentName); Fail.IfArgumentNull(argumentValue, argumentName); if (string.IsNullOrWhiteSpace(argumentValue)) { throw Fail.Because(Violation.WhenArgumentWhitespace(argumentName)); } }
public static void IfCollectionContains <T>( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] IEnumerable <T> collection, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] Func <T, bool> func, Violation message ) { Fail.IfArgumentNull(collection, nameof(collection)); T element = collection.FirstOrDefault(func); Fail.IfNotNull(element, message); }
public static T CastEnumOrFail <T>([CanBeNull][NoEnumeration] this Enum value, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string name) { Fail.RequiresArgumentName(name); Type type = value.GetType(); if (Enum.IsDefined(type, value) == false) { throw Fail.Because(Violation.WhenEnumOutOfRange <T>(name, value)); } return(value.CastOrFail <T>(name)); }
public static T CastOrFail <T>([CanBeNull][NoEnumeration] this object value, [CanBeNull] string name = null) { Type castType = typeof(T); Fail.IfNull(value, Violation.WhenCannotCast <T>(name ?? "object", value)); if (castType.IsEnum) { Fail.IfEnumNotDefined <T>(value); return((T)Enum.ToObject(castType, value)); } Fail.IfNotCastable <T>(value, Violation.WhenCannotCast <T>(name ?? "object", value)); return((T)value); }
public static void IfCollectionContainsNull <T>( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] IEnumerable <T> collection, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string collectionName ) where T : class { Fail.RequiresCollectionName(collectionName); if (collection == null) { throw Fail.Because(Violation.WhenCollectionIsNull(collectionName)); } if (collection.Contains(null)) { throw Fail.Because(Violation.WhenCollectionContainsNull(collectionName)); } }
public static void IfTooLong( [CanBeNull] string value, int maxLength, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string name) { Fail.RequiresArgumentName(name); if (value == null) { return; } int currentLength = value.Length; if (currentLength > maxLength) { throw Fail.Because(Violation.WhenTooLong(name, currentLength, maxLength)); } }
public static T OrFailIfCollectionEmpty <T>( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this T collection, Violation message ) where T : IEnumerable { if (collection == null) { throw Fail.Because(message); } if (collection.IsEmpty()) { throw Fail.Because(message); } return(collection); }
public static T OrFailIfCollectionEmpty <T>( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] this T collection, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string collectionName ) where T : IEnumerable { Fail.RequiresCollectionName(collectionName); if (collection == null) { throw Fail.Because(Violation.WhenCollectionIsNull(collectionName)); } if (collection.IsEmpty()) { throw Fail.Because(Violation.WhenCollectionIsEmpty(collectionName)); } return(collection); }
public static void IfNotNull <T>([CanBeNull][NoEnumeration] T value, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string name) { Fail.RequiresArgumentName(name); Fail.IfNotNull(value, Violation.WhenVariableIsNotNull(name)); }
public static void IfNullOrNotCastable <T>([CanBeNull][NoEnumeration] object value) { Fail.IfNull(value, Violation.WhenCannotCast <T>("object", value)); Fail.IfNotCastable <T>(value, Violation.WhenCannotCast <T>("object", value)); }
public static DesignByContractViolationException BecauseEnumOutOfRange <T>(T value) where T : struct { // ReSharper disable once HeapView.BoxingAllocation return(Fail.Because(Violation.WhenEnumOutOfRange <T>(null, value))); }