Skip to content

Commit 9e848ed

Browse files
Merge pull request #45 from sharpcode-it/engineering87-develop
Add new Dictionary extension methods
2 parents 887cc76 + 9acd0d3 commit 9e848ed

File tree

3 files changed

+221
-17
lines changed

3 files changed

+221
-17
lines changed

SharpHelpers/SharpHelpers.UnitTest/Dictionary/DictionaryTest.cs

Lines changed: 163 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,184 @@
44
using SharpCoding.SharpHelpers;
55
using System;
66
using System.Collections.Generic;
7-
using System.Linq;
87

98
namespace SharpHelpers.UnitTest.Dictionary
109
{
11-
1210
[TestClass]
1311
public class DictionaryTest
1412
{
1513
[TestMethod]
16-
public void TestAddFormat()
14+
public void AddFormat_ShouldAddFormattedStringToDictionary()
1715
{
18-
var dic = new Dictionary<int, DateTime>() { { 1, DateTime.Now }, { 2, DateTime.Now } };
19-
16+
// Arrange
17+
var dictionary = new Dictionary<int, string>();
18+
19+
// Act
20+
dictionary.AddFormat(1, "Hello, {0}!", "World");
21+
22+
// Assert
23+
Assert.AreEqual("Hello, World!", dictionary[1]);
2024
}
25+
2126
[TestMethod]
22-
public void TestRemoveAll()
27+
[ExpectedException(typeof(ArgumentNullException))]
28+
public void AddFormat_ShouldThrowArgumentNullException_WhenDictionaryIsNull()
2329
{
24-
var dic = new Dictionary<int, DateTime>() { { 1, DateTime.Now }, { 2, DateTime.Now } };
25-
dic.RemoveAll(a=> a.Key < 2 );
26-
Assert.IsTrue(dic.Count() == 1);
30+
// Arrange
31+
Dictionary<int, string> dictionary = null;
2732

33+
// Act
34+
dictionary.AddFormat(1, "Hello, {0}!", "World");
35+
36+
// Assert - [ExpectedException] handles the assertion
2837
}
38+
2939
[TestMethod]
30-
public void TestGetOrCreate()
40+
public void RemoveAll_ShouldRemoveItemsBasedOnCondition()
3141
{
32-
var dic = new Dictionary<int,DateTime>() { { 1, DateTime.Now }, { 2, DateTime.Now} };
33-
Assert.IsNotNull(dic.GetOrCreate(1));
34-
Assert.IsNotNull(dic.GetOrCreate(3));
42+
// Arrange
43+
var dictionary = new Dictionary<int, string>
44+
{
45+
{ 1, "Apple" },
46+
{ 2, "Banana" },
47+
{ 3, "Avocado" }
48+
};
3549

36-
50+
// Act
51+
dictionary.RemoveAll(kvp => kvp.Value.StartsWith("A"));
52+
53+
// Assert
54+
Assert.AreEqual(1, dictionary.Count);
55+
Assert.IsTrue(dictionary.ContainsKey(2));
3756
}
38-
}
3957

40-
}
58+
[TestMethod]
59+
[ExpectedException(typeof(ArgumentNullException))]
60+
public void RemoveAll_ShouldThrowArgumentNullException_WhenDictionaryIsNull()
61+
{
62+
// Arrange
63+
Dictionary<int, string> dictionary = null;
64+
65+
// Act
66+
dictionary.RemoveAll(kvp => kvp.Value.StartsWith("A"));
67+
68+
// Assert - [ExpectedException] handles the assertion
69+
}
70+
71+
[TestMethod]
72+
public void GetOrCreate_ShouldCreateAndReturnNewValue_WhenKeyDoesNotExist()
73+
{
74+
// Arrange
75+
var dictionary = new Dictionary<int, List<string>>();
76+
77+
// Act
78+
var result = dictionary.GetOrCreate(1);
79+
80+
// Assert
81+
Assert.IsNotNull(result);
82+
Assert.AreEqual(0, result.Count);
83+
Assert.IsTrue(dictionary.ContainsKey(1));
84+
}
85+
86+
[TestMethod]
87+
public void AddOrUpdate_ShouldAddNewItem_WhenKeyDoesNotExist()
88+
{
89+
// Arrange
90+
var dictionary = new Dictionary<int, string>();
91+
92+
// Act
93+
dictionary.AddOrUpdate(1, "NewValue");
94+
95+
// Assert
96+
Assert.AreEqual("NewValue", dictionary[1]);
97+
}
98+
99+
[TestMethod]
100+
public void AddOrUpdate_ShouldUpdateExistingItem_WhenKeyExists()
101+
{
102+
// Arrange
103+
var dictionary = new Dictionary<int, string>
104+
{
105+
{ 1, "OldValue" }
106+
};
107+
108+
// Act
109+
dictionary.AddOrUpdate(1, "UpdatedValue");
110+
111+
// Assert
112+
Assert.AreEqual("UpdatedValue", dictionary[1]);
113+
}
114+
115+
[TestMethod]
116+
public void RemoveIfExists_ShouldReturnTrueAndRemoveItem_WhenKeyExists()
117+
{
118+
// Arrange
119+
var dictionary = new Dictionary<int, string>
120+
{
121+
{ 1, "Value" }
122+
};
123+
124+
// Act
125+
var result = dictionary.RemoveIfExists(1);
126+
127+
// Assert
128+
Assert.IsTrue(result);
129+
Assert.IsFalse(dictionary.ContainsKey(1));
130+
}
131+
132+
[TestMethod]
133+
public void RemoveIfExists_ShouldReturnFalse_WhenKeyDoesNotExist()
134+
{
135+
// Arrange
136+
var dictionary = new Dictionary<int, string>();
137+
138+
// Act
139+
var result = dictionary.RemoveIfExists(1);
140+
141+
// Assert
142+
Assert.IsFalse(result);
143+
}
144+
145+
[TestMethod]
146+
public void Merge_ShouldMergeDictionariesAndUpdateValues()
147+
{
148+
// Arrange
149+
var dictionary = new Dictionary<int, string>
150+
{
151+
{ 1, "Value1" },
152+
{ 2, "Value2" }
153+
};
154+
155+
var otherDictionary = new Dictionary<int, string>
156+
{
157+
{ 2, "UpdatedValue2" },
158+
{ 3, "Value3" }
159+
};
160+
161+
// Act
162+
dictionary.Merge(otherDictionary);
163+
164+
// Assert
165+
Assert.AreEqual(3, dictionary.Count);
166+
Assert.AreEqual("UpdatedValue2", dictionary[2]);
167+
Assert.AreEqual("Value3", dictionary[3]);
168+
}
169+
170+
[TestMethod]
171+
public void ToReadableString_ShouldReturnFormattedStringRepresentationOfDictionary()
172+
{
173+
// Arrange
174+
var dictionary = new Dictionary<int, string>
175+
{
176+
{ 1, "Value1" },
177+
{ 2, "Value2" }
178+
};
179+
180+
// Act
181+
var result = dictionary.ToReadableString();
182+
183+
// Assert
184+
Assert.AreEqual("{1: Value1, 2: Value2}", result);
185+
}
186+
}
187+
}

SharpHelpers/SharpHelpers.UnitTest/SharpHelpers.UnitTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0" />
1111
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
1212
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
1313
</ItemGroup>

SharpHelpers/SharpHelpers/DictionaryHelper.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,63 @@ public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> di
6363
dictionary[key] = ret;
6464
}
6565
return ret;
66+
}
67+
68+
/// <summary>
69+
/// Tries to add a key-value pair to the dictionary. If the key already exists, it updates the value.
70+
/// </summary>
71+
/// <param name="dictionary">The dictionary to operate on.</param>
72+
/// <param name="key">The key to add or update.</param>
73+
/// <param name="value">The value to associate with the key.</param>
74+
public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
75+
{
76+
if (dictionary.ContainsKey(key))
77+
{
78+
dictionary[key] = value;
79+
}
80+
else
81+
{
82+
dictionary.Add(key, value);
83+
}
84+
}
85+
86+
/// <summary>
87+
/// Removes the entry with the specified key if it exists in the dictionary, and returns a boolean indicating success.
88+
/// </summary>
89+
/// <param name="dictionary">The dictionary to operate on.</param>
90+
/// <param name="key">The key to remove.</param>
91+
/// <returns>True if the key was found and removed, otherwise false.</returns>
92+
public static bool RemoveIfExists<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key)
93+
{
94+
return dictionary.Remove(key);
95+
}
96+
97+
/// <summary>
98+
/// Merges the entries from another dictionary into the current dictionary. If a key already exists, its value is updated.
99+
/// </summary>
100+
/// <param name="dictionary">The dictionary to operate on.</param>
101+
/// <param name="otherDictionary">The dictionary to merge from.</param>
102+
public static void Merge<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, Dictionary<TKey, TValue> otherDictionary)
103+
{
104+
foreach (var kvp in otherDictionary)
105+
{
106+
dictionary.AddOrUpdate(kvp.Key, kvp.Value);
107+
}
108+
}
109+
110+
/// <summary>
111+
/// Converts the dictionary into a readable string format, useful for debugging.
112+
/// </summary>
113+
/// <param name="dictionary">The dictionary to convert to a string.</param>
114+
/// <returns>A string representation of the dictionary.</returns>
115+
public static string ToReadableString<TKey, TValue>(this Dictionary<TKey, TValue> dictionary)
116+
{
117+
var entries = new List<string>();
118+
foreach (var kvp in dictionary)
119+
{
120+
entries.Add($"{kvp.Key}: {kvp.Value}");
121+
}
122+
return "{" + string.Join(", ", entries) + "}";
66123
}
67124
}
68125
}

0 commit comments

Comments
 (0)