diff --git a/SharpHelpers/SharpHelpers.UnitTest/Dictionary/DictionaryTest.cs b/SharpHelpers/SharpHelpers.UnitTest/Dictionary/DictionaryTest.cs index 6e63f7e..27535ae 100644 --- a/SharpHelpers/SharpHelpers.UnitTest/Dictionary/DictionaryTest.cs +++ b/SharpHelpers/SharpHelpers.UnitTest/Dictionary/DictionaryTest.cs @@ -4,37 +4,184 @@ using SharpCoding.SharpHelpers; using System; using System.Collections.Generic; -using System.Linq; namespace SharpHelpers.UnitTest.Dictionary { - [TestClass] public class DictionaryTest { [TestMethod] - public void TestAddFormat() + public void AddFormat_ShouldAddFormattedStringToDictionary() { - var dic = new Dictionary() { { 1, DateTime.Now }, { 2, DateTime.Now } }; - + // Arrange + var dictionary = new Dictionary(); + + // Act + dictionary.AddFormat(1, "Hello, {0}!", "World"); + + // Assert + Assert.AreEqual("Hello, World!", dictionary[1]); } + [TestMethod] - public void TestRemoveAll() + [ExpectedException(typeof(ArgumentNullException))] + public void AddFormat_ShouldThrowArgumentNullException_WhenDictionaryIsNull() { - var dic = new Dictionary() { { 1, DateTime.Now }, { 2, DateTime.Now } }; - dic.RemoveAll(a=> a.Key < 2 ); - Assert.IsTrue(dic.Count() == 1); + // Arrange + Dictionary dictionary = null; + // Act + dictionary.AddFormat(1, "Hello, {0}!", "World"); + + // Assert - [ExpectedException] handles the assertion } + [TestMethod] - public void TestGetOrCreate() + public void RemoveAll_ShouldRemoveItemsBasedOnCondition() { - var dic = new Dictionary() { { 1, DateTime.Now }, { 2, DateTime.Now} }; - Assert.IsNotNull(dic.GetOrCreate(1)); - Assert.IsNotNull(dic.GetOrCreate(3)); + // Arrange + var dictionary = new Dictionary + { + { 1, "Apple" }, + { 2, "Banana" }, + { 3, "Avocado" } + }; - + // Act + dictionary.RemoveAll(kvp => kvp.Value.StartsWith("A")); + + // Assert + Assert.AreEqual(1, dictionary.Count); + Assert.IsTrue(dictionary.ContainsKey(2)); } - } -} + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void RemoveAll_ShouldThrowArgumentNullException_WhenDictionaryIsNull() + { + // Arrange + Dictionary dictionary = null; + + // Act + dictionary.RemoveAll(kvp => kvp.Value.StartsWith("A")); + + // Assert - [ExpectedException] handles the assertion + } + + [TestMethod] + public void GetOrCreate_ShouldCreateAndReturnNewValue_WhenKeyDoesNotExist() + { + // Arrange + var dictionary = new Dictionary>(); + + // Act + var result = dictionary.GetOrCreate(1); + + // Assert + Assert.IsNotNull(result); + Assert.AreEqual(0, result.Count); + Assert.IsTrue(dictionary.ContainsKey(1)); + } + + [TestMethod] + public void AddOrUpdate_ShouldAddNewItem_WhenKeyDoesNotExist() + { + // Arrange + var dictionary = new Dictionary(); + + // Act + dictionary.AddOrUpdate(1, "NewValue"); + + // Assert + Assert.AreEqual("NewValue", dictionary[1]); + } + + [TestMethod] + public void AddOrUpdate_ShouldUpdateExistingItem_WhenKeyExists() + { + // Arrange + var dictionary = new Dictionary + { + { 1, "OldValue" } + }; + + // Act + dictionary.AddOrUpdate(1, "UpdatedValue"); + + // Assert + Assert.AreEqual("UpdatedValue", dictionary[1]); + } + + [TestMethod] + public void RemoveIfExists_ShouldReturnTrueAndRemoveItem_WhenKeyExists() + { + // Arrange + var dictionary = new Dictionary + { + { 1, "Value" } + }; + + // Act + var result = dictionary.RemoveIfExists(1); + + // Assert + Assert.IsTrue(result); + Assert.IsFalse(dictionary.ContainsKey(1)); + } + + [TestMethod] + public void RemoveIfExists_ShouldReturnFalse_WhenKeyDoesNotExist() + { + // Arrange + var dictionary = new Dictionary(); + + // Act + var result = dictionary.RemoveIfExists(1); + + // Assert + Assert.IsFalse(result); + } + + [TestMethod] + public void Merge_ShouldMergeDictionariesAndUpdateValues() + { + // Arrange + var dictionary = new Dictionary + { + { 1, "Value1" }, + { 2, "Value2" } + }; + + var otherDictionary = new Dictionary + { + { 2, "UpdatedValue2" }, + { 3, "Value3" } + }; + + // Act + dictionary.Merge(otherDictionary); + + // Assert + Assert.AreEqual(3, dictionary.Count); + Assert.AreEqual("UpdatedValue2", dictionary[2]); + Assert.AreEqual("Value3", dictionary[3]); + } + + [TestMethod] + public void ToReadableString_ShouldReturnFormattedStringRepresentationOfDictionary() + { + // Arrange + var dictionary = new Dictionary + { + { 1, "Value1" }, + { 2, "Value2" } + }; + + // Act + var result = dictionary.ToReadableString(); + + // Assert + Assert.AreEqual("{1: Value1, 2: Value2}", result); + } + } +} \ No newline at end of file diff --git a/SharpHelpers/SharpHelpers.UnitTest/SharpHelpers.UnitTest.csproj b/SharpHelpers/SharpHelpers.UnitTest/SharpHelpers.UnitTest.csproj index 056e5df..666fc5a 100644 --- a/SharpHelpers/SharpHelpers.UnitTest/SharpHelpers.UnitTest.csproj +++ b/SharpHelpers/SharpHelpers.UnitTest/SharpHelpers.UnitTest.csproj @@ -7,7 +7,7 @@ - + diff --git a/SharpHelpers/SharpHelpers/DictionaryHelper.cs b/SharpHelpers/SharpHelpers/DictionaryHelper.cs index 2a61b50..53b0f1d 100644 --- a/SharpHelpers/SharpHelpers/DictionaryHelper.cs +++ b/SharpHelpers/SharpHelpers/DictionaryHelper.cs @@ -63,6 +63,63 @@ public static TValue GetOrCreate(this IDictionary di dictionary[key] = ret; } return ret; + } + + /// + /// Tries to add a key-value pair to the dictionary. If the key already exists, it updates the value. + /// + /// The dictionary to operate on. + /// The key to add or update. + /// The value to associate with the key. + public static void AddOrUpdate(this Dictionary dictionary, TKey key, TValue value) + { + if (dictionary.ContainsKey(key)) + { + dictionary[key] = value; + } + else + { + dictionary.Add(key, value); + } + } + + /// + /// Removes the entry with the specified key if it exists in the dictionary, and returns a boolean indicating success. + /// + /// The dictionary to operate on. + /// The key to remove. + /// True if the key was found and removed, otherwise false. + public static bool RemoveIfExists(this Dictionary dictionary, TKey key) + { + return dictionary.Remove(key); + } + + /// + /// Merges the entries from another dictionary into the current dictionary. If a key already exists, its value is updated. + /// + /// The dictionary to operate on. + /// The dictionary to merge from. + public static void Merge(this Dictionary dictionary, Dictionary otherDictionary) + { + foreach (var kvp in otherDictionary) + { + dictionary.AddOrUpdate(kvp.Key, kvp.Value); + } + } + + /// + /// Converts the dictionary into a readable string format, useful for debugging. + /// + /// The dictionary to convert to a string. + /// A string representation of the dictionary. + public static string ToReadableString(this Dictionary dictionary) + { + var entries = new List(); + foreach (var kvp in dictionary) + { + entries.Add($"{kvp.Key}: {kvp.Value}"); + } + return "{" + string.Join(", ", entries) + "}"; } } }