• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentRest / 13399786835

18 Feb 2025 08:48PM UTC coverage: 57.756% (-0.4%) from 58.107%
13399786835

push

github

pwelter34
add support for nullable, minor cleanup, improve UrlBuilder

293 of 646 branches covered (45.36%)

Branch coverage included in aggregate %.

238 of 380 new or added lines in 20 files covered. (62.63%)

3 existing lines in 3 files now uncovered.

824 of 1288 relevant lines covered (63.98%)

56.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.91
/src/FluentRest/DictionaryExtensions.cs
1
using System.Diagnostics.CodeAnalysis;
2

3
namespace FluentRest;
4

5
/// <summary>
6
/// Extension methods for dictionary
7
/// </summary>
8
public static class DictionaryExtensions
9
{
10
    /// <summary>
11
    /// Adds a key/value pair to the dictionary if the key does not already exist.
12
    /// </summary>
13
    /// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
14
    /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
15
    /// <param name="dictionary">The dictionary.</param>
16
    /// <param name="key">The key of the element to add.</param>
17
    /// <param name="valueFactory">The function used to generate a value for the key.</param>
18
    /// <returns>
19
    /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value for the key as returned by valueFactory if the key was not in the dictionary.
20
    /// </returns>
21
    /// <exception cref="ArgumentNullException"><paramref name="key" /> is <see langword="null" /></exception>
22
    public static TValue GetOrAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory)
23
    {
24
        if (key is null)
72!
25
            throw new ArgumentNullException(nameof(key));
×
26

27
        if (dictionary.TryGetValue(key, out var value))
72!
28
            return value;
×
29

30
        var factoryValue = valueFactory(key);
72✔
31
        dictionary.Add(key, factoryValue);
72✔
32

33
        return factoryValue;
72✔
34
    }
35

36
    /// <summary>
37
    /// Attempts to add the specified key and value to the Dictionary.
38
    /// </summary>
39
    /// <typeparam name="TKey">The type of the key.</typeparam>
40
    /// <typeparam name="TValue">The type of the value.</typeparam>
41
    /// <param name="dictionary">The dictionary to perform an action upon.</param>
42
    /// <param name="key">The key of the element to add.</param>
43
    /// <param name="value">The value of the element to add.</param>
44
    /// <returns>
45
    ///   <c>true</c> if the key/value pair was added to the Dictionary successfully. If the key already exists, this method returns <c>false</c>.
46
    /// </returns>
47
    /// <exception cref="ArgumentNullException"><paramref name="key" /> is <see langword="null" /></exception>
48
    public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
49
    {
NEW
50
        if (key is null)
×
51
            throw new ArgumentNullException(nameof(key));
×
52

53
        if (dictionary.ContainsKey(key))
×
54
            return false;
×
55

56
        dictionary.Add(key, value);
×
57
        return true;
×
58
    }
59

60
    /// <summary>
61
    /// Attempts to remove and return the value with the specified key from the Dictionary.
62
    /// </summary>
63
    /// <typeparam name="TKey">The type of the key.</typeparam>
64
    /// <typeparam name="TValue">The type of the value.</typeparam>
65
    /// <param name="dictionary">The Dictionary to perform an action upon.</param>
66
    /// <param name="key">The key of the element to remove and return.</param>
67
    /// <param name="value">When this method returns, value contains the object removed from the Dictionary or the default value if the operation failed.</param>
68
    /// <returns><c>true</c> if an object was removed successfully; otherwise, <c>false</c>.</returns>
69
    /// <exception cref="ArgumentNullException"><paramref name="key" /> is <see langword="null" /></exception>
70
    public static bool TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, [MaybeNullWhen(false)] out TValue? value)
71
    {
NEW
72
        if (key is null)
×
73
            throw new ArgumentNullException(nameof(key));
×
74

75
        if (!dictionary.TryGetValue(key, out value))
×
76
            return false;
×
77

78
        return dictionary.Remove(key);
×
79
    }
80

81
    /// <summary>
82
    /// Compares the existing value for the specified key with a specified value, and if they are equal, updates the key with a third value.
83
    /// </summary>
84
    /// <typeparam name="TKey">The type of the key.</typeparam>
85
    /// <typeparam name="TValue">The type of the value.</typeparam>
86
    /// <param name="dictionary">The dictionary to perform an action upon.</param>
87
    /// <param name="key">The key whose value is compared with comparisonValue and possibly replaced.</param>
88
    /// <param name="newValue">The value that replaces the value of the element with key if the comparison results in equality.</param>
89
    /// <param name="comparisonValue">The value that is compared to the value of the element with key.</param>
90
    /// <returns><c>true</c> if the value with key was equal to comparisonValue and replaced with newValue; otherwise, <c>false</c>.</returns>
91
    /// <exception cref="System.ArgumentNullException">Thrown if key is a <c>null</c> reference </exception>
92
    public static bool TryUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue newValue, TValue comparisonValue)
93
    {
NEW
94
        if (key is null)
×
95
            throw new ArgumentNullException(nameof(key));
×
96

97
        if (!dictionary.TryGetValue(key, out var value))
×
98
            return false;
×
99

100
        if (!Equals(value, comparisonValue))
×
101
            return false;
×
102

103
        dictionary[key] = newValue;
×
104
        return true;
×
105
    }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc