Skip to content

Commit d4dc4ba

Browse files
committed
Added ExceptionAssert to get a better error message when the exception message does not contain the expected message.
1 parent a025869 commit d4dc4ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+147
-119
lines changed

tests/Magick.NET.Tests/Coders/TheMapCoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void ShouldThrowExceptionWhenDimensionsNotSpecified(uint? width, uint? he
4242
using var image = new MagickImage();
4343

4444
var exception = Assert.Throws<MagickOptionErrorException>(() => image.Read(tempFile.File, settings));
45-
Assert.Contains("must specify image size", exception.Message);
45+
ExceptionAssert.Contains("must specify image size", exception);
4646
}
4747

4848
[Fact]
@@ -58,7 +58,7 @@ public void ShouldThrowExceptionWhenDepthNotSpecified()
5858
using var image = new MagickImage();
5959

6060
var exception = Assert.Throws<MagickOptionErrorException>(() => image.Read(tempFile.File, settings));
61-
Assert.Contains("must specify image depth", exception.Message);
61+
ExceptionAssert.Contains("must specify image depth", exception);
6262
}
6363

6464
[Fact]

tests/Magick.NET.Tests/Coders/TheTiffCoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ShouldThrowExceptionWhenImageContainsInvalidTag()
1919

2020
var exception = Assert.Throws<MagickCoderErrorException>(() => image.Read(Files.Coders.IgnoreTagTIF));
2121

22-
Assert.Contains(@$"Null count for ""Tag {_tag}""", exception.Message);
22+
ExceptionAssert.Contains(@$"Null count for ""Tag {_tag}""", exception);
2323
}
2424

2525
[Fact]
@@ -31,7 +31,7 @@ public void ShouldIgnoreTheSpecifiedTags()
3131

3232
var settings = new MagickReadSettings(new TiffReadDefines
3333
{
34-
IgnoreTags = new string[] { _tag },
34+
IgnoreTags = [_tag],
3535
});
3636

3737
image.Settings.RemoveDefine(MagickFormat.Tiff, "ignore-tags");

tests/Magick.NET.Tests/Formats/Bmp/BmpReadDefinesTests/TheIgnoreFileSizeProperty.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void ShouldNotAllowReadingBmpWithInvalidFileSizeByDefault()
6262
image.Settings.SetDefines(defines);
6363

6464
var exception = Assert.Throws<MagickCorruptImageErrorException>(() => image.Read(Files.Coders.InvalidCrcBMP));
65-
Assert.Contains("length and filesize do not match", exception.Message);
65+
ExceptionAssert.Contains("length and filesize do not match", exception);
6666
}
6767
}
6868
}

tests/Magick.NET.Tests/Formats/Pdf/PdfInfoTests/TheCreateMethod.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ public void ShouldThrowExceptionWhenFileIsPng()
7474

7575
if (exception is MagickDelegateErrorException delegateErrorException)
7676
{
77-
Assert.Single(exception.RelatedExceptions);
78-
Assert.Contains("Error: /syntaxerror in pdfopen", exception.RelatedExceptions.First().Message);
77+
Assert.Single(delegateErrorException.RelatedExceptions);
78+
ExceptionAssert.Contains("Error: /syntaxerror in pdfopen", delegateErrorException);
7979
}
8080
else
8181
{
82-
Assert.Contains("Unable to determine the page count.", exception.Message);
82+
ExceptionAssert.Contains("Unable to determine the page count.", exception);
8383
}
8484
}
8585
}

tests/Magick.NET.Tests/Formats/Pdf/PdfReadDefinesTests/ThePasswordProperty.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,7 @@ public void ShouldNotBeAbleToOpenFileWithNullPassword()
7676
}
7777
catch (MagickDelegateErrorException exception)
7878
{
79-
var message = exception.Message;
80-
81-
var relatedException = exception.RelatedExceptions.FirstOrDefault();
82-
if (relatedException is not null)
83-
message += relatedException.Message;
84-
85-
Assert.Contains("This file requires a password for access.", message);
79+
ExceptionAssert.Contains("This file requires a password for access.", exception);
8680
return;
8781
}
8882

tests/Magick.NET.Tests/Helpers/StreamWrapperTests/TheCreateForReadingMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void ShouldThrowExceptionWhenStreamIsNotReadable()
1717
using var stream = TestStream.ThatCannotRead();
1818

1919
var exception = Assert.Throws<ArgumentException>("stream", () => StreamWrapper.CreateForReading(stream));
20-
Assert.Contains("readable", exception.Message);
20+
ExceptionAssert.Contains("readable", exception);
2121
}
2222

2323
[Fact]

tests/Magick.NET.Tests/Helpers/StreamWrapperTests/TheCreateForWritingMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void ShouldThrowExceptionWhenStreamIsNotWritable()
1717
using var stream = TestStream.ThatCannotWrite();
1818

1919
var exception = Assert.Throws<ArgumentException>("stream", () => StreamWrapper.CreateForWriting(stream));
20-
Assert.Contains("writable", exception.Message);
20+
ExceptionAssert.Contains("writable", exception);
2121
}
2222

2323
[Fact]

tests/Magick.NET.Tests/MagickImageCollectionTests/TheAddRangeMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public void ShouldThrowExceptionWhenInvalid()
159159
using var images = new MagickImageCollection();
160160
var exception = Assert.Throws<MagickBlobErrorException>(() => images.Add(Files.Missing));
161161

162-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
162+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
163163
}
164164

165165
[Fact]

tests/Magick.NET.Tests/MagickImageCollectionTests/TheConstructor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void ShouldThrowExceptionWhenFileNameIsInvalid()
239239
{
240240
var exception = Assert.Throws<MagickBlobErrorException>(() => new MagickImageCollection(Files.Missing));
241241

242-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
242+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
243243
}
244244

245245
[Fact]

tests/Magick.NET.Tests/MagickImageCollectionTests/ThePingMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public void ShouldThrowExceptionWhenFileNameIsInvalid()
210210
using var images = new MagickImageCollection();
211211

212212
var exception = Assert.Throws<MagickBlobErrorException>(() => images.Ping(Files.Missing));
213-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
213+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
214214
}
215215

216216
[Fact]

tests/Magick.NET.Tests/MagickImageCollectionTests/TheReadAsyncMethod.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public async Task ShouldUseTheCorrectReaderWhenFormatIsSet()
223223

224224
var exception = await Assert.ThrowsAsync<MagickCorruptImageErrorException>(() => images.ReadAsync(stream, MagickFormat.Png));
225225

226-
Assert.Contains("ReadPNGImage", exception.Message);
226+
ExceptionAssert.Contains("ReadPNGImage", exception);
227227
}
228228
}
229229

@@ -272,7 +272,7 @@ public async Task ShouldUseTheCorrectReaderWhenFormatIsSet()
272272

273273
var exception = await Assert.ThrowsAsync<MagickCorruptImageErrorException>(() => images.ReadAsync(stream, settings));
274274

275-
Assert.Contains("ReadPNGImage", exception.Message);
275+
ExceptionAssert.Contains("ReadPNGImage", exception);
276276
}
277277
}
278278
}

tests/Magick.NET.Tests/MagickImageCollectionTests/TheReadMethod.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public void ShouldUseTheCorrectReaderWhenFormatIsSet()
205205
using var images = new MagickImageCollection();
206206

207207
var exception = Assert.Throws<MagickCorruptImageErrorException>(() => images.Read(bytes, MagickFormat.Png));
208-
Assert.Contains("ReadPNGImage", exception.Message);
208+
ExceptionAssert.Contains("ReadPNGImage", exception);
209209
}
210210
}
211211

@@ -240,7 +240,7 @@ public void ShouldUseTheCorrectReaderWhenFormatIsSet()
240240
using var images = new MagickImageCollection();
241241

242242
var exception = Assert.Throws<MagickCorruptImageErrorException>(() => images.Read(bytes, settings));
243-
Assert.Contains("ReadPNGImage", exception.Message);
243+
ExceptionAssert.Contains("ReadPNGImage", exception);
244244
}
245245

246246
[Fact]
@@ -332,7 +332,7 @@ public void ShouldThrowExceptionWhenFileNameIsInvalid()
332332
using var images = new MagickImageCollection();
333333

334334
var exception = Assert.Throws<MagickBlobErrorException>(() => images.Read(Files.Missing));
335-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
335+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
336336
}
337337

338338
[Fact]
@@ -463,7 +463,7 @@ public void ShouldUseTheCorrectReaderWhenFormatIsSet()
463463
using var images = new MagickImageCollection();
464464

465465
var exception = Assert.Throws<MagickCorruptImageErrorException>(() => images.Read(stream, MagickFormat.Png));
466-
Assert.Contains("ReadPNGImage", exception.Message);
466+
ExceptionAssert.Contains("ReadPNGImage", exception);
467467
}
468468
}
469469

@@ -510,7 +510,7 @@ public void ShouldUseTheCorrectReaderWhenFormatIsSet()
510510
using var images = new MagickImageCollection();
511511

512512
var exception = Assert.Throws<MagickCorruptImageErrorException>(() => images.Read(stream, settings));
513-
Assert.Contains("ReadPNGImage", exception.Message);
513+
ExceptionAssert.Contains("ReadPNGImage", exception);
514514
}
515515
}
516516
}

tests/Magick.NET.Tests/MagickImageInfoTests/TheConstructor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void ShouldThrowExceptionWhenFileNameIsInvalid()
8080
new MagickImageInfo(Files.Missing);
8181
});
8282

83-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
83+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
8484
}
8585
}
8686

tests/Magick.NET.Tests/MagickImageInfoTests/TheReadCollectionMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void ShouldThrowExceptionWhenFileNameIsInvalid()
8080
MagickImageInfo.ReadCollection(Files.Missing).ToArray();
8181
});
8282

83-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
83+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
8484
}
8585

8686
[Fact]

tests/Magick.NET.Tests/MagickImageInfoTests/TheReadMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void ShouldThrowExceptionWhenFileNameIsInvalid()
9898
imageInfo.Read(Files.Missing);
9999
});
100100

101-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
101+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
102102
}
103103

104104
[Fact]

tests/Magick.NET.Tests/MagickImageTests/TheConstructor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,16 @@ public void ShouldUseBaseDirectoryOfCurrentAppDomainWhenFileNameStartsWithTilde(
233233
var exception = Assert.Throws<MagickBlobErrorException>(() => new MagickImage("~/test.gif"));
234234
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
235235

236-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
237-
Assert.Contains(baseDirectory, exception.Message);
236+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
237+
ExceptionAssert.Contains(baseDirectory, exception);
238238
}
239239

240240
[Fact]
241241
public void ShouldNotUseBaseDirectoryOfCurrentAppDomainWhenFileNameIsTilde()
242242
{
243243
var exception = Assert.Throws<MagickMissingDelegateErrorException>(() => new MagickImage("~"));
244244

245-
Assert.Contains("error/constitute.c/ReadImage", exception.Message);
245+
ExceptionAssert.Contains("error/constitute.c/ReadImage", exception);
246246
}
247247
}
248248

tests/Magick.NET.Tests/MagickImageTests/TheCopyPixelsMethod.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void ShouldThrowExceptionWhenWidthIsTooHigh()
106106
using var destination = new MagickImage(MagickColors.Black, 50, 50);
107107

108108
var exception = Assert.Throws<MagickOptionErrorException>(() => destination.CopyPixels(source, new MagickGeometry(51, 50), 0, 0));
109-
Assert.Contains("geometry does not contain image", exception.Message);
109+
ExceptionAssert.Contains("geometry does not contain image", exception);
110110
}
111111

112112
[Fact]
@@ -117,7 +117,7 @@ public void ShouldThrowExceptionWhenHeightIsTooHigh()
117117

118118
var exception = Assert.Throws<MagickOptionErrorException>(() => destination.CopyPixels(source, new MagickGeometry(50, 51), 0, 0));
119119

120-
Assert.Contains("geometry does not contain image", exception.Message);
120+
ExceptionAssert.Contains("geometry does not contain image", exception);
121121
}
122122

123123
[Fact]
@@ -128,7 +128,7 @@ public void ShouldThrowExceptionWhenXIsTooHigh()
128128

129129
var exception = Assert.Throws<MagickOptionErrorException>(() => destination.CopyPixels(source, new MagickGeometry(50, 50), 1, 0));
130130

131-
Assert.Contains("geometry does not contain image", exception.Message);
131+
ExceptionAssert.Contains("geometry does not contain image", exception);
132132
}
133133

134134
[Fact]
@@ -139,7 +139,7 @@ public void ShouldThrowExceptionWhenYIsTooHigh()
139139

140140
var exception = Assert.Throws<MagickOptionErrorException>(() => destination.CopyPixels(source, new MagickGeometry(50, 50), 0, 1));
141141

142-
Assert.Contains("geometry does not contain image", exception.Message);
142+
ExceptionAssert.Contains("geometry does not contain image", exception);
143143
}
144144

145145
[Fact]

tests/Magick.NET.Tests/MagickImageTests/TheCropToTilesMethod.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void ShouldThrowExceptionWhenWidthIsZero()
2626
using var image = new MagickImage();
2727

2828
var exception = Assert.Throws<MagickCorruptImageErrorException>(() => image.CropToTiles(0, 1));
29-
Assert.Contains("negative or zero image size", exception.Message);
29+
ExceptionAssert.Contains("negative or zero image size", exception);
3030
}
3131

3232
[Fact]
@@ -35,7 +35,7 @@ public void ShouldThrowExceptionWhenHeightIsZero()
3535
using var image = new MagickImage();
3636

3737
var exception = Assert.Throws<MagickCorruptImageErrorException>(() => image.CropToTiles(1, 0));
38-
Assert.Contains("negative or zero image size", exception.Message);
38+
ExceptionAssert.Contains("negative or zero image size", exception);
3939
}
4040

4141
[Fact]

tests/Magick.NET.Tests/MagickImageTests/TheFrameMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void ShouldThrowExceptionWhenFrameIsLessThanImageSize()
1818
using var image = new MagickImage(Files.MagickNETIconPNG);
1919
var exception = Assert.Throws<MagickOptionErrorException>(() => { image.Frame(6, 6, 7, 7); });
2020

21-
Assert.Contains("frame is less than image size", exception.Message);
21+
ExceptionAssert.Contains("frame is less than image size", exception);
2222
}
2323

2424
[Fact]

tests/Magick.NET.Tests/MagickImageTests/TheImportPixelsMethod.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void ShouldThrowExceptionWhenArrayIsEmpty()
4141
using var image = new MagickImage();
4242

4343
var exception = Assert.Throws<ArgumentException>("data", () => image.ImportPixels(Array.Empty<byte>(), settings));
44-
Assert.Contains("Value cannot be empty.", exception.Message);
44+
ExceptionAssert.Contains("Value cannot be empty.", exception);
4545
}
4646

4747
[Fact]
@@ -60,7 +60,7 @@ public void ShouldThrowExceptionWhenMappingIsEmpty()
6060
using var image = new MagickImage();
6161

6262
var exception = Assert.Throws<ArgumentException>("settings", () => image.ImportPixels(new byte[] { 215 }, settings));
63-
Assert.Contains("Pixel storage mapping should be defined.", exception.Message);
63+
ExceptionAssert.Contains("Pixel storage mapping should be defined.", exception);
6464
}
6565

6666
[Fact]
@@ -71,7 +71,7 @@ public void ShouldThrowExceptionWhenStorageTypeIsUndefined()
7171
using var image = new MagickImage();
7272

7373
var exception = Assert.Throws<ArgumentException>("settings", () => image.ImportPixels(new byte[] { 215 }, settings));
74-
Assert.Contains("Storage type should not be undefined.", exception.Message);
74+
ExceptionAssert.Contains("Storage type should not be undefined.", exception);
7575
}
7676

7777
[Fact]
@@ -137,7 +137,7 @@ public void ShouldThrowExceptionWhenArrayIsEmpty()
137137
using var image = new MagickImage();
138138

139139
var exception = Assert.Throws<ArgumentException>("data", () => image.ImportPixels(Array.Empty<byte>(), 0, settings));
140-
Assert.Contains("Value cannot be empty.", exception.Message);
140+
ExceptionAssert.Contains("Value cannot be empty.", exception);
141141
}
142142

143143
[Fact]
@@ -156,7 +156,7 @@ public void ShouldThrowExceptionWhenOffsetExceedsArrayLength()
156156
using var image = new MagickImage();
157157

158158
var exception = Assert.Throws<ArgumentException>("offset", () => image.ImportPixels(new byte[] { 215 }, 1, settings));
159-
Assert.Contains("The offset should not exceed the length of the array.", exception.Message);
159+
ExceptionAssert.Contains("The offset should not exceed the length of the array.", exception);
160160
}
161161

162162
[Fact]
@@ -167,7 +167,7 @@ public void ShouldThrowExceptionWhenLengthIsTooLow()
167167
using var image = new MagickImage();
168168

169169
var exception = Assert.Throws<ArgumentException>("data", () => image.ImportPixels(new byte[] { 215, 215 }, 1, settings));
170-
Assert.Contains("The data length is 2 but should be at least 4.", exception.Message);
170+
ExceptionAssert.Contains("The data length is 2 but should be at least 4.", exception);
171171
}
172172

173173
[Fact]

tests/Magick.NET.Tests/MagickImageTests/TheMorphologyMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void ShouldThrowExceptionWhenKernelCannotBeParsed()
4040
};
4141

4242
var exception = Assert.Throws<MagickOptionErrorException>(() => image.Morphology(settings));
43-
Assert.Contains("Unable to parse kernel.", exception.Message);
43+
ExceptionAssert.Contains("Unable to parse kernel.", exception);
4444
}
4545

4646
[Fact]

tests/Magick.NET.Tests/MagickImageTests/ThePingMethod.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public void ShouldUseTheCorrectReaderWhenFormatIsSet()
183183

184184
var exception = Assert.Throws<MagickCorruptImageErrorException>(() => image.Ping(bytes, settings));
185185

186-
Assert.Contains("ReadPNGImage", exception.Message);
186+
ExceptionAssert.Contains("ReadPNGImage", exception);
187187
}
188188

189189
[Fact]
@@ -260,7 +260,7 @@ public void ShouldThrowExceptionWhenFileIsMissing()
260260
using var image = new MagickImage();
261261

262262
var exception = Assert.Throws<MagickBlobErrorException>(() => image.Ping(Files.Missing));
263-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
263+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
264264
}
265265

266266
[Fact]
@@ -269,7 +269,7 @@ public void ShouldThrowExceptionWhenFileWithFormatIsMissing()
269269
using var image = new MagickImage();
270270

271271
var exception = Assert.Throws<MagickBlobErrorException>(() => image.Ping("png:" + Files.Missing));
272-
Assert.Contains("error/blob.c/OpenBlob", exception.Message);
272+
ExceptionAssert.Contains("error/blob.c/OpenBlob", exception);
273273
}
274274

275275
[Fact]
@@ -498,7 +498,7 @@ public void ShouldUseTheCorrectReaderWhenFormatIsSet()
498498
using var image = new MagickImage();
499499

500500
var exception = Assert.Throws<MagickCorruptImageErrorException>(() => image.Ping(stream, settings));
501-
Assert.Contains("ReadPNGImage", exception.Message);
501+
ExceptionAssert.Contains("ReadPNGImage", exception);
502502
}
503503

504504
[Fact]

0 commit comments

Comments
 (0)