Skip to content
Open

init #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions CSUtilities.Tests/Exceptions/ValueExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -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<ArgumentOutOfRangeException>(() => zero.ThrowIf<int, ArgumentOutOfRangeException>((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<ArgumentOutOfRangeException>(action);
}
else
{
action();
}
}
}
}
14 changes: 0 additions & 14 deletions CSUtilities.Tests/Extensions/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,4 @@ public void TrowIfNullOrEmptyTest(string value)
Assert.Throws<ArgumentException>(() => value.TrowIfNullOrEmpty("Message in case of null or empty"));
}
}

public class ObjectExtensionsTests
{
[Fact]
public void ThrowIfTest()
{
int zero = 0;

Assert.Throws<ArgumentOutOfRangeException>(() => zero.ThrowIf<int, ArgumentOutOfRangeException>((value) =>
{
return value == 0;
}));
}
}
}
2 changes: 1 addition & 1 deletion CSUtilities/CSUtilities.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<Compile Include="$(MSBuildThisFileDirectory)Attributes\StringValueAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\BigEndianConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)EnvironmentVars.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Exceptions\ValueExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\IDictionaryExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\ObjectExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Text\CodePage.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\DefaultEndianConverter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Converters\EndianConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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>(T obj);

Expand Down Expand Up @@ -52,5 +54,17 @@ public static void ThrowIf<T, E>(this T parameter, Check<T> check, string messag
throw Activator.CreateInstance(typeof(E), message) as E;
}
}

public static void InRange<T>(this T parameter, T min, T max, string? message = null, bool inclusive = true)
where T : IComparable<T>
{
int down = parameter.CompareTo(min);
int up = parameter.CompareTo(max);

if (up > 0 && down < 0)
{
throw new ArgumentOutOfRangeException(nameof(parameter), message);
}
}
}
}