Exemplo n.º 1
0
        public void AddRange_NullLookup_Exception()
        {
            var subject = new KeyValuesCollection <int, int>();
            ILookup <int, int> @null = null;

            Assert.That(() => subject.AddRange(@null), Throws.ArgumentNullException);
        }
Exemplo n.º 2
0
        public void AddRange_NullValues_Exception()
        {
            var subject = new KeyValuesCollection();
            IEnumerable <string> @null = null;

            Assert.That(() => subject.AddRange("k", @null), Throws.ArgumentNullException);
        }
Exemplo n.º 3
0
        public void Add_EmptyKey_NoProblem()
        {
            var subject = new KeyValuesCollection();

            Assert.That(() => subject.Add(string.Empty, "v"), Throws.Nothing);
            Assert.That(subject, Has.Count.EqualTo(1));
        }
Exemplo n.º 4
0
        private static string buildString(KeyValuesCollection collection,
                                          Func <string, string> valueEncoding,
                                          Action <StringBuilder> prependAction)
        {
            Guard.AgainstNullArgument("collection", collection);

            string str = collection.Keys
                         .Aggregate(new StringBuilder(),
                                    (sb, k) =>
            {
                collection.GetValues(k).ForEach(v =>
                {
                    sb.Append(valueEncoding(k));
                    sb.Append(EQ);
                    sb.Append(valueEncoding(v));
                    sb.Append(AMP);
                });
                return(sb);
            },
                                    sb =>
            {
                if (sb.Length > 0)
                {
                    prependAction(sb);
                    sb.Remove(sb.Length - 1, 1);
                }
                return(sb.ToString());
            });

            return(str);
        }
Exemplo n.º 5
0
        public void AddRange_EmptyValues_KeyNotAdded()
        {
            var subject = new KeyValuesCollection();

            subject.AddRange("k", Enumerable.Empty <string>());

            Assert.That(subject, Is.Empty);
        }
Exemplo n.º 6
0
        public void Clear_EmptyCollection_RemainEmpty()
        {
            var empty = new KeyValuesCollection <int, string>();

            empty.Clear();

            Assert.That(empty, Is.Empty);
        }
Exemplo n.º 7
0
        public void Indexer_ExistingKey_CollectionOfValues()
        {
            var subject = new KeyValuesCollection {
                { "k", "v" }
            };

            Assert.That(subject["k"], Is.EqualTo(new[] { "v" }));
        }
Exemplo n.º 8
0
        public void Contains_Null_Exception()
        {
            var subject = new KeyValuesCollection {
                { "k", "v" }
            };

            Assert.That(() => subject.Contains(null), Throws.ArgumentNullException);
        }
Exemplo n.º 9
0
        public void Contains_ElementsNotInCollection_False()
        {
            var subject = new KeyValuesCollection {
                { "k", "v" }
            };

            Assert.That(subject.Contains("v"), Is.False, "element with key v is not there");
        }
Exemplo n.º 10
0
        public void Indexer_NonExistingKey_Empty()
        {
            var subject = new KeyValuesCollection {
                { "k", "v" }
            };

            Assert.That(subject["missing"], Is.Not.Null.And.Empty);
        }
Exemplo n.º 11
0
        public void Contains_ElementsInCollection_True()
        {
            var subject = new KeyValuesCollection {
                { "k", "v" }
            };

            Assert.That(subject.Contains("k"), Is.True);
        }
Exemplo n.º 12
0
        public void Add_NullValue_NoProblem()
        {
            var subject = new KeyValuesCollection();

            Assert.That(() => subject.Add("k", null), Throws.Nothing);
            Assert.That(subject.Count, Is.EqualTo(1));
            Assert.That(subject["k"], Must.Have.Count(Is.EqualTo(1)));
        }
Exemplo n.º 13
0
        public void Add_NonExistingKeys_AddsTopLevelElements()
        {
            var subject = new KeyValuesCollection();

            subject.Add("k", "v");

            Assert.That(subject, Has.Count.EqualTo(1));
            Assert.That(subject["k"], Is.EqualTo(new[] { "v" }));
        }
        public void ToQueryString_TwoElements_AmpSeparatedPairs()
        {
            string qs = new KeyValuesCollection {
                { "key1", "value1" }, { "key2", "value2" }
            }
            .ToQueryString();

            Assert.That(qs, Is.EqualTo("?key1=value1&key2=value2"));
        }
        public void ToDecodedQueryString_MultiValuedKey_RepeatsKeyQs()
        {
            string qs = new KeyValuesCollection {
                { "key", "value1" }, { "key", "value2" }
            }
            .ToDecodedQueryString();

            Assert.That(qs, Is.EqualTo("?key=value1&key=value2"));
        }
        public void ToQuery_EmptyValuedElements_Kept()
        {
            string q = new KeyValuesCollection {
                { "key", string.Empty }
            }
            .ToQuery();

            Assert.That(q, Is.EqualTo("key="));
        }
        public void ToQueryString_NullValuedElements_Kept()
        {
            string qs = new KeyValuesCollection {
                { "key", null }
            }
            .ToQueryString();

            Assert.That(qs, Is.EqualTo("?key="));
        }
        public void ToQuery_OneElement_PairWithoutAmp()
        {
            string q = new KeyValuesCollection {
                { "key", "value" }
            }
            .ToQuery();

            Assert.That(q, Is.EqualTo("key=value"));
        }
        public void ToQueryString_EmptyKeyedElements_Kept()
        {
            string qs = new KeyValuesCollection {
                { string.Empty, "value" }
            }
            .ToQueryString();

            Assert.That(qs, Is.EqualTo("?=value"));
        }
        public void ToQueryString_EncodeableElementsEscaped_EncodedKeysAndValues()
        {
            string qs = new KeyValuesCollection {
                { "key 1", "value /1" }
            }
            .ToQueryString();

            Assert.That(qs, Is.EqualTo("?key%201=value%20%2F1"));
        }
        public void ToDecodedQuery_NullValuedElements_Kept()
        {
            string q = new KeyValuesCollection {
                { "key", null }
            }
            .ToDecodedQuery();

            Assert.That(q, Is.EqualTo("key="));
        }
Exemplo n.º 22
0
        public void Remove_ExistingKey_TrueAndOutOfCollection()
        {
            var subject = new KeyValuesCollection {
                { "a", "b" }
            };

            Assert.That(subject.Remove("a"), Is.True);
            Assert.That(subject.Contains("a"), Is.False);
        }
        public void ToDecodedQueryString_OneElement_PairWithoutAmp()
        {
            string qs = new KeyValuesCollection {
                { "key", "value" }
            }
            .ToDecodedQueryString();

            Assert.That(qs, Is.EqualTo("?key=value"));
        }
Exemplo n.º 24
0
        public void Remove_MissingKey_TrueAndOutOfCollection()
        {
            var subject = new KeyValuesCollection {
                { "a", "b" }
            };

            Assert.That(subject.Remove("missing"), Is.False);
            Assert.That(subject, Has.Count.EqualTo(1));
        }
        public void ToDecodedQuery_EncodeableElementsEscaped_NotEncodedKeysAndValues()
        {
            string q = new KeyValuesCollection {
                { "key 1", "value /1" }
            }
            .ToDecodedQuery();

            Assert.That(q, Is.EqualTo("key 1=value /1"));
        }
        public void ToDecodedQueryString_EmptyValuedElements_Kept()
        {
            string qs = new KeyValuesCollection {
                { "key", string.Empty }
            }
            .ToDecodedQueryString();

            Assert.That(qs, Is.EqualTo("?key="));
        }
        public void ToDecodedQuery_EmptyKeyedElements_Kept()
        {
            string q = new KeyValuesCollection {
                { string.Empty, "value" }
            }
            .ToDecodedQuery();

            Assert.That(q, Is.EqualTo("=value"));
        }
        public void ToDecodedQuery_TwoElements_AmpSeparatedPairs()
        {
            string q = new KeyValuesCollection {
                { "key1", "value1" }, { "key2", "value2" }
            }
            .ToDecodedQuery();

            Assert.That(q, Is.EqualTo("key1=value1&key2=value2"));
        }
Exemplo n.º 29
0
        public void AddRange_AddMultipleValues_ToSameKey()
        {
            var subject = new KeyValuesCollection();

            subject.AddRange("k", "a", "b", "c");             // using params
            Assert.That(subject["k"], Must.Have.Count(Is.EqualTo(3)));

            subject.AddRange("k", Enumerable.Repeat("x", 2));             // using collection
            Assert.That(subject["k"], Must.Have.Count(Is.EqualTo(5)));
        }
Exemplo n.º 30
0
        public void NonGenericEnumerator_YieldsIGroupings()
        {
            var subject = new KeyValuesCollection <int, string> {
                { 1, "one" }, { 2, "two" }
            };

            System.Collections.IEnumerable nonGeneric = subject;

            Assert.That(nonGeneric, Has.All.InstanceOf <IGrouping <int, string> >());
        }