public static void IfNotNull <T>([CanBeNull][NoEnumeration] T value, [NotNull] string message, [NotNull] params object[] args) { // TODO:mace (from:mace on:17-11-2016) This method should be splitted to 5 generic methods to prevent unnecesesary memory allocation Fail.RequiresMessage(message, args); if (value != null) { throw Fail.Because(message, args); } }
public static void IfEqual <TExpected, TActual>([CanBeNull] TExpected unexpected, [CanBeNull] TActual actual, [NotNull] string message) { Fail.RequiresMessage(message); if (object.Equals(unexpected, actual)) { 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); }
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 IfNotEqual <TExpected, TActual>( [CanBeNull] TExpected expected, [CanBeNull] TActual actual, Violation message ) { if (object.Equals(expected, actual) == false) { throw Fail.Because(message); } }
public static void IfFalse( [AssertionCondition(AssertionConditionType.IS_FALSE)] bool value, [NotNull] string message) { Fail.RequiresMessage(message); if (value == false) { throw Fail.Because(message); } }
public static void IfNull <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)] T value, [NotNull] string message) { Fail.RequiresMessage(message); if (value == null) { throw Fail.Because(message); } }
public static void IfArgumentNull <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)][NoEnumeration] T argumentValue, [NotNull] string argumentName) { Fail.RequiresArgumentName(argumentName); if (argumentValue == null) { throw Fail.Because("Argument '{0}' was null.", argumentName); } }
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 IfTrue <TArgument1>( [AssertionCondition(AssertionConditionType.IS_TRUE)] bool value, [NotNull] string message, [CanBeNull] TArgument1 arg1) { Fail.RequiresMessage(message); if (value) { throw Fail.Because(message, arg1); } }
public static void IfTrue( [AssertionCondition(AssertionConditionType.IS_TRUE)] bool value, [NotNull] string message, [NotNull] params object[] args) { Fail.RequiresMessage(message, args); if (value) { throw Fail.Because(message, args); } }
//[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 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 IfArgumentEmpty( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string argumentValue, [NotNull] string argumentName) { Fail.RequiresArgumentName(argumentName); Fail.IfArgumentNull(argumentValue, argumentName); if (argumentValue.Length == 0) { throw Fail.Because("Argument '{0}' was empty.", argumentName); } }
public static T OrFail <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)][NoEnumeration] this T value, [NotNull] string name) { Fail.RequiresArgumentName(name); if (value == null) { throw Fail.Because("'{0}' is null and it shouldn't be", name); } return(value); }
public static void IfNull <T, TArgument1, TArgument2>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)] T value, [NotNull] string message, [CanBeNull] TArgument1 arg1, [CanBeNull] TArgument2 arg2) { Fail.RequiresMessage(message); if (value == null) { throw Fail.Because(message, arg1, arg2); } }
public static void IfFalse <TArgument1, TArgument2>( [AssertionCondition(AssertionConditionType.IS_FALSE)] bool value, [NotNull] string message, [CanBeNull] TArgument1 arg1, [CanBeNull] TArgument2 arg2) { Fail.RequiresMessage(message); if (value == false) { throw Fail.Because(message, arg1, arg2); } }
public static void IfArgumentWhiteSpace( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string argumentValue, [NotNull] string argumentName) { Fail.RequiresArgumentName(argumentName); Fail.IfArgumentNull(argumentValue, argumentName); if (string.IsNullOrWhiteSpace(argumentValue)) { throw Fail.Because("Argument '{0}' was empty.", argumentName); } }
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 void IfEqual <TExpected, TActual, TArgument1, TArgument2, TArgument3>([CanBeNull] TExpected unexpected, [CanBeNull] TActual actual, [NotNull] string message, [CanBeNull] TArgument1 arg1, [CanBeNull] TArgument2 arg2, [CanBeNull] TArgument3 arg3) { Fail.RequiresMessage(message); if (object.Equals(unexpected, actual)) { throw Fail.Because(message, arg1, arg2, arg3); } }
public static T OrFail <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)][NoEnumeration] this T value, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] string name) { Fail.RequiresArgumentName(name); if (value == null) { throw Fail.Because(Violation.WhenVariableIsNull(name)); } return(value); }
public static T FailIfNull <T>( [CanBeNull][AssertionCondition(AssertionConditionType.IS_NOT_NULL)][NoEnumeration] this T value, [NotNull] string message, [NotNull] params object[] args) { Fail.RequiresMessage(message, args); if (value == null) { throw Fail.Because(message, args); } return(value); }
public static void IfEmpty( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string value, [NotNull] string message, [NotNull] params object[] args) { Fail.RequiresMessage(message, args); Fail.IfNull(value, message, args); if (value.Length == 0) { throw Fail.Because(message, args); } }
public static void IfNotCastable([CanBeNull][NoEnumeration] object value, [NotNull][System.Diagnostics.CodeAnalysis.NotNull] Type expectedType, Violation message) { Fail.RequiresType(expectedType); if (value == null) { return; } if (expectedType.IsInstanceOfType(value) == false) { throw Fail.Because(message); } }
public static void IfWhitespace( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] string value, [NotNull] string message, [NotNull] params object[] args) { Fail.RequiresMessage(message, args); Fail.IfNull(value, message, args); if (string.IsNullOrWhiteSpace(value)) { throw Fail.Because(message, args); } }
public static void IfNotCastable([CanBeNull][NoEnumeration] object value, [NotNull] Type expectedType, [NotNull] string message, [NotNull] params object[] args) { Fail.RequiresType(expectedType); Fail.RequiresMessage(message, args); if (value == null) { return; } if (expectedType.IsInstanceOfType(value) == false) { throw Fail.Because(message, args); } }
public static void IfCollectionEmpty <T>( [CanBeNull, AssertionCondition(AssertionConditionType.IS_NOT_NULL)] IEnumerable <T> collection, [NotNull] string collectionName) { Fail.RequiresCollectionName(collectionName); if (collection == null) { throw Fail.Because("Collection '{0}' should not be null but it is.", collectionName); } if (collection.Any() == false) { throw Fail.Because("Collection '{0}' should not be empty but it is.", collectionName); } }
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); }