From 85f4e8c733d0343086cf76449f7660b9f4d71d61 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 2 Apr 2023 01:16:12 +0100 Subject: [PATCH] test: bring coverage to 100% for AddOrUpdate --- X10D.Tests/src/Collections/DictionaryTests.cs | 92 ++++++++++++++++++- 1 file changed, 87 insertions(+), 5 deletions(-) diff --git a/X10D.Tests/src/Collections/DictionaryTests.cs b/X10D.Tests/src/Collections/DictionaryTests.cs index 9fb8be2..907b1f9 100644 --- a/X10D.Tests/src/Collections/DictionaryTests.cs +++ b/X10D.Tests/src/Collections/DictionaryTests.cs @@ -7,7 +7,7 @@ namespace X10D.Tests.Collections; public class DictionaryTests { [TestMethod] - public void AddOrUpdate_ShouldAddNewKey_IfNotExists() + public void AddOrUpdate_ShouldAddNewKey_IfNotExists_GivenConcreteDictionary() { var dictionary = new Dictionary(); Assert.IsFalse(dictionary.ContainsKey(1)); @@ -32,7 +32,32 @@ public class DictionaryTests } [TestMethod] - public void AddOrUpdate_ShouldUpdateKey_IfExists() + public void AddOrUpdate_ShouldAddNewKey_IfNotExists_GivenIDictionary() + { + IDictionary dictionary = new Dictionary(); + Assert.IsFalse(dictionary.ContainsKey(1)); + + dictionary.AddOrUpdate(1, "one", (_, _) => string.Empty); + Assert.IsTrue(dictionary.ContainsKey(1)); + Assert.AreEqual("one", dictionary[1]); + + dictionary.Clear(); + Assert.IsFalse(dictionary.ContainsKey(1)); + + dictionary.AddOrUpdate(1, _ => "one", (_, _) => string.Empty); + Assert.IsTrue(dictionary.ContainsKey(1)); + Assert.AreEqual("one", dictionary[1]); + + dictionary.Clear(); + Assert.IsFalse(dictionary.ContainsKey(1)); + + dictionary.AddOrUpdate(1, (_, _) => "one", (_, _, _) => string.Empty, 0); + Assert.IsTrue(dictionary.ContainsKey(1)); + Assert.AreEqual("one", dictionary[1]); + } + + [TestMethod] + public void AddOrUpdate_ShouldUpdateKey_IfExists_GivenConcreteDirection() { var dictionary = new Dictionary {[1] = "one"}; Assert.IsTrue(dictionary.ContainsKey(1)); @@ -60,7 +85,35 @@ public class DictionaryTests } [TestMethod] - public void AddOrUpdate_ShouldThrow_GivenNullDictionary() + public void AddOrUpdate_ShouldUpdateKey_IfExists_GivenIDictionary() + { + IDictionary dictionary = new Dictionary {[1] = "one"}; + Assert.IsTrue(dictionary.ContainsKey(1)); + Assert.AreEqual("one", dictionary[1]); + + dictionary.AddOrUpdate(1, string.Empty, (_, _) => "two"); + Assert.IsTrue(dictionary.ContainsKey(1)); + Assert.AreEqual("two", dictionary[1]); + + dictionary.Clear(); + Assert.IsFalse(dictionary.ContainsKey(1)); + dictionary[1] = "one"; + + dictionary.AddOrUpdate(1, _ => string.Empty, (_, _) => "two"); + Assert.IsTrue(dictionary.ContainsKey(1)); + Assert.AreEqual("two", dictionary[1]); + + dictionary.Clear(); + Assert.IsFalse(dictionary.ContainsKey(1)); + dictionary[1] = "one"; + + dictionary.AddOrUpdate(1, (_, _) => string.Empty, (_, _, _) => "two", 0); + Assert.IsTrue(dictionary.ContainsKey(1)); + Assert.AreEqual("two", dictionary[1]); + } + + [TestMethod] + public void AddOrUpdate_ShouldThrow_GivenNullDictionary_GivenConcreteDictionary() { Dictionary? dictionary = null; Assert.ThrowsException(() => dictionary!.AddOrUpdate(1, string.Empty, (_, _) => string.Empty)); @@ -71,7 +124,18 @@ public class DictionaryTests } [TestMethod] - public void AddOrUpdate_ShouldThrow_GivenNullUpdateValueFactory() + public void AddOrUpdate_ShouldThrow_GivenNullDictionary_GivenIDictionary() + { + IDictionary? dictionary = null; + Assert.ThrowsException(() => dictionary!.AddOrUpdate(1, string.Empty, (_, _) => string.Empty)); + Assert.ThrowsException(() => + dictionary!.AddOrUpdate(1, _ => string.Empty, (_, _) => string.Empty)); + Assert.ThrowsException(() => + dictionary!.AddOrUpdate(1, (_, _) => string.Empty, (_, _, _) => string.Empty, 0)); + } + + [TestMethod] + public void AddOrUpdate_ShouldThrow_GivenNullUpdateValueFactory_GivenConcreteDictionary() { var dictionary = new Dictionary(); Assert.ThrowsException(() => dictionary.AddOrUpdate(1, string.Empty, null!)); @@ -80,7 +144,16 @@ public class DictionaryTests } [TestMethod] - public void AddOrUpdate_ShouldThrow_GivenNullAddValueFactory() + public void AddOrUpdate_ShouldThrow_GivenNullUpdateValueFactory_GivenIDictionary() + { + IDictionary dictionary = new Dictionary(); + Assert.ThrowsException(() => dictionary.AddOrUpdate(1, string.Empty, null!)); + Assert.ThrowsException(() => dictionary.AddOrUpdate(1, _ => string.Empty, null!)); + Assert.ThrowsException(() => dictionary.AddOrUpdate(1, (_, _) => string.Empty, null!, 0)); + } + + [TestMethod] + public void AddOrUpdate_ShouldThrow_GivenNullAddValueFactory_GivenConcreteDictionary() { var dictionary = new Dictionary(); Func? addValueFactory = null; @@ -88,6 +161,15 @@ public class DictionaryTests Assert.ThrowsException(() => dictionary.AddOrUpdate(1, null!, (_, _, _) => "one", 0)); } + [TestMethod] + public void AddOrUpdate_ShouldThrow_GivenNullAddValueFactory_GivenIDictionary() + { + IDictionary dictionary = new Dictionary(); + Func? addValueFactory = null; + Assert.ThrowsException(() => dictionary.AddOrUpdate(1, addValueFactory!, (_, _) => "one")); + Assert.ThrowsException(() => dictionary.AddOrUpdate(1, null!, (_, _, _) => "one", 0)); + } + [TestMethod] public void ToConnectionString_ShouldReturnConnectionString() {