Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up tests #365

Merged
merged 2 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions MetadataExtractor.Tests/DirectoryExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ private static Directory BuildDirectory(IEnumerable<object> values)
return directory;
}

private static readonly IEnumerable<object> _singleValues = new object[]
{
private static readonly IEnumerable<object> _singleValues =
[
(byte)1,
(sbyte)2,
(short)3,
Expand All @@ -164,10 +164,10 @@ private static Directory BuildDirectory(IEnumerable<object> values)
(double)11,
new Rational(12, 1),
"13"
};
];

private static readonly IEnumerable<object> _singleZeroValues = new object[]
{
private static readonly IEnumerable<object> _singleZeroValues =
[
(byte)0,
(sbyte)0,
(short)0,
Expand All @@ -181,10 +181,10 @@ private static Directory BuildDirectory(IEnumerable<object> values)
(double)0,
new Rational(0, 0),
"0"
};
];

private static readonly IEnumerable<object> _arraysOfSingleValues = new object[]
{
private static readonly IEnumerable<object> _arraysOfSingleValues =
[
new byte[] { 1 },
new sbyte[] { 2 },
new short[] { 3 },
Expand All @@ -198,10 +198,10 @@ private static Directory BuildDirectory(IEnumerable<object> values)
new double[] { 11 },
new[] { new Rational(12, 1) },
new[] { "13" }
};
];

private static readonly IEnumerable<object> _arraysOfSingleZeroValues = new object[]
{
private static readonly IEnumerable<object> _arraysOfSingleZeroValues =
[
new byte[] { 0 },
new sbyte[] { 0 },
new short[] { 0 },
Expand All @@ -215,7 +215,7 @@ private static Directory BuildDirectory(IEnumerable<object> values)
new double[] { 0 },
new[] { new Rational(0, 0) },
new[] { "0" }
};
];

#endregion
}
Expand Down
2 changes: 1 addition & 1 deletion MetadataExtractor.Tests/DirectoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void UnderlyingInt()
Assert.Equal(value.ToString(), _directory.GetString(tagType));
Assert.True(_directory.TryGetRational(tagType, out Rational rational));
Assert.Equal(new Rational(value, 1), rational);
Assert.Equal(new[] { value }, _directory.GetInt32Array(tagType));
Assert.Equal([value], _directory.GetInt32Array(tagType));
Assert.Equal(new[] { unchecked((byte)value) }, _directory.GetByteArray(tagType));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static AdobeJpegDirectory ProcessBytes(string filePath)
public void SegmentTypes()
{
Assert.Equal(
new[] { JpegSegmentType.AppE },
[JpegSegmentType.AppE],
((IJpegSegmentMetadataReader)new AdobeJpegReader()).SegmentTypes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ExifReaderGremlinTest(ITestOutputHelper output)
}

[Fact(Skip = "Don't run on CI machines as it takes an age to complete")]
public void DoesntThrowNoMatterWhat()
public void DoesNotThrowNoMatterWhat()
{
RunGremlinTest("Data/withExif.jpg.app1");
}
Expand Down
8 changes: 4 additions & 4 deletions MetadataExtractor.Tests/Formats/Exif/ExifReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static IList<Directory> ProcessSegmentBytes(string filePath, JpegSegmentT
{
var segment = new JpegSegment(type, TestDataUtil.GetBytes(filePath), 0);

return new ExifReader().ReadJpegSegments(new[] { segment }).ToList();
return new ExifReader().ReadJpegSegments([segment]).ToList();
}

public static T ProcessSegmentBytes<T>(string filePath, JpegSegmentType type) where T : Directory
Expand Down Expand Up @@ -43,8 +43,8 @@ public void LoadFujifilmJpeg()
[Fact]
public void ReadJpegSegmentWithNoExifData()
{
var badExifSegment = new JpegSegment(JpegSegmentType.App1, new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, offset: 0);
var directories = new ExifReader().ReadJpegSegments(new[] { badExifSegment });
var badExifSegment = new JpegSegment(JpegSegmentType.App1, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], offset: 0);
var directories = new ExifReader().ReadJpegSegments([badExifSegment]);
Assert.Empty(directories);
}

Expand Down Expand Up @@ -122,7 +122,7 @@ public void DifferenceImageAndThumbnailOrientations()
{
// This metadata contains different orientations for the thumbnail and the main image.
// These values used to be merged into a single directory, causing errors.
// This unit test demonstrates correct behaviour.
// This unit test demonstrates correct behavior.
var directories = ProcessSegmentBytes("Data/repeatedOrientationTagWithDifferentValues.jpg.app1", JpegSegmentType.App1).ToList();

var ifd0Directory = directories.OfType<ExifIfd0Directory>().First();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class ExifSubIfdDescriptorTest
[Fact]
public void UserCommentDescription_EmptyEncoding()
{
var commentBytes = Encoding.UTF8.GetBytes("\x0\x0\x0\x0\x0\x0\x0\x0This is a comment");
var commentBytes = "\x0\x0\x0\x0\x0\x0\x0\x0This is a comment"u8.ToArray();
var directory = new ExifSubIfdDirectory();
directory.Set(ExifDirectoryBase.TagUserComment, commentBytes);
var descriptor = new ExifSubIfdDescriptor(directory);
Expand All @@ -21,7 +21,7 @@ public void UserCommentDescription_EmptyEncoding()
[Fact]
public void UserCommentDescription_BlankAscii()
{
var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0\x0 ");
var commentBytes = "ASCII\x0\x0\x0 "u8.ToArray();
var directory = new ExifSubIfdDirectory();
directory.Set(ExifDirectoryBase.TagUserComment, commentBytes);
var descriptor = new ExifSubIfdDescriptor(directory);
Expand All @@ -32,7 +32,7 @@ public void UserCommentDescription_BlankAscii()
public void UserCommentDescription_ZeroLengthAscii1()
{
// the 10-byte encoding region is only partially full
var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0\x0");
var commentBytes = "ASCII\x0\x0\x0"u8.ToArray();
var directory = new ExifSubIfdDirectory();
directory.Set(ExifDirectoryBase.TagUserComment, commentBytes);
var descriptor = new ExifSubIfdDescriptor(directory);
Expand All @@ -43,7 +43,7 @@ public void UserCommentDescription_ZeroLengthAscii1()
public void UserCommentDescription_ZeroLengthAscii2()
{
// fill the 10-byte encoding region
var commentBytes = Encoding.UTF8.GetBytes("ASCII\x0\x0\x0\x0\x0");
var commentBytes = "ASCII\x0\x0\x0\x0\x0"u8.ToArray();
var directory = new ExifSubIfdDirectory();
directory.Set(ExifDirectoryBase.TagUserComment, commentBytes);
var descriptor = new ExifSubIfdDescriptor(directory);
Expand Down
2 changes: 1 addition & 1 deletion MetadataExtractor.Tests/Formats/Icc/IccReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Extract_InvalidData()
public void ReadJpegSegments_InvalidData()
{
var app2 = new JpegSegment(JpegSegmentType.App2, TestDataUtil.GetBytes("Data/iccDataInvalid1.jpg.app2"), offset: 0);
var directory = new IccReader().ReadJpegSegments(new[] { app2 });
var directory = new IccReader().ReadJpegSegments([app2]);
Assert.NotNull(directory);
Assert.True(directory.Single().HasError);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void ReadAllSegments()
[Fact]
public void ReadSpecificSegments()
{
var segments = ReadSegments("Data/withExifAndIptc.jpg", new[] { JpegSegmentType.App0, JpegSegmentType.App2 });
var segments = ReadSegments("Data/withExifAndIptc.jpg", [JpegSegmentType.App0, JpegSegmentType.App2]);

Assert.Equal(2, segments.Count);

Expand Down
28 changes: 14 additions & 14 deletions MetadataExtractor.Tests/Formats/Netpbm/NetpbmReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ public sealed class NetpbmReaderTest
[Fact]
public void HeaderParsing()
{
Verify("P1 1024 768 255 ", formatType: 1, width: 1024, height: 768, maxVal: null);
Verify("P2 1024 768 255 ", formatType: 2, width: 1024, height: 768, maxVal: null);
Verify("P3 1024 768 255 ", formatType: 3, width: 1024, height: 768, maxVal: null);
Verify("P4 1024 768 255 ", formatType: 4, width: 1024, height: 768, maxVal: null);
Verify("P5 1024 768 255 ", formatType: 5, width: 1024, height: 768, maxVal: null);
Verify("P6 1024 768 255 ", formatType: 6, width: 1024, height: 768, maxVal: null);
Verify("P2\t1024\t768\t255 ", formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\n1024\n768\n255\n", formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\r\n1024\r\n768\r\n255\n", formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\r\n# comment\r\n1024\r\n768\r\n255\n", formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\r\n1024\r\n# comment\r\n768\r\n255\n", formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\r\n1024 # comment\r\n768\r\n255\n", formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P1 1024 768 255 "u8, formatType: 1, width: 1024, height: 768, maxVal: null);
Verify("P2 1024 768 255 "u8, formatType: 2, width: 1024, height: 768, maxVal: null);
Verify("P3 1024 768 255 "u8, formatType: 3, width: 1024, height: 768, maxVal: null);
Verify("P4 1024 768 255 "u8, formatType: 4, width: 1024, height: 768, maxVal: null);
Verify("P5 1024 768 255 "u8, formatType: 5, width: 1024, height: 768, maxVal: null);
Verify("P6 1024 768 255 "u8, formatType: 6, width: 1024, height: 768, maxVal: null);
Verify("P2\t1024\t768\t255 "u8, formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\n1024\n768\n255\n"u8, formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\r\n1024\r\n768\r\n255\n"u8, formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\r\n# comment\r\n1024\r\n768\r\n255\n"u8, formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\r\n1024\r\n# comment\r\n768\r\n255\n"u8, formatType: 2, width: 1024, height: 768, maxVal: 255);
Verify("P2\r\n1024 # comment\r\n768\r\n255\n"u8, formatType: 2, width: 1024, height: 768, maxVal: 255);
}

private static void Verify(string content, int formatType, int width, int height, int? maxVal)
private static void Verify(ReadOnlySpan<byte> content, int formatType, int width, int height, int? maxVal)
{
var stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
var stream = new MemoryStream(content.ToArray());
var directory = new NetpbmReader().Extract(stream);

Assert.Equal(formatType, directory.GetInt32(NetpbmHeaderDirectory.TagFormatType));
Expand Down
30 changes: 15 additions & 15 deletions MetadataExtractor.Tests/Formats/Png/PngDescriptorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public sealed class PngDescriptorTest
[Fact]
public void GetColorTypeDescription()
{
PngDirectory directory = new PngDirectory(PngChunkType.IHDR);
PngDescriptor descriptor = new PngDescriptor(directory);
var directory = new PngDirectory(PngChunkType.IHDR);
var descriptor = new PngDescriptor(directory);

directory.Set(PngDirectory.TagColorType, 6);
Assert.Equal("True Color with Alpha", descriptor.GetColorTypeDescription());
Expand All @@ -23,8 +23,8 @@ public void GetColorTypeDescription()
[Fact]
public void GetCompressionTypeDescription()
{
PngDirectory directory = new PngDirectory(PngChunkType.IHDR);
PngDescriptor descriptor = new PngDescriptor(directory);
var directory = new PngDirectory(PngChunkType.IHDR);
var descriptor = new PngDescriptor(directory);

directory.Set(PngDirectory.TagCompressionType, 0);
Assert.Equal("Deflate", descriptor.GetCompressionTypeDescription());
Expand All @@ -34,8 +34,8 @@ public void GetCompressionTypeDescription()
[Fact]
public void GetFilterMethodDescription()
{
PngDirectory directory = new PngDirectory(PngChunkType.IHDR);
PngDescriptor descriptor = new PngDescriptor(directory);
var directory = new PngDirectory(PngChunkType.IHDR);
var descriptor = new PngDescriptor(directory);

directory.Set(PngDirectory.TagFilterMethod, 0);
Assert.Equal("Adaptive", descriptor.GetFilterMethodDescription());
Expand Down Expand Up @@ -67,8 +67,8 @@ public void GetPaletteHasTransparencyDescription()
[Fact]
public void GetIsSrgbColorSpaceDescription()
{
PngDirectory directory = new PngDirectory(PngChunkType.sRGB);
PngDescriptor descriptor = new PngDescriptor(directory);
var directory = new PngDirectory(PngChunkType.sRGB);
var descriptor = new PngDescriptor(directory);

directory.Set(PngDirectory.TagSrgbRenderingIntent, 0);
Assert.Equal("Perceptual", descriptor.GetIsSrgbColorSpaceDescription());
Expand All @@ -78,8 +78,8 @@ public void GetIsSrgbColorSpaceDescription()
[Fact]
public void GetUnitSpecifierDescription()
{
PngDirectory directory = new PngDirectory(PngChunkType.pHYs);
PngDescriptor descriptor = new PngDescriptor(directory);
var directory = new PngDirectory(PngChunkType.pHYs);
var descriptor = new PngDescriptor(directory);

directory.Set(PngDirectory.TagUnitSpecifier, 1);
Assert.Equal("Metres", descriptor.GetUnitSpecifierDescription());
Expand All @@ -92,11 +92,11 @@ public void GetTextualDataDescription()
var latin1Encoding = Encoding.GetEncoding("iso-8859-1"); // Latin-1

var textPairs = new List<KeyValuePair>();
StringValue value = new StringValue(latin1Encoding.GetBytes("value"), latin1Encoding);
var value = new StringValue(latin1Encoding.GetBytes("value"), latin1Encoding);
textPairs.Add(new KeyValuePair("keyword", value));

PngDirectory directory = new PngDirectory(PngChunkType.tEXt);
PngDescriptor descriptor = new PngDescriptor(directory);
var directory = new PngDirectory(PngChunkType.tEXt);
var descriptor = new PngDescriptor(directory);
directory.Set(PngDirectory.TagTextualData, textPairs);
Assert.Equal("keyword: value", descriptor.GetTextualDataDescription());
Assert.Equal("keyword: value", directory.GetDescription(PngDirectory.TagTextualData));
Expand All @@ -117,8 +117,8 @@ public void GetTextualDataDescription()
[Fact]
public void GetBackgroundColorDescription()
{
PngDirectory directory = new PngDirectory(PngChunkType.bKGD);
PngDescriptor descriptor = new PngDescriptor(directory);
var directory = new PngDirectory(PngChunkType.bKGD);
var descriptor = new PngDescriptor(directory);

directory.Set(PngDirectory.TagBackgroundColor, new byte[] { 52 });
Assert.Equal("Palette Index 52", descriptor.GetBackgroundColorDescription());
Expand Down
6 changes: 3 additions & 3 deletions MetadataExtractor.Tests/IO/IndexedReaderTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public void WithShiftedBaseOffset()
Assert.Equal(10, reader.Length);
Assert.Equal(0, reader.GetByte(0));
Assert.Equal(1, reader.GetByte(1));
Assert.Equal(new byte[] { 0, 1 }, reader.GetBytes(0, 2));
Assert.Equal([0, 1], reader.GetBytes(0, 2));
Assert.Equal(4, reader.ToUnshiftedOffset(4));

reader = reader.WithShiftedBaseOffset(2);
Expand All @@ -300,7 +300,7 @@ public void WithShiftedBaseOffset()
Assert.Equal(8, reader.Length);
Assert.Equal(2, reader.GetByte(0));
Assert.Equal(3, reader.GetByte(1));
Assert.Equal(new byte[] { 2, 3 }, reader.GetBytes(0, 2));
Assert.Equal([2, 3], reader.GetBytes(0, 2));
Assert.Equal(6, reader.ToUnshiftedOffset(4));

reader = reader.WithShiftedBaseOffset(2);
Expand All @@ -309,7 +309,7 @@ public void WithShiftedBaseOffset()
Assert.Equal(6, reader.Length);
Assert.Equal(4, reader.GetByte(0));
Assert.Equal(5, reader.GetByte(1));
Assert.Equal(new byte[] { 4, 5 }, reader.GetBytes(0, 2));
Assert.Equal([4, 5], reader.GetBytes(0, 2));
Assert.Equal(8, reader.ToUnshiftedOffset(4));
}
}
Expand Down
22 changes: 11 additions & 11 deletions MetadataExtractor.Tests/IO/SequentialReaderTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void GetByte_OutOfBounds()
[Fact]
public void GetInt16()
{
Assert.Equal(-1, CreateReader(new[] { (byte)0xff, (byte)0xff }).GetInt16());
Assert.Equal(-1, CreateReader([0xff, 0xff]).GetInt16());

var buffer = new byte[] { 0x00, 0x01, 0x7F, 0xFF };

Expand Down Expand Up @@ -90,7 +90,7 @@ public void GetUInt16_OutOfBounds()
[Fact]
public void GetInt32()
{
Assert.Equal(-1, CreateReader(new[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }).GetInt32());
Assert.Equal(-1, CreateReader([0xff, 0xff, 0xff, 0xff]).GetInt32());

var buffer = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };

Expand All @@ -108,7 +108,7 @@ public void GetInt32()
[Fact]
public void GetUInt32()
{
Assert.Equal(4294967295u, CreateReader(new byte[] { 0xff, 0xff, 0xff, 0xff }).GetUInt32());
Assert.Equal(4294967295u, CreateReader([0xff, 0xff, 0xff, 0xff]).GetUInt32());

var buffer = new byte[] { 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };

Expand Down Expand Up @@ -182,7 +182,7 @@ public void GetFloat32()
const int NanBits = 0x7fc00000;
Assert.True(float.IsNaN(BitConverter.ToSingle(BitConverter.GetBytes(NanBits), 0)));

var reader = CreateReader(new byte[] { 0x7f, 0xc0, 0x00, 0x00 });
var reader = CreateReader([0x7f, 0xc0, 0x00, 0x00]);
Assert.True(float.IsNaN(reader.GetFloat32()));
}

Expand All @@ -192,29 +192,29 @@ public void GetFloat64()
const long NanBits = unchecked((long)0xfff0000000000001L);
Assert.True(double.IsNaN(BitConverter.Int64BitsToDouble(NanBits)));

var reader = CreateReader(new byte[] { 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 });
var reader = CreateReader([0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]);
Assert.True(double.IsNaN(reader.GetDouble64()));
}

[Fact]
public void GetNullTerminatedString()
{
var bytes = new byte[] { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 };
var bytes = "ABCDEFG"u8.ToArray();

// Test max length
for (var i = 0; i < bytes.Length; i++)
Assert.Equal("ABCDEFG".Substring(0, i - 0), CreateReader(bytes).GetNullTerminatedString(i));

Assert.Equal(string.Empty, CreateReader(new byte[] { 0 }).GetNullTerminatedString(10));
Assert.Equal("A", CreateReader(new byte[] { 0x41, 0 }).GetNullTerminatedString(10));
Assert.Equal("AB", CreateReader(new byte[] { 0x41, 0x42, 0 }).GetNullTerminatedString(10));
Assert.Equal("AB", CreateReader(new byte[] { 0x41, 0x42, 0, 0x43 }).GetNullTerminatedString(10));
Assert.Equal(string.Empty, CreateReader([0]).GetNullTerminatedString(10));
Assert.Equal("A", CreateReader([0x41, 0]).GetNullTerminatedString(10));
Assert.Equal("AB", CreateReader([0x41, 0x42, 0]).GetNullTerminatedString(10));
Assert.Equal("AB", CreateReader([0x41, 0x42, 0, 0x43]).GetNullTerminatedString(10));
}

[Fact]
public void GetString()
{
var bytes = new byte[] { 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 };
var bytes = "ABCDEFG"u8.ToArray();
var expected = Encoding.UTF8.GetString(bytes);

Assert.Equal(bytes.Length, expected.Length);
Expand Down
Loading