コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
 public static void IfNotEqual <TExpected, TActual>(
     [CanBeNull] TExpected expected,
     [CanBeNull] TActual actual,
     Violation message
     )
 {
     if (object.Equals(expected, actual) == false)
     {
         throw Fail.Because(message);
     }
 }
コード例 #7
0
        public static void IfFalse(
            [AssertionCondition(AssertionConditionType.IS_FALSE)] bool value,
            [NotNull] string message)
        {
            Fail.RequiresMessage(message);

            if (value == false)
            {
                throw Fail.Because(message);
            }
        }
コード例 #8
0
        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);
            }
        }
コード例 #9
0
        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);
            }
        }
コード例 #10
0
        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));
            }
        }
コード例 #11
0
        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);
            }
        }
コード例 #12
0
        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);
            }
        }
コード例 #13
0
        //[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));
            }
        }
コード例 #14
0
        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);
        }
コード例 #15
0
        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));
            }
        }
コード例 #16
0
        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);
            }
        }
コード例 #17
0
        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);
        }
コード例 #18
0
        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);
            }
        }
コード例 #19
0
        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);
            }
        }
コード例 #20
0
        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);
            }
        }
コード例 #21
0
        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));
        }
コード例 #22
0
        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);
            }
        }
コード例 #23
0
        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);
        }
コード例 #24
0
        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);
        }
コード例 #25
0
        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);
            }
        }
コード例 #26
0
        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);
            }
        }
コード例 #27
0
        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);
            }
        }
コード例 #28
0
        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);
            }
        }
コード例 #29
0
        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);
            }
        }
コード例 #30
0
        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);
        }