From f9456c17974368bca8bb00c48bf3aa61a994bc26 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 1 Jan 2017 13:13:53 +1100 Subject: [PATCH] Better configuration tests --- tests/ImageSharp.Tests/ConfigurationTests.cs | 285 ++++++++++++++----- 1 file changed, 211 insertions(+), 74 deletions(-) diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs index 507b835fba..74a0773419 100644 --- a/tests/ImageSharp.Tests/ConfigurationTests.cs +++ b/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; + /// + /// Tests the configuration class. + /// public class ConfigurationTests { - private class TestFormat : IImageFormat + /// + /// Test that the default configuration is not null. + /// + [Fact] + public void TestDefultConfigurationIsNotNull() { - private IImageDecoder decoder; - private IImageEncoder encoder; - private string mimeType; - private string extension; - private IEnumerable 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; } } + /// + /// Test that the default configuration parallel options is not null. + /// + [Fact] + public void TestDefultConfigurationParallelOptionsIsNotNull() + { + Assert.True(Configuration.Default.ParallelOptions != null); + } - public string MimeType { get { return this.mimeType; } set { this.mimeType = value; } } + /// + /// Test that the default configuration parallel options max degrees of parallelism matches the + /// environment processor count. + /// + [Fact] + public void TestDefultConfigurationMaxDegreeOfParallelism() + { + Assert.True(Configuration.Default.ParallelOptions.MaxDegreeOfParallelism == Environment.ProcessorCount); + } - public string Extension { get { return this.extension; } set { this.extension = value; } } + /// + /// Test that the default configuration parallel options is not null. + /// + [Fact] + public void TestDefultConfigurationImageFormatsIsNotNull() + { + Assert.True(Configuration.Default.ImageFormats != null); + } - public IEnumerable SupportedExtensions { get { return this.supportedExtensions; } set { this.supportedExtensions = value; } } + /// + /// Tests the method throws an exception + /// when the format is null. + /// + [Fact] + public void TestAddImageFormatThrowsWithNullFormat() + { + Assert.Throws(() => + { + Configuration.Default.AddImageFormat(null); + }); + } - public int HeaderSize { get { throw new NotImplementedException(); } } + /// + /// Tests the method throws an exception + /// when the encoder is null. + /// + [Fact] + public void TestAddImageFormatThrowsWithNullEncoder() + { + TestFormat format = new TestFormat { Encoder = null }; - public bool IsSupportedFileFormat(byte[] header) { throw new NotImplementedException(); } + Assert.Throws(() => + { + Configuration.Default.AddImageFormat(format); + }); } + /// + /// Tests the method throws an exception + /// when the decoder is null. + /// [Fact] - public void AddImageFormatGuardNull() + public void TestAddImageFormatThrowsWithNullDecoder() { - ArgumentException exception; + TestFormat format = new TestFormat { Decoder = null }; - exception = Assert.Throws(() => + Assert.Throws(() => { - Configuration.Default.AddImageFormat(null); + Configuration.Default.AddImageFormat(format); }); + } - var format = new TestFormat(); - format.Decoder = null; + /// + /// Tests the method throws an exception + /// when the mime type is null or an empty string. + /// + [Fact] + public void TestAddImageFormatThrowsWithNullOrEmptyMimeType() + { + TestFormat format = new TestFormat { MimeType = null }; - exception = Assert.Throws(() => + Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); - Assert.Contains("decoder", exception.Message); - format = new TestFormat(); - format.Encoder = null; + format = new TestFormat { MimeType = string.Empty }; - exception = Assert.Throws(() => + Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); - Assert.Contains("encoder", exception.Message); + } - format = new TestFormat(); - format.MimeType = null; + /// + /// Tests the method throws an exception + /// when the extension is null or an empty string. + /// + [Fact] + public void TestAddImageFormatThrowsWithNullOrEmptyExtension() + { + TestFormat format = new TestFormat { Extension = null }; - exception = Assert.Throws(() => + Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); - Assert.Contains("mime type", exception.Message); - format = new TestFormat(); - format.MimeType = ""; + format = new TestFormat { Extension = string.Empty }; - exception = Assert.Throws(() => + Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); - Assert.Contains("mime type", exception.Message); + } - format = new TestFormat(); - format.Extension = null; + /// + /// Tests the method throws an exception + /// when the supported extensions list is null or empty. + /// + [Fact] + public void TestAddImageFormatThrowsWenSupportedExtensionsIsNullOrEmpty() + { + TestFormat format = new TestFormat { SupportedExtensions = null }; - exception = Assert.Throws(() => + Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); - Assert.Contains("extension", exception.Message); - format = new TestFormat(); - format.Extension = ""; + format = new TestFormat { SupportedExtensions = Enumerable.Empty() }; - exception = Assert.Throws(() => + Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); - Assert.Contains("extension", exception.Message); + } - format = new TestFormat(); - format.SupportedExtensions = null; + /// + /// Tests the method throws an exception + /// when the supported extensions list does not contain the default extension. + /// + [Fact] + public void TestAddImageFormatThrowsWithoutDefaultExtension() + { + TestFormat format = new TestFormat { Extension = "test" }; - exception = Assert.Throws(() => + Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); - Assert.Contains("supported extensions", exception.Message); + } - format = new TestFormat(); - format.SupportedExtensions = Enumerable.Empty(); + /// + /// Tests the method throws an exception + /// when the supported extensions list contains an empty string. + /// + [Fact] + public void TestAddImageFormatThrowsWithEmptySupportedExtension() + { + TestFormat format = new TestFormat + { + Extension = "test", + SupportedExtensions = new[] { "test", string.Empty } + }; - exception = Assert.Throws(() => + Assert.Throws(() => { Configuration.Default.AddImageFormat(format); }); - Assert.Contains("supported extensions", exception.Message); } + /// + /// Test that the method ignores adding duplicate image formats. + /// + [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); + } + + /// + /// Test that the default image constructors use default configuration. + /// [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(() => - { - Configuration.Default.AddImageFormat(format); - }); - Assert.Contains("should contain", exception.Message); + /// + /// Test that the default image constructor copies the configuration. + /// + [Fact] + public void TestImageCopiesConfiguration() + { + Configuration.Default.AddImageFormat(new PngFormat()); - format.SupportedExtensions = new string[] { "test", "" }; - exception = Assert.Throws(() => + 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)); + } + + /// + /// A test image format for testing the configuration. + /// + private class TestFormat : IImageFormat + { + /// + /// Initializes a new instance of the class. + /// + 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" }; + } + + /// + public IImageDecoder Decoder { get; set; } + + /// + public IImageEncoder Encoder { get; set; } + + /// + public string MimeType { get; set; } + + /// + public string Extension { get; set; } + + /// + public IEnumerable SupportedExtensions { get; set; } + + /// + public int HeaderSize + { + get + { + throw new NotImplementedException(); + } + } + + /// + public bool IsSupportedFileFormat(byte[] header) + { + throw new NotImplementedException(); + } } } -} +} \ No newline at end of file