diff --git a/src/SixLabors.Core/Helpers/Guard.cs b/src/SixLabors.Core/Helpers/Guard.cs index e433de44e..4a738d8b3 100644 --- a/src/SixLabors.Core/Helpers/Guard.cs +++ b/src/SixLabors.Core/Helpers/Guard.cs @@ -56,7 +56,7 @@ namespace SixLabors throw new ArgumentException(message, parameterName); } - throw new ArgumentException("Value cannot be null or empty and cannot contain only blanks.", parameterName); + throw new ArgumentException("Value cannot be null, empty, or cannot contain only whitespace.", parameterName); } } @@ -92,7 +92,7 @@ namespace SixLabors /// The maximum value. /// The name of the parameter that is to be checked. /// The type of the value. - /// + /// /// is greater than the maximum value. /// public static void MustBeLessThan(TValue value, TValue max, string parameterName) @@ -112,7 +112,7 @@ namespace SixLabors /// The maximum value. /// The name of the parameter that is to be checked. /// The type of the value. - /// + /// /// is greater than the maximum value. /// public static void MustBeLessThanOrEqualTo(TValue value, TValue max, string parameterName) @@ -132,7 +132,7 @@ namespace SixLabors /// The minimum value. /// The name of the parameter that is to be checked. /// The type of the value. - /// + /// /// is less than the minimum value. /// public static void MustBeGreaterThan(TValue value, TValue min, string parameterName) @@ -140,9 +140,7 @@ namespace SixLabors { if (value.CompareTo(min) <= 0) { - throw new ArgumentOutOfRangeException( - parameterName, - $"Value must be greater than {min}."); + throw new ArgumentOutOfRangeException(parameterName, $"Value must be greater than {min}."); } } @@ -154,7 +152,7 @@ namespace SixLabors /// The minimum value. /// The name of the parameter that is to be checked. /// The type of the value. - /// + /// /// is less than the minimum value. /// public static void MustBeGreaterThanOrEqualTo(TValue value, TValue min, string parameterName) @@ -175,7 +173,7 @@ namespace SixLabors /// The maximum value. /// The name of the parameter that is to be checked. /// The type of the value. - /// + /// /// is less than the minimum value of greater than the maximum value. /// public static void MustBeBetweenOrEqualTo(TValue value, TValue min, TValue max, string parameterName) @@ -191,7 +189,7 @@ namespace SixLabors /// Verifies, that the method parameter with specified target value is true /// and throws an exception if it is found to be so. /// - /// + /// /// The target value, which cannot be false. /// /// @@ -201,11 +199,11 @@ namespace SixLabors /// The error message, if any to add to the exception. /// /// - /// is false + /// is false /// - public static void IsTrue(bool target, string parameterName, string message) + public static void IsTrue(bool value, string parameterName, string message) { - if (!target) + if (!value) { throw new ArgumentException(message, parameterName); } @@ -215,35 +213,35 @@ namespace SixLabors /// Verifies, that the method parameter with specified target value is false /// and throws an exception if it is found to be so. /// - /// The target value, which cannot be true. + /// The target value, which cannot be true. /// The name of the parameter that is to be checked. /// The error message, if any to add to the exception. /// - /// is true + /// is true /// - public static void IsFalse(bool target, string parameterName, string message) + public static void IsFalse(bool value, string parameterName, string message) { - if (target) + if (value) { throw new ArgumentException(message, parameterName); } } /// - /// Verifies, that the `target` span has the length of 'minSpan', or longer. + /// Verifies, that the `target` span has the length of 'minLength', or longer. /// /// The element type of the spans - /// The target span. + /// The target span. /// The minimum length. /// The name of the parameter that is to be checked. /// - /// is true + /// The length of is less than . /// - public static void MustBeSizedAtLeast(ReadOnlySpan target, int minLength, string parameterName) + public static void MustBeSizedAtLeast(ReadOnlySpan value, int minLength, string parameterName) { - if (target.Length < minLength) + if (value.Length < minLength) { - throw new ArgumentException($"Span-s must be at least of length {minLength}!", parameterName); + throw new ArgumentException($"The size must be at least {minLength}.", parameterName); } } } diff --git a/tests/SixLabors.Core.Tests/Helpers/GuardTests.cs b/tests/SixLabors.Core.Tests/Helpers/GuardTests.cs new file mode 100644 index 000000000..c91ebbcd6 --- /dev/null +++ b/tests/SixLabors.Core.Tests/Helpers/GuardTests.cs @@ -0,0 +1,286 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace SixLabors.Helpers.Tests +{ + public class GuardTests + { + [Fact] + public void NotNull_TargetNotNull_ThrowsNoException() + { + Guard.NotNull("test", "myParamName"); + } + + [Fact] + public void NotNull_TargetNull_ThrowsException() + { + Assert.Throws(() => + { + Guard.NotNull(null, "myParamName"); + }); + } + + [Fact] + public void NotNull_TargetNullWithMessage_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNull(null, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Fact] + public void NotNullOrEmpty_TargetNotNullOrEmpty_ThrowsNoException() + { + Guard.NotNullOrEmpty("test", "myParamName"); + } + + [Fact] + public void NotNullOrEmpty_TargetNull_ThrowsException() + { + Assert.Throws(() => + { + Guard.NotNullOrEmpty(null, "myParamName"); + }); + } + + [Fact] + public void NotNullOrEmpty_TargetWhitespace_ThrowsException() + { + Assert.Throws(() => + { + Guard.NotNullOrEmpty("\n\n", "myParamName"); + }); + } + + [Fact] + public void NotNullOrEmpty_TargetEmpty_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNullOrEmpty(string.Empty, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("Value cannot be null, empty, or cannot contain only whitespace.")); + } + + [Fact] + public void NotNullOrEmpty_TargetEmptyWithMessage_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNullOrEmpty(string.Empty, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Fact] + public void NotNullOrEmptyIEnumerable_TargetNotNullOrEmpty_ThrowsNoException() + { + Guard.NotNullOrEmpty(new string[] { "test" }, "myParamName"); + } + + [Fact] + public void NotNullOrEmptyIEnumerable_TargetNull_ThrowsException() + { + Assert.Throws(() => + { + Guard.NotNullOrEmpty((IEnumerable)null, "myParamName"); + }); + } + + [Fact] + public void NotNullOrEmptyIEnumerable_TargetEmpty_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNullOrEmpty(new string[] { }, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("Value cannot be empty.")); + } + + [Fact] + public void NotNullOrEmptyIEnumerable_TargetEmptyWithMessage_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.NotNullOrEmpty(new string[] { }, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Fact] + public void MustBeLessThan_IsLess_ThrowsNoException() + { + Guard.MustBeLessThan(0, 1, "myParamName"); + } + + [Theory] + [InlineData(2, 1)] + [InlineData(1, 1)] + public void MustBeLessThan_IsGreaterOrEqual_ThrowsNoException(int value, int max) + { + var exception = Assert.Throws(() => + { + Guard.MustBeLessThan(value, max, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be less than {max}.")); + } + + [Theory] + [InlineData(0, 1)] + [InlineData(1, 1)] + public void MustBeLessThanOrEqualTo_IsLessOrEqual_ThrowsNoException(int value, int max) + { + Guard.MustBeLessThanOrEqualTo(value, max, "myParamName"); + } + + [Fact] + public void MustBeLessThanOrEqualTo_IsGreater_ThrowsNoException() + { + var exception = Assert.Throws(() => + { + Guard.MustBeLessThanOrEqualTo(2, 1, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be less than or equal to 1.")); + } + + [Fact] + public void MustBeGreaterThan_IsGreater_ThrowsNoException() + { + Guard.MustBeGreaterThan(2, 1, "myParamName"); + } + + [Theory] + [InlineData(1, 2)] + [InlineData(1, 1)] + public void MustBeGreaterThan_IsLessOrEqual_ThrowsNoException(int value, int min) + { + var exception = Assert.Throws(() => + { + Guard.MustBeGreaterThan(value, min, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be greater than {min}.")); + } + + [Theory] + [InlineData(2, 1)] + [InlineData(1, 1)] + public void MustBeGreaterThanOrEqualTo_IsGreaterOrEqual_ThrowsNoException(int value, int min) + { + Guard.MustBeGreaterThanOrEqualTo(value, min, "myParamName"); + } + + [Fact] + public void MustBeGreaterThanOrEqualTo_IsLess_ThrowsNoException() + { + var exception = Assert.Throws(() => + { + Guard.MustBeGreaterThanOrEqualTo(1, 2, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be greater than or equal to 2.")); + } + + [Theory] + [InlineData(1, 1, 3)] + [InlineData(2, 1, 3)] + [InlineData(3, 1, 3)] + public void MustBeBetweenOrEqualTo_IsBetweenOrEqual_ThrowsNoException(int value, int min, int max) + { + Guard.MustBeBetweenOrEqualTo(value, min, max, "myParamName"); + } + + [Theory] + [InlineData(0, 1, 3)] + [InlineData(4, 1, 3)] + public void MustBeBetweenOrEqualTo_IsLessOrGreater_ThrowsNoException(int value, int min, int max) + { + var exception = Assert.Throws(() => + { + Guard.MustBeBetweenOrEqualTo(value, min, max, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"Value must be greater than or equal to {min} and less than or equal to {max}.")); + } + + [Fact] + public void IsTrue_IsTrue_ThrowsNoException() + { + Guard.IsTrue(true, "myParamName", "myTestMessage"); + } + + [Fact] + public void IsTrue_IsFalse_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.IsTrue(false, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Fact] + public void IsFalse_IsFalse_ThrowsNoException() + { + Guard.IsFalse(false, "myParamName", "myTestMessage"); + } + + [Fact] + public void IsFalse_IsTrue_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.IsFalse(true, "myParamName", "myTestMessage"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains("myTestMessage")); + } + + [Theory] + [InlineData(new int[] { 1, 2 }, 1)] + [InlineData(new int[] { 1, 2 }, 2)] + public void MustBeSizedAtLeast_LengthIsGreaterOrEqual_ThrowsNoException(int[] value, int minLength) + { + Guard.MustBeSizedAtLeast(value, minLength, "myParamName"); + } + + [Fact] + public void MustBeSizedAtLeast_LengthIsLess_ThrowsException() + { + var exception = Assert.Throws(() => + { + Guard.MustBeSizedAtLeast(new int[] { 1, 2 }, 3, "myParamName"); + }); + + Assert.Equal("myParamName", exception.ParamName); + Assert.True(exception.Message.Contains($"The size must be at least 3.")); + } + } +}