< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Utilities.ReadOnlyDictionaryAdapter
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/ReadOnlyDictionaryAdapter.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
81%
Covered lines: 18
Uncovered lines: 4
Coverable lines: 22
Total lines: 94
Line coverage: 81.8%
Branch coverage
70%
Covered branches: 14
Total branches: 20
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
get_Item(...)100%22100%
get_Keys()50%44100%
get_Values()100%11100%
get_Count()100%210%
ContainsKey(...)100%22100%
TryGetValue(...)75%4475%
GetEnumerator()66.66%66100%
System.Collections.IEnumerable.GetEnumerator()100%210%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/ReadOnlyDictionaryAdapter.cs

#LineLine coverage
 1using System.Collections;
 2
 3namespace Kestrun.Utilities;
 4
 5/// <summary>
 6/// Adapts a non-generic IDictionary to a read-only dictionary with string keys and nullable object values.
 7/// </summary>
 8/// <remarks>
 9/// Initializes a new instance of the <see cref="ReadOnlyDictionaryAdapter"/> class with the specified non-generic dicti
 10/// </remarks>
 11/// <param name="inner">The non-generic <see cref="IDictionary"/> to adapt.</param>
 12/// <exception cref="ArgumentNullException">Thrown when <paramref name="inner"/> is null.</exception>
 413public sealed class ReadOnlyDictionaryAdapter(IDictionary inner) : IReadOnlyDictionary<string, object?>
 14{
 415    private readonly IDictionary _inner = inner ?? throw new ArgumentNullException(nameof(inner));
 16
 17    /// <summary>
 18    /// Gets the value associated with the specified string key, or null if the key does not exist.
 19    /// </summary>
 20    /// <param name="key">The string key whose value to get.</param>
 21    /// <returns>The value associated with the specified key, or null if the key is not found.</returns>
 22    public object? this[string key]
 23    {
 24        get
 25        {
 626            ArgumentNullException.ThrowIfNull(key);
 627            return _inner.Contains(key) ? _inner[key] : null;
 28        }
 29    }
 30
 31    /// <summary>
 32    /// Gets an enumerable collection of the string keys in the adapted dictionary.
 33    /// </summary>
 34    public IEnumerable<string> Keys =>
 1035        _inner.Keys.Cast<object>().Select(k => k?.ToString() ?? string.Empty);
 36
 37    /// <summary>
 38    /// Gets an enumerable collection of the values in the adapted dictionary.
 39    /// </summary>
 40    public IEnumerable<object?> Values =>
 541        Keys.Select(k => this[k]);
 42
 43    /// <summary>
 44    /// Gets the number of key/value pairs contained in the adapted dictionary.
 45    /// </summary>
 046    public int Count => _inner.Count;
 47
 48    /// <summary>
 49    /// Determines whether the adapted dictionary contains an element with the specified string key.
 50    /// </summary>
 51    /// <param name="key">The string key to locate in the dictionary.</param>
 52    /// <returns>true if the dictionary contains an element with the specified key; otherwise, false.</returns>
 253    public bool ContainsKey(string key) => key is not null && _inner.Contains(key);
 54
 55    /// <summary>
 56    /// Attempts to get the value associated with the specified string key.
 57    /// </summary>
 58    /// <param name="key">The string key whose value to get.</param>
 59    /// <param name="value">When this method returns, contains the value associated with the specified key, if the key i
 60    /// <returns>true if the dictionary contains an element with the specified key; otherwise, false.</returns>
 61    public bool TryGetValue(string key, out object? value)
 62    {
 263        if (key is null)
 64        {
 065            value = null;
 066            return false;
 67        }
 68
 269        if (_inner.Contains(key))
 70        {
 171            value = _inner[key];
 172            return true;
 73        }
 74
 175        value = null;
 176        return false;
 77    }
 78
 79    /// <summary>
 80    /// Returns an enumerator that iterates through the key/value pairs in the adapted dictionary.
 81    /// </summary>
 82    /// <returns>An enumerator for the key/value pairs.</returns>
 83    public IEnumerator<KeyValuePair<string, object?>> GetEnumerator()
 84    {
 1085        foreach (DictionaryEntry entry in _inner)
 86        {
 387            var k = entry.Key?.ToString()
 388                       ?? throw new InvalidOperationException("Underlying dictionary contains a null key.");
 389            yield return new KeyValuePair<string, object?>(k, entry.Value);
 90        }
 291    }
 92
 093    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 94}