using System; using System.Collections.Generic; using System.Linq; using System.Text; using NUnit.Framework; using Developpez.Dotnet.Collections; namespace Developpez.Dotnet.Tests.Collections { [TestFixture] public class DictionaryExtensionsTests { private Dictionary _dictionary; [SetUp] public void Setup() { _dictionary = new Dictionary(); _dictionary.Add("test", "test"); } [Test] public void Test_GetValueOrDefault_NoDefault() { string expected = "test"; string actual = _dictionary.GetValueOrDefault("test"); Assert.AreEqual(expected, actual); expected = null; actual = _dictionary.GetValueOrDefault("toto"); Assert.AreEqual(expected, actual); } [Test] public void Test_GetValueOrDefault_WithDefault() { string expected = "test"; string actual = _dictionary.GetValueOrDefault("test", "default"); Assert.AreEqual(expected, actual); expected = "default"; actual = _dictionary.GetValueOrDefault("toto", "default"); Assert.AreEqual(expected, actual); } [Test] public void Test_AsReadOnly() { var dic = _dictionary.AsReadOnly(); Assert.IsTrue(dic.IsReadOnly); } [Test] public void Test_AsReadOnly_WithCopy() { var dic = new Dictionary(_dictionary); var readOnlyDic = dic.AsReadOnly(false); var readOnlyCopy = dic.AsReadOnly(true); dic.Remove("test"); dic.Add("foo", "bar"); Assert.IsFalse(readOnlyDic.ContainsKey("test")); Assert.IsTrue(readOnlyDic.ContainsKey("foo")); Assert.IsTrue(readOnlyCopy.ContainsKey("test")); Assert.IsFalse(readOnlyCopy.ContainsKey("foo")); } [Test] public void Test_ToDictionary() { var d1 = new Dictionary { { "a", 1 }, { "b", 2 }, { "c", 3 } }; var d2 = new Dictionary { { "d", 4 }, { "e", 5 } }; var expected = new Dictionary { { "a", 1 }, { "b", 2 }, { "c", 3 }, { "d", 4 }, { "e", 5 } }; var actual = d1.Concat(d2).ToDictionary(); Assert2.AreSequenceEqual( expected.OrderBy(kvp => kvp.Key), actual.OrderBy(kvp => kvp.Key)); } } }