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

Changed message of exception to improve readability #1306

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions Src/Idioms/EmptyGuidBehaviorExpectation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;

namespace AutoFixture.Idioms
{
Expand Down Expand Up @@ -34,16 +35,23 @@ public void Verify(IGuardClauseCommand command)
{
command.Execute(Guid.Empty);
}
catch (ArgumentException)
catch (ArgumentException e)
{
return;
if (string.Equals(e.ParamName, command.RequestedParameterName, StringComparison.Ordinal))
{
return;
}

throw command.CreateInvalidParamNameException(EmptyGuid, e);
}
catch (Exception e)
{
throw command.CreateException("\"Guid.Empty\"", e);
throw command.CreateException(EmptyGuid, e);
}

throw command.CreateException("\"Guid.Empty\"");
throw command.CreateException(EmptyGuid);
}

private const string EmptyGuid = "\"Guid.Empty\"";
}
}
17 changes: 5 additions & 12 deletions Src/Idioms/EmptyStringBehaviorExpectation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,16 @@ public void Verify(IGuardClauseCommand command)
return;
}

throw command.CreateException(
"<empty string>",
string.Format(CultureInfo.InvariantCulture,
"Guard Clause prevented it, however the thrown exception contains invalid parameter name. " +
"Ensure you pass correct parameter name to the ArgumentException constructor.{0}" +
"Expected parameter name: {1}{0}Actual parameter name: {2}",
Environment.NewLine,
command.RequestedParameterName,
e.ParamName),
e);
throw command.CreateInvalidParamNameException(EmptyString, e);
}
catch (Exception e)
{
throw command.CreateException("<empty string>", e);
throw command.CreateException(EmptyString, e);
}

throw command.CreateException("<empty string>");
throw command.CreateException(EmptyString);
}

private const string EmptyString = "<empty string>";
}
}
27 changes: 27 additions & 0 deletions Src/Idioms/GuardClauseCommandExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Globalization;

namespace AutoFixture.Idioms
{
internal static class GuardClauseCommandExtensions
{
public static Exception CreateInvalidParamNameException(this IGuardClauseCommand command, string value,
ArgumentException innerException)
{
if (command == null) throw new ArgumentNullException(nameof(command));
if (value == null) throw new ArgumentNullException(nameof(value));
if (innerException == null) throw new ArgumentNullException(nameof(innerException));

return command.CreateException(
value,
string.Format(CultureInfo.InvariantCulture,
"a Guard Clause prevented it; however, the thrown exception contains an invalid parameter name. " +
"Ensure you pass the correct parameter name to the ArgumentException constructor.{0}" +
"Expected parameter name: {1}{0}Actual parameter name: {2}",
Environment.NewLine,
command.RequestedParameterName,
innerException.ParamName),
innerException);
}
}
}
19 changes: 7 additions & 12 deletions Src/Idioms/NullReferenceBehaviorExpectation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,20 @@ public void Verify(IGuardClauseCommand command)
catch (ArgumentNullException e)
{
if (string.Equals(e.ParamName, command.RequestedParameterName, StringComparison.Ordinal))
{
return;
}

throw command.CreateException(
"<null>",
string.Format(CultureInfo.InvariantCulture,
"Guard Clause prevented it, however the thrown exception contains invalid parameter name. " +
"Ensure you pass correct parameter name to the ArgumentNullException constructor.{0}" +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this assertion will not report anymore the correct exception type. You might add the exception type as an argument for the exception factory. Looks like current tests did not assert this behavior. Could you add the appropriate tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What wrong exception? I am not sure I follow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@C0DK on line 62 it says ArgumentNullException. In the method that replaces this code block, the exception that's mentioned is ArgumentException.

"Expected parameter name: {1}{0}Actual parameter name: {2}",
Environment.NewLine,
command.RequestedParameterName,
e.ParamName),
e);
throw command.CreateInvalidParamNameException(Null, e);
}
catch (Exception e)
{
throw command.CreateException("null", e);
throw command.CreateException(Null, e);
}

throw command.CreateException("null");
throw command.CreateException(Null);
}

private const string Null = "null";
}
}
17 changes: 5 additions & 12 deletions Src/Idioms/WhiteSpaceStringBehaviorExpectation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,16 @@ public void Verify(IGuardClauseCommand command)
return;
}

throw command.CreateException(
"<white space>",
string.Format(CultureInfo.InvariantCulture,
"Guard Clause prevented it, however the thrown exception contains invalid parameter name. " +
"Ensure you pass correct parameter name to the ArgumentException constructor.{0}" +
"Expected parameter name: {1}{0}Actual parameter name: {2}",
Environment.NewLine,
command.RequestedParameterName,
e.ParamName),
e);
throw command.CreateInvalidParamNameException(WhiteSpace, e);
}
catch (Exception e)
{
throw command.CreateException("<white space>", e);
throw command.CreateException(WhiteSpace, e);
}

throw command.CreateException("<white space>");
throw command.CreateException(WhiteSpace);
}

private const string WhiteSpace = "<white space>";
}
}
6 changes: 3 additions & 3 deletions Src/IdiomsUnitTest/GuardClauseAssertionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ public void VerifyNonProperlyGuardedConstructorThrowsException()
var constructorInfo = typeof(NonProperlyGuardedClass).GetConstructors().Single();

var exception = Assert.Throws<GuardClauseException>(() => sut.Verify(constructorInfo));
Assert.Contains("Guard Clause prevented it, however", exception.Message);
Assert.Contains("Guard Clause prevented it; however", exception.Message);
}

[Fact]
Expand All @@ -945,11 +945,11 @@ public void VerifyNonProperlyGuardedPropertyThrowsException()
var propertyInfo = typeof(NonProperlyGuardedClass).GetProperty(nameof(NonProperlyGuardedClass.Property));

var exception = Assert.Throws<GuardClauseException>(() => sut.Verify(propertyInfo));
Assert.Contains("Guard Clause prevented it, however", exception.Message);
Assert.Contains("Guard Clause prevented it; however", exception.Message);
}

[Theory]
[InlineData(nameof(NonProperlyGuardedClass.Method), "Guard Clause prevented it, however")]
[InlineData(nameof(NonProperlyGuardedClass.Method), "Guard Clause prevented it; however")]
[InlineData(nameof(NonProperlyGuardedClass.DeferredMethodReturningGenericEnumerable), "deferred")]
[InlineData(nameof(NonProperlyGuardedClass.DeferredMethodReturningGenericEnumerator), "deferred")]
[InlineData(nameof(NonProperlyGuardedClass.DeferredMethodReturningNonGenericEnumerable), "deferred")]
Expand Down