Exemplo n.º 1
0
        public void validator_inspect_isNotNull_after_if()
        {
            IEnsure <string> obj    = Ensure.That("");
            IEnsure <string> actual = obj.If(s => true);

            Assert.IsNotNull(actual);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public void validator_name_using_Named_fluent_interface()
        {
            string expected = "paramName";

            IEnsure <int> obj = Ensure.That(0)
                                .Named(expected);

            Assert.AreEqual <string>(expected, obj.Name);
        }
Exemplo n.º 4
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 within a given range.
        /// </summary>
        /// <typeparam name="T">The inspected value type.</typeparam>
        /// <param name="validator">The Ensure class to extend.</param>
        /// <param name="lowerBoundary">The lower boundary.</param>
        /// <param name="higherBoundary">The higher boundary.</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 outside the expected range.</exception>
        public static IEnsure <T> IsWithin <T>(this IEnsure <T> validator, T lowerBoundary, T higherBoundary, Boundary boundaryBehavior) where T : IComparable <T>
        {
            var includeLower  = (boundaryBehavior & Boundary.IncludeLower) == Boundary.IncludeLower;
            var includeHigher = (boundaryBehavior & Boundary.IncludeHigher) == Boundary.IncludeHigher;

            validator.IsGreaterThen(lowerBoundary, includeLower ? Or.Equal : Or.NotEqual)
            .IsSmallerThen(higherBoundary, includeHigher ? Or.Equal : Or.NotEqual);

            return(validator);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        /// <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);
        }
Exemplo n.º 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 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);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Determines whether the specified enumeration value is defined.
        /// </summary>
        /// <typeparam name="T">THe type of the enumeration to inspect.</typeparam>
        /// <param name="validator">The current validator.</param>
        /// <returns>
        /// The Ensure instance for fluent interface usage.
        /// </returns>
        /// <exception cref="NotSupportedException">A <c>NotSupportedException</c> is raised if the supplied type T is not an enum type.</exception>
        /// <exception cref="EnumValueOutOfRangeException">An <c>EnumValueOutOfRangeException</c>
        /// is raised if the supplied enum value is not defined.</exception>
        public static IEnsure <T> IsDefined <T>(this IEnsure <T> validator)
        {
            if (!typeof(T).Is <Enum>())
            {
                throw new NotSupportedException("Only enum types are supported by this ensure extension");
            }

            var enumType = validator.Value.GetType();

            if (!Enum.IsDefined(enumType, validator.Value))
            {
                validator.Throw(new EnumValueOutOfRangeException(Resources.Exceptions.EnumValidatorNotDefinedException));
            }

            return(validator);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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 and not a null 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>
 /// <exception cref="ArgumentNullException">An <c>ArgumentNullException</c>
 /// is raised if the current inspected object is a null string.</exception>
 public static IEnsure <string> IsNotNullNorEmpty(this IEnsure <string> validator)
 {
     return(validator.IsNotNull().IsNotEmpty());
 }
Exemplo n.º 12
0
        public void stringEnsureTest_IsNotEmpty_valid()
        {
            IEnsure <string> obj = Ensure.That("foo").IsNotEmpty();

            Assert.IsNotNull(obj);
        }
Exemplo n.º 13
0
 public FileClient(IProperty property, IEnsure ensure)
 {
     _property = property;
     _ensure   = ensure;
 }
Exemplo n.º 14
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 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>
 /// <returns>
 /// The Ensure instance for fluent interface usage.
 /// </returns>
 /// <exception cref="ArgumentOutOfRangeException">An <c>ArgumentOutOfRangeException</c>
 /// is raised if the current inspected object is smaller then the expected value.</exception>
 public static IEnsure <T> IsGreaterThen <T>(this IEnsure <T> validator, T expected) where T : IComparable <T>
 {
     return(ComparableEnsureExtension.IsGreaterThen(validator, expected, Or.NotEqual));
 }
Exemplo n.º 15
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 within a given range.
 /// </summary>
 /// <typeparam name="T">The inspected value type.</typeparam>
 /// <param name="validator">The Ensure class to extend.</param>
 /// <param name="lowerBoundary">The lower boundary.</param>
 /// <param name="higherBoundary">The higher boundary.</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 outside the expected range.</exception>
 public static IEnsure <T> IsWithin <T>(this IEnsure <T> validator, T lowerBoundary, T higherBoundary) where T : IComparable <T>
 {
     return(ComparableEnsureExtension.IsWithin(validator, lowerBoundary, higherBoundary, Boundary.ExcludeBounds));
 }
Exemplo n.º 16
0
        public void validator_instpect_on_notNullString_valid()
        {
            IEnsure <string> obj = Ensure.That("Foo").IsNotNull();

            Assert.IsNotNull(obj);
        }
Exemplo n.º 17
0
 /// <summary>
 /// Throws an <see cref="ArgumentException"/> if the currently
 /// inspected <see cref="Type"/> does not match the specified Type.
 /// </summary>
 /// <typeparam name="T">The <see cref="Type"/> to compare.</typeparam>
 /// <param name="validator">The current validator.</param>
 /// <returns>The current validator for fluent interface usage.</returns>
 public static IEnsure <Type> Is <T>(this IEnsure <Type> validator)
 {
     return(validator.IsTrue(data => data.Is <T>()));
 }
Exemplo n.º 18
0
        public void validator_inspect_isNotNull()
        {
            IEnsure <string> obj = Ensure.That("");

            Assert.IsNotNull(obj);
        }