예제 #1
0
        public void ShouldHandleConcreteClassInheritingFromDictionary()
        {
            var dictionary = new ConcreteDictionary()
            {
                { "Name", "Bob" }
            };
            var result = UpcastDictionary.Create(dictionary);

            Assert.AreEqual(dictionary["Name"], result["Name"]);
        }
예제 #2
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.");
        }
예제 #3
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.");
        }
예제 #4
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.");
        }
예제 #5
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.");
        }
예제 #6
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.");
        }
예제 #7
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.");
        }
예제 #8
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.");
        }
예제 #9
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.");
        }
예제 #10
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.");
        }
예제 #11
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.");
        }
예제 #12
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.");
        }
예제 #13
0
        public void ShouldReturnNullForNull()
        {
            IDictionary <string, object> result = UpcastDictionary.Create(null);

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