GuardClauses icon indicating copy to clipboard operation
GuardClauses copied to clipboard

Adds `NullOrOutOfRange` clauses, making a new option for Nullable classes/structs to `OutOfRange`.

Open rafaelsc opened this issue 4 years ago • 1 comments

The current 3.2.0 Nuget Release. Don't have any support for Nullable classes/structs to OutOfRange

private static void TestStruct(int x)
{
	Guard.Against.OutOfRange(x, nameof(x), -10, 10); // OK
	Guard.Against.OutOfRange<int>(x, nameof(x), -10, 10); // Compiler Error
}

private static void TestNullableStruct(int? x)
{
	Guard.Against.OutOfRange(x, nameof(x), -10, 10); // Compiler Error
	Guard.Against.OutOfRange<int?>(x, nameof(x), -10, 10); // Compiler Error
}

private static void TestClass(Version x)
{
	var firstVersion = new Version(1, 0);
	var cuurentVersion = new Version(10, 0);

	Guard.Against.OutOfRange(x, nameof(x), firstVersion, cuurentVersion); // Compiler Error
	Guard.Against.OutOfRange<Version>(x, nameof(x), firstVersion, cuurentVersion); // Compiler Error
}

private static void TestClassWithNull(Version x)
{
	var firstVersion = new Version(1, 0);
	var cuurentVersion = new Version(10, 0);

	Guard.Against.OutOfRange(null, nameof(x), firstVersion, cuurentVersion); // Compiler Error
	Guard.Against.OutOfRange<Version>(null, nameof(x), firstVersion, cuurentVersion); // Compiler Error
}

The current main branch code-base added some support to classes to OutOfRange, but not for Nullable.

private static void TestStruct(int x)
{
	Guard.Against.OutOfRange(x, nameof(x), -10, 10); // OK
	Guard.Against.OutOfRange<int>(x, nameof(x), -10, 10); // OK
}

private static void TestNullableStruct(int? x)
{
	Guard.Against.OutOfRange(x, nameof(x), -10, 10); // Compiler Error
	Guard.Against.OutOfRange<int?>(x, nameof(x), -10, 10); // Compiler Error
}

private static void TestClass(Version x)
{
	var firstVersion = new Version(1, 0);
	var cuurentVersion = new Version(10, 0);
	
	Guard.Against.OutOfRange(x, nameof(x), firstVersion, cuurentVersion); // Compilation OK, but NullReferenceException in Runtime when x is null
	Guard.Against.OutOfRange<Version>(x, nameof(x), firstVersion, cuurentVersion);  // Compilation OK, but NullReferenceException in Runtime when x is null
}

private static void TestClassWithNull(Version x)
{
	var firstVersion = new Version(1, 0);
	var cuurentVersion = new Version(10, 0);

	Guard.Against.OutOfRange((Version)null, nameof(x), firstVersion, cuurentVersion); // Compilation OK, but NullReferenceException in Runtime when x is null
	Guard.Against.OutOfRange<Version>((Version)null, nameof(x), firstVersion, cuurentVersion); // Compilation OK, but NullReferenceException in Runtime when x is null
	
	Guard.Against.OutOfRange(null, nameof(x), firstVersion, cuurentVersion); // Compiler Error
	Guard.Against.OutOfRange<Version>(null, nameof(x), firstVersion, cuurentVersion); // Compiler Error
}

GuardClauses could add a NullOrOutOfRange clause to add support to Nullable classes/structs. And checking the input parameter fo null values, thowing ArgumentNullException insted of a NullReferenceException.

See this dotnetfiddle https://dotnetfiddle.net/nbXa8U Kind Related: #139

rafaelsc avatar Oct 25 '21 06:10 rafaelsc

I should be able to quickly add this to a 4.1 release if you want to update your PR. Thanks!

ardalis avatar Jan 18 '22 20:01 ardalis