예제 #1
0
        public void GuardAgainstZeroOrLess_ZeroOrLess_ThrowsException(int zeroOrLess)
        {
            var ex1 = Assert.Throws <ArgumentException>(() => Guard.Against.ZeroOrLess(zeroOrLess, nameof(zeroOrLess)));

            AssertMessage(ex1.Message);

            var ex2 = Assert.Throws <ArgumentException>(() => Guard.Against.ZeroOrLess(Convert.ToDecimal(zeroOrLess), nameof(zeroOrLess)));

            AssertMessage(ex2.Message);

            var ex3 = Assert.Throws <ArgumentException>(() => Guard.Against.ZeroOrLess(Convert.ToDouble(zeroOrLess), nameof(zeroOrLess)));

            AssertMessage(ex3.Message);

            void AssertMessage(string actualMessage)
            {
                if (zeroOrLess == 0)
                {
                    Assert.Equal("Number cannot be zero. (Parameter 'zeroOrLess')", actualMessage);
                }
                else
                {
                    Assert.Equal("Number cannot be negative. (Parameter 'zeroOrLess')", actualMessage);
                }
            }
        }
예제 #2
0
        public void GuardAgainstNull_Null_ThrowsException()
        {
            var ex1 = Assert.Throws <ArgumentNullException>(() =>
            {
                object myParam = null;
                Guard.Against.Null(myParam, nameof(myParam));
            });

            Assert.Equal("Parameter cannot be null. (Parameter 'myParam')", ex1.Message);

            var ex2 = Assert.Throws <ArgumentNullException>(() =>
            {
                object param1 = new object();
                object param2 = new object();
                object param3 = null;
                object param4 = null;

                Guard.Against
                .Null(param1, nameof(param1))
                .Null(param2, nameof(param2))
                .Null(param3, nameof(param3))
                .Null(param4, nameof(param4));
            });

            Assert.Equal("Parameter cannot be null. (Parameter 'param3')", ex2.Message);
        }
예제 #3
0
        public void GuardAgainstFalse_False_ThrowsException()
        {
            var ex1 = Assert.Throws <ArgumentException>(() =>
            {
                bool myParam = false;
                Guard.Against.False(myParam, nameof(myParam));
            });

            Assert.Equal("Parameter cannot be false. (Parameter 'myParam')", ex1.Message);

            var ex2 = Assert.Throws <ArgumentException>(() =>
            {
                string myParam = "abc";
                Guard.Against.False(_ => myParam == "a");
            });

            Assert.Equal("Predicate cannot be false.", ex2.Message);

            var ex3 = Assert.Throws <ArgumentException>(() =>
            {
                string myParam = "abc";
                Guard.Against.False(_ => myParam == "a", "My message when the expression is false!");
            });

            Assert.Equal("My message when the expression is false!", ex3.Message);
        }
예제 #4
0
        public void GuardAgainstNotExists_NotExists_ThrowsException(int value)
        {
            var orderStatus = (OrderStatus)value;
            var ex          = Assert.Throws <ArgumentException>(() => Guard.Against.NotExists(orderStatus));

            Assert.Equal($"{value} not exists in the OrderStatus.", ex.Message);
        }
        public void EnsureContentType_NotUtf8ApplicationJson_ThrowsException(string encoding, string mediaType)
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(string.Empty, Encoding.GetEncoding(encoding), mediaType)
            };

            Assert.Throws <HttpRequestException>(() => response.EnsureContentType());
        }
예제 #6
0
        public void GuardAgainstEmpty_Empty_ThrowsException()
        {
            var ex = Assert.Throws <ArgumentException>(() =>
            {
                var myGuid = Guid.Empty;
                Guard.Against.Empty(myGuid, nameof(myGuid));
            });

            Assert.Equal("Guid cannot be empty. (Parameter 'myGuid')", ex.Message);
        }
예제 #7
0
        public void GuardAgainstEmpty_Empty_ThrowsException()
        {
            var ex = Assert.Throws <ArgumentException>(() =>
            {
                var myCollection = new List <int>();
                Guard.Against.Empty(myCollection, nameof(myCollection));
            });

            Assert.Equal("Collection cannot be empty. (Parameter 'myCollection')", ex.Message);
        }
예제 #8
0
        public void GuardAgainstNotEmpty_NotEmpty_ThrowsException()
        {
            var ex = Assert.Throws <ArgumentException>(() =>
            {
                var myGuid = Guid.NewGuid();
                Guard.Against.NotEmpty(myGuid, nameof(myGuid));
            });

            Assert.Equal("Guid should be empty. (Parameter 'myGuid')", ex.Message);
        }
예제 #9
0
        public void GuardAgainstNullOrWhiteSpace_NullOrWhiteSpace_ThrowsException(string myParam)
        {
            var ex1 = Assert.Throws <ArgumentNullException>(() => Guard.Against.NullOrWhiteSpace(myParam, nameof(myParam)));

            Assert.Equal("Parameter cannot be null, empty or consists exclusively of white-space characters. (Parameter 'myParam')", ex1.Message);

            var ex2 = Assert.Throws <ArgumentNullException>(() => Guard.Against.NullOrWhiteSpace(myParam, nameof(myParam), "My custom error message"));

            Assert.Equal("My custom error message (Parameter 'myParam')", ex2.Message);
        }
예제 #10
0
        public void NewNif_InvalidNif_ThrowsException(string invalidNif)
        {
            var ex1 = Assert.Throws <FormatException>(() => new Nif(invalidNif));

            Assert.Equal($"Invalid NIF: {invalidNif}.", ex1.Message);

            var ex2 = Assert.Throws <FormatException>(() => (Nif)invalidNif);

            Assert.Equal($"Invalid NIF: {invalidNif}.", ex2.Message);

            var ex3 = Assert.Throws <FormatException>(() => Nif.NewNif(invalidNif));

            Assert.Equal($"Invalid NIF: {invalidNif}.", ex3.Message);
        }
예제 #11
0
        public void NewUrl_InvalidUrl_ThrowsException(string invalidUrl)
        {
            var ex1 = Assert.Throws <FormatException>(() => new Url(invalidUrl));

            Assert.Equal($"Invalid URL: {invalidUrl}.", ex1.Message);

            var ex2 = Assert.Throws <FormatException>(() => (Url)invalidUrl);

            Assert.Equal($"Invalid URL: {invalidUrl}.", ex2.Message);

            var ex3 = Assert.Throws <FormatException>(() => Url.NewUrl(invalidUrl));

            Assert.Equal($"Invalid URL: {invalidUrl}.", ex3.Message);
        }
예제 #12
0
        public void NewEmail_InvalidEmail_ThrowsException(string invalidEmail)
        {
            var ex1 = Assert.Throws <FormatException>(() => new Email(invalidEmail));

            Assert.Equal($"Invalid e-mail address: {invalidEmail}.", ex1.Message);

            var ex2 = Assert.Throws <FormatException>(() => (Email)invalidEmail);

            Assert.Equal($"Invalid e-mail address: {invalidEmail}.", ex2.Message);

            var ex3 = Assert.Throws <FormatException>(() => Email.NewEmail(invalidEmail));

            Assert.Equal($"Invalid e-mail address: {invalidEmail}.", ex3.Message);
        }
예제 #13
0
        public void GuardAgainstLessThan5_LessThan5_ThrowsException(int lessThan5)
        {
            var ex1 = Assert.Throws <ArgumentException>(() => Guard.Against.LessThan(5, lessThan5, nameof(lessThan5)));

            AssertMessage(ex1.Message);

            var ex2 = Assert.Throws <ArgumentException>(() => Guard.Against.LessThan(Convert.ToDecimal(5), lessThan5, nameof(lessThan5)));

            AssertMessage(ex2.Message);

            var ex3 = Assert.Throws <ArgumentException>(() => Guard.Against.LessThan(Convert.ToDouble(5), lessThan5, nameof(lessThan5)));

            AssertMessage(ex3.Message);

            void AssertMessage(string actualMessage) => Assert.Equal($"Number cannot be less than {lessThan5}. (Parameter 'lessThan5')", actualMessage);
        }
예제 #14
0
        public void GuardAgainstPositive_Positive_ThrowsException(int positive)
        {
            var ex1 = Assert.Throws <ArgumentException>(() => Guard.Against.Positive(positive, nameof(positive)));

            AssertMessage(ex1.Message);

            var ex2 = Assert.Throws <ArgumentException>(() => Guard.Against.Positive(Convert.ToDecimal(positive), nameof(positive)));

            AssertMessage(ex2.Message);

            var ex3 = Assert.Throws <ArgumentException>(() => Guard.Against.Positive(Convert.ToDouble(positive), nameof(positive)));

            AssertMessage(ex3.Message);

            void AssertMessage(string actualMessage) => Assert.Equal("Number cannot be positive. (Parameter 'positive')", actualMessage);
        }
예제 #15
0
        public void GuardAgainstZero_Zero_ThrowsException()
        {
            var zero = 0;

            var ex1 = Assert.Throws <ArgumentException>(() => Guard.Against.Zero(zero, nameof(zero)));

            AssertMessage(ex1.Message);

            var ex2 = Assert.Throws <ArgumentException>(() => Guard.Against.Zero(Convert.ToDecimal(zero), nameof(zero)));

            AssertMessage(ex2.Message);

            var ex3 = Assert.Throws <ArgumentException>(() => Guard.Against.Zero(Convert.ToDouble(zero), nameof(zero)));

            AssertMessage(ex3.Message);

            void AssertMessage(string actualMessage) => Assert.Equal("Number cannot be zero. (Parameter 'zero')", actualMessage);
        }
예제 #16
0
 public void GuardAgainstNotMatch_PatternNotMatched_ThrowsException(string myParam, string pattern)
 {
     Assert.Throws <ArgumentException>(() => Guard.Against.NotMatch(myParam, pattern, nameof(myParam)));
 }
예제 #17
0
        public void GuardAgainstExactLength_NotExactLength_ThrowsException(string myParam)
        {
            var ex = Assert.Throws <ArgumentOutOfRangeException>(() => Guard.Against.Length(3, myParam, nameof(myParam)));

            Assert.Equal("myParam must be length 3. (Parameter 'myParam')", ex.Message);
        }
예제 #18
0
        public void GuardAgainstInvalidEmail_InvalidEmail_ThrowsException(string invalidEmail)
        {
            var ex = Assert.Throws <FormatException>(() => Guard.Against.InvalidEmail(invalidEmail));

            Assert.Equal("Invalid e-mail address: {0}.".Format(invalidEmail), ex.Message);
        }
예제 #19
0
        public void GuardAgainstInvalidNif_InvalidNif_ThrowsException(string invalidNif)
        {
            var ex = Assert.Throws <FormatException>(() => Guard.Against.InvalidNif(invalidNif));

            Assert.Equal("Invalid NIF: {0}.".Format(invalidNif), ex.Message);
        }
예제 #20
0
        public void GuardAgainstMinLengthMaxLength_OutsideMinLengthMaxLength_ThrowsException(string myParam)
        {
            var ex = Assert.Throws <ArgumentOutOfRangeException>(() => Guard.Against.Length(1, 5, myParam, nameof(myParam)));

            Assert.Equal("myParam must be at least 1 character, and at most 5 characters. (Parameter 'myParam')", ex.Message);
        }
예제 #21
0
        public void GuardAgainstMinLength_OutsideMinLength_ThrowsException(string myParam)
        {
            var ex = Assert.Throws <ArgumentException>(() => Guard.Against.MinLength(3, myParam, nameof(myParam)));

            Assert.Equal("myParam must be at least 3 characters. (Parameter 'myParam')", ex.Message);
        }
예제 #22
0
        public void GuardAgainstGreaterThan5_GreaterThan5_ThrowsException(int greaterThan5)
        {
            var ex = Assert.Throws <ArgumentException>(() => Guard.Against.GreaterThan(5, greaterThan5, nameof(greaterThan5)));

            Assert.Equal($"Number cannot be greater than {greaterThan5}. (Parameter 'greaterThan5')", ex.Message);
        }
예제 #23
0
        public void GuardAgainstNullOrEmpty_NullOrEmpty_ThrowsException(string myParam)
        {
            var ex = Assert.Throws <ArgumentNullException>(() => Guard.Against.NullOrEmpty(myParam, nameof(myParam)));

            Assert.Equal("Parameter cannot be null or empty. (Parameter 'myParam')", ex.Message);
        }