Example #1
0
        protected static void ThrowsException <T, TException>(
            Guard.ArgumentInfo <T> argument,
            Action <Guard.ArgumentInfo <T> > testWithoutMessage,
            Action <Guard.ArgumentInfo <T>, string> testWithMessage,
            bool allowMessageMismatch = false)
            where TException : Exception
        {
            var ex = Assert.Throws <TException>(() => testWithoutMessage(argument));

            if (ex is ArgumentException argEx)
            {
                Assert.Same(argument.Name, argEx.ParamName);
            }

            var message = RandomMessage;

            ex    = Assert.Throws <TException>(() => testWithMessage(argument, message));
            argEx = ex as ArgumentException;
            if (argEx != null)
            {
                Assert.Same(argument.Name, argEx.ParamName);
            }

            if (!allowMessageMismatch)
            {
                Assert.StartsWith(message, ex.Message);
            }
        }
Example #2
0
 protected static ArgumentException[] ThrowsArgumentException <T>(
     Guard.ArgumentInfo <T> argument,
     Action <Guard.ArgumentInfo <T> > testWithoutMessage,
     Action <Guard.ArgumentInfo <T>, string> testWithMessage,
     bool allowMessageMismatch = false,
     bool doNotTestScoping     = false)
 => ThrowsArgumentException(argument, testWithoutMessage, null, testWithMessage, allowMessageMismatch, doNotTestScoping);
Example #3
0
        protected static ArgumentNullException[] ThrowsArgumentNullException <T>(
            Guard.ArgumentInfo <T> argument,
            Action <Guard.ArgumentInfo <T> > testWithoutMessage,
            Func <string, bool> testGeneratedMessage,
            Action <Guard.ArgumentInfo <T>, string> testWithMessage,
            bool allowMessageMismatch = false)
        {
            var exWithoutMessage = Assert.Throws <ArgumentNullException>(
                argument.Name,
                () => testWithoutMessage(argument));

            if (testGeneratedMessage != null)
            {
                Assert.True(testGeneratedMessage(exWithoutMessage.Message));
            }

            var message       = RandomMessage;
            var exWithMessage = Assert.Throws <ArgumentNullException>(
                argument.Name,
                () => testWithMessage(argument, message));

            if (!allowMessageMismatch)
            {
                Assert.StartsWith(message, exWithMessage.Message);
            }

            var modified = argument.Modify(argument.Value);

            ThrowsArgumentException(
                modified, testWithoutMessage, testGeneratedMessage, testWithMessage, allowMessageMismatch);

            return(new[] { exWithoutMessage, exWithMessage });
        }
Example #4
0
 protected static ArgumentException[] ThrowsArgumentException <T>(
     Guard.ArgumentInfo <T> argument,
     Action <Guard.ArgumentInfo <T> > testWithoutMessage,
     Func <string, bool> testGeneratedMessage,
     Action <Guard.ArgumentInfo <T>, string> testWithMessage,
     bool allowMessageMismatch = false)
 => ThrowsArgumentException <T, ArgumentException>(
     argument, testWithoutMessage, testGeneratedMessage, testWithMessage, allowMessageMismatch);
Example #5
0
        protected static TException[] ThrowsArgumentException <TArgument, TException>(
            Guard.ArgumentInfo <TArgument> argument,
            Action <Guard.ArgumentInfo <TArgument> > testWithoutMessage,
            Func <string, bool> testGeneratedMessage,
            Action <Guard.ArgumentInfo <TArgument>, string> testWithMessage,
            bool allowMessageMismatch = false)
            where TException : ArgumentException
        {
            var isBase = typeof(TException) == typeof(ArgumentException);

            if (!isBase && argument.Modified)
            {
                var result = ThrowsArgumentException <TArgument, ArgumentException>(
                    argument, testWithoutMessage, testGeneratedMessage, testWithMessage, allowMessageMismatch);

                return(new[] { result[0] as TException, result[1] as TException });
            }

            var exWithoutMessage = Assert.Throws <TException>(
                argument.Name,
                () => testWithoutMessage(argument));

            if (exWithoutMessage is ArgumentOutOfRangeException rangeExWithoutMessage)
            {
                Assert.Equal(argument.Secure, rangeExWithoutMessage.ActualValue == null);
            }

            if (testGeneratedMessage != null)
            {
                Assert.True(testGeneratedMessage(exWithoutMessage.Message));
            }

            var message       = RandomMessage;
            var exWithMessage = Assert.Throws <TException>(
                argument.Name,
                () => testWithMessage(argument, message));

            if (exWithMessage is ArgumentOutOfRangeException rangeExWithMessage)
            {
                Assert.Equal(argument.Secure, rangeExWithMessage.ActualValue == null);
            }

            if (!allowMessageMismatch)
            {
                Assert.StartsWith(message, exWithMessage.Message);
            }

            if (!isBase)
            {
                var modified = argument.Modify(argument.Value);
                ThrowsArgumentException(
                    modified, testWithoutMessage, testGeneratedMessage, testWithMessage, allowMessageMismatch);
            }

            return(new[] { exWithoutMessage, exWithMessage });
        }
Example #6
0
        public void GenericReferenceType(string value)
        {
            for (var i = 0; i < 2; i++)
            {
                var arg = new Guard.ArgumentInfo <object>(
                    value, nameof(value), i % 2 == 0, i % 2 != 0);

                var typedArg = arg.Type <string>();
                Assert.IsType <Guard.ArgumentInfo <string> >(typedArg);
                Assert.Equal(arg.Modified, typedArg.Modified);
                Assert.Equal(arg.Secure, typedArg.Secure);
            }

            var valueArg = Guard.Argument(value as object, nameof(value))
                           .NotType <int>();

            if (value is null)
            {
                ThrowsArgumentException(
                    valueArg,
                    arg => arg.Type <int>(),
                    (arg, message) => arg.Type <int>(o =>
                {
                    Assert.Same(value, o);
                    return(message);
                }));

                valueArg.NotType <string>();
                return;
            }

            ThrowsArgumentException(
                valueArg,
                arg => arg.Type <int>(),
                (arg, message) => arg.Type <int>(o =>
            {
                Assert.Same(value, o);
                return(message);
            }));

            ThrowsArgumentException(
                valueArg,
                arg => arg.NotType <string>(),
                (arg, message) => arg.NotType <string>(o =>
            {
                Assert.Same(value, o);
                return(message);
            }));
        }
Example #7
0
        /// <summary>
        /// Check whether a type is an implementation of AlgorithmConfiguration.
        /// </summary>
        /// <param name="argumentInfo">The argument to check.</param>
        /// <param name="message">Optional message generator.</param>
        /// <typeparam name="T">The type of argument.</typeparam>
        /// <returns>The initial <paramref name="argumentInfo"/>.</returns>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the provided type is not an AlgorithmConfiguration.</exception>
        public static Guard.ArgumentInfo <T> IsAlgorithmConfiguration <T>(
            this Guard.ArgumentInfo <T> argumentInfo,
            Func <T, string> message = null)
            where T : Type
        {
            if (!argumentInfo.HasValue())
            {
                throw new ArgumentOutOfRangeException($"Cannot check, provided {nameof(argumentInfo)} has no value.");
            }

            if (Reflections.IsAlgorithmConfiguration(argumentInfo.Value))
            {
                return(argumentInfo);
            }

            if (message == null)
            {
                throw new ArgumentOutOfRangeException($"{argumentInfo.Value} is not an AlgorithmConfiguration.");
            }

            throw new ArgumentOutOfRangeException(message(argumentInfo.Value));
        }
Example #8
0
        /// <summary>
        /// Checks whether an argument is within an inclusive range.
        /// </summary>
        /// <param name="argumentInfo">The argument to check.</param>
        /// <param name="min">The minimum value of the range.</param>
        /// <param name="max">The maximum value of the range.</param>
        /// <param name="message">Optional message generator.</param>
        /// <typeparam name="T">The type of the argument.</typeparam>
        /// <returns>The initial <paramref name="argumentInfo"/>.</returns>
        /// <exception cref="ArgumentOutOfRangeException">If the value of the argumentInfo was outside the provided range.</exception>
        public static Guard.ArgumentInfo <T> InRangeInclusive <T>(
            this Guard.ArgumentInfo <T> argumentInfo,
            decimal min,
            decimal max,
            Func <T, string> message = null)
            where T : IComparable
        {
            if (!argumentInfo.HasValue())
            {
                throw new ArgumentOutOfRangeException($"Cannot check, provided {nameof(argumentInfo)} has no value..");
            }

            if (argumentInfo.Value.CompareTo(min) >= 0 && argumentInfo.Value.CompareTo(max) <= 0)
            {
                return(argumentInfo);
            }

            if (message == null)
            {
                throw new ArgumentOutOfRangeException($"{argumentInfo.Name} was not in range [{min}, {max}]");
            }

            throw new ArgumentOutOfRangeException(message(argumentInfo.Value));
        }
Example #9
0
 protected static ArgumentOutOfRangeException[] ThrowsArgumentOutOfRangeException <T>(
     Guard.ArgumentInfo <T> argument,
     Action <Guard.ArgumentInfo <T> > testWithoutMessage,
     Action <Guard.ArgumentInfo <T>, string> testWithMessage,
     bool allowMessageMismatch = false)
 => ThrowsArgumentOutOfRangeException(argument, testWithoutMessage, null, testWithMessage, allowMessageMismatch);
Example #10
0
 internal static Guard.ArgumentInfo <DateTime> IsNoFutureDate(this Guard.ArgumentInfo <DateTime> argument)
 {
     return(argument.LessThan(DateTime.Today.AddDays(1)));
 }