コード例 #1
0
        public void ShouldReturnNullIfWrongNumberOfGenericArguments()
        {
            object source = new List <string>();
            IDictionary <string, object> result = UpcastDictionary.Create(source);

            Assert.IsNull(result, "Null should be returned for generic types with the wrong number of type arguments.");
        }
コード例 #2
0
        public void ShouldReturnArgumentIfIDictionary_string_object()
        {
            object source = new Dictionary <string, object>();
            IDictionary <string, object> result = UpcastDictionary.Create(source);

            Assert.AreSame(source, result, "The up-cast wrapper should not be applied if already a IDictionary<string, object>.");
        }
コード例 #3
0
        public void ShouldReturnNullIfNotGenericType()
        {
            object source = String.Empty;
            IDictionary <string, object> result = UpcastDictionary.Create(source);

            Assert.IsNull(result, "Null should be returned for non-generic types.");
        }
コード例 #4
0
        public void ShouldReturnNullIfNotDictionaryType()
        {
            object source = (Converter <string, object>)(s => (object)s);
            IDictionary <string, object> result = UpcastDictionary.Create(source);

            Assert.IsNull(result, "Null should be returned for non-dictionary types.");
        }
コード例 #5
0
        public void ShouldReturnUpcastWrapperForDictionary_string_TValue()
        {
            object source = new Dictionary <string, string>();
            IDictionary <string, object> result = UpcastDictionary.Create(source);

            Assert.IsInstanceOfType(result, typeof(UpcastDictionary <string>), "The source was not wrapped.");
        }
コード例 #6
0
        public void ShouldReturnNullIfFirstGenericTypeArgumentIsNotAString()
        {
            object source = new Dictionary <object, object>();
            IDictionary <string, object> result = UpcastDictionary.Create(source);

            Assert.IsNull(result, "Null should be returned if the first generic type argument is not a string.");
        }
コード例 #7
0
 public void ShouldNotFindKeyIfNotInWrappedDictionary_Indexer()
 {
     var source = new Dictionary <string, int>()
     {
         { "Age", 100 }
     };
     IDictionary <string, object> result = UpcastDictionary.Create(source);
     object value = result["Name"];
 }
コード例 #8
0
        public void ShouldHandleConcreteClassInheritingFromDictionary()
        {
            var dictionary = new ConcreteDictionary()
            {
                { "Name", "Bob" }
            };
            var result = UpcastDictionary.Create(dictionary);

            Assert.AreEqual(dictionary["Name"], result["Name"]);
        }
コード例 #9
0
        public void ShouldGetCount()
        {
            var source = new Dictionary <string, int>()
            {
                { "Age", 100 }, { "Weight", 45 }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);

            Assert.AreEqual(source.Count, result.Count, "The source and Upcast dictionary should have the same count.");
        }
コード例 #10
0
        public void ShouldFindPairInWrappedDictionary()
        {
            var source = new Dictionary <string, int>()
            {
                { "Age", 100 }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            bool contains = result.Contains(new KeyValuePair <string, object>("Age", 100));

            Assert.IsTrue(contains, "The pair should have been found.");
        }
コード例 #11
0
        public void ShouldNotFindPairIfValueWrongType()
        {
            var source = new Dictionary <string, int>()
            {
                { "Age", 100 }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            bool contains = result.Contains(new KeyValuePair <string, object>("Age", "Blah"));

            Assert.IsFalse(contains, "The pair should not have been found.");
        }
コード例 #12
0
        public void ShouldFindKeyIfInWrappedDictionary_Indexer()
        {
            var source = new Dictionary <string, string>()
            {
                { "Name", "Bob" }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            object value = result["Name"];

            Assert.AreSame(source["Name"], value, "The value in the underlying dictionary should have been returned.");
        }
コード例 #13
0
        public void ShouldNotFindKeyIfNotInWrappedDictionary()
        {
            object source = new Dictionary <string, string>()
            {
                { "Name", "Bob" }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            bool containsKey = result.ContainsKey("Age");

            Assert.IsFalse(containsKey, "The key Age should not have been found.");
        }
コード例 #14
0
        public void ShouldFindKeyIfInWrappedDictionary_TryGetValue()
        {
            var source = new Dictionary <string, string>()
            {
                { "Name", "Bob" }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            bool found = result.TryGetValue("Name", out object value);

            Assert.IsTrue(found, "The key should have been found.");
            Assert.AreSame(source["Name"], value, "The value in the underlying dictionary should have been returned.");
        }
コード例 #15
0
        public void ShouldReturnValuesAsObjects()
        {
            var source = new Dictionary <string, int>()
            {
                { "Age", 100 }, { "Weight", 500 }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            ICollection sourceValues            = source.Values;
            ICollection wrappedValues           = result.Values.ToArray();

            CollectionAssert.AreEquivalent(sourceValues, wrappedValues, "The underlying values were not returned.");
        }
コード例 #16
0
        public void ShouldNotFindKeyIfNotInWrappedDictionary_TryGetValue()
        {
            var source = new Dictionary <string, int>()
            {
                { "Age", 100 }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            bool found = result.TryGetValue("Name", out object value);

            Assert.IsFalse(found, "The key should not have been found.");
            Assert.IsNull(value, "The value should be null even if the actual type is a struct.");
        }
コード例 #17
0
        public void ShouldFindKeysInWrappedDictionary()
        {
            var source = new Dictionary <string, string>()
            {
                { "Name", "Bob" }, { "Age", "100" }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            ICollection sourceKeys  = source.Keys;
            ICollection wrappedKeys = result.Keys.ToArray();

            CollectionAssert.AreEquivalent(sourceKeys, wrappedKeys, "The same keys should have been found in both collections.");
        }
コード例 #18
0
        public void ShouldCopyPairsToArray()
        {
            var source = new Dictionary <string, int>()
            {
                { "Age", 100 }, { "Weight", 45 }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            var array = new KeyValuePair <string, object> [2];

            result.CopyTo(array, 0);
            var expected = new KeyValuePair <string, object>[]
            {
                new KeyValuePair <string, object>("Age", 100),
                new KeyValuePair <string, object>("Weight", 45)
            };

            CollectionAssert.AreEqual(expected, array, "The pairs were not copied.");
        }
コード例 #19
0
        public void ShouldGetEnumerator()
        {
            var source = new Dictionary <string, int>()
            {
                { "Age", 100 }, { "Weight", 45 }
            };
            IDictionary <string, object> result = UpcastDictionary.Create(source);
            IEnumerator <KeyValuePair <string, object> > enumerator = result.GetEnumerator();
            var values = new List <KeyValuePair <string, object> >();

            while (enumerator.MoveNext())
            {
                values.Add(enumerator.Current);
            }
            var expected = new KeyValuePair <string, object>[]
            {
                new KeyValuePair <string, object>("Age", 100),
                new KeyValuePair <string, object>("Weight", 45)
            };

            CollectionAssert.AreEqual(expected, values, "The enumerator did not return the correct pairs.");
        }
コード例 #20
0
        public void ShouldReturnNullForNull()
        {
            IDictionary <string, object> result = UpcastDictionary.Create(null);

            Assert.IsNull(result, "Null should be returned for null.");
        }