1 // Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
2 
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Net;
6 using Microsoft.TestCommon;
7 using Xunit;
8 using Xunit.Extensions;
9 using Assert = Microsoft.TestCommon.AssertEx;
10 
11 namespace System.Web.Http
12 {
13     public class DictionaryExtensionsTest
14     {
15         [Fact]
IsCorrectType()16         public void IsCorrectType()
17         {
18             Assert.Type.HasProperties(typeof(DictionaryExtensions), TypeAssert.TypeProperties.IsStatic | TypeAssert.TypeProperties.IsClass);
19         }
20 
21         [Fact]
TryGetValueThrowsOnNullCollection()22         public void TryGetValueThrowsOnNullCollection()
23         {
24             string value;
25             Assert.ThrowsArgumentNull(() => DictionaryExtensions.TryGetValue<string>(null, String.Empty, out value), "collection");
26         }
27 
28         [Fact]
TryGetValueThrowsOnNullKey()29         public void TryGetValueThrowsOnNullKey()
30         {
31             IDictionary<string, object> dict = new Dictionary<string, object>();
32             string value;
33             Assert.ThrowsArgumentNull(() => dict.TryGetValue<string>(null, out value), "key");
34         }
35 
36         public static TheoryDataSet<object> DictionaryValues
37         {
38             get
39             {
40                 return new TheoryDataSet<object>
41                 {
42                     "test",
43                     new string[] { "A", "B", "C" },
44                     8,
45                     new List<int> {1, 2, 3},
46                     1D,
47                     (IEnumerable<double>)new List<double> { 1D, 2D, 3D },
48                     new Uri("http://some.host"),
49                     Guid.NewGuid(),
50                     HttpStatusCode.NotImplemented,
51                     new HttpStatusCode[] { HttpStatusCode.Accepted, HttpStatusCode.Ambiguous, HttpStatusCode.BadGateway }
52                 };
53             }
54         }
55 
56         [Fact]
TryGetValueReturnsFalse()57         public void TryGetValueReturnsFalse()
58         {
59             // Arrange
60             IDictionary<string, object> dict = new Dictionary<string, object>();
61 
62             // Act
63             string resultValue = null;
64             bool result = dict.TryGetValue("notfound", out resultValue);
65 
66             // Assert
67             Assert.False(result);
68             Assert.Null(resultValue);
69         }
70 
71         [Theory]
72         [PropertyData("DictionaryValues")]
TryGetValueReturnsTrue(T value)73         public void TryGetValueReturnsTrue<T>(T value)
74         {
75             // Arrange
76             IDictionary<string, object> dict = new Dictionary<string, object>()
77             {
78                 { "key", value }
79             };
80 
81 
82             // Act
83             T resultValue;
84             bool result = DictionaryExtensions.TryGetValue(dict, "key", out resultValue);
85 
86             // Assert
87             Assert.True(result);
88             Assert.Equal(typeof(T), resultValue.GetType());
89             Assert.Equal(value, resultValue);
90         }
91 
92         [Fact]
GetValueThrowsOnNullCollection()93         public void GetValueThrowsOnNullCollection()
94         {
95             Assert.ThrowsArgumentNull(() => DictionaryExtensions.GetValue<string>(null, String.Empty), "collection");
96         }
97 
98         [Fact]
GetValueThrowsOnNullKey()99         public void GetValueThrowsOnNullKey()
100         {
101             IDictionary<string, object> dict = new Dictionary<string, object>();
102             Assert.ThrowsArgumentNull(() => dict.GetValue<string>(null), "key");
103         }
104 
105         [Fact]
GetValueThrowsOnNotFound()106         public void GetValueThrowsOnNotFound()
107         {
108             IDictionary<string, object> dict = new Dictionary<string, object>();
109             Assert.Throws<InvalidOperationException>(() => dict.GetValue<string>("notfound"));
110         }
111 
112         [Theory]
113         [PropertyData("DictionaryValues")]
GetValueReturnsValue(T value)114         public void GetValueReturnsValue<T>(T value)
115         {
116             // Arrange
117             IDictionary<string, object> dict = new Dictionary<string, object>()
118             {
119                 { "key", value }
120             };
121 
122             // Act
123             T resultValue = DictionaryExtensions.GetValue<T>(dict, "key");
124 
125             // Assert
126             Assert.Equal(typeof(T), resultValue.GetType());
127             Assert.Equal(value, resultValue);
128         }
129 
130         [Fact]
FindKeysWithPrefixRecognizesRootChilden()131         public void FindKeysWithPrefixRecognizesRootChilden()
132         {
133             // Arrange
134             IDictionary<string, int> dict = new Dictionary<string, int>()
135             {
136                 { "[0]", 1 },
137                 { "Name", 2 },
138                 { "Address.Street", 3 },
139                 { "", 4 }
140             };
141 
142             // Act
143             List<int> results = DictionaryExtensions.FindKeysWithPrefix<int>(dict, "").Select(kvp => kvp.Value).ToList();
144 
145             // Assert
146             Assert.Equal(4, results.Count);
147             Assert.Contains(1, results);
148             Assert.Contains(2, results);
149             Assert.Contains(3, results);
150             Assert.Contains(4, results);
151         }
152     }
153 }
154