Browse Source

[SL.Core] Remove additional unused Guard methods

af/octree-no-pixelmap
Jason Nelson 8 years ago
parent
commit
9e4008604f
  1. 58
      src/SixLabors.Core/Helpers/Guard.cs
  2. 79
      tests/SixLabors.Core.Tests/Helpers/GuardTests.cs

58
src/SixLabors.Core/Helpers/Guard.cs

@ -14,60 +14,6 @@ namespace SixLabors
[DebuggerStepThrough]
internal static class Guard
{
/// <summary>
/// Verifies, that the method parameter with specified object value is not null
/// and throws an exception if it is found to be so.
/// </summary>
/// <param name="target">The target object, which cannot be null.</param>
/// <param name="parameterName">The name of the parameter that is to be checked.</param>
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null</exception>
/// <typeparam name="T">The type of the object to verify</typeparam>
public static void NotNull<T>(T target, string parameterName)
where T : class
{
if (target is null)
{
throw new ArgumentNullException(parameterName);
}
}
/// <summary>
/// Verifies, that the string method parameter with specified object value and message
/// is not null, not empty and does not contain only blanks and throws an exception
/// if the object is null.
/// </summary>
/// <param name="target">The target string, which should be checked against being null or empty.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="target"/> is empty or contains only blanks.</exception>
public static void NotNullOrEmpty(string target, string parameterName)
{
NotNull(target, parameterName);
if (string.IsNullOrWhiteSpace(target))
{
throw new ArgumentException("Value cannot be null, empty, or cannot contain only whitespace.", parameterName);
}
}
/// <summary>
/// Verifies, that the enumeration is not null and not empty.
/// </summary>
/// <typeparam name="T">The type of objects in the <paramref name="target"/></typeparam>
/// <param name="target">The target enumeration, which should be checked against being null or empty.</param>
/// <param name="parameterName">Name of the parameter.</param>
/// <exception cref="ArgumentNullException"><paramref name="target"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="target"/> is empty.</exception>
public static void NotNullOrEmpty<T>(IEnumerable<T> target, string parameterName)
{
NotNull(target, parameterName);
if (!target.Any())
{
throw new ArgumentException("Value cannot be empty.", parameterName);
}
}
/// <summary>
/// Verifies that the specified value is less than a maximum value
/// and throws an exception if it is not.
@ -80,7 +26,7 @@ namespace SixLabors
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName)
where TValue : IComparable<TValue>
where TValue : IComparable<TValue>
{
if (value.CompareTo(max) >= 0)
{
@ -100,7 +46,7 @@ namespace SixLabors
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName)
where TValue : IComparable<TValue>
where TValue : IComparable<TValue>
{
if (value.CompareTo(max) > 0)
{

79
tests/SixLabors.Core.Tests/Helpers/GuardTests.cs

@ -2,91 +2,12 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
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<ArgumentNullException>(() =>
{
Guard.NotNull((object)null, "myParamName");
});
}
[Fact]
public void NotNullOrEmpty_TargetNotNullOrEmpty_ThrowsNoException()
{
Guard.NotNullOrEmpty("test", "myParamName");
}
[Fact]
public void NotNullOrEmpty_TargetNull_ThrowsException()
{
Assert.Throws<ArgumentNullException>(() =>
{
Guard.NotNullOrEmpty(null, "myParamName");
});
}
[Fact]
public void NotNullOrEmpty_TargetWhitespace_ThrowsException()
{
Assert.Throws<ArgumentException>(() =>
{
Guard.NotNullOrEmpty("\n\n", "myParamName");
});
}
[Fact]
public void NotNullOrEmpty_TargetEmpty_ThrowsException()
{
var exception = Assert.Throws<ArgumentException>(() =>
{
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 NotNullOrEmptyIEnumerable_TargetNotNullOrEmpty_ThrowsNoException()
{
Guard.NotNullOrEmpty(new string[] { "test" }, "myParamName");
}
[Fact]
public void NotNullOrEmptyIEnumerable_TargetNull_ThrowsException()
{
Assert.Throws<ArgumentNullException>(() =>
{
Guard.NotNullOrEmpty((IEnumerable<string>)null, "myParamName");
});
}
[Fact]
public void NotNullOrEmptyIEnumerable_TargetEmpty_ThrowsException()
{
var exception = Assert.Throws<ArgumentException>(() =>
{
Guard.NotNullOrEmpty(new string[] { }, "myParamName");
});
Assert.Equal("myParamName", exception.ParamName);
Assert.True(exception.Message.Contains("Value cannot be empty."));
}
[Fact]
public void MustBeLessThan_IsLess_ThrowsNoException()
{

Loading…
Cancel
Save