ghuntley / conditions

If you are just starting out on a new project I recommend using https://github.com/adamralph/liteguard instead of conditions. Especially if using in a mobile application, less allocations and better performance. This project is deprecated and needs a new maintainer. Contact me if you want to take over.
MIT License
56 stars 10 forks source link

ArgumentException and subclasses should provide ParamName property #8

Open migig opened 8 years ago

migig commented 8 years ago

If I check for a null argument:

WithExceptionOnFailure<ArgumentNullException>().Requires(foo, nameof(foo)).IsNotNull();

In testing, one can assert that it throws an ArgumentNullException, and also get the name of the invalid argument from exception.ParamName.

But, for ArgumentException, this doesn't work as expected, because exception.ParamName == null. (Unsure whether the same is true for ArgumentOutOfRangeException.)

So: when library throws an ArgumentNullException it passes the paramName argument, but it doesn't do that when throwing ArgumentException.

This makes some testing quite difficult. When one is testing some method which has multiple arguments any of which could be invalid, the ParamName property tells you WHICH ONE was the problem.

I've looked into the source, but I admit I don't understand how the throwing works, and where to make this fix.

migig commented 8 years ago

Test case:

var foo = "";
WithExceptionOnFailure<ArgumentException>().Requires(foo, nameof(foo)).IsNotEmpty()

In the exception that will be thrown:

exception.ParamName == null