< Summary

Information
Class: SwiftCollections.SwiftThrowHelper
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Utility/SwiftThrowHelper.cs
Line coverage
93%
Covered lines: 31
Uncovered lines: 2
Coverable lines: 33
Total lines: 97
Line coverage: 93.9%
Branch coverage
83%
Covered branches: 15
Total branches: 18
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Utility/SwiftThrowHelper.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics.CodeAnalysis;
 4using System.Runtime.CompilerServices;
 5
 6namespace SwiftCollections;
 7
 8public static class SwiftThrowHelper
 9{
 10#nullable enable
 11
 12    /// <inheritdoc cref="ArgumentNullException"/>
 13    [MethodImpl(MethodImplOptions.NoInlining)]
 14    public static void ThrowIfNull([NotNull] object? argument, string? paramName = null)
 47287415    {
 47287416        if (argument is null)
 2717            ThrowArgumentNullException(paramName);
 47284718    }
 19
 20    /// <summary>
 21    /// Throws an exception if the specified value is null and nulls are not allowed for TValue.
 22    /// </summary>
 23    /// <param name="value">The value to check.</param>
 24    /// <param name="defaultValue">A default value of type TValue used to determine if nulls are illegal.</param>
 25    /// <exception cref="ArgumentNullException">The value is null and TValue is a value type.</exception>
 26    [MethodImpl(MethodImplOptions.NoInlining)]
 27    public static void ThrowIfNullAndNullsAreIllegal<TValue>(object value, TValue? defaultValue)
 628    {
 629        if (value == null && !(defaultValue == null))
 030            ThrowArgumentNullException(nameof(value));
 631    }
 32
 33    [DoesNotReturn]
 34    [MethodImpl(MethodImplOptions.NoInlining)]
 35    private static void ThrowArgumentNullException(string? paramName) =>
 2736        throw new ArgumentNullException(paramName);
 37
 38    /// <inheritdoc cref="ArgumentOutOfRangeException"/>
 39    [MethodImpl(MethodImplOptions.NoInlining)]
 40    public static void ThrowIfNegative(int value, string? paramName = null)
 7841    {
 7842        if (value < 0)
 043            ThrowArgumentOutOfRangeException(value, paramName);
 7844    }
 45
 46    /// <inheritdoc cref="ArgumentOutOfRangeException"/>
 47    [MethodImpl(MethodImplOptions.NoInlining)]
 48    public static void ThrowIfNegativeOrZero(int value, string? paramName = null)
 9549    {
 9550        if (value < 0 || value == 0)
 251            ThrowArgumentOutOfRangeException(value, paramName);
 9352    }
 53
 54    [DoesNotReturn]
 55    [MethodImpl(MethodImplOptions.NoInlining)]
 56    private static void ThrowArgumentOutOfRangeException(int value, string? paramName) =>
 257        throw new ArgumentOutOfRangeException(paramName, $"{paramName} must be greater than zero. Value: {value}");
 58
 59    /// <inheritdoc cref="ObjectDisposedException"/>
 60    [MethodImpl(MethodImplOptions.NoInlining)]
 61    public static void ThrowIfDisposed([DoesNotReturnIf(true)] bool condition, string? objectName = null)
 19562    {
 19563        if (condition)
 1164            ThrowObjectDisposedException(objectName);
 18465    }
 66
 67    [DoesNotReturn]
 68    [MethodImpl(MethodImplOptions.NoInlining)]
 69    private static void ThrowObjectDisposedException(string? objectName) =>
 1170        throw new ObjectDisposedException(objectName);
 71
 72    /// <inheritdoc cref="KeyNotFoundException"/>
 73    [MethodImpl(MethodImplOptions.NoInlining)]
 74    public static void ThrowIfKeyInvalid(int index, object? key = null)
 416275    {
 416276        if (index < 0)
 377            ThrowKeyNotFoundException(index, key);
 415978    }
 79
 80    [DoesNotReturn]
 81    [MethodImpl(MethodImplOptions.NoInlining)]
 82    private static void ThrowKeyNotFoundException(int index, object? key) =>
 383        throw new KeyNotFoundException($"Key not found: {key}");
 84
 85    /// <inheritdoc cref="IndexOutOfRangeException"/>
 86    [MethodImpl(MethodImplOptions.NoInlining)]
 87    public static void ThrowIfIndexInvalid(int index, int count)
 20588    {
 20589        if ((uint)index >= (uint)count)
 190            ThrowIndexOutOfRangeException(index);
 20491    }
 92
 93    [DoesNotReturn]
 94    [MethodImpl(MethodImplOptions.NoInlining)]
 95    private static void ThrowIndexOutOfRangeException(int value) =>
 196        throw new IndexOutOfRangeException($"Index out of range: {value}");
 97}