// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Collections.Generic; using System.Linq; using System.Net; using Microsoft.TestCommon; using Xunit; using Xunit.Extensions; using Assert = Microsoft.TestCommon.AssertEx; namespace System.Web.Http { public class DictionaryExtensionsTest { [Fact] public void IsCorrectType() { Assert.Type.HasProperties(typeof(DictionaryExtensions), TypeAssert.TypeProperties.IsStatic | TypeAssert.TypeProperties.IsClass); } [Fact] public void TryGetValueThrowsOnNullCollection() { string value; Assert.ThrowsArgumentNull(() => DictionaryExtensions.TryGetValue(null, String.Empty, out value), "collection"); } [Fact] public void TryGetValueThrowsOnNullKey() { IDictionary dict = new Dictionary(); string value; Assert.ThrowsArgumentNull(() => dict.TryGetValue(null, out value), "key"); } public static TheoryDataSet DictionaryValues { get { return new TheoryDataSet { "test", new string[] { "A", "B", "C" }, 8, new List {1, 2, 3}, 1D, (IEnumerable)new List { 1D, 2D, 3D }, new Uri("http://some.host"), Guid.NewGuid(), HttpStatusCode.NotImplemented, new HttpStatusCode[] { HttpStatusCode.Accepted, HttpStatusCode.Ambiguous, HttpStatusCode.BadGateway } }; } } [Fact] public void TryGetValueReturnsFalse() { // Arrange IDictionary dict = new Dictionary(); // Act string resultValue = null; bool result = dict.TryGetValue("notfound", out resultValue); // Assert Assert.False(result); Assert.Null(resultValue); } [Theory] [PropertyData("DictionaryValues")] public void TryGetValueReturnsTrue(T value) { // Arrange IDictionary dict = new Dictionary() { { "key", value } }; // Act T resultValue; bool result = DictionaryExtensions.TryGetValue(dict, "key", out resultValue); // Assert Assert.True(result); Assert.Equal(typeof(T), resultValue.GetType()); Assert.Equal(value, resultValue); } [Fact] public void GetValueThrowsOnNullCollection() { Assert.ThrowsArgumentNull(() => DictionaryExtensions.GetValue(null, String.Empty), "collection"); } [Fact] public void GetValueThrowsOnNullKey() { IDictionary dict = new Dictionary(); Assert.ThrowsArgumentNull(() => dict.GetValue(null), "key"); } [Fact] public void GetValueThrowsOnNotFound() { IDictionary dict = new Dictionary(); Assert.Throws(() => dict.GetValue("notfound")); } [Theory] [PropertyData("DictionaryValues")] public void GetValueReturnsValue(T value) { // Arrange IDictionary dict = new Dictionary() { { "key", value } }; // Act T resultValue = DictionaryExtensions.GetValue(dict, "key"); // Assert Assert.Equal(typeof(T), resultValue.GetType()); Assert.Equal(value, resultValue); } [Fact] public void FindKeysWithPrefixRecognizesRootChilden() { // Arrange IDictionary dict = new Dictionary() { { "[0]", 1 }, { "Name", 2 }, { "Address.Street", 3 }, { "", 4 } }; // Act List results = DictionaryExtensions.FindKeysWithPrefix(dict, "").Select(kvp => kvp.Value).ToList(); // Assert Assert.Equal(4, results.Count); Assert.Contains(1, results); Assert.Contains(2, results); Assert.Contains(3, results); Assert.Contains(4, results); } } }