Update new ArgumentOutOfRangeException.Throw methods to include ActualValue (#79157)
authorStephen Toub <stoub@microsoft.com>
Mon, 5 Dec 2022 18:06:12 +0000 (13:06 -0500)
committerGitHub <noreply@github.com>
Mon, 5 Dec 2022 18:06:12 +0000 (13:06 -0500)
src/libraries/System.Private.CoreLib/src/System/ArgumentOutOfRangeException.cs
src/libraries/System.Runtime/tests/System/ArgumentOutOfRangeExceptionTests.cs

index 912df39..830a425 100644 (file)
@@ -90,45 +90,45 @@ namespace System
         public virtual object? ActualValue => _actualValue;
 
         [DoesNotReturn]
-        private static void ThrowZero(string? paramName)
+        private static void ThrowZero<T>(string? paramName, T value)
         {
-            throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName));
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonZero, paramName));
         }
 
         [DoesNotReturn]
-        private static void ThrowNegative(string? paramName)
+        private static void ThrowNegative<T>(string? paramName, T value)
         {
-            throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName));
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegative, paramName));
         }
 
         [DoesNotReturn]
-        private static void ThrowNegativeOrZero(string? paramName)
+        private static void ThrowNegativeOrZero<T>(string? paramName, T value)
         {
-            throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName));
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeNonNegativeNonZero, paramName));
         }
 
         [DoesNotReturn]
-        private static void ThrowGreater<T>(string? paramName, T other)
+        private static void ThrowGreater<T>(string? paramName, T value, T other)
         {
-            throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, paramName, other));
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLessOrEqual, paramName, other));
         }
 
         [DoesNotReturn]
-        private static void ThrowGreaterEqual<T>(string? paramName, T other)
+        private static void ThrowGreaterEqual<T>(string? paramName, T value, T other)
         {
-            throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLess, paramName, other));
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeLess, paramName, other));
         }
 
         [DoesNotReturn]
-        private static void ThrowLess<T>(string? paramName, T other)
+        private static void ThrowLess<T>(string? paramName, T value, T other)
         {
-            throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, other));
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreaterOrEqual, paramName, other));
         }
 
         [DoesNotReturn]
-        private static void ThrowLessEqual<T>(string? paramName, T other)
+        private static void ThrowLessEqual<T>(string? paramName, T value, T other)
         {
-            throw new ArgumentOutOfRangeException(paramName, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, other));
+            throw new ArgumentOutOfRangeException(paramName, value, SR.Format(SR.ArgumentOutOfRange_Generic_MustBeGreater, paramName, other));
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is zero.</summary>
@@ -138,7 +138,7 @@ namespace System
             where T : INumberBase<T>
         {
             if (T.IsZero(value))
-                ThrowZero(paramName);
+                ThrowZero(paramName, value);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative.</summary>
@@ -148,7 +148,7 @@ namespace System
             where T : INumberBase<T>
         {
             if (T.IsNegative(value))
-                ThrowNegative(paramName);
+                ThrowNegative(paramName, value);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative or zero.</summary>
@@ -158,7 +158,7 @@ namespace System
             where T : INumberBase<T>
         {
             if (T.IsNegative(value) || T.IsZero(value))
-                ThrowNegativeOrZero(paramName);
+                ThrowNegativeOrZero(paramName, value);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than <paramref name="other"/>.</summary>
@@ -169,7 +169,7 @@ namespace System
             where T : IComparable<T>
         {
             if (value.CompareTo(other) > 0)
-                ThrowGreater(paramName, other);
+                ThrowGreater(paramName, value, other);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is greater than or equal <paramref name="other"/>.</summary>
@@ -180,7 +180,7 @@ namespace System
             where T : IComparable<T>
         {
             if (value.CompareTo(other) >= 0)
-                ThrowGreaterEqual(paramName, other);
+                ThrowGreaterEqual(paramName, value, other);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than <paramref name="other"/>.</summary>
@@ -191,7 +191,7 @@ namespace System
             where T : IComparable<T>
         {
             if (value.CompareTo(other) < 0)
-                ThrowLess(paramName, other);
+                ThrowLess(paramName, value, other);
         }
 
         /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is less than or equal <paramref name="other"/>.</summary>
@@ -202,7 +202,7 @@ namespace System
             where T : IComparable<T>
         {
             if (value.CompareTo(other) <= 0)
-                ThrowLessEqual(paramName, other);
+                ThrowLessEqual(paramName, value, other);
         }
     }
 }
index a9c364f..777bf25 100644 (file)
@@ -78,58 +78,58 @@ namespace System.Tests
         [Fact]
         public static void GenericHelpers_ThrowIfZero_Throws()
         {
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<int>(0));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<uint>(0));
+            Assert.Equal(0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<int>(0)).ActualValue);
+            Assert.Equal(0u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<uint>(0)).ActualValue);
 
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<float>(0.0f));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<float>(-0.0f));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<double>(0));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<double>(+0.0));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<double>(-0.0));
+            Assert.Equal(0.0f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<float>(0.0f)).ActualValue);
+            Assert.Equal(-0.0f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<float>(-0.0f)).ActualValue);
+            Assert.Equal((double)0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<double>(0)).ActualValue);
+            Assert.Equal(+0.0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<double>(+0.0)).ActualValue);
+            Assert.Equal(-0.0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, ZeroHelper<double>(-0.0)).ActualValue);
         }
 
         [Fact]
         public static void GenericHelpers_ThrowIfNegativeZero_Throws()
         {
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NegativeOrZeroHelper<int>(-1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NegativeOrZeroHelper<float>(-0.0f));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NegativeOrZeroHelper<double>(-0.0));
+            Assert.Equal(-1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NegativeOrZeroHelper<int>(-1)).ActualValue);
+            Assert.Equal(-0.0f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NegativeOrZeroHelper<float>(-0.0f)).ActualValue);
+            Assert.Equal(-0.0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, NegativeOrZeroHelper<double>(-0.0)).ActualValue);
         }
 
         [Fact]
         public static void GenericHelpers_ThrowIfGreaterThan_Throws()
         {
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<int>(1, 0));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<uint>(1, 0));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<double>(1.000000001, 1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<float>(1.00001f, 1));
+            Assert.Equal(1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<int>(1, 0)).ActualValue);
+            Assert.Equal(1u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<uint>(1, 0)).ActualValue);
+            Assert.Equal(1.000000001, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<double>(1.000000001, 1)).ActualValue);
+            Assert.Equal(1.00001f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanHelper<float>(1.00001f, 1)).ActualValue);
         }
 
         [Fact]
         public static void GenericHelpers_ThrowIfGreaterThanOrEqual_Throws()
         {
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<int>(1, 1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<uint>(1, 1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<double>(1, 1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<float>(1, 1));
+            Assert.Equal(1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<int>(1, 1)).ActualValue);
+            Assert.Equal(1u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<uint>(1, 1)).ActualValue);
+            Assert.Equal((double)1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<double>(1, 1)).ActualValue);
+            Assert.Equal(1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, GreaterThanOrEqualHelper<float>(1, 1)).ActualValue);
         }
 
         [Fact]
         public static void GenericHelpers_ThrowIfLessThan_Throws()
         {
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<int>(0, 1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<uint>(0, 1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<double>(1, 1.000000001));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<float>(1, 1.00001f));
+            Assert.Equal(0, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<int>(0, 1)).ActualValue);
+            Assert.Equal(0u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<uint>(0, 1)).ActualValue);
+            Assert.Equal((double)1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<double>(1, 1.000000001)).ActualValue);
+            Assert.Equal(1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanHelper<float>(1, 1.00001f)).ActualValue);
         }
 
         [Fact]
         public static void GenericHelpers_ThrowIfLessThanOrEqual_Throws()
         {
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<int>(1, 1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<uint>(1, 1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<double>(1, 1));
-            AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<float>(1, 1));
+            Assert.Equal(1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<int>(1, 1)).ActualValue);
+            Assert.Equal(1u, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<uint>(1, 1)).ActualValue);
+            Assert.Equal((double)1, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<double>(1, 1)).ActualValue);
+            Assert.Equal(1f, AssertExtensions.Throws<ArgumentOutOfRangeException>(HelpersParamName, LessThanOrEqualHelper<float>(1, 1)).ActualValue);
         }
     }
 }