Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Guard"/>.
        /// </summary>
        /// <param name="value">T value</param>
        /// <param name="guardException"><see cref="GuardException"/></param>
        public Guard(T value, GuardException guardException)
        {
            if (guardException == null)
            {
                throw Error.GuardExceptionMustNotBeNull();
            }

            Value          = value;
            GuardException = guardException;
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Guard"/>.
        /// </summary>
        /// <param name="values">Object values</param>
        /// <param name="guardException"><see cref="GuardException"/></param>
        public Guard(object[] values, GuardException guardException)
        {
            if (guardException == null)
            {
                throw Error.GuardExceptionMustNotBeNull();
            }

            Values         = values;
            GuardException = guardException;
        }
Beispiel #3
0
        public void MessageNullFailure(string name, string message)
        {
            var exception = new GuardException(message, name);

            Assert.IsTrue(string.Equals(exception.ParamName, name, System.StringComparison.Ordinal), $"Parameter name ({ exception.ParamName ?? "null" }) does not match name ({ name ?? "null" })");

            var messageSuffix   = string.IsNullOrEmpty(name) ? "" : $" (Parameter '{ name }')";
            var expectedMessage = $"{ message }{messageSuffix}";

            Assert.IsTrue(string.Equals(exception.Message, expectedMessage, System.StringComparison.Ordinal), $"Parameter message ({ exception.Message ?? "null" }) does not match message ({ expectedMessage })");
        }
Beispiel #4
0
        public void GuardExceptionShouldBeAssignableToSystemExceptionType()
        {
            // Arrange
            var exceptionType = typeof(Exception);

            // Act
            var exception = new GuardException("message");

            // Assert
            exception.Should().BeAssignableTo(exceptionType);
        }
Beispiel #5
0
        public void CreateShouldFail()
        {
            // Arrange
            string         value          = null;
            GuardException guardException = null;

            // Act
            Action ctor = () => new Guard <string>(value, guardException);

            // Assert
            ctor.Should().Throw <GuardException>();
        }
Beispiel #6
0
        public void CreateShouldSucceed()
        {
            // Arrange
            var value = "value";
            var guardExceptionMessage = "Value must not be null.";
            var guardException        = new GuardException(guardExceptionMessage);

            // Act
            Action ctor = () => new Guard <string>(value, guardException);

            // Assert
            ctor.Should().NotThrow();
        }
Beispiel #7
0
        public void AgainstNullShouldReturnProvidedExceptionWhenNullableTypeValueIsNull()
        {
            // Arrange
            int?value = null;
            var guardExceptionMessage = "Value must not be null.";
            var guardException        = new GuardException(guardExceptionMessage);
            var guard = new Guard <int?>(value, guardException);

            // Act
            Action act = () => { guard.AgainstNull(); };

            // Assert
            act.Should().Throw <GuardException>().WithMessage(guardExceptionMessage);
        }
Beispiel #8
0
        public void AgainstNullShouldNotThrowWhenNullableTypeValueIsNotNull()
        {
            // Arrange
            Nullable <int> value = 1;
            var            guardExceptionMessage = "Value must not be null.";
            var            guardException        = new GuardException(guardExceptionMessage);
            var            guard = new Guard <Nullable <int> >(value, guardException);

            // Act
            Action act = () => { guard.AgainstNull(); };

            // Assert
            act.Should().NotThrow();
        }
Beispiel #9
0
        public void AgainstNullShouldNotThrowWhenReferenceTypeValueIsNotNull()
        {
            // Arrange
            string value = "A not null value.";
            var    guardExceptionMessage = "Value must not be null.";
            var    guardException        = new GuardException(guardExceptionMessage);
            var    guard = new Guard <string>(value, guardException);

            // Act
            Action act = () => { guard.AgainstNull(); };

            // Assert
            act.Should().NotThrow();
        }
Beispiel #10
0
        public void EachAgainstNullMustNotThrowWhenSomeObjectIsNull()
        {
            // Arrange
            object[] value = new object[] { new object(), null, new object() };


            var guardExceptionMessage = "Value must not be null.";
            var guardException        = new GuardException(guardExceptionMessage);
            var guard = new Guard <object[]>(value, guardException);

            // Act
            Action act = () => { guard.EachAgainstNull(); };

            // Assert
            act.Should().Throw <GuardException>().WithMessage(guardExceptionMessage);
        }
Beispiel #11
0
 /// <summary>
 /// Guard on 'object[]'.
 /// </summary>
 /// <param name="values">object values</param>
 /// <param name="guardException"><see cref="GuardException"/></param>
 /// <returns></returns>
 public static Guard <object[]> On(object[] values, GuardException guardException)
 {
     return(new Guard <object[]>(values, guardException));
 }
Beispiel #12
0
 /// <summary>
 /// Guard on 'T'.
 /// </summary>
 /// <typeparam name="T">Bound value type</typeparam>
 /// <param name="value">T value</param>
 /// <param name="guardException"><see cref="GuardException"/></param>
 /// <returns></returns>
 public static Guard <T> On <T>(T value, GuardException guardException)
 {
     return(new Guard <T>(value, guardException));
 }