Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit #3

Merged
merged 33 commits into from
Nov 11, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Null checks
Marco Bertschi committed Nov 3, 2019
commit fa308661499cc28b72335a60452ec3cde826c04d
4 changes: 1 addition & 3 deletions src/BBT.StructureTools/BBT.StructureTools.csproj
Original file line number Diff line number Diff line change
@@ -24,9 +24,7 @@
<ItemGroup>
<PackageReference Include="BBT.Maybe" Version="2.0.0" />
<PackageReference Include="BBT.StrategyPattern" Version="1.0.0" />
<PackageReference Include="FluentAssertions" Version="5.9.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.4">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
143 changes: 143 additions & 0 deletions src/BBT.StructureTools/Extension/StructureToolsArgumentChecks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
namespace BBT.StructureTools.Extension
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

/// <summary>
/// Common runtime checks that throw <see cref="ArgumentException"/> upon failure.
/// </summary>
public static class StructureToolsArgumentChecks
{
/// <summary>
/// Throws an exception if the specified parameter's value is null.
/// </summary>
/// <typeparam name="T">The type of the parameter.</typeparam>
/// <param name="value">The value of the argument.</param>
/// <param name="parameterName">The name of the parameter to include in any thrown exception.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <c>null</c>.</exception>
[DebuggerStepThrough]
public static void NotNull<T>([ValidatedNotNull]this T value, string parameterName)
where T : class
{
if (value == null)
{
throw new ArgumentNullException(parameterName);
}
}

/// <summary>
/// Throws an exception if the specified parameter's value is null, empty or consists only of white-space characters.
/// </summary>
/// <param name="value">The value of the argument.</param>
/// <param name="parameterName">The name of the parameter to include in any thrown exception.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is empty or consists only of white-space characters.</exception>
[DebuggerStepThrough]
public static void NotNullOrWhiteSpace([ValidatedNotNull]this string value, string parameterName)
{
if (value == null)
{
throw new ArgumentNullException(parameterName);
}

if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentOutOfRangeException(parameterName);
}
}

/// <summary>
/// Throws an exception if the specified parameter's value is negative.
/// </summary>
/// <param name="value">The value of the argument.</param>
/// <param name="parameterName">The name of the parameter to include in any thrown exception.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is negative.</exception>
[DebuggerStepThrough]
public static void NotNegative(this int value, string parameterName)
{
if (value < 0)
{
throw new ArgumentOutOfRangeException(parameterName);
}
}

/// <summary>
/// Throws an exception if the specified parameter's value is negative or zero.
/// </summary>
/// <param name="value">The value of the argument.</param>
/// <param name="parameterName">The name of the parameter to include in any thrown exception.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> is negative or zero.</exception>
[DebuggerStepThrough]
public static void NotNegativeOrZero(this int value, string parameterName)
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException(parameterName);
}
}

/// <summary>
/// Throws an exception if the specified parameter's value is null or empty.
/// </summary>
/// <typeparam name="T">The type of the parameter.</typeparam>
/// <param name="value">The value of the argument.</param>
/// <param name="parameterName">The name of the parameter to include in any thrown exception.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is empty.</exception>
[DebuggerStepThrough]
public static void NotNullOrEmpty<T>(this IEnumerable<T> value, string parameterName)
{
// ReSharper disable once PossibleMultipleEnumeration
value.NotNull(parameterName);

// ReSharper disable once PossibleMultipleEnumeration
if (!value.Any())
{
throw new ArgumentException("Empty list.", parameterName);
}
}

/// <summary>
/// Throws an exception if the specified parameter's value is null, empty or contains an empty element.
/// </summary>
/// <typeparam name="T">The type of the parameter.</typeparam>
/// <param name="value">The value of the argument.</param>
/// <param name="parameterName">The name of the parameter to include in any thrown exception.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> contains an empty element.</exception>
[DebuggerStepThrough]
public static void NotNullOrEmptyElement<T>(this IEnumerable<T> value, string parameterName)
{
// ReSharper disable once PossibleMultipleEnumeration
value.NotNull(parameterName);

// ReSharper disable once PossibleMultipleEnumeration
if (value.Any(x => x == null))
{
throw new ArgumentOutOfRangeException(parameterName, "List contains.");
}
}

/// <summary>
/// Throws an exception if the specified parameter's value is null, empty or contains an empty element.
/// </summary>
/// <typeparam name="T">The type of the parameter.</typeparam>
/// <param name="value">The value of the argument.</param>
/// <param name="parameterName">The name of the parameter to include in any thrown exception.</param>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is empty.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="value"/> contains an empty element.</exception>
[DebuggerStepThrough]
public static void NotNullOrEmptyOrEmptyElement<T>(this IEnumerable<T> value, string parameterName)
{
// ReSharper disable once PossibleMultipleEnumeration
value.NotNullOrEmpty(parameterName);

// ReSharper disable once PossibleMultipleEnumeration
value.NotNullOrEmptyElement(parameterName);
}
}
}
12 changes: 12 additions & 0 deletions src/BBT.StructureTools/Extension/ValidatedNotNullAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace BBT.StructureTools.Extension
{
using System;

/// <summary>
/// Indicates to code analysis that a method validates a particular parameter.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter)]
internal sealed class ValidatedNotNullAttribute : Attribute
{
}
}