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(); }
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); }
public void When_clearing_dictionary() { EasyDictionary <string, Person> dic = new EasyDictionary <string, Person>(p => p.Id) { new Person("A", 1), new Person("B", 2) }; dic.ShouldNotBeNull(); dic.Count.ShouldBe(2); dic.ContainsKey("A").ShouldBeTrue(); dic.ContainsKey("B").ShouldBeTrue(); dic.Clear(); dic.Count.ShouldBe(0); dic.ContainsKey("A").ShouldBeFalse(); dic.ContainsKey("B").ShouldBeFalse(); }
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(); }
public void When_using_as_readonly_dictionary() { var p1 = new Person("A", 1); var p2 = new Person("B", 2); var comparer = StringComparer.OrdinalIgnoreCase; Func <Person, string> selector = p => p.Id; IReadOnlyDictionary <string, Person> dic = new EasyDictionary <string, Person>( selector, 10, comparer) { p1, p2 }; dic.ShouldNotBeNull(); dic.Count.ShouldBe(2); dic.Keys.ShouldBe(new [] { "A", "B" }); dic.Values.ShouldBe(new [] { p1, p2 }); dic.ContainsKey("A").ShouldBeTrue(); dic.ContainsKey("a").ShouldBeTrue(); dic.ContainsKey("B").ShouldBeTrue(); dic.ContainsKey("b").ShouldBeTrue(); dic.ContainsKey("C").ShouldBeFalse(); dic.ContainsKey("c").ShouldBeFalse(); Person found; dic.TryGetValue("A", out found).ShouldBeTrue(); found.ShouldBe(p1); dic.TryGetValue("a", out found).ShouldBeTrue(); found.ShouldBe(p1); dic.TryGetValue("B", out found).ShouldBeTrue(); found.ShouldBe(p2); dic.TryGetValue("b", out found).ShouldBeTrue(); found.ShouldBe(p2); }
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(); }
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(); }