diff --git a/CSUtilities.Tests/Exceptions/ValueExtensionsTests.cs b/CSUtilities.Tests/Exceptions/ValueExtensionsTests.cs new file mode 100644 index 0000000..5a8d19f --- /dev/null +++ b/CSUtilities.Tests/Exceptions/ValueExtensionsTests.cs @@ -0,0 +1,47 @@ +using CSUtilities.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Xunit; + +namespace CSUtilities.Tests.Exceptions +{ + public class ValueExtensionsTests + { + [Fact] + public void ThrowIfTest() + { + int zero = 0; + + Assert.Throws(() => zero.ThrowIf((value) => + { + return value == 0; + })); + } + + [Theory] + [InlineData(-10, 0, 10, true)] + [InlineData(5, 0, 10, false)] + [InlineData(20, 0, 10, true)] + [InlineData(0, 0, 10, false)] + [InlineData(10, 0, 10, false)] + public void InRangeTest(double value, double min, double max, bool shouldThrow) + { + Action action = () => + { + value.InRange(min, max); + }; + + if (shouldThrow) + { + Assert.Throws(action); + } + else + { + action(); + } + } + } +} diff --git a/CSUtilities.Tests/Extensions/StringExtensionsTests.cs b/CSUtilities.Tests/Extensions/StringExtensionsTests.cs index e8575f2..deb95d0 100644 --- a/CSUtilities.Tests/Extensions/StringExtensionsTests.cs +++ b/CSUtilities.Tests/Extensions/StringExtensionsTests.cs @@ -29,18 +29,4 @@ public void TrowIfNullOrEmptyTest(string value) Assert.Throws(() => value.TrowIfNullOrEmpty("Message in case of null or empty")); } } - - public class ObjectExtensionsTests - { - [Fact] - public void ThrowIfTest() - { - int zero = 0; - - Assert.Throws(() => zero.ThrowIf((value) => - { - return value == 0; - })); - } - } } diff --git a/CSUtilities/CSUtilities.projitems b/CSUtilities/CSUtilities.projitems index fa503bd..c76ab14 100644 --- a/CSUtilities/CSUtilities.projitems +++ b/CSUtilities/CSUtilities.projitems @@ -12,8 +12,8 @@ + - diff --git a/CSUtilities/Extensions/ObjectExtensions.cs b/CSUtilities/Exceptions/ValueExtensions.cs similarity index 69% rename from CSUtilities/Extensions/ObjectExtensions.cs rename to CSUtilities/Exceptions/ValueExtensions.cs index 90efe69..3bcfa7c 100644 --- a/CSUtilities/Extensions/ObjectExtensions.cs +++ b/CSUtilities/Exceptions/ValueExtensions.cs @@ -1,13 +1,15 @@ using System; +using System.Collections.Generic; +using System.Text; -namespace CSUtilities.Extensions +namespace CSUtilities.Exceptions { #if PUBLIC public #else internal #endif - static class ObjectExtensions + static class ValueExtensions { public delegate bool Check(T obj); @@ -52,5 +54,17 @@ public static void ThrowIf(this T parameter, Check check, string messag throw Activator.CreateInstance(typeof(E), message) as E; } } + + public static void InRange(this T parameter, T min, T max, string? message = null, bool inclusive = true) + where T : IComparable + { + int down = parameter.CompareTo(min); + int up = parameter.CompareTo(max); + + if (up > 0 && down < 0) + { + throw new ArgumentOutOfRangeException(nameof(parameter), message); + } + } } }