test: bring coverage to 100% for AddOrUpdate

This commit is contained in:
Oliver Booth 2023-04-02 01:16:12 +01:00
parent bfd3a5663d
commit 85f4e8c733
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
1 changed files with 87 additions and 5 deletions

View File

@ -7,7 +7,7 @@ namespace X10D.Tests.Collections;
public class DictionaryTests public class DictionaryTests
{ {
[TestMethod] [TestMethod]
public void AddOrUpdate_ShouldAddNewKey_IfNotExists() public void AddOrUpdate_ShouldAddNewKey_IfNotExists_GivenConcreteDictionary()
{ {
var dictionary = new Dictionary<int, string>(); var dictionary = new Dictionary<int, string>();
Assert.IsFalse(dictionary.ContainsKey(1)); Assert.IsFalse(dictionary.ContainsKey(1));
@ -32,7 +32,32 @@ public class DictionaryTests
} }
[TestMethod] [TestMethod]
public void AddOrUpdate_ShouldUpdateKey_IfExists() public void AddOrUpdate_ShouldAddNewKey_IfNotExists_GivenIDictionary()
{
IDictionary<int, string> dictionary = new Dictionary<int, string>();
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<int, string> {[1] = "one"}; var dictionary = new Dictionary<int, string> {[1] = "one"};
Assert.IsTrue(dictionary.ContainsKey(1)); Assert.IsTrue(dictionary.ContainsKey(1));
@ -60,7 +85,35 @@ public class DictionaryTests
} }
[TestMethod] [TestMethod]
public void AddOrUpdate_ShouldThrow_GivenNullDictionary() public void AddOrUpdate_ShouldUpdateKey_IfExists_GivenIDictionary()
{
IDictionary<int, string> dictionary = new Dictionary<int, string> {[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<int, string>? dictionary = null; Dictionary<int, string>? dictionary = null;
Assert.ThrowsException<ArgumentNullException>(() => dictionary!.AddOrUpdate(1, string.Empty, (_, _) => string.Empty)); Assert.ThrowsException<ArgumentNullException>(() => dictionary!.AddOrUpdate(1, string.Empty, (_, _) => string.Empty));
@ -71,7 +124,18 @@ public class DictionaryTests
} }
[TestMethod] [TestMethod]
public void AddOrUpdate_ShouldThrow_GivenNullUpdateValueFactory() public void AddOrUpdate_ShouldThrow_GivenNullDictionary_GivenIDictionary()
{
IDictionary<int, string>? dictionary = null;
Assert.ThrowsException<ArgumentNullException>(() => dictionary!.AddOrUpdate(1, string.Empty, (_, _) => string.Empty));
Assert.ThrowsException<ArgumentNullException>(() =>
dictionary!.AddOrUpdate(1, _ => string.Empty, (_, _) => string.Empty));
Assert.ThrowsException<ArgumentNullException>(() =>
dictionary!.AddOrUpdate(1, (_, _) => string.Empty, (_, _, _) => string.Empty, 0));
}
[TestMethod]
public void AddOrUpdate_ShouldThrow_GivenNullUpdateValueFactory_GivenConcreteDictionary()
{ {
var dictionary = new Dictionary<int, string>(); var dictionary = new Dictionary<int, string>();
Assert.ThrowsException<ArgumentNullException>(() => dictionary.AddOrUpdate(1, string.Empty, null!)); Assert.ThrowsException<ArgumentNullException>(() => dictionary.AddOrUpdate(1, string.Empty, null!));
@ -80,7 +144,16 @@ public class DictionaryTests
} }
[TestMethod] [TestMethod]
public void AddOrUpdate_ShouldThrow_GivenNullAddValueFactory() public void AddOrUpdate_ShouldThrow_GivenNullUpdateValueFactory_GivenIDictionary()
{
IDictionary<int, string> dictionary = new Dictionary<int, string>();
Assert.ThrowsException<ArgumentNullException>(() => dictionary.AddOrUpdate(1, string.Empty, null!));
Assert.ThrowsException<ArgumentNullException>(() => dictionary.AddOrUpdate(1, _ => string.Empty, null!));
Assert.ThrowsException<ArgumentNullException>(() => dictionary.AddOrUpdate(1, (_, _) => string.Empty, null!, 0));
}
[TestMethod]
public void AddOrUpdate_ShouldThrow_GivenNullAddValueFactory_GivenConcreteDictionary()
{ {
var dictionary = new Dictionary<int, string>(); var dictionary = new Dictionary<int, string>();
Func<int, string>? addValueFactory = null; Func<int, string>? addValueFactory = null;
@ -88,6 +161,15 @@ public class DictionaryTests
Assert.ThrowsException<ArgumentNullException>(() => dictionary.AddOrUpdate(1, null!, (_, _, _) => "one", 0)); Assert.ThrowsException<ArgumentNullException>(() => dictionary.AddOrUpdate(1, null!, (_, _, _) => "one", 0));
} }
[TestMethod]
public void AddOrUpdate_ShouldThrow_GivenNullAddValueFactory_GivenIDictionary()
{
IDictionary<int, string> dictionary = new Dictionary<int, string>();
Func<int, string>? addValueFactory = null;
Assert.ThrowsException<ArgumentNullException>(() => dictionary.AddOrUpdate(1, addValueFactory!, (_, _) => "one"));
Assert.ThrowsException<ArgumentNullException>(() => dictionary.AddOrUpdate(1, null!, (_, _, _) => "one", 0));
}
[TestMethod] [TestMethod]
public void ToConnectionString_ShouldReturnConnectionString() public void ToConnectionString_ShouldReturnConnectionString()
{ {