Browse Source

Add Guard tests

Former-commit-id: f55de41ddb9ea4ee387ceb462b4786af7df5cacb
Former-commit-id: 636f57e4157a3f388ca28ee1e9e83cd11615f301
Former-commit-id: ea4683211d00a728dffab492db02b5537b9d38d4
af/merge-core
James Jackson-South 11 years ago
parent
commit
9db729459d
  1. 2
      src/ImageProcessor/Common/Extensions/ByteExtensions.cs
  2. 15
      src/ImageProcessor/Common/Helpers/Guard.cs
  3. 2
      src/ImageProcessor/Formats/Gif/LzwDecoder.cs
  4. 4
      src/ImageProcessor/Formats/Gif/Quantizer/OctreeQuantizer.cs
  5. 4
      src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs
  6. 10
      src/ImageProcessor/Formats/Jpg/LibJpeg/BitStream.cs
  7. 4
      src/ImageProcessor/ImageBase.cs
  8. 3
      src/ImageProcessor/Properties/AssemblyInfo.cs
  9. 209
      tests/ImageProcessor.Tests/Helpers/GuardTests.cs
  10. 3
      tests/ImageProcessor.Tests/ImageProcessor.Tests.csproj

2
src/ImageProcessor/Common/Extensions/ByteExtensions.cs

@ -29,7 +29,7 @@ namespace ImageProcessor
public static byte[] ToArrayByBitsLength(this byte[] bytes, int bits)
{
Guard.NotNull(bytes, "bytes");
Guard.GreaterThan(bits, 0, "bits");
Guard.MustBeGreaterThan(bits, 0, "bits");
byte[] result;

15
src/ImageProcessor/Common/Helpers/Guard.cs

@ -11,11 +11,12 @@
namespace ImageProcessor
{
using System;
using System.Globalization;
using System.Diagnostics;
/// <summary>
/// Provides methods to protect against invalid parameters.
/// </summary>
[DebuggerStepThrough]
internal static class Guard
{
/// <summary>
@ -70,7 +71,7 @@ namespace ImageProcessor
if (string.IsNullOrWhiteSpace(target))
{
throw new ArgumentException("String parameter cannot be null or empty and cannot contain only blanks.", parameterName);
throw new ArgumentException("Value cannot be null or empty and cannot contain only blanks.", parameterName);
}
}
@ -85,7 +86,7 @@ namespace ImageProcessor
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
public static void LessThan<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue>
public static void MustBeLessThan<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue>
{
if (value.CompareTo(max) >= 0)
{
@ -106,7 +107,7 @@ namespace ImageProcessor
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is greater than the maximum value.
/// </exception>
public static void LessEquals<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue>
public static void MustBeLessThanOrEqualTo<TValue>(TValue value, TValue max, string parameterName) where TValue : IComparable<TValue>
{
if (value.CompareTo(max) > 0)
{
@ -127,7 +128,7 @@ namespace ImageProcessor
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
public static void GreaterThan<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue>
public static void MustBeGreaterThan<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue>
{
if (value.CompareTo(min) <= 0)
{
@ -148,7 +149,7 @@ namespace ImageProcessor
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is less than the minimum value.
/// </exception>
public static void GreaterEquals<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue>
public static void MustBeGreaterThanOrEqualTo<TValue>(TValue value, TValue min, string parameterName) where TValue : IComparable<TValue>
{
if (value.CompareTo(min) < 0)
{
@ -170,7 +171,7 @@ namespace ImageProcessor
/// <exception cref="ArgumentException">
/// <paramref name="value"/> is less than the minimum value of greater than the maximum value.
/// </exception>
public static void BetweenEquals<TValue>(TValue value, TValue min, TValue max, string parameterName) where TValue : IComparable<TValue>
public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TValue max, string parameterName) where TValue : IComparable<TValue>
{
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
{

2
src/ImageProcessor/Formats/Gif/LzwDecoder.cs

@ -56,7 +56,7 @@ namespace ImageProcessor.Formats
/// <returns>The decoded and uncompressed array.</returns>
public byte[] DecodePixels(int width, int height, int dataSize)
{
Guard.LessThan(dataSize, int.MaxValue, nameof(dataSize));
Guard.MustBeLessThan(dataSize, int.MaxValue, nameof(dataSize));
// The resulting index table.
byte[] pixels = new byte[width * height];

4
src/ImageProcessor/Formats/Gif/Quantizer/OctreeQuantizer.cs

@ -57,8 +57,8 @@ namespace ImageProcessor.Formats
public OctreeQuantizer(int maxColors, int maxColorBits)
: base(false)
{
Guard.LessEquals(maxColors, 255, "maxColors");
Guard.BetweenEquals(maxColorBits, 1, 8, "maxColorBits");
Guard.MustBeLessThanOrEqualTo(maxColors, 255, "maxColors");
Guard.MustBeBetweenOrEqualTo(maxColorBits, 1, 8, "maxColorBits");
// Construct the Octree
this.octree = new Octree(maxColorBits);

4
src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs

@ -42,8 +42,8 @@ namespace ImageProcessor.Formats
/// </summary>
public QuantizedImage(int width, int height, Bgra[] palette, byte[] pixels)
{
Guard.GreaterThan(width, 0, nameof(width));
Guard.GreaterThan(height, 0, nameof(height));
Guard.MustBeGreaterThan(width, 0, nameof(width));
Guard.MustBeGreaterThan(height, 0, nameof(height));
Guard.NotNull(palette, nameof(palette));
Guard.NotNull(pixels, nameof(pixels));

10
src/ImageProcessor/Formats/Jpg/LibJpeg/BitStream.cs

@ -129,7 +129,7 @@ namespace ImageProcessor.Formats
/// </returns>
public virtual int Read(int bitCount)
{
Guard.LessEquals(this.Tell() + bitCount, this.BitsAllocated(), "bitCount");
Guard.MustBeLessThanOrEqualTo(this.Tell() + bitCount, this.BitsAllocated(), "bitCount");
return this.ReadBits(bitCount);
}
@ -150,7 +150,7 @@ namespace ImageProcessor.Formats
const int MaxBitsInStorage = sizeof(int) * BitsInByte;
Guard.LessEquals(bitCount, MaxBitsInStorage, "bitCount");
Guard.MustBeLessThanOrEqualTo(bitCount, MaxBitsInStorage, "bitCount");
for (int i = 0; i < bitCount; ++i)
{
@ -251,7 +251,7 @@ namespace ImageProcessor.Formats
// This stream is then divided into 8-bit bytes, high-order bit first.
// Thus, codes can straddle byte boundaries arbitrarily. After the EOD marker (code value 257),
// any leftover bits in the final byte are set to 0.
Guard.BetweenEquals(bitsCount, 0, 32, "bitsCount");
Guard.MustBeBetweenOrEqualTo(bitsCount, 0, 32, "bitsCount");
if (bitsCount == 0)
{
@ -338,7 +338,7 @@ namespace ImageProcessor.Formats
/// </param>
private void SeekSet(int position)
{
Guard.GreaterEquals(position, 0, "position");
Guard.MustBeGreaterThanOrEqualTo(position, 0, "position");
int byteDisplacement = position / BitsInByte;
this.stream.Seek(byteDisplacement, SeekOrigin.Begin);
@ -356,7 +356,7 @@ namespace ImageProcessor.Formats
private void SeekCurrent(int position)
{
int result = this.Tell() + position;
Guard.BetweenEquals(position, 0, this.BitsAllocated(), "position");
Guard.MustBeBetweenOrEqualTo(position, 0, this.BitsAllocated(), "position");
this.SeekSet(result);
}

4
src/ImageProcessor/ImageBase.cs

@ -40,8 +40,8 @@ namespace ImageProcessor
/// </exception>
protected ImageBase(int width, int height)
{
Guard.GreaterThan(width, 0, "width");
Guard.GreaterThan(height, 0, "height");
Guard.MustBeGreaterThan(width, 0, "width");
Guard.MustBeGreaterThan(height, 0, "height");
this.Width = width;
this.Height = height;

3
src/ImageProcessor/Properties/AssemblyInfo.cs

@ -28,3 +28,6 @@ using System.Runtime.InteropServices;
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.0.0.0")]
// Ensure the internals can be tested.
[assembly: InternalsVisibleTo("ImageProcessor.Tests")]

209
tests/ImageProcessor.Tests/Helpers/GuardTests.cs

@ -0,0 +1,209 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GuardTests.cs" company="James South">
// Copyright © James South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Tests the <see cref="Guard" /> helper.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Tests.Helpers
{
using System;
using System.Diagnostics.CodeAnalysis;
using Xunit;
/// <summary>
/// Tests the <see cref="Guard"/> helper.
/// </summary>
public class GuardTests
{
/// <summary>
/// Tests that the <see cref="M:Guard.NotNull"/> method throws when the argument is null.
/// </summary>
[Fact]
public void NotNullThrowsWhenArgIsNull()
{
Assert.Throws<ArgumentNullException>(() => Guard.NotNull(null, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.NotNull"/> method throws when the argument name is empty.
/// </summary>
[Fact]
public void NotNullThrowsWhenArgNameEmpty()
{
Assert.Throws<ArgumentNullException>(() => Guard.NotNull(null, string.Empty));
}
/// <summary>
/// Tests that the <see cref="M:Guard.NotEmpty"/> method throws when the argument is empty.
/// </summary>
[Fact]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1122:UseStringEmptyForEmptyStrings", Justification = "Reviewed. Suppression is OK here.")]
public void NotEmptyThrowsWhenEmpty()
{
Assert.Throws<ArgumentException>(() => Guard.NotNullOrEmpty("", string.Empty));
}
/// <summary>
/// Tests that the <see cref="M:Guard.NotEmpty"/> method throws when the argument is whitespace.
/// </summary>
[Fact]
public void NotEmptyThrowsWhenWhitespace()
{
Assert.Throws<ArgumentException>(() => Guard.NotNullOrEmpty(" ", string.Empty));
}
/// <summary>
/// Tests that the <see cref="M:Guard.NotEmpty"/> method throws when the argument name is null.
/// </summary>
[Fact]
public void NotEmptyThrowsWhenParameterNameNull()
{
Assert.Throws<ArgumentNullException>(() => Guard.NotNullOrEmpty(null, null));
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThan"/> method throws when the argument is greater.
/// </summary>
[Fact]
public void LessThanThrowsWhenArgIsGreater()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeLessThan(1, 0, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThan"/> method throws when the argument is equal.
/// </summary>
[Fact]
public void LessThanThrowsWhenArgIsEqual()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeLessThan(1, 1, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThanOrEqual"/> method throws when the argument is greater.
/// </summary>
[Fact]
public void LessThanOrEqualToThrowsWhenArgIsGreater()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeLessThanOrEqualTo(1, 0, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThanOrEqual"/> method does not throw when the argument
/// is less.
/// </summary>
[Fact]
public void LessThanOrEqualToDoesNotThrowWhenArgIsLess()
{
Exception ex = Record.Exception(() => Guard.MustBeLessThanOrEqualTo(0, 1, "foo"));
Assert.Null(ex);
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThanOrEqual"/> method does not throw when the argument
/// is equal.
/// </summary>
[Fact]
public void LessThanOrEqualToDoesNotThrowWhenArgIsEqual()
{
Exception ex = Record.Exception(() => Guard.MustBeLessThanOrEqualTo(1, 1, "foo"));
Assert.Equal(1, 1);
Assert.Null(ex);
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThan"/> method throws when the argument is greater.
/// </summary>
[Fact]
public void GreaterThanThrowsWhenArgIsLess()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeGreaterThan(0, 1, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThan"/> method throws when the argument is greater.
/// </summary>
[Fact]
public void GreaterThanThrowsWhenArgIsEqual()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeGreaterThan(1, 1, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThan"/> method throws when the argument name is greater.
/// </summary>
[Fact]
public void GreaterThanOrEqualToThrowsWhenArgIsLess()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeGreaterThanOrEqualTo(0, 1, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThanOrEqual"/> method does not throw when the argument
/// is less.
/// </summary>
[Fact]
public void GreaterThanOrEqualToDoesNotThrowWhenArgIsGreater()
{
Exception ex = Record.Exception(() => Guard.MustBeGreaterThanOrEqualTo(1, 0, "foo"));
Assert.Null(ex);
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeLessThanOrEqual"/> method does not throw when the argument
/// is equal.
/// </summary>
[Fact]
public void GreaterThanOrEqualToDoesNotThrowWhenArgIsEqual()
{
Exception ex = Record.Exception(() => Guard.MustBeGreaterThanOrEqualTo(1, 1, "foo"));
Assert.Equal(1, 1);
Assert.Null(ex);
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeBetweenOrEqualTo"/> method throws when the argument is less.
/// </summary>
[Fact]
public void BetweenOrEqualToThrowsWhenArgIsLess()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeBetweenOrEqualTo(-2, -1, 1, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeBetweenOrEqualTo"/> method throws when the argument is greater.
/// </summary>
[Fact]
public void BetweenOrEqualToThrowsWhenArgIsGreater()
{
Assert.Throws<ArgumentOutOfRangeException>(() => Guard.MustBeBetweenOrEqualTo(2, -1, 1, "foo"));
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeBetweenOrEqualTo"/> method does not throw when the argument
/// is equal.
/// </summary>
[Fact]
public void BetweenOrEqualToDoesNotThrowWhenArgIsEqual()
{
Exception ex = Record.Exception(() => Guard.MustBeBetweenOrEqualTo(1, 1, 1, "foo"));
Assert.Null(ex);
}
/// <summary>
/// Tests that the <see cref="M:Guard.MustBeBetweenOrEqualTo"/> method does not throw when the argument
/// is equal.
/// </summary>
[Fact]
public void BetweenOrEqualToDoesNotThrowWhenArgIsBetween()
{
Exception ex = Record.Exception(() => Guard.MustBeBetweenOrEqualTo(0, -1, 1, "foo"));
Assert.Null(ex);
}
}
}

3
tests/ImageProcessor.Tests/ImageProcessor.Tests.csproj

@ -54,6 +54,7 @@
<Compile Include="Colors\ColorConversionTests.cs" />
<Compile Include="Colors\ColorTests.cs" />
<Compile Include="Formats\EncoderDecoderTests.cs" />
<Compile Include="Helpers\GuardTests.cs" />
<Compile Include="Numerics\RectangleTests.cs" />
<Compile Include="Numerics\PointTests.cs" />
<Compile Include="Numerics\SizeTests.cs" />
@ -79,4 +80,4 @@
<Error Condition="!Exists('..\..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.core.2.0.0\build\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.props'))" />
<Error Condition="!Exists('..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\xunit.runner.visualstudio.2.0.0\build\net20\xunit.runner.visualstudio.props'))" />
</Target>
</Project>
</Project>
Loading…
Cancel
Save