< Summary

Class:Azure.Core.Argument
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\Argument.cs
Covered lines:11
Uncovered lines:23
Coverable lines:34
Total lines:183
Line coverage:32.3% (11 of 34)
Covered branches:8
Total branches:30
Branch coverage:26.6% (8 of 30)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
AssertNotNull(...)-100%100%
AssertNotNull(...)-0%0%
AssertNotNullOrEmpty(...)-0%0%
AssertNotNullOrEmpty(...)-100%100%
AssertNotNullOrWhiteSpace(...)-0%0%
AssertNotDefault(...)-100%100%
AssertInRange(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\Argument.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4#if AZURE_NULLABLE
 5#nullable enable
 6#endif
 7
 8using System;
 9using System.Collections;
 10using System.Collections.Generic;
 11using System.Diagnostics.CodeAnalysis;
 12
 13namespace Azure.Core
 14{
 15    /// <summary>
 16    /// Argument validation.
 17    /// </summary>
 18    /// <remarks>
 19    ///   <para>This class should be shared via source using Azure.Core.props and contain only common argument validatio
 20    ///     It is declared partial so that you can use the same familiar class name but extend it with project-specific 
 21    ///     To extend the functionality of this class, just declare your own partial <see cref="Argument"/> class with p
 22    ///   </para>
 23    ///   <para>
 24    ///     Be sure to document exceptions thrown by these methods on your public methods.
 25    ///   </para>
 26    /// </remarks>
 27    internal static partial class Argument
 28    {
 29        // TODO: Add nullability attributes as needed when possible.
 30
 31        /// <summary>
 32        /// Throws if <paramref name="value"/> is null.
 33        /// </summary>
 34        /// <param name="value">The value to validate.</param>
 35        /// <param name="name">The name of the parameter.</param>
 36        /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
 37#if AZURE_NULLABLE
 38        public static void AssertNotNull<T>([AllowNull, NotNull] T value, string name) where T : class?
 39#else
 40        public static void AssertNotNull<T>(T value, string name) where T : class
 41#endif
 42        {
 408243            if (value is null)
 44            {
 6445                throw new ArgumentNullException(name);
 46            }
 401847        }
 48
 49        /// <summary>
 50        /// Throws if <paramref name="value"/> has not been initialized.
 51        /// </summary>
 52        /// <param name="value">The value to validate.</param>
 53        /// <param name="name">The name of the parameter.</param>
 54        /// <exception cref="ArgumentNullException"><paramref name="value"/> has not been initialized.</exception>
 55        public static void AssertNotNull<T>(T? value, string name) where T : struct
 56        {
 057            if (!value.HasValue)
 58            {
 059                throw new ArgumentNullException(name);
 60            }
 061        }
 62
 63        /// <summary>
 64        /// Throws if <paramref name="value"/> is null or an empty collection.
 65        /// </summary>
 66        /// <param name="value">The value to validate.</param>
 67        /// <param name="name">The name of the parameter.</param>
 68        /// <exception cref="ArgumentException"><paramref name="value"/> is an empty collection.</exception>
 69        /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
 70#if AZURE_NULLABLE
 71        public static void AssertNotNullOrEmpty<T>([AllowNull, NotNull] IEnumerable<T> value, string name)
 72#else
 73        public static void AssertNotNullOrEmpty<T>(IEnumerable<T> value, string name)
 74#endif
 75        {
 076            if (value is null)
 77            {
 078                throw new ArgumentNullException(name);
 79            }
 80
 81            // .NET Framework's Enumerable.Any() always allocates an enumerator, so we optimize for collections here.
 082            if (value is ICollection<T> collectionOfT && collectionOfT.Count == 0)
 83            {
 084                throw new ArgumentException("Value cannot be an empty collection.", name);
 85            }
 86
 087            if (value is ICollection collection && collection.Count == 0)
 88            {
 089                throw new ArgumentException("Value cannot be an empty collection.", name);
 90            }
 91
 092            using IEnumerator<T> e = value.GetEnumerator();
 093            if (!e.MoveNext())
 94            {
 095                throw new ArgumentException("Value cannot be an empty collection.", name);
 96            }
 097        }
 98
 99        /// <summary>
 100        /// Throws if <paramref name="value"/> is null or an empty string.
 101        /// </summary>
 102        /// <param name="value">The value to validate.</param>
 103        /// <param name="name">The name of the parameter.</param>
 104        /// <exception cref="ArgumentException"><paramref name="value"/> is an empty string.</exception>
 105        /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
 106#if AZURE_NULLABLE
 107        public static void AssertNotNullOrEmpty([AllowNull, NotNull] string value, string name)
 108#else
 109        public static void AssertNotNullOrEmpty(string value, string name)
 110#endif
 111        {
 1296112            if (value is null)
 113            {
 40114                throw new ArgumentNullException(name);
 115            }
 116
 1256117            if (value.Length == 0)
 118            {
 36119                throw new ArgumentException("Value cannot be an empty string.", name);
 120            }
 1220121        }
 122
 123        /// <summary>
 124        /// Throws if <paramref name="value"/> is null, an empty string, or consists only of white-space characters.
 125        /// </summary>
 126        /// <param name="value">The value to validate.</param>
 127        /// <param name="name">The name of the parameter.</param>
 128        /// <exception cref="ArgumentException"><paramref name="value"/> is an empty string or consists only of white-sp
 129        /// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
 130#if AZURE_NULLABLE
 131        public static void AssertNotNullOrWhiteSpace([AllowNull, NotNull] string value, string name)
 132#else
 133        public static void AssertNotNullOrWhiteSpace(string value, string name)
 134#endif
 135        {
 0136            if (value is null)
 137            {
 0138                throw new ArgumentNullException(name);
 139            }
 140
 0141            if (string.IsNullOrWhiteSpace(value))
 142            {
 0143                throw new ArgumentException("Value cannot be empty or contain only white-space characters.", name);
 144            }
 0145        }
 146
 147        /// <summary>
 148        /// Throws if <paramref name="value"/> is the default value for type <typeparamref name="T"/>.
 149        /// </summary>
 150        /// <typeparam name="T">The type of structure to validate which implements <see cref="IEquatable{T}"/>.</typepar
 151        /// <param name="value">The value to validate.</param>
 152        /// <param name="name">The name of the parameter.</param>
 153        /// <exception cref="ArgumentException"><paramref name="value"/> is the default value for type <typeparamref nam
 154        public static void AssertNotDefault<T>(ref T value, string name) where T : struct, IEquatable<T>
 155        {
 172156            if (value.Equals(default))
 157            {
 4158                throw new ArgumentException("Value cannot be empty.", name);
 159            }
 168160        }
 161
 162        /// <summary>
 163        /// Throws if <paramref name="value"/> is less than the <paramref name="minimum"/> or greater than the <paramref
 164        /// </summary>
 165        /// <typeparam name="T">The type of to validate which implements <see cref="IComparable{T}"/>.</typeparam>
 166        /// <param name="value">The value to validate.</param>
 167        /// <param name="minimum">The minimum value to compare.</param>
 168        /// <param name="maximum">The maximum value to compare.</param>
 169        /// <param name="name">The name of the parameter.</param>
 170        public static void AssertInRange<T>(T value, T minimum, T maximum, string name) where T : notnull, IComparable<T
 171        {
 0172            if (minimum.CompareTo(value) > 0)
 173            {
 0174                throw new ArgumentOutOfRangeException(name, "Value is less than the minimum allowed.");
 175            }
 176
 0177            if (maximum.CompareTo(value) < 0)
 178            {
 0179                throw new ArgumentOutOfRangeException(name, "Value is greater than the maximum allowed.");
 180            }
 0181        }
 182    }
 183}