From 7139a5284824cf8e9cf129f454edfa89ee2f142f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 14 Jan 2023 17:42:23 +1000 Subject: [PATCH] Refactor Identify tests --- src/ImageSharp/Image.FromBytes.cs | 36 +++------ src/ImageSharp/Image.FromFile.cs | 55 ++++++------- src/ImageSharp/Image.FromStream.cs | 71 +++++++---------- .../Formats/Bmp/BmpDecoderTests.cs | 4 +- .../Formats/Bmp/BmpMetadataTests.cs | 2 +- .../Formats/GeneralFormatTests.cs | 4 +- .../Formats/Gif/GifDecoderTests.cs | 2 +- .../Formats/Pbm/PbmMetadataTests.cs | 6 +- .../Formats/Png/PngDecoderTests.cs | 4 +- .../Formats/Png/PngMetadataTests.cs | 6 +- .../Formats/Tga/TgaFileHeaderTests.cs | 2 +- .../Formats/Tiff/BigTiffDecoderTests.cs | 4 +- .../Formats/Tiff/TiffDecoderTests.cs | 4 +- .../Formats/Tiff/TiffMetadataTests.cs | 4 +- .../Formats/WebP/WebpDecoderTests.cs | 2 +- .../Image/ImageTests.Decode_Cancellation.cs | 2 +- .../Image/ImageTests.Identify.cs | 79 ++++++++----------- 17 files changed, 126 insertions(+), 161 deletions(-) diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs index 0c209214d..414ed05c2 100644 --- a/src/ImageSharp/Image.FromBytes.cs +++ b/src/ImageSharp/Image.FromBytes.cs @@ -55,41 +55,31 @@ public abstract partial class Image /// /// Reads the raw image information from the specified stream without fully decoding it. - /// A return value indicates whether the operation succeeded. /// /// The byte array containing encoded image data to read the header from. - /// - /// When this method returns, contains the raw image information; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// - /// if the information can be read; otherwise, - /// The data is null. - /// The data is not readable. - public static bool TryIdentify(ReadOnlySpan buffer, [NotNullWhen(true)] out ImageInfo? info) - => TryIdentify(DecoderOptions.Default, buffer, out info); + /// The . + /// The image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static ImageInfo Identify(ReadOnlySpan buffer) + => Identify(DecoderOptions.Default, buffer); /// /// Reads the raw image information from the specified span of bytes without fully decoding it. - /// A return value indicates whether the operation succeeded. /// /// The general decoder options. /// The byte span containing encoded image data to read the header from. - /// - /// When this method returns, contains the raw image information; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// - /// if the information can be read; otherwise, - /// The configuration is null. - /// The data is null. - /// The data is not readable. - public static unsafe bool TryIdentify(DecoderOptions options, ReadOnlySpan buffer, [NotNullWhen(true)] out ImageInfo? info) + /// The . + /// The options are null. + /// The image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static unsafe ImageInfo Identify(DecoderOptions options, ReadOnlySpan buffer) { fixed (byte* ptr = buffer) { using UnmanagedMemoryStream stream = new(ptr, buffer.Length); - return TryIdentify(options, stream, out info); + return Identify(options, stream); } } diff --git a/src/ImageSharp/Image.FromFile.cs b/src/ImageSharp/Image.FromFile.cs index a8ed73c82..4bd36397f 100644 --- a/src/ImageSharp/Image.FromFile.cs +++ b/src/ImageSharp/Image.FromFile.cs @@ -84,70 +84,71 @@ public abstract partial class Image /// A return value indicates whether the operation succeeded. /// /// The image file to open and to read the header from. - /// - /// When this method returns, contains the raw image information; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// /// if the information can be read; otherwise, - public static bool TryIdentify(string path, [NotNullWhen(true)] out ImageInfo? info) - => TryIdentify(DecoderOptions.Default, path, out info); + /// The path is null. + /// The file stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static ImageInfo Identify(string path) + => Identify(DecoderOptions.Default, path); /// /// Reads the raw image information from the specified file path without fully decoding it. - /// A return value indicates whether the operation succeeded. /// /// The general decoder options. /// The image file to open and to read the header from. - /// - /// When this method returns, contains the raw image information; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// - /// if the information can be read; otherwise, + /// The . /// The options are null. - public static bool TryIdentify(DecoderOptions options, string path, [NotNullWhen(true)] out ImageInfo? info) + /// The path is null. + /// The file stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static ImageInfo Identify(DecoderOptions options, string path) { Guard.NotNull(options, nameof(options)); using Stream stream = options.Configuration.FileSystem.OpenRead(path); - return TryIdentify(options, stream, out info); + return Identify(options, stream); } /// /// Reads the raw image information from the specified stream without fully decoding it. - /// A return value indicates whether the operation succeeded. /// /// The image file to open and to read the header from. /// The token to monitor for cancellation requests. /// The options are null. /// - /// The representing the asynchronous operation. + /// The representing the asynchronous operation. /// - public static Task> TryIdentifyAsync( - string path, - CancellationToken cancellationToken = default) - => TryIdentifyAsync(DecoderOptions.Default, path, cancellationToken); + /// The path is null. + /// The file stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Task IdentifyAsync(string path, CancellationToken cancellationToken = default) + => IdentifyAsync(DecoderOptions.Default, path, cancellationToken); /// /// Reads the raw image information from the specified stream without fully decoding it. - /// A return value indicates whether the operation succeeded. /// /// The general decoder options. /// The image file to open and to read the header from. /// The token to monitor for cancellation requests. - /// The options are null. /// - /// The representing the asynchronous operation. + /// The representing the asynchronous operation. /// - public static async Task> TryIdentifyAsync( + /// The options are null. + /// The path is null. + /// The file stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static async Task IdentifyAsync( DecoderOptions options, string path, CancellationToken cancellationToken = default) { Guard.NotNull(options, nameof(options)); using Stream stream = options.Configuration.FileSystem.OpenRead(path); - return await TryIdentifyAsync(options, stream, cancellationToken).ConfigureAwait(false); + return await IdentifyAsync(options, stream, cancellationToken).ConfigureAwait(false); } /// diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index 9da77d66e..b48055ea7 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -91,87 +91,70 @@ public abstract partial class Image /// /// Reads the raw image information from the specified stream without fully decoding it. - /// A return value indicates whether the operation succeeded. /// /// The image stream to read the header from. - /// - /// When this method returns, contains the raw image information; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// /// if the information can be read; otherwise, /// The stream is null. - /// The stream is not readable. - /// Image contains invalid content. - public static bool TryIdentify(Stream stream, [NotNullWhen(true)] out ImageInfo? info) - => TryIdentify(DecoderOptions.Default, stream, out info); + /// The stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static ImageInfo Identify(Stream stream) + => Identify(DecoderOptions.Default, stream); /// /// Reads the raw image information from the specified stream without fully decoding it. - /// A return value indicates whether the operation succeeded. /// /// The general decoder options. /// The image stream to read the information from. - /// - /// When this method returns, contains the raw image information; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// - /// if the information can be read; otherwise, + /// The . /// The options are null. /// The stream is null. - /// The stream is not readable. - /// Image contains invalid content. - public static bool TryIdentify(DecoderOptions options, Stream stream, [NotNullWhen(true)] out ImageInfo? info) - { - info = WithSeekableStream(options, stream, s => InternalIdentify(options, s)); - return info is not null; - } + /// The stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static ImageInfo Identify(DecoderOptions options, Stream stream) + => WithSeekableStream(options, stream, s => InternalIdentify(options, s)); /// /// Reads the raw image information from the specified stream without fully decoding it. - /// A return value indicates whether the operation succeeded. /// /// The image stream to read the information from. /// The token to monitor for cancellation requests. - /// The stream is null. - /// The stream is not readable. - /// Image contains invalid content. /// - /// The representing the asynchronous operation. + /// The representing the asynchronous operation. /// - public static Task> TryIdentifyAsync( + /// The stream is null. + /// The stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Task IdentifyAsync( Stream stream, CancellationToken cancellationToken = default) - => TryIdentifyAsync(DecoderOptions.Default, stream, cancellationToken); + => IdentifyAsync(DecoderOptions.Default, stream, cancellationToken); /// /// Reads the raw image information from the specified stream without fully decoding it. - /// A return value indicates whether the operation succeeded. /// /// The general decoder options. /// The image stream to read the information from. /// The token to monitor for cancellation requests. - /// The options are null. - /// The stream is null. - /// The stream is not readable. - /// Image contains invalid content. /// - /// The representing the asynchronous operation. + /// The representing the asynchronous operation. /// - public static async Task> TryIdentifyAsync( + /// The options are null. + /// The stream is null. + /// The stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Task IdentifyAsync( DecoderOptions options, Stream stream, CancellationToken cancellationToken = default) - { - ImageInfo? info = await WithSeekableStreamAsync( + => WithSeekableStreamAsync( options, stream, (s, ct) => InternalIdentifyAsync(options, s, ct), - cancellationToken).ConfigureAwait(false); - - return new() { Value = info }; - } + cancellationToken); /// /// Creates a new instance of the class from the given stream. diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index 035481e01..e76f21d0e 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -475,7 +475,7 @@ public class BmpDecoderTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.Equal(expectedPixelSize, imageInfo.PixelType?.BitsPerPixel); } @@ -493,7 +493,7 @@ public class BmpDecoderTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.Equal(expectedWidth, imageInfo.Width); Assert.Equal(expectedHeight, imageInfo.Height); diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpMetadataTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpMetadataTests.cs index 1aef3e748..1d8435671 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpMetadataTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpMetadataTests.cs @@ -38,7 +38,7 @@ public class BmpMetadataTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); BmpMetadata bitmapMetadata = imageInfo.Metadata.GetBmpMetadata(); Assert.NotNull(bitmapMetadata); diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 303703f74..1d84d6600 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -256,7 +256,7 @@ public class GeneralFormatTests image.Save(memoryStream, format); memoryStream.Position = 0; - Image.TryIdentify(memoryStream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(memoryStream); Assert.Equal(imageInfo.Width, width); Assert.Equal(imageInfo.Height, height); @@ -270,7 +270,7 @@ public class GeneralFormatTests using MemoryStream memoryStream = new(invalid); - Assert.Throws(() => Image.TryIdentify(invalid, out ImageInfo imageInfo)); + Assert.Throws(() => Image.Identify(invalid)); } private static IImageFormat GetFormat(string format) diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index 589baa613..022451668 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -123,7 +123,7 @@ public class GifDecoderTests TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.Equal(expectedPixelSize, imageInfo.PixelType.BitsPerPixel); diff --git a/tests/ImageSharp.Tests/Formats/Pbm/PbmMetadataTests.cs b/tests/ImageSharp.Tests/Formats/Pbm/PbmMetadataTests.cs index b4c39293d..499607772 100644 --- a/tests/ImageSharp.Tests/Formats/Pbm/PbmMetadataTests.cs +++ b/tests/ImageSharp.Tests/Formats/Pbm/PbmMetadataTests.cs @@ -36,7 +36,7 @@ public class PbmMetadataTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); PbmMetadata bitmapMetadata = imageInfo.Metadata.GetPbmMetadata(); Assert.NotNull(bitmapMetadata); @@ -55,7 +55,7 @@ public class PbmMetadataTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); PbmMetadata bitmapMetadata = imageInfo.Metadata.GetPbmMetadata(); Assert.NotNull(bitmapMetadata); @@ -74,7 +74,7 @@ public class PbmMetadataTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); PbmMetadata bitmapMetadata = imageInfo.Metadata.GetPbmMetadata(); Assert.NotNull(bitmapMetadata); diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 85849cdff..7e6f5138a 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -307,7 +307,7 @@ public partial class PngDecoderTests TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.Equal(expectedPixelSize, imageInfo.PixelType.BitsPerPixel); @@ -508,7 +508,7 @@ public partial class PngDecoderTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); PngMetadata metadata = imageInfo.Metadata.GetPngMetadata(); Assert.True(metadata.HasTransparency); } diff --git a/tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs index 351b44ee0..9653a0656 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngMetadataTests.cs @@ -232,7 +232,7 @@ public class PngMetadataTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); PngMetadata meta = imageInfo.Metadata.GetFormatMetadata(PngFormat.Instance); VerifyTextDataIsPresent(meta); @@ -244,7 +244,7 @@ public class PngMetadataTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.NotNull(imageInfo.Metadata.ExifProfile); ExifProfile exif = imageInfo.Metadata.ExifProfile; @@ -281,7 +281,7 @@ public class PngMetadataTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.NotNull(imageInfo.Metadata.ExifProfile); diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs index f1efe47f9..bf24ba350 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs @@ -41,7 +41,7 @@ public class TgaFileHeaderTests { using MemoryStream stream = new(data); - Image.TryIdentify(stream, out ImageInfo info); + ImageInfo info = Image.Identify(stream); TgaMetadata tgaData = info.Metadata.GetTgaMetadata(); Assert.Equal(bitsPerPixel, tgaData.BitsPerPixel); Assert.Equal(width, info.Width); diff --git a/tests/ImageSharp.Tests/Formats/Tiff/BigTiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/BigTiffDecoderTests.cs index de7e74720..5c110844c 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/BigTiffDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/BigTiffDecoderTests.cs @@ -62,7 +62,7 @@ public class BigTiffDecoderTests : TiffDecoderBaseTester { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo info); + ImageInfo info = Image.Identify(stream); Assert.Equal(expectedPixelSize, info.PixelType?.BitsPerPixel); Assert.Equal(expectedWidth, info.Width); @@ -84,7 +84,7 @@ public class BigTiffDecoderTests : TiffDecoderBaseTester { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo info); + ImageInfo info = Image.Identify(stream); Assert.NotNull(info.Metadata); Assert.Equal(expectedByteOrder, info.Metadata.GetTiffMetadata().ByteOrder); diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs index 26f006b34..8c43fd81d 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs @@ -32,7 +32,7 @@ public class TiffDecoderTests : TiffDecoderBaseTester { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo info); + ImageInfo info = Image.Identify(stream); Assert.Equal(expectedPixelSize, info.PixelType?.BitsPerPixel); Assert.Equal(expectedWidth, info.Width); @@ -50,7 +50,7 @@ public class TiffDecoderTests : TiffDecoderBaseTester { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo info); + ImageInfo info = Image.Identify(stream); Assert.NotNull(info.Metadata); Assert.Equal(expectedByteOrder, info.Metadata.GetTiffMetadata().ByteOrder); diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs index 4d5f66503..2a809e895 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffMetadataTests.cs @@ -80,7 +80,7 @@ public class TiffMetadataTests TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); TiffMetadata tiffMetadata = imageInfo.Metadata.GetTiffMetadata(); @@ -96,7 +96,7 @@ public class TiffMetadataTests TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); TiffMetadata tiffMetadata = imageInfo.Metadata.GetTiffMetadata(); diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs index 4d5d2aefd..003e5ad4c 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs @@ -41,7 +41,7 @@ public class WebpDecoderTests { TestFile testFile = TestFile.Create(imagePath); using MemoryStream stream = new(testFile.Bytes, false); - Image.TryIdentify(stream, out ImageInfo imageInfo); + ImageInfo imageInfo = Image.Identify(stream); Assert.NotNull(imageInfo); Assert.Equal(expectedWidth, imageInfo.Width); Assert.Equal(expectedHeight, imageInfo.Height); diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs b/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs index d4a77d2c1..d8d9f4fe2 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs @@ -34,7 +34,7 @@ public partial class ImageTests { using FileStream fs = File.OpenRead(TestFile.GetInputFileFullPath(file)); CancellationToken preCancelled = new(canceled: true); - await Assert.ThrowsAnyAsync(async () => await Image.TryIdentifyAsync(fs, preCancelled)); + await Assert.ThrowsAnyAsync(async () => await Image.IdentifyAsync(fs, preCancelled)); } private static TheoryData CreateLoadData() diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs b/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs index 9d0aa7a75..0cda1c1fb 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs @@ -33,7 +33,7 @@ public partial class ImageTests [Fact] public void FromBytes_GlobalConfiguration() { - Image.TryIdentify(ActualImageBytes, out ImageInfo info); + ImageInfo info = Image.Identify(ActualImageBytes); Assert.Equal(ExpectedImageSize, info.Size()); Assert.Equal(ExpectedGlobalFormat, info.Metadata.DecodedImageFormat); } @@ -42,7 +42,7 @@ public partial class ImageTests public void FromBytes_CustomConfiguration() { DecoderOptions options = new() { Configuration = this.LocalConfiguration }; - Image.TryIdentify(options, this.ByteArray, out ImageInfo info); + ImageInfo info = Image.Identify(options, this.ByteArray); Assert.Equal(this.LocalImageInfo, info); } @@ -50,7 +50,7 @@ public partial class ImageTests [Fact] public void FromFileSystemPath_GlobalConfiguration() { - Image.TryIdentify(ActualImagePath, out ImageInfo info); + ImageInfo info = Image.Identify(ActualImagePath); Assert.NotNull(info); Assert.Equal(ExpectedGlobalFormat, info.Metadata.DecodedImageFormat); @@ -61,7 +61,7 @@ public partial class ImageTests { DecoderOptions options = new() { Configuration = this.LocalConfiguration }; - Image.TryIdentify(options, this.MockFilePath, out ImageInfo info); + ImageInfo info = Image.Identify(options, this.MockFilePath); Assert.Equal(this.LocalImageInfo, info); } @@ -70,7 +70,7 @@ public partial class ImageTests public void FromStream_GlobalConfiguration() { using MemoryStream stream = new(ActualImageBytes); - Image.TryIdentify(stream, out ImageInfo info); + ImageInfo info = Image.Identify(stream); Assert.NotNull(info); Assert.Equal(ExpectedGlobalFormat, info.Metadata.DecodedImageFormat); @@ -80,7 +80,7 @@ public partial class ImageTests public void FromStream_GlobalConfiguration_NoFormat() { using MemoryStream stream = new(ActualImageBytes); - Image.TryIdentify(stream, out ImageInfo info); + ImageInfo info = Image.Identify(stream); Assert.NotNull(info); } @@ -91,7 +91,7 @@ public partial class ImageTests using MemoryStream stream = new(ActualImageBytes); using NonSeekableStream nonSeekableStream = new(stream); - Image.TryIdentify(nonSeekableStream, out ImageInfo info); + ImageInfo info = Image.Identify(nonSeekableStream); Assert.NotNull(info); Assert.Equal(ExpectedGlobalFormat, info.Metadata.DecodedImageFormat); @@ -103,7 +103,7 @@ public partial class ImageTests using MemoryStream stream = new(ActualImageBytes); using NonSeekableStream nonSeekableStream = new(stream); - Image.TryIdentify(nonSeekableStream, out ImageInfo info); + ImageInfo info = Image.Identify(nonSeekableStream); Assert.NotNull(info); } @@ -113,7 +113,7 @@ public partial class ImageTests { DecoderOptions options = new() { Configuration = this.LocalConfiguration }; - Image.TryIdentify(options, this.DataStream, out ImageInfo info); + ImageInfo info = Image.Identify(options, this.DataStream); Assert.Equal(this.LocalImageInfo, info); } @@ -123,7 +123,7 @@ public partial class ImageTests { DecoderOptions options = new() { Configuration = this.LocalConfiguration }; - Image.TryIdentify(options, this.DataStream, out ImageInfo info); + ImageInfo info = Image.Identify(options, this.DataStream); Assert.Equal(this.LocalImageInfo, info); } @@ -133,7 +133,7 @@ public partial class ImageTests { DecoderOptions options = new() { Configuration = new() }; - Assert.Throws(() => Image.TryIdentify(options, this.DataStream, out ImageInfo info)); + Assert.Throws(() => Image.Identify(options, this.DataStream)); } [Fact] @@ -156,7 +156,7 @@ public partial class ImageTests })); using Stream stream = zipFile.Entries[0].Open(); - Assert.Throws(() => Image.TryIdentify(stream, out ImageInfo info)); + Assert.Throws(() => Image.Identify(stream)); } [Fact] @@ -165,10 +165,8 @@ public partial class ImageTests using MemoryStream stream = new(ActualImageBytes); AsyncStreamWrapper asyncStream = new(stream, () => false); - Attempt attempt = await Image.TryIdentifyAsync(asyncStream); - - Assert.True(attempt.Success); - Assert.NotNull(attempt.Value); + ImageInfo info = await Image.IdentifyAsync(asyncStream); + Assert.NotNull(info); } [Fact] @@ -176,11 +174,10 @@ public partial class ImageTests { using MemoryStream stream = new(ActualImageBytes); AsyncStreamWrapper asyncStream = new(stream, () => false); - Attempt attempt = await Image.TryIdentifyAsync(asyncStream); + ImageInfo info = await Image.IdentifyAsync(asyncStream); - Assert.True(attempt.Success); - Assert.Equal(ExpectedImageSize, attempt.Value.Size()); - Assert.Equal(ExpectedGlobalFormat, attempt.Value.Metadata.DecodedImageFormat); + Assert.Equal(ExpectedImageSize, info.Size()); + Assert.Equal(ExpectedGlobalFormat, info.Metadata.DecodedImageFormat); } [Fact] @@ -190,10 +187,9 @@ public partial class ImageTests using NonSeekableStream nonSeekableStream = new(stream); AsyncStreamWrapper asyncStream = new(nonSeekableStream, () => false); - Attempt attempt = await Image.TryIdentifyAsync(asyncStream); + ImageInfo info = await Image.IdentifyAsync(asyncStream); - Assert.True(attempt.Success); - Assert.NotNull(attempt.Value); + Assert.NotNull(info); } [Fact] @@ -203,11 +199,10 @@ public partial class ImageTests using NonSeekableStream nonSeekableStream = new(stream); AsyncStreamWrapper asyncStream = new(nonSeekableStream, () => false); - Attempt attempt = await Image.TryIdentifyAsync(asyncStream); + ImageInfo info = await Image.IdentifyAsync(asyncStream); - Assert.True(attempt.Success); - Assert.Equal(ExpectedImageSize, attempt.Value.Size()); - Assert.Equal(ExpectedGlobalFormat, attempt.Value.Metadata.DecodedImageFormat); + Assert.Equal(ExpectedImageSize, info.Size()); + Assert.Equal(ExpectedGlobalFormat, info.Metadata.DecodedImageFormat); } [Fact] @@ -230,7 +225,7 @@ public partial class ImageTests })); using Stream stream = zipFile.Entries[0].Open(); - await Assert.ThrowsAsync(async () => await Image.TryIdentifyAsync(stream)); + await Assert.ThrowsAsync(async () => await Image.IdentifyAsync(stream)); } [Fact] @@ -238,10 +233,9 @@ public partial class ImageTests { DecoderOptions options = new() { Configuration = this.LocalConfiguration }; - Attempt attempt = await Image.TryIdentifyAsync(options, this.MockFilePath); + ImageInfo info = await Image.IdentifyAsync(options, this.MockFilePath); - Assert.True(attempt.Success); - Assert.Equal(this.LocalImageInfo, attempt.Value); + Assert.Equal(this.LocalImageInfo, info); } [Fact] @@ -249,28 +243,26 @@ public partial class ImageTests { DecoderOptions options = new() { Configuration = this.LocalConfiguration }; - Attempt attempt = await Image.TryIdentifyAsync(options, this.MockFilePath); + ImageInfo info = await Image.IdentifyAsync(options, this.MockFilePath); - Assert.True(attempt.Success); - Assert.NotNull(attempt.Value); + Assert.NotNull(info); } [Fact] public async Task IdentifyWithFormatAsync_FromPath_GlobalConfiguration() { - Attempt attempt = await Image.TryIdentifyAsync(ActualImagePath); + ImageInfo info = await Image.IdentifyAsync(ActualImagePath); - Assert.Equal(ExpectedImageSize, attempt.Value.Size()); - Assert.Equal(ExpectedGlobalFormat, attempt.Value.Metadata.DecodedImageFormat); + Assert.Equal(ExpectedImageSize, info.Size()); + Assert.Equal(ExpectedGlobalFormat, info.Metadata.DecodedImageFormat); } [Fact] public async Task FromPathAsync_GlobalConfiguration() { - Attempt attempt = await Image.TryIdentifyAsync(ActualImagePath); + ImageInfo info = await Image.IdentifyAsync(ActualImagePath); - Assert.True(attempt.Success); - Assert.Equal(ExpectedImageSize, attempt.Value.Size()); + Assert.Equal(ExpectedImageSize, info.Size()); } [Fact] @@ -279,10 +271,9 @@ public partial class ImageTests DecoderOptions options = new() { Configuration = this.LocalConfiguration }; AsyncStreamWrapper asyncStream = new(this.DataStream, () => false); - Attempt attempt = await Image.TryIdentifyAsync(options, asyncStream); + ImageInfo info = await Image.IdentifyAsync(options, asyncStream); - Assert.True(attempt.Success); - Assert.Equal(this.LocalImageInfo, attempt.Value); + Assert.Equal(this.LocalImageInfo, info); } [Fact] @@ -291,7 +282,7 @@ public partial class ImageTests DecoderOptions options = new() { Configuration = new() }; AsyncStreamWrapper asyncStream = new(this.DataStream, () => false); - return Assert.ThrowsAsync(async () => await Image.TryIdentifyAsync(options, asyncStream)); + return Assert.ThrowsAsync(async () => await Image.IdentifyAsync(options, asyncStream)); } } }