// 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;
using System.Threading.Tasks;
namespace CommunityToolkit.Diagnostics;
///
partial class Guard
{
///
/// Asserts that the input instance is in a completed state.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is not in a completed state.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsCompleted(Task task, [CallerArgumentExpression("task")] string name = "")
{
if (task.IsCompleted)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsCompleted(task, name);
}
///
/// Asserts that the input instance is not in a completed state.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is in a completed state.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotCompleted(Task task, [CallerArgumentExpression("task")] string name = "")
{
if (!task.IsCompleted)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotCompleted(task, name);
}
///
/// Asserts that the input instance has been completed successfully.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if has not been completed successfully.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsCompletedSuccessfully(Task task, [CallerArgumentExpression("task")] string name = "")
{
if (task.Status == TaskStatus.RanToCompletion)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsCompletedSuccessfully(task, name);
}
///
/// Asserts that the input instance has not been completed successfully.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if has been completed successfully.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotCompletedSuccessfully(Task task, [CallerArgumentExpression("task")] string name = "")
{
if (task.Status != TaskStatus.RanToCompletion)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotCompletedSuccessfully(task, name);
}
///
/// Asserts that the input instance is faulted.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is not faulted.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsFaulted(Task task, [CallerArgumentExpression("task")] string name = "")
{
if (task.IsFaulted)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsFaulted(task, name);
}
///
/// Asserts that the input instance is not faulted.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is faulted.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotFaulted(Task task, [CallerArgumentExpression("task")] string name = "")
{
if (!task.IsFaulted)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotFaulted(task, name);
}
///
/// Asserts that the input instance is canceled.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is not canceled.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsCanceled(Task task, [CallerArgumentExpression("task")] string name = "")
{
if (task.IsCanceled)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsCanceled(task, name);
}
///
/// Asserts that the input instance is not canceled.
///
/// The input instance to test.
/// The name of the input parameter being tested.
/// Thrown if is canceled.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void IsNotCanceled(Task task, [CallerArgumentExpression("task")] string name = "")
{
if (!task.IsCanceled)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForIsNotCanceled(task, name);
}
///
/// Asserts that the input instance has a specific status.
///
/// The input instance to test.
/// The task status that is accepted.
/// The name of the input parameter being tested.
/// Thrown if doesn't match .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasStatusEqualTo(Task task, TaskStatus status, [CallerArgumentExpression("task")] string name = "")
{
if (task.Status == status)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasStatusEqualTo(task, status, name);
}
///
/// Asserts that the input instance has not a specific status.
///
/// The input instance to test.
/// The task status that is accepted.
/// The name of the input parameter being tested.
/// Thrown if matches .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void HasStatusNotEqualTo(Task task, TaskStatus status, [CallerArgumentExpression("task")] string name = "")
{
if (task.Status != status)
{
return;
}
ThrowHelper.ThrowArgumentExceptionForHasStatusNotEqualTo(task, status, name);
}
}