Browse Source

Better configuration tests

pull/68/head
James Jackson-South 9 years ago
parent
commit
f9456c1797
  1. 285
      tests/ImageSharp.Tests/ConfigurationTests.cs

285
tests/ImageSharp.Tests/ConfigurationTests.cs

@ -7,145 +7,282 @@ namespace ImageSharp.Tests
{
using System;
using System.Collections.Generic;
using System.Linq;
using ImageSharp.Formats;
using Xunit;
using System.Linq;
/// <summary>
/// Tests the configuration class.
/// </summary>
public class ConfigurationTests
{
private class TestFormat : IImageFormat
/// <summary>
/// Test that the default configuration is not null.
/// </summary>
[Fact]
public void TestDefultConfigurationIsNotNull()
{
private IImageDecoder decoder;
private IImageEncoder encoder;
private string mimeType;
private string extension;
private IEnumerable<string> supportedExtensions;
public TestFormat()
{
this.decoder = new JpegDecoder();
this.encoder = new JpegEncoder();
this.extension = "jpg";
this.mimeType = "image/test";
this.supportedExtensions = new string[] { "jpg" };
}
public IImageDecoder Decoder { get { return this.decoder; } set { this.decoder = value; } }
Assert.True(Configuration.Default != null);
}
public IImageEncoder Encoder { get { return this.encoder; } set { this.encoder = value; } }
/// <summary>
/// Test that the default configuration parallel options is not null.
/// </summary>
[Fact]
public void TestDefultConfigurationParallelOptionsIsNotNull()
{
Assert.True(Configuration.Default.ParallelOptions != null);
}
public string MimeType { get { return this.mimeType; } set { this.mimeType = value; } }
/// <summary>
/// Test that the default configuration parallel options max degrees of parallelism matches the
/// environment processor count.
/// </summary>
[Fact]
public void TestDefultConfigurationMaxDegreeOfParallelism()
{
Assert.True(Configuration.Default.ParallelOptions.MaxDegreeOfParallelism == Environment.ProcessorCount);
}
public string Extension { get { return this.extension; } set { this.extension = value; } }
/// <summary>
/// Test that the default configuration parallel options is not null.
/// </summary>
[Fact]
public void TestDefultConfigurationImageFormatsIsNotNull()
{
Assert.True(Configuration.Default.ImageFormats != null);
}
public IEnumerable<string> SupportedExtensions { get { return this.supportedExtensions; } set { this.supportedExtensions = value; } }
/// <summary>
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
/// when the format is null.
/// </summary>
[Fact]
public void TestAddImageFormatThrowsWithNullFormat()
{
Assert.Throws<ArgumentNullException>(() =>
{
Configuration.Default.AddImageFormat(null);
});
}
public int HeaderSize { get { throw new NotImplementedException(); } }
/// <summary>
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
/// when the encoder is null.
/// </summary>
[Fact]
public void TestAddImageFormatThrowsWithNullEncoder()
{
TestFormat format = new TestFormat { Encoder = null };
public bool IsSupportedFileFormat(byte[] header) { throw new NotImplementedException(); }
Assert.Throws<ArgumentNullException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
}
/// <summary>
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
/// when the decoder is null.
/// </summary>
[Fact]
public void AddImageFormatGuardNull()
public void TestAddImageFormatThrowsWithNullDecoder()
{
ArgumentException exception;
TestFormat format = new TestFormat { Decoder = null };
exception = Assert.Throws<ArgumentNullException>(() =>
Assert.Throws<ArgumentNullException>(() =>
{
Configuration.Default.AddImageFormat(null);
Configuration.Default.AddImageFormat(format);
});
}
var format = new TestFormat();
format.Decoder = null;
/// <summary>
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
/// when the mime type is null or an empty string.
/// </summary>
[Fact]
public void TestAddImageFormatThrowsWithNullOrEmptyMimeType()
{
TestFormat format = new TestFormat { MimeType = null };
exception = Assert.Throws<ArgumentNullException>(() =>
Assert.Throws<ArgumentNullException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("decoder", exception.Message);
format = new TestFormat();
format.Encoder = null;
format = new TestFormat { MimeType = string.Empty };
exception = Assert.Throws<ArgumentNullException>(() =>
Assert.Throws<ArgumentException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("encoder", exception.Message);
}
format = new TestFormat();
format.MimeType = null;
/// <summary>
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
/// when the extension is null or an empty string.
/// </summary>
[Fact]
public void TestAddImageFormatThrowsWithNullOrEmptyExtension()
{
TestFormat format = new TestFormat { Extension = null };
exception = Assert.Throws<ArgumentNullException>(() =>
Assert.Throws<ArgumentNullException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("mime type", exception.Message);
format = new TestFormat();
format.MimeType = "";
format = new TestFormat { Extension = string.Empty };
exception = Assert.Throws<ArgumentException>(() =>
Assert.Throws<ArgumentException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("mime type", exception.Message);
}
format = new TestFormat();
format.Extension = null;
/// <summary>
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
/// when the supported extensions list is null or empty.
/// </summary>
[Fact]
public void TestAddImageFormatThrowsWenSupportedExtensionsIsNullOrEmpty()
{
TestFormat format = new TestFormat { SupportedExtensions = null };
exception = Assert.Throws<ArgumentNullException>(() =>
Assert.Throws<ArgumentNullException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("extension", exception.Message);
format = new TestFormat();
format.Extension = "";
format = new TestFormat { SupportedExtensions = Enumerable.Empty<string>() };
exception = Assert.Throws<ArgumentException>(() =>
Assert.Throws<ArgumentException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("extension", exception.Message);
}
format = new TestFormat();
format.SupportedExtensions = null;
/// <summary>
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
/// when the supported extensions list does not contain the default extension.
/// </summary>
[Fact]
public void TestAddImageFormatThrowsWithoutDefaultExtension()
{
TestFormat format = new TestFormat { Extension = "test" };
exception = Assert.Throws<ArgumentNullException>(() =>
Assert.Throws<ArgumentException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("supported extensions", exception.Message);
}
format = new TestFormat();
format.SupportedExtensions = Enumerable.Empty<string>();
/// <summary>
/// Tests the <see cref="M:Configuration.AddImageFormat"/> method throws an exception
/// when the supported extensions list contains an empty string.
/// </summary>
[Fact]
public void TestAddImageFormatThrowsWithEmptySupportedExtension()
{
TestFormat format = new TestFormat
{
Extension = "test",
SupportedExtensions = new[] { "test", string.Empty }
};
exception = Assert.Throws<ArgumentException>(() =>
Assert.Throws<ArgumentException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("supported extensions", exception.Message);
}
/// <summary>
/// Test that the <see cref="M:Configuration.AddImageFormat"/> method ignores adding duplicate image formats.
/// </summary>
[Fact]
public void TestConfigurationIgnoresDuplicateImageFormats()
{
Configuration.Default.AddImageFormat(new PngFormat());
Configuration.Default.AddImageFormat(new PngFormat());
Assert.True(Configuration.Default.ImageFormats.Count(i => i.GetType() == typeof(PngFormat)) == 1);
}
/// <summary>
/// Test that the default image constructors use default configuration.
/// </summary>
[Fact]
public void AddImageFormatChecks()
public void TestImageUsesDefaultConfiguration()
{
TestFormat format = new TestFormat();
Configuration.Default.AddImageFormat(new PngFormat());
Image image = new Image(1, 1);
Assert.Equal(image.Configuration.ParallelOptions, Configuration.Default.ParallelOptions);
Assert.Equal(image.Configuration.ImageFormats, Configuration.Default.ImageFormats);
}
format.Extension = "test";
var exception = Assert.Throws<ArgumentException>(() =>
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("should contain", exception.Message);
/// <summary>
/// Test that the default image constructor copies the configuration.
/// </summary>
[Fact]
public void TestImageCopiesConfiguration()
{
Configuration.Default.AddImageFormat(new PngFormat());
format.SupportedExtensions = new string[] { "test", "" };
exception = Assert.Throws<ArgumentException>(() =>
Image image = new Image(1, 1);
Image image2 = new Image(image);
Assert.Equal(image2.Configuration.ParallelOptions, image.Configuration.ParallelOptions);
Assert.True(image2.Configuration.ImageFormats.SequenceEqual(image.Configuration.ImageFormats));
}
/// <summary>
/// A test image format for testing the configuration.
/// </summary>
private class TestFormat : IImageFormat
{
/// <summary>
/// Initializes a new instance of the <see cref="TestFormat"/> class.
/// </summary>
public TestFormat()
{
Configuration.Default.AddImageFormat(format);
});
Assert.Contains("empty values", exception.Message);
this.Decoder = new JpegDecoder();
this.Encoder = new JpegEncoder();
this.Extension = "jpg";
this.MimeType = "image/test";
this.SupportedExtensions = new[] { "jpg" };
}
/// <inheritdoc />
public IImageDecoder Decoder { get; set; }
/// <inheritdoc />
public IImageEncoder Encoder { get; set; }
/// <inheritdoc />
public string MimeType { get; set; }
/// <inheritdoc />
public string Extension { get; set; }
/// <inheritdoc />
public IEnumerable<string> SupportedExtensions { get; set; }
/// <inheritdoc />
public int HeaderSize
{
get
{
throw new NotImplementedException();
}
}
/// <inheritdoc />
public bool IsSupportedFileFormat(byte[] header)
{
throw new NotImplementedException();
}
}
}
}
}
Loading…
Cancel
Save