< Summary

Line coverage
71%
Covered lines: 41
Uncovered lines: 16
Coverable lines: 57
Total lines: 131
Line coverage: 71.9%
Branch coverage
50%
Covered branches: 8
Total branches: 16
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
ToString()0%440%
GetEnumerator()100%11100%
System.Collections.Generic.IEnumerable<System.String>.GetEnumerator()100%110%
System.Collections.IEnumerable.GetEnumerator()100%110%
.ctor(...)100%22100%
MoveNext()100%66100%
Dispose()100%11100%
System.Collections.IEnumerator.Reset()100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderStringValues.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Collections;
 5using System.Collections.Generic;
 6
 7namespace System.Net.Http.Headers
 8{
 9    /// <summary>Provides a collection of header string values.</summary>
 10    public readonly struct HeaderStringValues : IReadOnlyCollection<string>
 11    {
 12        /// <summary>The associated header.  This is used only for producing a string from <see cref="_value"/> when it'
 13        private readonly HeaderDescriptor _header;
 14        /// <summary>A string or string array (or null if the instance is default).</summary>
 15        private readonly object _value;
 16
 17        /// <summary>Initializes the instance.</summary>
 18        /// <param name="descriptor">The header descriptor associated with the header value.</param>
 19        /// <param name="value">The header value.</param>
 20        internal HeaderStringValues(HeaderDescriptor descriptor, string value)
 44721        {
 44722            _header = descriptor;
 44723            _value = value;
 44724        }
 25
 26        /// <summary>Initializes the instance.</summary>
 27        /// <param name="descriptor">The header descriptor associated with the header values.</param>
 28        /// <param name="values">The header values.</param>
 29        internal HeaderStringValues(HeaderDescriptor descriptor, string[] values)
 511830        {
 511831            _header = descriptor;
 511832            _value = values;
 511833        }
 34
 35        /// <summary>Gets the number of header values in the collection.</summary>
 036        public int Count => _value switch
 037        {
 038            string => 1,
 039            string[] values => values.Length,
 040            _ => 0
 041        };
 42
 43        /// <summary>Gets a string containing all the headers in the collection.</summary>
 44        /// <returns></returns>
 045        public override string ToString() => _value switch
 046        {
 047            string value => value,
 048            string[] values => string.Join(_header.Separator, values),
 049            _ => string.Empty,
 050        };
 51
 52        /// <summary>Gets an enumerator for all of the strings in the collection.</summary>
 53        /// <returns></returns>
 556554        public Enumerator GetEnumerator() => new Enumerator(_value);
 55
 56        /// <inheritdoc/>
 057        IEnumerator<string> IEnumerable<string>.GetEnumerator() => GetEnumerator();
 58
 59        /// <inheritdoc/>
 060        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 61
 62        /// <summary>Enumerates the elements of a <see cref="HeaderStringValues"/>.</summary>
 63        public struct Enumerator : IEnumerator<string>
 64        {
 65            /// <summary>If this wraps a string[], that array. Otherwise, null.</summary>
 66            private readonly string[]? _values;
 67            /// <summary>The current string header value.  If this wraps a single string, that string.</summary>
 68            private string? _current;
 69            /// <summary>Current state of the iteration.</summary>
 70            private int _index;
 71
 72            /// <summary>Initializes the enumerator with a string or string[].</summary>
 73            /// <param name="value">The string or string[] value, or null if this collection is empty.</param>
 74            internal Enumerator(object value)
 556575            {
 556576                if (value is string s)
 44777                {
 44778                    _values = null;
 44779                    _current = s;
 44780                }
 81                else
 511882                {
 511883                    _values = value as string[];
 511884                    _current = null;
 511885                }
 86
 556587                _index = 0;
 556588            }
 89
 90            /// <inheritdoc/>
 91            public bool MoveNext()
 8436092            {
 8436093                int index = _index;
 8436094                if (index < 0)
 44795                {
 44796                    return false;
 97                }
 98
 8391399                string[]? values = _values;
 83913100                if (values != null)
 83466101                {
 83466102                    if ((uint)index < (uint)values.Length)
 78348103                    {
 78348104                        _index = index + 1;
 78348105                        _current = values[index];
 78348106                        return true;
 107                    }
 108
 5118109                    _index = -1;
 5118110                    return false;
 111                }
 112
 447113                _index = -1;
 447114                return _current != null;
 84360115            }
 116
 117            /// <inheritdoc/>
 78795118            public string Current => _current!;
 119
 120            /// <inheritdoc/>
 0121            object IEnumerator.Current => Current;
 122
 123            /// <inheritdoc/>
 11130124            public void Dispose() { }
 125
 126            /// <inheritdoc/>
 0127            void IEnumerator.Reset() => throw new NotSupportedException();
 128        }
 129    }
 130}
 131