// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.CompilerServices;
#nullable enable
namespace Microsoft.Toolkit.Diagnostics
{
///
/// Helper methods to verify conditions when running code.
///
public static partial class Guard
{
///
/// Asserts that the input instance must be or empty.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is neither nor empty.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNullOrEmpty(string? text, string name)
{
if (!string.IsNullOrEmpty(text))
{
ThrowHelper.ThrowArgumentExceptionForIsNullOrEmpty(text, name);
}
}
///
/// Asserts that the input instance must not be or empty.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is or empty.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrEmpty(string? text, string name)
{
if (string.IsNullOrEmpty(text))
{
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrEmpty(text, name);
}
}
///
/// Asserts that the input instance must be or whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is neither nor whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNullOrWhitespace(string? text, string name)
{
if (!string.IsNullOrWhiteSpace(text))
{
ThrowHelper.ThrowArgumentExceptionForIsNullOrWhitespace(text, name);
}
}
///
/// Asserts that the input instance must not be or whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is or whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotNullOrWhitespace(string? text, string name)
{
if (string.IsNullOrWhiteSpace(text))
{
ThrowHelper.ThrowArgumentExceptionForIsNotNullOrWhitespace(text, name);
}
}
///
/// Asserts that the input instance must be empty.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is empty.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsEmpty(string text, string name)
{
if (text.Length != 0)
{
ThrowHelper.ThrowArgumentExceptionForIsEmpty(text, name);
}
}
///
/// Asserts that the input instance must not be empty.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is empty.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotEmpty(string text, string name)
{
if (text.Length == 0)
{
ThrowHelper.ThrowArgumentExceptionForIsNotEmpty(text, name);
}
}
///
/// Asserts that the input instance must be whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is neither nor whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsWhitespace(string text, string name)
{
if (!string.IsNullOrWhiteSpace(text))
{
ThrowHelper.ThrowArgumentExceptionForIsWhitespace(text, name);
}
}
///
/// Asserts that the input instance must not be or whitespace.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is or whitespace.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotWhitespace(string text, string name)
{
if (string.IsNullOrWhiteSpace(text))
{
ThrowHelper.ThrowArgumentExceptionForIsNotWhitespace(text, name);
}
}
///
/// Asserts that the input instance must have a size of a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is != .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeEqualTo(string text, int size, string name)
{
if (text.Length != size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeEqualTo(text, size, name);
}
}
///
/// Asserts that the input instance must have a size not equal to a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is == .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeNotEqualTo(string text, int size, string name)
{
if (text.Length == size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeNotEqualTo(text, size, name);
}
}
///
/// Asserts that the input instance must have a size over a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is <= .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeOver(string text, int size, string name)
{
if (text.Length <= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeOver(text, size, name);
}
}
///
/// Asserts that the input instance must have a size of at least specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is < .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeAtLeast(string text, int size, string name)
{
if (text.Length < size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeAtLeast(text, size, name);
}
}
///
/// Asserts that the input instance must have a size of less than a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is >= .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThan(string text, int size, string name)
{
if (text.Length >= size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThan(text, size, name);
}
}
///
/// Asserts that the input instance must have a size of less than or equal to a specified value.
///
/// The input instance to check the size for.
/// The target size to test.
/// The name of the input parameter being tested.
/// Thrown if the size of is > .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasSizeLessThanOrEqualTo(string text, int size, string name)
{
if (text.Length > size)
{
ThrowHelper.ThrowArgumentExceptionForHasSizeLessThanOrEqualTo(text, size, name);
}
}
}
}