Browse Source

React to Gaurd changes

af/merge-core
Jason Nelson 8 years ago
parent
commit
54c26c476a
  1. 26
      src/ImageSharp/Common/Helpers/Guard.cs
  2. 2
      src/ImageSharp/ImageExtensions.cs
  3. 12
      tests/ImageSharp.Tests/Helpers/GuardTests.cs

26
src/ImageSharp/Common/Helpers/Guard.cs

@ -4,7 +4,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
namespace SixLabors.ImageSharp namespace SixLabors.ImageSharp
{ {
@ -19,17 +18,11 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="value">The target object, which cannot be null.</param> /// <param name="value">The target object, which cannot be null.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param> /// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <param name="message">The error message, if any to add to the exception.</param>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception> /// <exception cref="ArgumentNullException"><paramref name="value"/> is null</exception>
public static void NotNull(object value, string parameterName, string message = "") public static void NotNull(object value, string parameterName)
{ {
if (value == null) if (value == null)
{ {
if (!string.IsNullOrWhiteSpace(message))
{
throw new ArgumentNullException(parameterName, message);
}
throw new ArgumentNullException(parameterName); throw new ArgumentNullException(parameterName);
} }
} }
@ -60,17 +53,16 @@ namespace SixLabors.ImageSharp
/// <typeparam name="T">The type of objects in the <paramref name="value"/></typeparam> /// <typeparam name="T">The type of objects in the <paramref name="value"/></typeparam>
/// <param name="value">The target enumeration, which should be checked against being null or empty.</param> /// <param name="value">The target enumeration, which should be checked against being null or empty.</param>
/// <param name="parameterName">Name of the parameter.</param> /// <param name="parameterName">Name of the parameter.</param>
/// <param name="message">The error message, if any to add to the exception.</param>
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception> /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="value"/> is empty.</exception> /// <exception cref="ArgumentException"><paramref name="value"/> is empty.</exception>
public static void NotNullOrEmpty<T>(IEnumerable<T> value, string parameterName) public static void NotNullOrEmpty<T>(ICollection<T> value, string parameterName)
{ {
if (value == null) if (value == null)
{ {
throw new ArgumentNullException(parameterName); throw new ArgumentNullException(parameterName);
} }
if (!value.Any()) if (value.Count == 0)
{ {
throw new ArgumentException("Must not be empty.", parameterName); throw new ArgumentException("Must not be empty.", parameterName);
} }
@ -182,15 +174,9 @@ namespace SixLabors.ImageSharp
/// Verifies, that the method parameter with specified target value is true /// Verifies, that the method parameter with specified target value is true
/// and throws an exception if it is found to be so. /// and throws an exception if it is found to be so.
/// </summary> /// </summary>
/// <param name="target"> /// <param name="target">The target value, which cannot be false.</param>
/// The target value, which cannot be false. /// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// </param> /// <param name="message">The error message, if any to add to the exception.</param>
/// <param name="parameterName">
/// The name of the parameter that is to be checked.
/// </param>
/// <param name="message">
/// The error message, if any to add to the exception.
/// </param>
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// <paramref name="target"/> is false /// <paramref name="target"/> is false
/// </exception> /// </exception>

2
src/ImageSharp/ImageExtensions.cs

@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp
public static void Save<TPixel>(this Image<TPixel> source, string filePath) public static void Save<TPixel>(this Image<TPixel> source, string filePath)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
Guard.NotNullOrEmpty(filePath, nameof(filePath)); Guard.NotNullOrWhiteSpace(filePath, nameof(filePath));
string ext = Path.GetExtension(filePath).Trim('.'); string ext = Path.GetExtension(filePath).Trim('.');
IImageFormat format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext); IImageFormat format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext);

12
tests/ImageSharp.Tests/Helpers/GuardTests.cs

@ -35,27 +35,27 @@ namespace SixLabors.ImageSharp.Tests.Helpers
/// </summary> /// </summary>
[Fact] [Fact]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1122:UseStringEmptyForEmptyStrings", Justification = "Reviewed. Suppression is OK here.")] [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1122:UseStringEmptyForEmptyStrings", Justification = "Reviewed. Suppression is OK here.")]
public void NotEmptyThrowsWhenEmpty() public void NotEmptyOrWhiteSpaceThrowsWhenEmpty()
{ {
Assert.Throws<ArgumentException>(() => Guard.NotNullOrEmpty("", string.Empty)); Assert.Throws<ArgumentException>(() => Guard.NotNullOrWhiteSpace("", string.Empty));
} }
/// <summary> /// <summary>
/// Tests that the <see cref="M:Guard.NotEmpty"/> method throws when the argument is whitespace. /// Tests that the <see cref="M:Guard.NotEmpty"/> method throws when the argument is whitespace.
/// </summary> /// </summary>
[Fact] [Fact]
public void NotEmptyThrowsWhenWhitespace() public void NotEmptyOrWhiteSpaceThrowsOnWhitespace()
{ {
Assert.Throws<ArgumentException>(() => Guard.NotNullOrEmpty(" ", string.Empty)); Assert.Throws<ArgumentException>(() => Guard.NotNullOrWhiteSpace(" ", string.Empty));
} }
/// <summary> /// <summary>
/// Tests that the <see cref="M:Guard.NotEmpty"/> method throws when the argument name is null. /// Tests that the <see cref="M:Guard.NotEmpty"/> method throws when the argument name is null.
/// </summary> /// </summary>
[Fact] [Fact]
public void NotEmptyThrowsWhenParameterNameNull() public void NotEmptyOrWhiteSpaceThrowsWhenParameterNameNull()
{ {
Assert.Throws<ArgumentNullException>(() => Guard.NotNullOrEmpty(null, null)); Assert.Throws<ArgumentNullException>(() => Guard.NotNullOrWhiteSpace(null, null));
} }
/// <summary> /// <summary>

Loading…
Cancel
Save