Esempio n. 1
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);
        }
Esempio n. 2
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();
        }
Esempio n. 3
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();
        }