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

loresoft / FluentRest / 7318593291

25 Dec 2023 02:55AM CUT coverage: 52.206%. Remained the same
7318593291

Pull #158

github

web-flow
Merge 38e3e6116 into 7b58509ff
Pull Request #158: Bump xunit.runner.visualstudio from 2.5.4 to 2.5.6

182 of 438 branches covered (0.0%)

Branch coverage included in aggregate %.

516 of 899 relevant lines covered (57.4%)

86.76 hits per line

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

20.45
/src/FluentRest/DictionaryExtensions.cs
1
using System;
2
using System.Collections.Generic;
3

4
namespace FluentRest;
5

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

28
        if (dictionary.TryGetValue(key, out var value))
1,773✔
29
            return value;
1,179✔
30

31
        value = valueFactory(key);
594✔
32
        dictionary.Add(key, value);
594✔
33

34
        return value;
594✔
35
    }
36

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

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

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

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

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

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

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

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

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

104
        dictionary[key] = newValue;
×
105
        return true;
×
106
    }
107
}
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