Example #1
0
        /// <summary>
        /// Extends the Ensure class when the inspected object is a generic <c>IComparabe</c>
        /// and can be used to ensure that the inspected value is greater then or equale to
        /// an expected value.
        /// </summary>
        /// <typeparam name="T">The inspected value type.</typeparam>
        /// <param name="validator">The Ensure class to extend.</param>
        /// <param name="expected">The expected value to compare to.</param>
        /// <param name="boundaryBehavior">The boundary behavior in order to
        /// be able to specify an OrEqual behavior.</param>
        /// <returns>
        /// The Ensure instance for fluent interface usage.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">An <c>ArgumentOutOfRangeException</c>
        /// is raised if the current inspected object is smaler then the expected value.</exception>
        public static IEnsure <T> IsGreaterThen <T>(this IEnsure <T> validator, T expected, Or boundaryBehavior) where T : IComparable <T>
        {
            validator.If(s =>
            {
                var result = s.CompareTo(expected);

                switch (boundaryBehavior)
                {
                case Or.Equal:
                    return(result < 0);

                case Or.NotEqual:
                    return(result <= 0);

                default:
                    throw new NotSupportedException();
                }
            })
            .ThenThrow(v =>
            {
                var msg = string.Format("The inspected value should be greater then{0} the given one.", boundaryBehavior == Or.Equal ? " or equal to" : "");
                return(new ArgumentOutOfRangeException(v.Name, v.GetFullErrorMessage(msg)));
            });

            return(validator);
        }
Example #2
0
        public void validator_inspect_isNotNull_after_if()
        {
            IEnsure <string> obj    = Ensure.That("");
            IEnsure <string> actual = obj.If(s => true);

            Assert.IsNotNull(actual);
        }
Example #3
0
        /// <summary>
        /// Extends the Ensure class when the inspected object is a <c>Guid</c> and can
        /// be used to ensure that the inspected Guid is not an empty Guid.
        /// </summary>
        /// <param name="validator">The Ensure class to extend.</param>
        /// <returns>
        /// The Ensure instance for fluent interface usage.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">An <c>ArgumentOutOfRangeException</c>
        /// is raised if the current inspected object is an empty Guid.</exception>
        public static IEnsure <Guid> IsNotEmpty(this IEnsure <Guid> validator)
        {
            validator.If(g => g == Guid.Empty)
            .ThenThrow(v =>
            {
                return(new ArgumentOutOfRangeException(v.Name, v.GetFullErrorMessage("The inspected guid value should be not empty.")));
            });

            return(validator);
        }
        /// <summary>
        /// Determines whether the specified index is inside the bounds of
        /// the inspected array.
        /// </summary>
        /// <typeparam name="T">The type of the elements of the array.</typeparam>
        /// <param name="validator">The current, extended, Ensure instance.</param>
        /// <param name="index">The index to validate.</param>
        /// <returns>The Ensure instance for fluent interface usage.</returns>
        public static IEnsure <T[]> ContainsIndex <T>(this IEnsure <T[]> validator, int index)
        {
            validator.If(data => (index < 0 || index >= data.Length))
            .ThenThrow(v =>
            {
                var paramName = string.IsNullOrEmpty(v.Name) ? "index" : v.Name;
                var message   = v.GetFullErrorMessage("The supplied Array does not contains the given index.");

                return(new ArgumentOutOfRangeException(paramName, message));
            });

            return(validator);
        }
Example #5
0
        /// <summary>
        /// Extends the Ensure class when the inspected object is a <c>string</c> and can
        /// be used to ensure that the inspected string matches the given regular expression.
        /// </summary>
        /// <param name="validator">The Ensure class to extend.</param>
        /// <param name="regExPattern">The regular expression pattern.</param>
        /// <returns>
        /// The Ensure instance for fluent interface usage.
        /// </returns>
        /// <exception cref="FormatException">A <c>FormatException</c>
        /// is raised if the current inspected object does not match the given regular expression.
        /// </exception>
        public static IEnsure <string> Matches(this IEnsure <string> validator, string regExPattern)
        {
            validator.If(s =>
            {
                bool match = Regex.IsMatch(validator.Value, regExPattern);

                return(!match);
            })
            .ThenThrow(v =>
            {
                return(new FormatException(v.GetFullErrorMessage("The inspected string value does not match the given format.")));
            });

            return(validator);
        }
Example #6
0
        /// <summary>
        /// Throws an <see cref="ArgumentNullException"/> if the currently
        /// inspected object is a null reference.
        /// </summary>
        /// <typeparam name="T">The type of the inspected object.</typeparam>
        /// <param name="validator">The current validator.</param>
        /// <returns>The current validator for fluent interface usage.</returns>
        public static IEnsure <T> IsNotNull <T>(this IEnsure <T> validator)
            where T : class
        {
            //var value = validator.GetValue<T>();
            //if ( value == null )
            //{
            //    throw new ArgumentNullException( validator.Name, validator.GetFullErrorMessage( "The inspected value should be non null." ) );
            //}
            validator.If(s => s == null)
            .ThenThrow(e =>
            {
                return(new ArgumentNullException(e.Name, e.GetFullErrorMessage("The inspected value should be non null.")));
            });

            return(validator);
        }
Example #7
0
        /// <summary>
        /// Extends the Ensure class when the inspected object is a <c>string</c> and can
        /// be used to ensure that the inspected string is not an empty string.
        /// </summary>
        /// <param name="validator">The Ensure class to extend.</param>
        /// <returns>
        /// The Ensure instance for fluent interface usage.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">An <c>ArgumentOutOfRangeException</c>
        /// is raised if the current inspected object is an empty string.</exception>
        public static IEnsure <string> IsNotEmpty(this IEnsure <string> validator)
        {
            //var value = validator.GetValue<string>();

            //if ( value != null && value.Length == 0 )
            //{
            //    throw new ArgumentOutOfRangeException( validator.Name, validator.GetFullErrorMessage( "The inspected string value should be not empty." ) );
            //}

            validator.If(s => s != null && s.Length == 0)
            .ThenThrow(e =>
            {
                return(new ArgumentOutOfRangeException(e.Name, e.GetFullErrorMessage("The inspected string value should be not empty.")));
            });

            return(validator);
        }