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

loresoft / FluentRest / 10834435986

12 Sep 2024 03:58PM UTC coverage: 58.107% (-1.1%) from 59.221%
10834435986

push

github

pwelter34
convert to HttpUtility.ParseQueryString

264 of 594 branches covered (44.44%)

Branch coverage included in aggregate %.

10 of 10 new or added lines in 1 file covered. (100.0%)

1 existing line in 1 file now uncovered.

829 of 1287 relevant lines covered (64.41%)

72.01 hits per line

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

15.91
/src/FluentRest/DictionaryExtensions.cs
1
namespace FluentRest;
2

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

25
        if (dictionary.TryGetValue(key, out var value))
108!
UNCOV
26
            return value;
×
27

28
        value = valueFactory(key);
108✔
29
        dictionary.Add(key, value);
108✔
30

31
        return value;
108✔
32
    }
33

34
    /// <summary>
35
    /// Attempts to add the specified key and value to the Dictionary.
36
    /// </summary>
37
    /// <typeparam name="TKey">The type of the key.</typeparam>
38
    /// <typeparam name="TValue">The type of the value.</typeparam>
39
    /// <param name="dictionary">The dictionary to perform an action upon.</param>
40
    /// <param name="key">The key of the element to add.</param>
41
    /// <param name="value">The value of the element to add.</param>
42
    /// <returns>
43
    ///   <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>.
44
    /// </returns>
45
    /// <exception cref="ArgumentNullException"><paramref name="key" /> is <see langword="null" /></exception>
46
    public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
47
    {
48
        if (key == null)
×
49
            throw new ArgumentNullException(nameof(key));
×
50

51
        if (dictionary.ContainsKey(key))
×
52
            return false;
×
53

54
        dictionary.Add(key, value);
×
55
        return true;
×
56
    }
57

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

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

76
        return dictionary.Remove(key);
×
77
    }
78

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

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

98
        if (!Equals(value, comparisonValue))
×
99
            return false;
×
100

101
        dictionary[key] = newValue;
×
102
        return true;
×
103
    }
104
}
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

© 2026 Coveralls, Inc