Skip to content

Commit

Permalink
ThrowIf
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Apr 29, 2024
1 parent 4b4bd97 commit 07c9cc4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
14 changes: 14 additions & 0 deletions CSUtilities.Tests/Extensions/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,18 @@ 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;

zero.ThrowIf<int, ArgumentOutOfRangeException>((value) =>
{
return value == 0;
});
}
}
}
20 changes: 20 additions & 0 deletions CSUtilities/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace CSUtilities.Extensions
{
internal static class ObjectExtensions
{
public delegate bool Check<T>(T obj);

public static void ThrowIfNull(this object parameter)
{
if (parameter != null)
Expand All @@ -27,5 +29,23 @@ public static void ThrowIfNull(this object parameter, string paramName, string m

throw new System.ArgumentNullException(paramName, message);
}

public static void ThrowIf<T, E>(this T parameter, Check<T> check)
where E : Exception, new()
{
if (check(parameter))
{
throw new E();
}
}

public static void ThrowIf<T, E>(this T parameter, Check<T> check, string message)
where E : Exception, new()
{
if (check(parameter))
{
throw Activator.CreateInstance(typeof(E), message) as E;
}
}
}
}
29 changes: 27 additions & 2 deletions CSUtilities/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;

namespace CSUtilities.Extensions
{
Expand All @@ -10,27 +9,53 @@ namespace CSUtilities.Extensions
/// </summary>
internal static class StringExtensions
{
/// <summary>
/// Indicates if the specified string is null
/// </summary>
/// <param name="str">The string to test.</param>
/// <returns></returns>
public static bool IsNull(this string str)
{
return str == null;
}

/// <summary>
/// Indicates whether the specified string is null or an empty string ("").
/// </summary>
/// <param name="str">The string to test.</param>
/// <returns>true if the value parameter is null or an empty string (""); otherwise, false.</returns>
public static bool IsNullOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}

/// <summary>
/// Indicates whether a specified string is null, empty, or consists only of white-space characters.
/// </summary>
/// <param name="str"></param>
/// <returns>true if the value parameter is null or System.String.Empty, or if value consists exclusively of white-space characters.</returns>
public static bool IsNullOrWhiteSpace(this string str)
{
return string.IsNullOrWhiteSpace(str);
}

/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <exception cref="ArgumentException"></exception>
public static void TrowIfNullOrEmpty(this string str)
{
str.TrowIfNullOrEmpty("String cannot be null or empty");
}

public static void TrowIfNullOrEmpty(this string str, string message = null)
/// <summary>
///
/// </summary>
/// <param name="str"></param>
/// <param name="message"></param>
/// <exception cref="ArgumentException"></exception>
public static void TrowIfNullOrEmpty(this string str, string message)
{
if (string.IsNullOrEmpty(str))
{
Expand Down

0 comments on commit 07c9cc4

Please sign in to comment.