< Summary

Information
Class: IceRpc.Features.FeatureCollection
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Features/FeatureCollection.cs
Tag: 275_13775359185
Line coverage
97%
Covered lines: 36
Uncovered lines: 1
Coverable lines: 37
Total lines: 81
Line coverage: 97.2%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage
90%
Covered methods: 9
Total methods: 10
Method coverage: 90%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Empty()100%11100%
get_IsReadOnly()100%11100%
.ctor()100%11100%
get_Item(...)100%44100%
set_Item(...)100%22100%
.ctor(...)100%22100%
Get()100%22100%
System.Collections.IEnumerable.GetEnumerator()100%210%
GetEnumerator()100%88100%
Set(...)100%11100%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Features/FeatureCollection.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Collections;
 4
 5namespace IceRpc.Features;
 6
 7/// <summary>Default implementation of <see cref="IFeatureCollection" />.</summary>
 8public class FeatureCollection : IFeatureCollection
 9{
 10    /// <summary>Gets a shared empty read-only instance.</summary>
 11    /// <value>An empty <see cref="FeatureCollection" />.</value>
 1173112    public static IFeatureCollection Empty { get; } = new FeatureCollection().AsReadOnly();
 13
 14    /// <summary>Gets a value indicating whether this feature collection is read-only or read-write.</summary>
 15    /// <value>Always <see langword="false" /> with this class.</value>
 15016    public bool IsReadOnly => false;
 17
 18    private readonly IFeatureCollection? _defaults;
 22819    private readonly Dictionary<Type, object> _features = new();
 20
 21    /// <inheritdoc/>
 22    public object? this[Type key]
 23    {
 90924        get => _features.TryGetValue(key, out object? value) ? value : _defaults?[key];
 25
 26        set
 32227        {
 32228            if (value is null)
 329            {
 330                _ = _features.Remove(key);
 331            }
 32            else
 31933            {
 31934                _features[key] = value;
 31935            }
 32236        }
 37    }
 38
 39    /// <summary>Constructs an empty read-write feature collection.</summary>
 4340    public FeatureCollection()
 4341    {
 4342    }
 43
 44    /// <summary>Constructs an empty read-write feature collection with defaults.</summary>
 45    /// <param name="defaults">The feature collection that provide default values.</param>
 18546    public FeatureCollection(IFeatureCollection defaults) =>
 47        // no need to query the empty read-only collection for defaults; any other feature collection (even empty and
 48        // read-only) can change over time
 18549        _defaults = defaults == Empty ? null : defaults;
 50
 51    /// <inheritdoc/>
 90552    public TFeature? Get<TFeature>() => this[typeof(TFeature)] is object value ? (TFeature)value : default;
 53
 54    /// <inheritdoc />
 055    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 56
 57    /// <summary>Returns an enumerator that iterates over this feature collection.</summary>
 58    /// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through this feature collection.
 59    /// </returns>
 60    public IEnumerator<KeyValuePair<Type, object>> GetEnumerator()
 1061    {
 5062        foreach (KeyValuePair<Type, object> pair in _features)
 1063        {
 1064            yield return pair;
 1065        }
 66
 1067        if (_defaults is not null)
 668        {
 4269            foreach (KeyValuePair<Type, object> pair in _defaults)
 1270            {
 1271                if (!_features.ContainsKey(pair.Key))
 872                {
 873                    yield return pair;
 874                }
 1275            }
 676        }
 1077    }
 78
 79    /// <inheritdoc/>
 31080    public void Set<TFeature>(TFeature? feature) => this[typeof(TFeature)] = feature;
 81}