From 0e31f96f9c795372b54bb583e2c2d74f5ff3c947 Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Thu, 12 Dec 2019 17:56:47 -0800 Subject: [PATCH] Document tests and add some more --- .../tests/Serialization/DictionaryTests.cs | 16 +++-- .../TestClasses.GenericCollections.cs | 70 ++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Text.Json/tests/Serialization/DictionaryTests.cs b/src/libraries/System.Text.Json/tests/Serialization/DictionaryTests.cs index e384ccf..cbd3b6e 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/DictionaryTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/DictionaryTests.cs @@ -6,6 +6,7 @@ using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Immutable; +using System.Collections.Specialized; using System.Reflection; using System.Text.Encodings.Web; using Xunit; @@ -937,6 +938,8 @@ namespace System.Text.Json.Serialization.Tests private class MyClass : IClass { } + private class MyNonGenericDictionary : Dictionary { } + private class MyFactory : JsonConverterFactory { public override bool CanConvert(Type typeToConvert) @@ -963,6 +966,10 @@ namespace System.Text.Json.Serialization.Tests } } + // This method generates 316 unique test cases for nested dictionaries up to 4 + // levels deep, along with matching JSON, encompassing the various planes of + // dictionaries that can be combined: generic, non-generic, BCL, user-derived, + // immutable, mutable, readonly, concurrent, specialized. private static IEnumerable<(Type, string)> NestedDictionaryTypeData() { string testJson = @"{""Key"":1}"; @@ -970,22 +977,23 @@ namespace System.Text.Json.Serialization.Tests List genericDictTypes = new List() { typeof(IDictionary<,>), - typeof(Dictionary<,>), typeof(ConcurrentDictionary<,>), + typeof(GenericIDictionaryWrapper<,>), }; List nonGenericDictTypes = new List() { - typeof(IDictionary), typeof(Hashtable), + typeof(OrderedDictionary), }; List baseDictionaryTypes = new List { + typeof(MyNonGenericDictionary), typeof(IReadOnlyDictionary), - typeof(Dictionary), typeof(ConcurrentDictionary), - typeof(ImmutableDictionary), + typeof(ImmutableDictionary), + typeof(GenericIDictionaryWrapper), }; baseDictionaryTypes.AddRange(nonGenericDictTypes); diff --git a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.GenericCollections.cs b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.GenericCollections.cs index d6ffc6c..b939d80 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.GenericCollections.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.GenericCollections.cs @@ -791,6 +791,76 @@ namespace System.Text.Json.Serialization.Tests } } + public class GenericIDictionaryWrapper : IDictionary + { + private Dictionary _dict = new Dictionary(); + + public TValue this[TKey key] { get => ((IDictionary)_dict)[key]; set => ((IDictionary)_dict)[key] = value; } + + public ICollection Keys => ((IDictionary)_dict).Keys; + + public ICollection Values => ((IDictionary)_dict).Values; + + public int Count => ((IDictionary)_dict).Count; + + public bool IsReadOnly => ((IDictionary)_dict).IsReadOnly; + + public void Add(TKey key, TValue value) + { + ((IDictionary)_dict).Add(key, value); + } + + public void Add(KeyValuePair item) + { + ((IDictionary)_dict).Add(item); + } + + public void Clear() + { + ((IDictionary)_dict).Clear(); + } + + public bool Contains(KeyValuePair item) + { + return ((IDictionary)_dict).Contains(item); + } + + public bool ContainsKey(TKey key) + { + return ((IDictionary)_dict).ContainsKey(key); + } + + public void CopyTo(KeyValuePair[] array, int arrayIndex) + { + ((IDictionary)_dict).CopyTo(array, arrayIndex); + } + + public IEnumerator> GetEnumerator() + { + return ((IDictionary)_dict).GetEnumerator(); + } + + public bool Remove(TKey key) + { + return ((IDictionary)_dict).Remove(key); + } + + public bool Remove(KeyValuePair item) + { + return ((IDictionary)_dict).Remove(item); + } + + public bool TryGetValue(TKey key, out TValue value) + { + return ((IDictionary)_dict).TryGetValue(key, out value); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return ((IDictionary)_dict).GetEnumerator(); + } + } + public class ReadOnlyStringToStringIDictionaryWrapper : StringToStringIDictionaryWrapper { public override bool IsReadOnly => true; -- 2.7.4