-
Notifications
You must be signed in to change notification settings - Fork 50
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
EnsureArg.IsNotNull without class requirement #140
Comments
Is this the case you want to support? E.g. a nullable int. [Theory]
[InlineData(null)]
public void ShouldThrow_WhenNull(int? i) =>
ShouldThrow<ArgumentNullException>(
ExceptionMessages.Common_IsNotNull_Failed,
() => Ensure.Any.IsNotNull(i, ParamName),
() => EnsureArg.IsNotNull(i, ParamName),
() => Ensure.That(i, ParamName).IsNotNull());
[Theory]
[InlineData(1)]
public void ShouldNotThrow_WhenNull(int? i) =>
ShouldNotThrow(
() => Ensure.Any.IsNotNull(i, ParamName),
() => EnsureArg.IsNotNull(i, ParamName),
() => Ensure.That(i, ParamName).IsNotNull()); |
It's worth noting that a int? a = null; is More actionable feedback -- since |
The code above are tests against existing code. |
I apologize for my silliness thanks Daniel for the patience! |
What I'm ultimately after is being able to use EnsureArg on a generic that does not have a class requirement. For example
|
So sorry to have dropped this! @cjvanwyk3 could you refactor your code to match more like what we have here under AnyArg? Ensure.That/src/projects/EnsureThat/Enforcers/AnyArg.cs Lines 8 to 26 in 73c5664
The thing is, we don't have an overload that checks != null in the case that '== null' is impossible. I'm trying to find a way that it makes sense in the general (I 100% relate to your use case), but am struggling to generalize it to everyone. One work around that's always available as you want things that aren't in the base library (although thank you for asking since it helps improve coverage ♥) is that the Ensure.That() syntax returns a Param<> object you can write any extension method imaginable against. So even if a particular case doesn't make it into the code base, it can still be supported for your style! |
@danielwertheim Thank you so much! The EnsureArg.HasValue(item); works great for us! |
Would it be possible to remove the class requirement on EnsureArg.IsNotNull? Maybe something like this:
public static T IsNotNull(T value, string paramName = null)
{
var type = typeof(T);
if ((type.IsClass || IsNullable(type)) && value == null)
{
throw new ArgumentNullException(paramName, "Value may not be null.");
}
The text was updated successfully, but these errors were encountered: