示例#1
0
        public void When_creating_easy_dictionary_with_key_selector_comparer_and_dictionary()
        {
            var comparer   = StringComparer.OrdinalIgnoreCase;
            var somePerson = new Person("A", 1);
            var someDic    = new Dictionary <string, Person> {
                ["B"] = somePerson
            };

            Func <Person, string>           selector = p => p.Id;
            EasyDictionary <string, Person> dic      = new EasyDictionary <string, Person>(
                selector, someDic, comparer);

            dic.ShouldNotBeNull();
            dic.KeySelector.ShouldBe(selector);
            dic.Count.ShouldBe(1);
            dic.Keys.ShouldNotBeEmpty();
            dic.Values.ShouldNotBeEmpty();
            dic.IsReadOnly.ShouldBeFalse();
            dic.Comparer.ShouldNotBe(EqualityComparer <string> .Default);
            dic.Comparer.ShouldBe(comparer);

            dic.Keys.ShouldBe(new[] { "A" });
            dic.Values.ShouldBe(new[] { somePerson });

            dic.ContainsKey("A").ShouldBeTrue();
            dic.Contains(somePerson).ShouldBeTrue();

            dic.ContainsKey("B").ShouldBeFalse();

            dic["A"].Age.ShouldBe(1);
        }
示例#2
0
        public void When_creating_easy_dictionary_with_key_selector_comparer_and_collection()
        {
            var collection = Enumerable.Range(1, 5).Select(n => new Person(n.ToString(), n)).ToList();
            var comparer   = StringComparer.OrdinalIgnoreCase;
            Func <Person, string>           selector = p => p.Id;
            EasyDictionary <string, Person> dic      = new EasyDictionary <string, Person>(
                selector, collection, comparer);

            dic.ShouldNotBeNull();
            dic.KeySelector.ShouldBe(selector);
            dic.Count.ShouldBe(5);
            dic.Keys.ShouldNotBeEmpty();
            dic.Values.ShouldNotBeEmpty();
            dic.IsReadOnly.ShouldBeFalse();
            dic.Comparer.ShouldNotBe(EqualityComparer <string> .Default);
            dic.Comparer.ShouldBe(comparer);

            dic.Keys.ShouldBe(new[] { "1", "2", "3", "4", "5" });
            dic.Values.ShouldBe(collection);

            dic.ContainsKey("1").ShouldBeTrue();
            dic.Contains(collection[0]).ShouldBeTrue();

            dic["1"].Age.ShouldBe(1);

            dic.ContainsKey("0").ShouldBeFalse();
        }
示例#3
0
        public void When_using_as_collection()
        {
            var p1 = new Person("A", 1);
            var p2 = new Person("B", 2);

            var comparer = StringComparer.OrdinalIgnoreCase;
            Func <Person, string> selector = p => p.Id;
            ICollection <Person>  dic      = new EasyDictionary <string, Person>(
                selector, 10, comparer)
            {
                p1,
                p2
            };

            dic.ShouldNotBeNull();
            dic.Count.ShouldBe(2);

            dic.Contains(p1).ShouldBeTrue();
            dic.Contains(p2).ShouldBeTrue();

            var p3 = new Person("C", 3);

            dic.Add(p3);

            dic.Contains(p3).ShouldBeTrue();

            dic.Remove(p1).ShouldBeTrue();

            dic.Count.ShouldBe(2);

            dic.Contains(p1).ShouldBeFalse();

            dic.Clear();

            dic.Count.ShouldBe(0);
        }
示例#4
0
        public void When_creating_easy_dictionary_with_key_selector_and_sequence()
        {
            var seq = Enumerable.Range(1, 5).Select(n => new Person(n.ToString(), n));
            Func <Person, string>           selector = p => p.Id;
            EasyDictionary <string, Person> dic      = new EasyDictionary <string, Person>(selector, seq);

            dic.ShouldNotBeNull();
            dic.KeySelector.ShouldBe(selector);
            dic.Count.ShouldBe(5);
            dic.Keys.ShouldNotBeEmpty();
            dic.Values.ShouldNotBeEmpty();
            dic.IsReadOnly.ShouldBeFalse();
            dic.Comparer.ShouldBe(EqualityComparer <string> .Default);

            dic.Keys.ShouldBe(new[] { "1", "2", "3", "4", "5" });
            dic.Values.ShouldBe(seq);

            dic.ContainsKey("1").ShouldBeTrue();
            dic.Contains(seq.First()).ShouldBeTrue();

            dic["1"].Age.ShouldBe(1);

            dic.ContainsKey("0").ShouldBeFalse();
        }
示例#5
0
        public void When_adding_and_removing_items_with_comparer()
        {
            var comparer = StringComparer.OrdinalIgnoreCase;
            Func <Person, string>           selector = p => p.Id;
            EasyDictionary <string, Person> dic      = new EasyDictionary <string, Person>(
                selector, comparer: comparer);

            var p1 = new Person("A", 1);
            var p2 = new Person("B", 2);

            dic.Contains(p1).ShouldBeFalse();
            dic.ContainsKey("A").ShouldBeFalse();
            dic.ContainsKey("a").ShouldBeFalse();

            dic.Contains(p2).ShouldBeFalse();
            dic.ContainsKey("B").ShouldBeFalse();
            dic.ContainsKey("b").ShouldBeFalse();

            dic.Add(p1);
            dic.Count.ShouldBe(1);

            dic.Keys.Count.ShouldBe(1);
            dic.Values.Count.ShouldBe(1);

            dic.Contains(p1).ShouldBeTrue();
            dic.ContainsKey("A").ShouldBeTrue();
            dic.ContainsKey("a").ShouldBeTrue();

            dic.TryGetValue("A", out var pA).ShouldBeTrue();
            pA.ShouldBe(pA);

            dic["A"].ShouldBe(pA);
            dic["a"].ShouldBe(pA);

            dic.Contains(p2).ShouldBeFalse();
            dic.ContainsKey("B").ShouldBeFalse();
            dic.ContainsKey("b").ShouldBeFalse();

            dic.TryGetValue("B", out var pB).ShouldBeFalse();

            dic.Add(p2);
            dic.Count.ShouldBe(2);

            dic.Keys.Count.ShouldBe(2);
            dic.Values.Count.ShouldBe(2);

            dic.Contains(p1).ShouldBeTrue();
            dic.ContainsKey("A").ShouldBeTrue();
            dic.ContainsKey("a").ShouldBeTrue();

            dic.Contains(p2).ShouldBeTrue();
            dic.ContainsKey("B").ShouldBeTrue();
            dic.ContainsKey("b").ShouldBeTrue();

            dic.TryGetValue("B", out pB).ShouldBeTrue();
            pB.ShouldBe(p2);

            dic["B"].ShouldBe(pB);
            dic["b"].ShouldBe(pB);

            dic.Remove("C").ShouldBeFalse();
            dic.Remove("c").ShouldBeFalse();
            dic.Remove(new Person("C", 3)).ShouldBeFalse();

            dic.Keys.Count.ShouldBe(2);
            dic.Values.Count.ShouldBe(2);

            dic.Remove("a").ShouldBeTrue();

            dic.Keys.Count.ShouldBe(1);
            dic.Values.Count.ShouldBe(1);

            dic.Contains(p1).ShouldBeFalse();
            dic.ContainsKey("A").ShouldBeFalse();
            dic.ContainsKey("a").ShouldBeFalse();

            dic.Contains(p2).ShouldBeTrue();
            dic.ContainsKey("B").ShouldBeTrue();
            dic.ContainsKey("b").ShouldBeTrue();

            dic.Remove(p2).ShouldBeTrue();

            dic.Keys.Count.ShouldBe(0);
            dic.Values.Count.ShouldBe(0);

            dic.Contains(p1).ShouldBeFalse();
            dic.ContainsKey("A").ShouldBeFalse();
            dic.ContainsKey("a").ShouldBeFalse();

            dic.Contains(p2).ShouldBeFalse();
            dic.ContainsKey("B").ShouldBeFalse();
            dic.ContainsKey("b").ShouldBeFalse();
        }
示例#6
0
        public void When_adding_and_removing_items()
        {
            Func <Person, string>           selector = p => p.Id;
            EasyDictionary <string, Person> dic      = new EasyDictionary <string, Person>(selector);

            var p1 = new Person("A", 1);
            var p2 = new Person("B", 2);

            dic.Contains(p1).ShouldBeFalse();
            dic.ContainsKey("A").ShouldBeFalse();
            dic.ContainsKey("a").ShouldBeFalse();

            dic.Contains(p2).ShouldBeFalse();
            dic.ContainsKey("B").ShouldBeFalse();
            dic.ContainsKey("b").ShouldBeFalse();

            dic.Add(p1);
            dic.Count.ShouldBe(1);

            dic.Keys.Count.ShouldBe(1);
            dic.Values.Count.ShouldBe(1);

            dic.Contains(p1).ShouldBeTrue();
            dic.ContainsKey("A").ShouldBeTrue();
            dic.ContainsKey("a").ShouldBeFalse();

            dic.TryGetValue("A", out var pA).ShouldBeTrue();
            pA.ShouldBe(pA);

            dic["A"].ShouldBe(pA);
            Should.Throw <KeyNotFoundException>(() => dic["a"].ToString());

            dic.Contains(p2).ShouldBeFalse();
            dic.ContainsKey("B").ShouldBeFalse();
            dic.ContainsKey("b").ShouldBeFalse();

            dic.TryGetValue("B", out var pB).ShouldBeFalse();

            dic.Add(p2);
            dic.Count.ShouldBe(2);

            dic.Keys.Count.ShouldBe(2);
            dic.Values.Count.ShouldBe(2);

            dic.Contains(p1).ShouldBeTrue();
            dic.ContainsKey("A").ShouldBeTrue();
            dic.ContainsKey("a").ShouldBeFalse();

            dic.Contains(p2).ShouldBeTrue();
            dic.ContainsKey("B").ShouldBeTrue();
            dic.ContainsKey("b").ShouldBeFalse();

            dic.TryGetValue("B", out pB).ShouldBeTrue();
            pB.ShouldBe(p2);

            dic["B"].ShouldBe(pB);
            Should.Throw <KeyNotFoundException>(() => dic["b"].ToString());

            dic.Remove("C").ShouldBeFalse();
            dic.Remove("c").ShouldBeFalse();
            dic.Remove(new Person("C", 3)).ShouldBeFalse();

            dic.Keys.Count.ShouldBe(2);
            dic.Values.Count.ShouldBe(2);

            dic.Remove("a").ShouldBeFalse();
            dic.Remove("A").ShouldBeTrue();

            dic.Keys.Count.ShouldBe(1);
            dic.Values.Count.ShouldBe(1);

            dic.Contains(p1).ShouldBeFalse();
            dic.ContainsKey("A").ShouldBeFalse();
            dic.ContainsKey("a").ShouldBeFalse();

            dic.Contains(p2).ShouldBeTrue();
            dic.ContainsKey("B").ShouldBeTrue();
            dic.ContainsKey("b").ShouldBeFalse();

            dic.Remove(p2).ShouldBeTrue();

            dic.Keys.Count.ShouldBe(0);
            dic.Values.Count.ShouldBe(0);

            dic.Contains(p1).ShouldBeFalse();
            dic.ContainsKey("A").ShouldBeFalse();
            dic.ContainsKey("a").ShouldBeFalse();

            dic.Contains(p2).ShouldBeFalse();
            dic.ContainsKey("B").ShouldBeFalse();
            dic.ContainsKey("b").ShouldBeFalse();
        }