< Summary

Class:Azure.Core.Argument
Assembly:Azure.Messaging.EventHubs
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\Argument.cs
C:\Git\azure-sdk-for-net\sdk\eventhub\Azure.Messaging.EventHubs.Shared\src\Core\Argument.cs
Covered lines:37
Uncovered lines:42
Coverable lines:79
Total lines:341
Line coverage:46.8% (37 of 79)
Covered branches:29
Total branches:76
Branch coverage:38.1% (29 of 76)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
AssertNotNull(...)-0%0%
AssertNotNull(...)-0%0%
AssertNotNullOrEmpty(...)-0%0%
AssertNotNullOrEmpty(...)-0%0%
AssertNotNullOrWhiteSpace(...)-60%50%
AssertNotDefault(...)-0%0%
AssertInRange(...)-60%50%
AssertInRange(...)-100%100%
AssertNotEmptyOrWhiteSpace(...)-40%33.33%
AssertNotNull(...)-100%100%
AssertNotNull(...)-100%83.33%
AssertNotTooLong(...)-75%75%
AssertNotNullOrEmpty(...)-36.36%25%
AssertNotNegative(...)-100%33.33%
AssertAtLeast(...)-100%100%
AssertNotNullOrEmpty(...)-100%100%
AssertNotDisposed(...)-100%100%
AssertNotClosed(...)-100%100%
AssertNotNullOrWhiteSpace(...)-60%50%
AssertWellFormedEventHubsNamespace(...)-100%66.67%
AssertNotDefault(...)-50%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        {
 043            if (value is null)
 44            {
 045                throw new ArgumentNullException(name);
 46            }
 047        }
 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        {
 0112            if (value is null)
 113            {
 0114                throw new ArgumentNullException(name);
 115            }
 116
 0117            if (value.Length == 0)
 118            {
 0119                throw new ArgumentException("Value cannot be an empty string.", name);
 120            }
 0121        }
 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        {
 8136            if (value is null)
 137            {
 0138                throw new ArgumentNullException(name);
 139            }
 140
 8141            if (string.IsNullOrWhiteSpace(value))
 142            {
 0143                throw new ArgumentException("Value cannot be empty or contain only white-space characters.", name);
 144            }
 8145        }
 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        {
 0156            if (value.Equals(default))
 157            {
 0158                throw new ArgumentException("Value cannot be empty.", name);
 159            }
 0160        }
 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        {
 110172            if (minimum.CompareTo(value) > 0)
 173            {
 0174                throw new ArgumentOutOfRangeException(name, "Value is less than the minimum allowed.");
 175            }
 176
 110177            if (maximum.CompareTo(value) < 0)
 178            {
 0179                throw new ArgumentOutOfRangeException(name, "Value is greater than the maximum allowed.");
 180            }
 110181        }
 182    }
 183}

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Globalization;
 6using Azure.Messaging.EventHubs;
 7
 8namespace Azure.Core
 9{
 10    /// <summary>
 11    ///   Provides a consistent means for verifying arguments and other invariants for a given
 12    ///   member.
 13    /// </summary>
 14    ///
 15    /// <remarks>
 16    ///   This class extends the <see cref="Azure.Core.Argument" /> type, referenced as a partial
 17    ///   class via shared source; the base partial class definition may be found in the "SharedSource"
 18    ///   folder of this project.
 19    /// </remarks>
 20    ///
 21    internal static partial class Argument
 22    {
 23        /// <summary>
 24        ///   Ensures that an argument's value is a string comprised of only whitespace, though
 25        ///   <c>null</c> is considered a valid value.  An <see cref="ArgumentException" /> is thrown
 26        ///   if that invariant is not met.
 27        /// </summary>
 28        ///
 29        /// <param name="argumentValue">The value of the argument to verify.</param>
 30        /// <param name="argumentName">The name of the argument being considered.</param>
 31        ///
 32        /// <exception cref="ArgumentException">The argument is empty or contains only white-space.</exception>
 33        ///
 34        public static void AssertNotEmptyOrWhiteSpace(string argumentValue, string argumentName)
 35        {
 036            if (argumentValue is null)
 37            {
 038                return;
 39            }
 40
 041            if (string.IsNullOrWhiteSpace(argumentValue))
 42            {
 1599243                throw new ArgumentException($"The argument '{argumentName}' may not be empty or white-space, though it m
 44            }
 10445        }
 46
 1588847        /// <summary>
 48        ///   Ensures that a string argument's length is below a maximum allowed threshold,
 49        ///   throwing an <see cref="ArgumentOutOfRangeException" /> if that invariant is not met.
 50        /// </summary>
 51        ///
 52        /// <param name="argumentValue">The value of the argument to verify.</param>
 53        /// <param name="maximumLength">The maximum allowable length for the <paramref name="argumentValue"/>; its lengt
 54        /// <param name="argumentName">The name of the argument being considered.</param>
 55        ///
 56        /// <exception cref="ArgumentOutOfRangeException"><paramref name="argumentValue"/> exceeds <paramref name="maxim
 5857        ///
 58        public static void AssertNotTooLong(string argumentValue, int maximumLength, string argumentName)
 259        {
 31660            if (argumentValue != null && argumentValue.Length > maximumLength)
 5661            {
 062                throw new ArgumentOutOfRangeException(argumentName, $"The argument '{argumentName}' cannot exceed {maxim
 63            }
 31664        }
 65
 66        /// <summary>
 67        ///   Ensures that an argument's value is not a negative value, throwing an
 68        ///   <see cref="ArgumentOutOfRangeException" /> if that invariant is not met.
 69        /// </summary>
 70        ///
 71        /// <param name="argumentValue">The value of the argument to verify.</param>
 72        /// <param name="argumentName">The name of the argument being considered.</param>
 73        ///
 74        /// <exception cref="ArgumentOutOfRangeException"><paramref name="argumentValue"/> is a negative <see cref="Time
 75        ///
 076        public static void AssertNotNegative(TimeSpan argumentValue, string argumentName)
 77        {
 66878            if (argumentValue < TimeSpan.Zero)
 79            {
 4080                throw new ArgumentOutOfRangeException(argumentName, $"Argument {argumentName} must be a non-negative tim
 81            }
 62882        }
 83
 084        /// <summary>
 85        ///   Ensures that an argument's value is at least as large as a given lower bound, throwing
 86        ///   <see cref="ArgumentException" /> if that invariant is not met.
 087        /// </summary>
 88        ///
 089        /// <param name="argumentValue">The value of the argument to verify.</param>
 90        /// <param name="minimumValue">The minimum to use for comparison; <paramref name="argumentValue"/> must be great
 91        /// <param name="argumentName">The name of the argument being considered.</param>
 092        ///
 093        /// <exception cref="ArgumentOutOfRangeException"><paramref name="argumentValue"/> is less than <paramref name="
 94        ///
 095        public static void AssertAtLeast(long argumentValue, long minimumValue, string argumentName)
 96        {
 14497            if (argumentValue < minimumValue)
 98            {
 2499                throw new ArgumentOutOfRangeException(argumentName, $"The value supplied must be greater than or equal t
 100            }
 120101        }
 102
 103        /// <summary>
 104        ///   Ensures that an instance has not been disposed, throwing an
 105        ///   <see cref="ObjectDisposedException" /> if that invariant is not met.
 106        /// </summary>
 107        ///
 108        /// <param name="wasDisposed"><c>true</c> if the target instance has been disposed; otherwise, <c>false</c>.</pa
 109        /// <param name="targetName">The name of the target instance that is being verified.</param>
 110        ///
 111        public static void AssertNotDisposed(bool wasDisposed, string targetName)
 5840112        {
 106113            if (wasDisposed)
 98114            {
 2115                throw new ObjectDisposedException(targetName, string.Format(CultureInfo.CurrentCulture, Resources.Closed
 116            }
 5846117        }
 118
 96119        /// <summary>
 120        ///   Ensures that an instance has not been closed, throwing an
 5646121        ///   <see cref="EventHubsException" /> if that invariant is not met.
 122        /// </summary>
 123        ///
 124        /// <param name="wasClosed"><c>true</c> if the target instance has been closed; otherwise, <c>false</c>.</param>
 125        /// <param name="targetName">The name of the target instance that is being verified.</param>
 126        ///
 127        /// <exception cref="EventHubsException"><paramref name="wasClosed"/> is <c>true</c>.</exception>
 128        ///
 129        public static void AssertNotClosed(bool wasClosed, string targetName)
 130        {
 480131            if (wasClosed)
 132            {
 32133                throw new EventHubsException(targetName, string.Format(CultureInfo.CurrentCulture, Resources.ClosedInsta
 134            }
 448135        }
 836136
 137        /// <summary>
 0138        ///   Ensures that an argument's value is a well-formed Event Hubs fully qualified namespace value,
 139        ///   throwing a <see cref="ArgumentException" /> if that invariant is not met.
 140        /// </summary>
 836141        ///
 142        /// <param name="argumentValue">The argument value.</param>
 0143        /// <param name="argumentName">Name of the argument.</param>
 144        ///
 836145        ///
 146        /// <exception cref="ArgumentException"><paramref name="argumentValue"/> is not a well-formed Event Hubs fully q
 147        ///
 148        public static void AssertWellFormedEventHubsNamespace(string argumentValue, string argumentName)
 149        {
 564150            argumentValue ??= string.Empty;
 151
 564152            if (Uri.CheckHostName(argumentValue) == UriHostNameType.Unknown)
 153            {
 30154                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Resources.InvalidFullyQualifiedN
 155            }
 534156        }
 157    }
 0158}