Skip to content

Commit

Permalink
Use AssertExtensions to test for null ArgumentException.ParamName
Browse files Browse the repository at this point in the history
Workaround xunit/xunit#2396
  • Loading branch information
ericstj committed Oct 7, 2021
1 parent 63faf78 commit 39d7874
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void InstanceArgumentInsteadOfTypeForStaticCall()
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
}));
Func<CallSite, object, object, object, object> target = site.Target;
Assert.Throws<ArgumentException>(null, () => target.Invoke(site, "Ceci n'est pas un type", 2, 2));
AssertExtensions.Throws<ArgumentException>(null, () => target.Invoke(site, "Ceci n'est pas un type", 2, 2));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public void Add_SetSiteNameDuplicate_ThrowsArgumentException()
container.Add(component1, "Name1");
container.Add(component2, "Name2");

Assert.Throws<ArgumentException>(null, () => component1.Site.Name = "Name2");
AssertExtensions.Throws<ArgumentException>(null, () => component1.Site.Name = "Name2");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void Indexer_SetNotSupported_ThrowsArgumentException(object owner)
MemberRelationship source = new MemberRelationship(owner, member);

var service = new NotSupportingMemberRelationshipService();
Assert.Throws<ArgumentException>(null, () => service[source] = source);
AssertExtensions.Throws<ArgumentException>(null, () => service[source] = source);
}

private class TestClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ public void AddService_NullServiceInstance_ThrowsArgumentNullException()
public void AddService_ServiceInstanceNotInstanceOfType_ThrowsArgumentException()
{
var container = new ServiceContainer();
Assert.Throws<ArgumentException>(null, () => container.AddService(typeof(int), new object()));
Assert.Throws<ArgumentException>(null, () => container.AddService(typeof(int), new object(), true));
Assert.Throws<ArgumentException>(null, () => container.AddService(typeof(int), new object(), false));
AssertExtensions.Throws<ArgumentException>(null, () => container.AddService(typeof(int), new object()));
AssertExtensions.Throws<ArgumentException>(null, () => container.AddService(typeof(int), new object(), true));
AssertExtensions.Throws<ArgumentException>(null, () => container.AddService(typeof(int), new object(), false));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void Ctor_NoSuchFontNameInCollection_ThrowsArgumentException()
{
using (var fontCollection = new PrivateFontCollection())
{
Assert.Throws<ArgumentException>(null, () => new FontFamily("Times New Roman", fontCollection));
AssertExtensions.Throws<ArgumentException>(null, () => new FontFamily("Times New Roman", fontCollection));
}
}

Expand Down
28 changes: 14 additions & 14 deletions src/libraries/System.Drawing.Common/tests/ImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void GetPropertyItem_InvokeExistsBitmapBmp_Success()
public void GetPropertyItem_NoSuchPropertyItemEmptyMemoryBitmap_ThrowsArgumentException(int propid)
{
using var bitmap = new Bitmap(1, 1);
Assert.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(propid));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(propid));
}

[Theory]
Expand All @@ -131,7 +131,7 @@ public void GetPropertyItem_NoSuchPropertyItemEmptyMemoryBitmap_ThrowsArgumentEx
public void GetPropertyItem_NoSuchPropertyItemEmptyImageBitmapBmp_ThrowsArgumentException(int propid)
{
using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("almogaver1bit.bmp"));
Assert.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(propid));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(propid));
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
Expand All @@ -149,17 +149,17 @@ public void RemovePropertyItem_InvokeMemoryBitmap_Success()

bitmap.RemovePropertyItem(PropertyTagExifUserComment);
Assert.Equal(new int[] { PropertyTagChrominanceTable, PropertyTagLuminanceTable }, bitmap.PropertyIdList);
Assert.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagExifUserComment));
Assert.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagExifUserComment));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagExifUserComment));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagExifUserComment));

bitmap.RemovePropertyItem(PropertyTagLuminanceTable);
Assert.Equal(new int[] { PropertyTagChrominanceTable }, bitmap.PropertyIdList);
Assert.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagLuminanceTable));
Assert.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagLuminanceTable));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagLuminanceTable));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagLuminanceTable));

bitmap.RemovePropertyItem(PropertyTagChrominanceTable);
Assert.Empty(bitmap.PropertyIdList);
Assert.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagChrominanceTable));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagChrominanceTable));
Assert.Throws<ExternalException>(() => bitmap.RemovePropertyItem(PropertyTagChrominanceTable));
}

Expand All @@ -170,17 +170,17 @@ public void RemovePropertyItem_InvokeBitmapJpg_Success()
using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("nature24bits.jpg"));
bitmap.RemovePropertyItem(PropertyTagExifUserComment);
Assert.Equal(new int[] { PropertyTagChrominanceTable, PropertyTagLuminanceTable }, bitmap.PropertyIdList);
Assert.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagExifUserComment));
Assert.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagExifUserComment));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagExifUserComment));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagExifUserComment));

bitmap.RemovePropertyItem(PropertyTagLuminanceTable);
Assert.Equal(new int[] { PropertyTagChrominanceTable }, bitmap.PropertyIdList);
Assert.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagLuminanceTable));
Assert.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagLuminanceTable));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagLuminanceTable));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(PropertyTagLuminanceTable));

bitmap.RemovePropertyItem(PropertyTagChrominanceTable);
Assert.Empty(bitmap.PropertyIdList);
Assert.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagChrominanceTable));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.GetPropertyItem(PropertyTagChrominanceTable));
Assert.Throws<ExternalException>(() => bitmap.RemovePropertyItem(PropertyTagChrominanceTable));
}

Expand Down Expand Up @@ -222,7 +222,7 @@ public void RemovePropertyItem_NoSuchPropertyNotEmptyMemoryBitmap_ThrowsArgument
bitmap.SetPropertyItem(item2);
bitmap.SetPropertyItem(item3);

Assert.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(propid));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(propid));
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
Expand All @@ -233,7 +233,7 @@ public void RemovePropertyItem_NoSuchPropertyNotEmptyMemoryBitmap_ThrowsArgument
public void RemovePropertyItem_NoSuchPropertyNotEmptyBitmapJpg_ThrowsArgumentException(int propid)
{
using var bitmap = new Bitmap(Helpers.GetTestBitmapPath("nature24bits.jpg"));
Assert.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(propid));
AssertExtensions.Throws<ArgumentException>(null, () => bitmap.RemovePropertyItem(propid));
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void SystemPens_Get_ReturnsExpected(Func<Pen> getPen, Color expectedColor
AssertExtensions.Throws<ArgumentException>(null, () => pen.StartCap = LineCap.RoundAnchor);
using (var matrix = new Matrix())
{
Assert.Throws<ArgumentException>(null, () => pen.Transform = matrix);
AssertExtensions.Throws<ArgumentException>(null, () => pen.Transform = matrix);
}
AssertExtensions.Throws<ArgumentException>(null, () => pen.Width = 10);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ public void CanReturnAnythingFromExplicitVoidCatch(bool useInterpreter)
[Fact]
public void CatchesMustReturnVoidWithVoidBody()
{
Assert.Throws<ArgumentException>(null, () =>
AssertExtensions.Throws<ArgumentException>(null, () =>
Expression.TryCatch(
Expression.Empty(),
Expression.Catch(typeof(InvocationExpression), Expression.Constant("hello")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,36 @@ public static void ExceptionforDuplicateKey()
{
writer.AddResource("duplicate", "value");

Assert.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", "value"));
Assert.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", new object()));
Assert.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", new byte[0]));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", "value"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", new object()));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", new byte[0]));

using (var stream = new MemoryStream())
{
Assert.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", stream));
Assert.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", stream, true));
Assert.Throws<ArgumentException>(null, () => writer.AddActivatorResource("duplicate", stream, "System.DayOfWeek", false));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", stream));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", stream, true));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddActivatorResource("duplicate", stream, "System.DayOfWeek", false));
}

Assert.Throws<ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicate", new byte[1], "System.DayOfWeek"));
Assert.Throws<ArgumentException>(null, () => writer.AddTypeConverterResource("duplicate", new byte[1], "System.DayOfWeek"));
Assert.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", "Monday", "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicate", new byte[1], "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddTypeConverterResource("duplicate", new byte[1], "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicate", "Monday", "System.DayOfWeek"));


Assert.Throws<ArgumentException>(null, () => writer.AddResource("Duplicate", "value"));
Assert.Throws<ArgumentException>(null, () => writer.AddResource("dUplicate", new object()));
Assert.Throws<ArgumentException>(null, () => writer.AddResource("duPlicate", new byte[0]));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("Duplicate", "value"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("dUplicate", new object()));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duPlicate", new byte[0]));

using (var stream = new MemoryStream())
{
Assert.Throws<ArgumentException>(null, () => writer.AddResource("dupLicate", stream));
Assert.Throws<ArgumentException>(null, () => writer.AddResource("duplIcate", stream, true));
Assert.Throws<ArgumentException>(null, () => writer.AddActivatorResource("dupliCate", stream, "System.DayOfWeek", false));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("dupLicate", stream));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplIcate", stream, true));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddActivatorResource("dupliCate", stream, "System.DayOfWeek", false));
}

Assert.Throws<ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicAte", new byte[1], "System.DayOfWeek"));
Assert.Throws<ArgumentException>(null, () => writer.AddTypeConverterResource("duplicaTe", new byte[1], "System.DayOfWeek"));
Assert.Throws<ArgumentException>(null, () => writer.AddResource("duplicatE", "Monday", "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicAte", new byte[1], "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddTypeConverterResource("duplicaTe", new byte[1], "System.DayOfWeek"));
AssertExtensions.Throws<ArgumentException>(null, () => writer.AddResource("duplicatE", "Monday", "System.DayOfWeek"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static void GePathRoot_Unix(string path)
[Fact]
public void GetFullPath_ThrowsOnEmbeddedNulls()
{
Assert.Throws<ArgumentException>(null, () => Path.GetFullPath("/gi\0t", "/foo/bar"));
AssertExtensions.Throws<ArgumentException>(null, () => Path.GetFullPath("/gi\0t", "/foo/bar"));
}

public static TheoryData<string, string> TestData_TrimEndingDirectorySeparator => new TheoryData<string, string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public void GetFullPath_CommonUnRooted_Windows(string path, string basePath, str
[Fact]
public void GetFullPath_ThrowsOnEmbeddedNulls()
{
Assert.Throws<ArgumentException>(null, () => Path.GetFullPath("/gi\0t", @"C:\foo\bar"));
AssertExtensions.Throws<ArgumentException>(null, () => Path.GetFullPath("/gi\0t", @"C:\foo\bar"));
}

public static TheoryData<string, string> TestData_TrimEndingDirectorySeparator => new TheoryData<string, string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ public void ReadWrite_NotEnoughSpaceInBuffer_ThrowsArgumentException(ulong byteO
var buffer = new SubBuffer(true);
buffer.Initialize(4);

Assert.Throws<ArgumentException>(null, () => buffer.Read<int>(byteOffset));
Assert.Throws<ArgumentException>(null, () => buffer.Write<int>(byteOffset, 2));
AssertExtensions.Throws<ArgumentException>(null, () => buffer.Read<int>(byteOffset));
AssertExtensions.Throws<ArgumentException>(null, () => buffer.Write<int>(byteOffset, 2));
}

[Fact]
Expand Down

0 comments on commit 39d7874

Please sign in to comment.