Пример #1
0
            public void ShouldHandleCollectionsOfArbitraryObjects()
            {
                var dict = CollectionExtensions.ToDictionary(new
                {
                    InnerCollection = new List <object>()
                    {
                        new { a = "a" },
                        3,
                        new MyPOCO()
                        {
                            Key1 = "value1"
                        }
                    }
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("InnerCollection");

                var innerCollectionObject = dict["InnerCollection"];

                innerCollectionObject.Should().NotBeNull();
                innerCollectionObject.Should().BeAssignableTo <IList <object> >();

                var innerCollection = (IList <object>)innerCollectionObject;

                innerCollection.Should().HaveCount(3);
                innerCollection.Should().Contain(o => o is IDictionary <string, object> &&
                                                 ((IDictionary <string, object>)o).Contains(
                                                     new KeyValuePair <string, object>("a", "a")));
                innerCollection.Should().Contain(3);
                innerCollection.Should().Contain(o => o is IDictionary <string, object> &&
                                                 ((IDictionary <string, object>)o).Contains(
                                                     new KeyValuePair <string, object>("Key1", "value1")));
            }
Пример #2
0
            public void ShouldHandleListOfArbitraryObjects()
            {
                var dict = CollectionExtensions.ToDictionary(new
                {
                    InnerList = new List <object>()
                    {
                        new { a = "a" },
                        "b",
                        3
                    }
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("InnerList");

                var innerListObject = dict["InnerList"];

                innerListObject.Should().NotBeNull();
                innerListObject.Should().BeAssignableTo <IList <object> >();

                var innerList = (IList <object>)innerListObject;

                innerList.Should().HaveCount(3);
                innerList[0].Should().BeAssignableTo <IDictionary <string, object> >();
                innerList[0].As <IDictionary <string, object> >().Should().Contain(new KeyValuePair <string, object>("a", "a"));
                innerList[1].Should().Be("b");
                innerList[2].As <int>().Should().Be(3);
            }
Пример #3
0
            public void ShouldHandleDictionaryOfArbitraryObjects()
            {
                var dict = CollectionExtensions.ToDictionary(new
                {
                    InnerDictionary = new Dictionary <string, object>()
                    {
                        { "a", new { a = "a" } },
                        { "b", "b" },
                        { "c", 3 }
                    }
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("InnerDictionary");

                var innerDictionaryObject = dict["InnerDictionary"];

                innerDictionaryObject.Should().NotBeNull();
                innerDictionaryObject.Should().BeAssignableTo <IDictionary <string, object> >();

                var innerDictionary = (IDictionary <string, object>)innerDictionaryObject;

                innerDictionary.Should().HaveCount(3);
                innerDictionary.Should().ContainKey("a");
                innerDictionary["a"].Should().BeAssignableTo <IDictionary <string, object> >();
                innerDictionary["a"].As <IDictionary <string, object> >().Should().Contain(new KeyValuePair <string, object>("a", "a"));
                innerDictionary.Should().Contain(new KeyValuePair <string, object>("b", "b"));
                innerDictionary.Should().Contain(new KeyValuePair <string, object>("c", 3));
            }
Пример #4
0
            public void ShouldHandleDictionary()
            {
                var dict = CollectionExtensions.ToDictionary(new
                {
                    InnerDictionary = new Dictionary <string, object>()
                    {
                        { "Key1", 1 },
                        { "Key2", "a" },
                        { "Key3", 0L }
                    }
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("InnerDictionary");

                var innerDictionaryObject = dict["InnerDictionary"];

                innerDictionaryObject.Should().NotBeNull();
                innerDictionaryObject.Should().BeAssignableTo <IDictionary <string, object> >();

                var innerDictionary = (IDictionary <string, object>)innerDictionaryObject;

                innerDictionary.Should().Contain(new[]
                {
                    new KeyValuePair <string, object>("Key1", 1),
                    new KeyValuePair <string, object>("Key2", "a"),
                    new KeyValuePair <string, object>("Key3", 0L)
                });
            }
Пример #5
0
            public void ShouldHandleDeeperObjects()
            {
                var dict = CollectionExtensions.ToDictionary(new
                {
                    InnerObject = new { Key1 = 1, Key2 = "a", Key3 = 0L }
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("InnerObject");

                var innerObjectObject = dict["InnerObject"];

                innerObjectObject.Should().NotBeNull();
                innerObjectObject.Should().BeAssignableTo <IDictionary <string, object> >();

                var innerObject = (IDictionary <string, object>)innerObjectObject;

                innerObject.Should().Contain(new[]
                {
                    new KeyValuePair <string, object>("Key1", 1),
                    new KeyValuePair <string, object>("Key2", "a"),
                    new KeyValuePair <string, object>("Key3", 0L)
                });
            }
Пример #6
0
            public void ShouldHandleAnonymousObjects()
            {
                var dict = CollectionExtensions.ToDictionary(new { key1 = "value1", key2 = "value2" });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(2);
                dict.Should().Contain(new[]
                {
                    new KeyValuePair <string, object>("key1", "value1"),
                    new KeyValuePair <string, object>("key2", "value2")
                });
            }
Пример #7
0
            public void ShouldHandleString()
            {
                var dict = CollectionExtensions.ToDictionary(new
                {
                    key = "value"
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("key");
                dict.Should().ContainValue("value");
            }
Пример #8
0
            public void ShouldHandleSimpleTypes(object value)
            {
                var dict = CollectionExtensions.ToDictionary(new
                {
                    key = value
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("key");
                dict.Should().ContainValue(value);
            }
Пример #9
0
            public void ShouldHandleArray()
            {
                var array = new byte[2];

                var dict = CollectionExtensions.ToDictionary(new
                {
                    key = array
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("key");
                dict.Should().ContainValue(array);
            }
Пример #10
0
            public void ShouldHandlePoco()
            {
                var dict = CollectionExtensions.ToDictionary(new MyPOCO()
                {
                    Key1 = "value1", Key2 = "value2"
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(2);
                dict.Should().Contain(new[]
                {
                    new KeyValuePair <string, object>("Key1", "value1"),
                    new KeyValuePair <string, object>("Key2", "value2")
                });
            }
Пример #11
0
            public void ShouldRaiseExceptionWhenDictionaryKeysAreNotStrings()
            {
                var ex = Record.Exception(() => CollectionExtensions.ToDictionary(new
                {
                    InnerDictionary = new Dictionary <int, object>()
                    {
                        { 1, new { a = "a" } },
                        { 2, "b" },
                        { 3, 3 }
                    }
                }));

                ex.Should().NotBeNull();
                ex.Should().BeOfType <InvalidOperationException>();
                ex.Message.Should().Contain("string keys");
            }
Пример #12
0
            public void ShouldHandleEnumerable()
            {
                var array = new[] { 1, 2, 3 };
                var value = new MyCollection <int>(array);

                var dict = CollectionExtensions.ToDictionary(new
                {
                    key = value
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("key");
                var s = dict["key"].ToContentString();

                s.Should().Be("[1, 2, 3]"); // GetEnumerator rather than the Name field
            }
Пример #13
0
            public void ShouldHandleCollections()
            {
                var dict = CollectionExtensions.ToDictionary(new
                {
                    InnerCollection = new List <int>()
                    {
                        1, 2, 3
                    }
                });

                dict.Should().NotBeNull();
                dict.Should().HaveCount(1);
                dict.Should().ContainKey("InnerCollection");

                var innerCollectionObject = dict["InnerCollection"];

                innerCollectionObject.Should().NotBeNull();
                innerCollectionObject.Should().BeAssignableTo <IList <int> >();

                var innerCollection = (IList <int>)innerCollectionObject;

                innerCollection.Should().Contain(new[] { 1, 2, 3 });
            }
Пример #14
0
            public void ShouldReturnNullGivenNull()
            {
                var dict = CollectionExtensions.ToDictionary(null);

                dict.Should().BeNull();
            }