< Summary

Class:Azure.ETag
Assembly:Azure.Core
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\ETag.cs
Covered lines:16
Uncovered lines:1
Coverable lines:17
Total lines:99
Line coverage:94.1% (16 of 17)
Covered branches:12
Total branches:14
Branch coverage:85.7% (12 of 14)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
op_Equality(...)-100%100%
op_Inequality(...)-100%100%
.cctor()-100%100%
Equals(...)-100%100%
Equals(...)-0%100%
Equals(...)-100%50%
GetHashCode()-100%100%
ToString()-100%50%
Parse(...)-100%100%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5
 6namespace Azure
 7{
 8    /// <summary>
 9    /// Represents an HTTP ETag.
 10    /// </summary>
 11    public readonly struct ETag : IEquatable<ETag>
 12    {
 13        private const char QuoteCharacter = '"';
 14        private const string QuoteString = "\"";
 15
 16        private readonly string _value;
 17
 18        /// <summary>
 19        /// Creates a new instance of <see cref="ETag"/>.
 20        /// </summary>
 21        /// <param name="etag">The string value of the ETag.</param>
 4622        public ETag(string etag) => _value = etag;
 23
 24        /// <summary>
 25        /// Compares equality of two <see cref="ETag"/> instances.
 26        /// </summary>
 27        /// <param name="left">The <see cref="ETag"/> to compare.</param>
 28        /// <param name="right">The <see cref="ETag"/> to compare to.</param>
 29        /// <returns><c>true</c> if values of both ETags are equal, otherwise <c>false</c>.</returns>
 830        public static bool operator ==(ETag left, ETag right) => left.Equals(right);
 31
 32        /// <summary>
 33        /// Compares inequality of two <see cref="ETag"/> instances.
 34        /// </summary>
 35        /// <param name="left">The <see cref="ETag"/> to compare.</param>
 36        /// <param name="right">The <see cref="ETag"/> to compare to.</param>
 37        /// <returns><c>true</c> if values of both ETags are not equal, otherwise <c>false</c>.</returns>
 438        public static bool operator !=(ETag left, ETag right) => !left.Equals(right);
 39
 40        /// <summary>
 41        /// Instance of <see cref="ETag"/> with the value. <code>*</code>
 42        /// </summary>
 243        public static readonly ETag All = new ETag("*");
 44
 45        /// <inheritdoc />
 46        public bool Equals(ETag other)
 47        {
 2648            return string.Equals(_value, other._value, StringComparison.Ordinal);
 49        }
 50        /// <summary>
 51        /// Indicates whether the value of current <see cref="ETag"/> is equal to the provided string.</summary>
 52        /// <param name="other">An object to compare with this object.</param>
 53        /// <returns>true if the current object is equal to the <paramref name="other">other</paramref> parameter; other
 54        public bool Equals(string? other)
 55        {
 056            return string.Equals(_value, other, StringComparison.Ordinal);
 57        }
 58
 59        /// <inheritdoc />
 60        public override bool Equals(object? obj)
 61        {
 662            return (obj is ETag other) && Equals(other);
 63        }
 64
 65        /// <inheritdoc />
 66        public override int GetHashCode()
 67        {
 1268            return _value?.GetHashCode() ?? 0;
 69        }
 70
 71        /// <summary>
 72        ///
 73        /// </summary>
 74        /// <returns>The string representation of this <see cref="ETag"/>.</returns>
 75        public override string ToString()
 76        {
 877            return _value ?? "<null>";
 78        }
 79
 80        internal static ETag Parse(string value)
 81        {
 1282            if (value == All._value)
 83            {
 284                return All;
 85            }
 1086            else if (value.StartsWith("W/", StringComparison.Ordinal))
 87            {
 288                throw new NotSupportedException("Weak ETags are not supported.");
 89            }
 890            else if (!value.StartsWith(QuoteString, StringComparison.Ordinal) ||
 891                     !value.EndsWith(QuoteString, StringComparison.Ordinal))
 92            {
 293                throw new ArgumentException("The value should be equal to * or be wrapped in quotes", nameof(value));
 94            }
 95
 696            return new ETag(value.Trim(QuoteCharacter));
 97        }
 98    }
 99}