Add some ReadOnlyDictionary tests (dotnet/corefx#9829)
authorHugh Bellamy <hughbellars@gmail.com>
Mon, 4 Jul 2016 15:14:37 +0000 (16:14 +0100)
committerStephen Toub <stoub@microsoft.com>
Mon, 4 Jul 2016 15:14:37 +0000 (11:14 -0400)
* Add some ReadOnlyDictionary tests

- Also fixes the DebuggerAttributes for when the proxy and parent have
different generic parameters by providing a customization overload

Commit migrated from https://github.com/dotnet/corefx/commit/f0fc81da301e070e69392b5cbafb058270a7a081

src/libraries/Common/tests/System/Collections/IDictionaryTest.cs
src/libraries/Common/tests/System/Diagnostics/DebuggerAttributes.cs
src/libraries/System.ObjectModel/src/System/Collections/Generic/DebugView.cs
src/libraries/System.ObjectModel/tests/ReadOnlyDictionary/ReadOnlyDictionaryTests.cs

index dac292b..ba21f27 100644 (file)
@@ -121,6 +121,13 @@ namespace Tests.Collections
         }
 
         [Fact]
+        public void Contains_NullKey_ThrowsArgumentNullException()
+        {
+            IDictionary dict = GetDictionary(new object[0]) as IDictionary;
+            Assert.Throws<ArgumentNullException>("key", () => dict.Contains(null));
+        }
+
+        [Fact]
         public void WhenDictionaryIsReadOnlyAddShouldThrow()
         {
             object[] items = GenerateItems(16);
index 2092782..97c1521 100644 (file)
@@ -22,7 +22,12 @@ namespace System.Diagnostics
 
         internal static void ValidateDebuggerTypeProxyProperties(Type type, object obj)
         {
-            Type proxyType = GetProxyType(type);
+            ValidateDebuggerTypeProxyProperties(type, type.GenericTypeArguments, obj);
+        }
+
+        internal static void ValidateDebuggerTypeProxyProperties(Type type, Type[] genericTypeArguments, object obj)
+        {
+            Type proxyType = GetProxyType(type, genericTypeArguments);
 
             // Create an instance of the proxy type, and make sure we can access all of the instance properties 
             // on the type without exception
@@ -33,12 +38,11 @@ namespace System.Diagnostics
             }
         }
 
-        internal static Type GetProxyType(object obj)
-        {
-            return GetProxyType(obj.GetType());
-        }
+        public static Type GetProxyType(object obj) => GetProxyType(obj.GetType());
+
+        public static Type GetProxyType(Type type) => GetProxyType(type, type.GenericTypeArguments);
 
-        private static Type GetProxyType(Type type)
+        private static Type GetProxyType(Type type, Type[] genericTypeArguments)
         {
             // Get the DebuggerTypeProxyAttibute for obj
             var attrs =
@@ -52,17 +56,12 @@ namespace System.Diagnostics
             }
             CustomAttributeData cad = attrs[0];
 
-            // Get the proxy type.  As written, this only works if the proxy and the target type
-            // have the same generic parameters, e.g. Dictionary<TKey,TValue> and Proxy<TKey,TValue>.
-            // It will not work with, for example, Dictionary<TKey,TValue>.Keys and Proxy<TKey>,
-            // as the former has two generic parameters and the latter only one.
             Type proxyType = cad.ConstructorArguments[0].ArgumentType == typeof(Type) ?
                 (Type)cad.ConstructorArguments[0].Value :
                 Type.GetType((string)cad.ConstructorArguments[0].Value);
-            var genericArguments = type.GenericTypeArguments;
-            if (genericArguments.Length > 0)
+            if (genericTypeArguments.Length > 0)
             {
-                proxyType = proxyType.MakeGenericType(genericArguments);
+                proxyType = proxyType.MakeGenericType(genericTypeArguments);
             }
 
             return proxyType;
index 2ba4536..586ca52 100644 (file)
@@ -39,7 +39,7 @@ namespace System.Collections.Generic
         public DictionaryDebugView(IDictionary<K, V> dictionary)
         {
             if (dictionary == null)
-                throw new ArgumentNullException("collection");
+                throw new ArgumentNullException(nameof(dictionary));
 
             _dict = dictionary;
         }
index 13e6501..d6fbc20 100644 (file)
@@ -6,13 +6,11 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using Xunit;
 using Tests.Collections;
+using System.Reflection;
+using System.Linq;
 
 namespace System.Collections.ObjectModel.Tests
 {
-    /// <summary>
-    /// Tests the public methods in ReadOnlyDictionary<TKey, Value>.
-    /// properly.
-    /// </summary>
     public class ReadOnlyDictionaryTests
     {
         /// <summary>
@@ -51,6 +49,10 @@ namespace System.Collections.ObjectModel.Tests
 
             IDictionary<int, string> dictAsIDictionary = dictionary;
             Assert.True(dictAsIDictionary.IsReadOnly, "ReadonlyDictionary Should be readonly");
+
+            IDictionary dictAsNonGenericIDictionary = dictionary;
+            Assert.True(dictAsNonGenericIDictionary.IsFixedSize);
+            Assert.True(dictAsNonGenericIDictionary.IsReadOnly);
         }
 
         /// <summary>
@@ -217,6 +219,22 @@ namespace System.Collections.ObjectModel.Tests
             DebuggerAttributes.ValidateDebuggerDisplayReferences(new ReadOnlyDictionary<int, int>(new Dictionary<int, int>()).Keys);
             DebuggerAttributes.ValidateDebuggerDisplayReferences(new ReadOnlyDictionary<int, int>(new Dictionary<int, int>()).Values);
         }
+
+        [Fact]
+        public static void DebuggerAttribute_NullDictionary_ThrowsArgumentNullException()
+        {
+            TargetInvocationException ex = Assert.Throws<TargetInvocationException>(() =>   DebuggerAttributes.ValidateDebuggerTypeProxyProperties(typeof(ReadOnlyDictionary<int, int>), null));
+            ArgumentNullException argumentNullException = Assert.IsType<ArgumentNullException>(ex.InnerException);
+            Assert.Equal("dictionary", argumentNullException.ParamName);
+        }
+        
+        [Fact]
+        public static void DebuggerAttribute_NullDictionaryKeys_ThrowsArgumentNullException()
+        {
+            TargetInvocationException ex = Assert.Throws<TargetInvocationException>(() => DebuggerAttributes.ValidateDebuggerTypeProxyProperties(typeof(ReadOnlyDictionary<int, int>.KeyCollection), new Type[] { typeof(int) }, null));
+            ArgumentNullException argumentNullException = Assert.IsType<ArgumentNullException>(ex.InnerException);
+            Assert.Equal("collection", argumentNullException.ParamName);
+        }
     }
 
     public class TestReadOnlyDictionary<TKey, TValue> : ReadOnlyDictionary<TKey, TValue>