private static IEnumerable <ITestCaseData> ToStringTestCases() { yield return(new TestCaseData("test") .SetName("Should return string") .Returns("test")); yield return(new TestCaseData(null) .SetName("Should generate toString of null object") .Returns("null")); yield return(new TestCaseData(new List <int?> { 1, 2, null, 3 }) .SetName("Should generate toString of collection") .Returns("[1, 2, null, 3]")); yield return(new TestCaseData(Lists.EmptyList <int>()) .SetName("Should generate toString of empty collection") .Returns("[]")); yield return(new TestCaseData((object)new object[] { "A", "B", 3, 4 }) .SetName("Should generate toString of array") .Returns("[A, B, 3, 4]")); yield return(new TestCaseData(Dictionaries.Create("A", 1, "B", 2, "C", 3)) .SetName("Should generate toString of dictionary") .Returns("[(A, 1), (B, 2), (C, 3)]")); yield return(new TestCaseData(Dictionaries.Create( "A", Lists.AsList(Lists.AsList(1, 2, 3), Lists.AsList(4, 5, 6)), "B", Lists.AsList(Lists.AsList(1), Lists.AsList(2)), "C", Lists.AsList(Lists.EmptyList <int>(), null))) .SetName("Should generate toString of complex dictionary") .Returns("[(A, [[1, 2, 3], [4, 5, 6]]), (B, [[1], [2]]), (C, [[], null])]")); }
private static IEnumerable <ITestCaseData> StringFormatTestCases() { yield return(new TestCaseData("test", new object[0]) .SetName("Should format string without params") .Returns("test")); yield return(new TestCaseData("This is '{0}'", new object[] { null }) .SetName("Should format null params") .Returns("This is 'null'")); yield return(new TestCaseData("{0}, {1}, {2}", new object[] { 1, "B", null }) .SetName("Should format multiple params") .Returns("1, B, null")); yield return(new TestCaseData("This is {0}", new object[] { Lists.AsList(1, 2, 3) }) .SetName("Should format list params") .Returns("This is [1, 2, 3]")); yield return(new TestCaseData("This is {0}", new object[] { Lists.AsList(Lists.AsList("A", "B"), Lists.AsList("C", "D"), Lists.EmptyList <string>()) }) .SetName("Should format nested list params") .Returns("This is [[A, B], [C, D], []]")); yield return(new TestCaseData("This is {0}", new object[] { Dictionaries.Create("A", 1, "B", 2) }) .SetName("Should format dictionary params") .Returns("This is [(A, 1), (B, 2)]")); }
public void ShouldBuildImmutableListMultiDictionaryAndReturnImmutableListValues() { // given var immutableListMultiDictionary = ImmutableListMultiDictionary <int, string> .Builder() .Put(1, "A") .Put(1, "A") .Put(1, "B") .PutAll(2, Lists.AsList("X", "Y", "Z")) .PutAll(ArrayListMultiDictionary <int, string> .Of(3, "Z", 3, "W")) .PutAll(Dictionaries.Create(4, "_")) .Build(); // when var values1 = immutableListMultiDictionary[1]; var values2 = immutableListMultiDictionary.Get(2); var values3 = immutableListMultiDictionary.Get(3); var values4 = immutableListMultiDictionary[4]; // then Check.That(values1).HasSize(3).And.Contains("A", "B").And.IsInstanceOf <ImmutableList <string> >(); Check.That(values1.DistinctElementCount()).IsEqualTo(Dictionaries.Create("A", 2, "B", 1)); Check.That(values2).HasSize(3).And.Contains("X", "Y", "Z").And.IsInstanceOf <ImmutableList <string> >(); Check.That(values2.DistinctElementCount()).IsEqualTo(Dictionaries.Create("X", 1, "Y", 1, "Z", 1)); Check.That(values3).HasSize(2).And.Contains("Z", "W").And.IsInstanceOf <ImmutableList <string> >(); Check.That(values3.DistinctElementCount()).IsEqualTo(Dictionaries.Create("Z", 1, "W", 1)); Check.That(values4).HasSize(1).And.Contains("_").And.IsInstanceOf <ImmutableList <string> >(); Check.That(values4.DistinctElementCount()).IsEqualTo(Dictionaries.Create("_", 1)); }
public void ShouldReturnObjectIfIsPresentInDictionary() { // given var expectedValue = DateTime.Now; var dictionary = Dictionaries.Create <int, DateTime?>(1, expectedValue); // when var value = dictionary.GetIfPresent(1); // then Check.That(value).IsEqualTo(expectedValue); }
public void ShouldExntedDictionaryByToImmutableSetMultiDictionaryMethod() { // given var dictionary = Dictionaries.Create("A", "A1", "B", "B1", "C", "C1"); // when var multiDictionary = dictionary.ToImmutableSetMultiDictionary(); // then Check.That(multiDictionary) .IsEqualTo(ImmutableSetMultiDictionary <string, string> .Of("A", "A1", "B", "B1", "C", "C1")); }
public void ShouldExtenedEnumerableByDistinctElementCountMethod() { // given var set = Sets.AsSet("A", "B", "C"); var list = Lists.AsList("A", "B", "B", "B", "C", "C", "D", "D", "D", "D"); // when var setDistinctElementsCount = set.DistinctElementCount(); var listDistinctElementsCount = list.DistinctElementCount(); // then Check.That(setDistinctElementsCount).IsEqualTo(Dictionaries.Create("A", 1, "B", 1, "C", 1)); Check.That(listDistinctElementsCount).IsEqualTo(Dictionaries.Create("A", 1, "B", 3, "C", 2, "D", 4)); }
public void ShouldConvertDictionaryIntoComparable() { // given var dictionary = new Dictionary <int, string>(); dictionary[1] = "A"; dictionary[2] = "B"; dictionary[3] = "C"; // when var comparable = dictionary.ToComparable(); // then Check.That(comparable).IsEqualTo(Dictionaries.Create(1, "A", 2, "B", 3, "C")); }
public void ShouldExtendDictionaryByEntriesMethod() { // given var dictionary = Dictionaries.Create("A", "A1", "B", "B1", "C", "C1"); // when var entries = dictionary.Entries(); // then Check.That(entries).HasSize(3); Check.That(entries).Contains( Dictionaries.Entry("A", "A1"), Dictionaries.Entry("B", "B1"), Dictionaries.Entry("C", "C1")); }
public void ShouldPreventImmutableMultiDictionaryPutAllEntries(ImmutableMultiDictionaryCreator creator) { // given var immutableMultiDictionary = creator.Create <int, string>(); var entries = Dictionaries.Create(1, "A", 2, "B"); // when var result = Try.To(() => immutableMultiDictionary.PutAll(entries)); // then var caughtException = result.IsFailure ? result.Error : null; Check.That(caughtException).IsNotNull(); Check.That(caughtException).IsInstanceOf <InvalidOperationException>(); Check.That(immutableMultiDictionary).IsEqualTo(creator.Create <int, string>()); }
public void ShouldConvertMultiDictionaryToDictionary(MultiDictionaryContext context) { // given var multiDictionary = context.Create <string, string>(); multiDictionary.PutAll("A", Lists.AsList("A1", "A2", "A3")); multiDictionary.Put("B", "B1"); multiDictionary.Put("C", "C1"); // when var dictionary = multiDictionary.ToDictionary(); // then Check.That(Objects.Equal(dictionary, Dictionaries.Create( "A", context.CollectionOf("A1", "A2", "A3"), "B", context.CollectionOf("B1"), "C", context.CollectionOf("C1")))) .IsTrue(); }
private static IEnumerable <ITestCaseData> ToStringTestCases() { yield return(new TestCaseData("test") .SetName("Should return string") .Returns("test")); yield return(new TestCaseData(null) .SetName("Should generate toString of null object") .Returns("null")); yield return(new TestCaseData(new List <int?> { 1, 2, null, 3 }) .SetName("Should generate toString of collection") .Returns("[1, 2, null, 3]")); yield return(new TestCaseData(Lists.EmptyList <int>()) .SetName("Should generate toString of empty collection") .Returns("[]")); yield return(new TestCaseData((object)new object[] { "A", "B", 3, 4 }) .SetName("Should generate toString of array") .Returns("[A, B, 3, 4]")); yield return(new TestCaseData(Dictionaries.Create("A", 1, "B", 2, "C", 3)) .SetName("Should generate toString of dictionary") .Returns("[(A, 1), (B, 2), (C, 3)]")); yield return(new TestCaseData(Dictionaries.Create( "A", Lists.AsList(Lists.AsList(1, 2, 3), Lists.AsList(4, 5, 6)), "B", Lists.AsList(Lists.AsList(1), Lists.AsList(2)), "C", Lists.AsList(Lists.EmptyList <int>(), null))) .SetName("Should generate toString of complex dictionary") .Returns("[(A, [[1, 2, 3], [4, 5, 6]]), (B, [[1], [2]]), (C, [[], null])]")); yield return(new TestCaseData(new DateTime(2016, 2, 9, 10, 45, 0, DateTimeKind.Utc)) .SetName("Should include UTC kind for DateTime") .Returns("2016-02-09 10:45:00Z")); yield return(new TestCaseData(new DateTime(2016, 2, 9, 10, 45, 0, DateTimeKind.Unspecified)) .SetName("Should ignore unspecified kind for DateTime") .Returns("2016-02-09 10:45:00")); }
public void ShouldReturImmutableMultiDictionaryValues(ImmutableMultiDictionaryCreator creator) { // given var immutableMultiDictionary = creator.Create(ArrayListMultiDictionary <int, string> .Of( 1, "A", 1, "B", 1, "C", 2, "A", 3, "C", 3, "X").Entries.ToArray()); // when var values = immutableMultiDictionary.Values; // then Check.That(immutableMultiDictionary.Count).IsEqualTo(6); Check.That(values).HasSize(6).And.Contains("A", "B", "C", "X"); Check.That(Objects.Equal(values.DistinctElementCount(), Dictionaries.Create("A", 2, "B", 1, "C", 2, "X", 1))) .IsTrue(); Check.That(values is IReadOnlyCollection <string>).IsTrue(); }
public void ShouldConvertImmutableListMultiDictionaryToDictionaryWithListAsValue() { // given var immutableListMultiDictionary = ImmutableListMultiDictionary <int, string> .Builder() .Put(1, "A") .Put(1, "A") .Put(1, "B") .PutAll(2, Lists.AsList("X", "Y", "Z")) .Build(); // when var dictionary = immutableListMultiDictionary.ToDictionary(); // then Check.That(dictionary).IsEqualTo(Dictionaries.Create( 1, Lists.AsList("A", "A", "B"), 2, Lists.AsList("X", "Y", "Z"))); Check.That(dictionary[1]).IsInstanceOf <List <string> >(); Check.That(dictionary[2]).IsInstanceOf <List <string> >(); }
public void ShouldPutEntriesFromDictionary(MultiDictionaryContext context) { // given var multiDictionary = context.Create <int, string>(); multiDictionary.Put(1, "B"); var dictionary = Dictionaries.Create(1, "A", 2, "B", 3, "C"); // when multiDictionary.PutAll(dictionary); // then Check.That(multiDictionary.Count).IsEqualTo(4); Check.That(multiDictionary.Values).HasSize(4); Check.That(multiDictionary.Values).Contains("A", "B", "B", "C"); Check.That(multiDictionary[1]).HasSize(2); Check.That(multiDictionary[1]).Contains("A", "B"); Check.That(multiDictionary[2]).ContainsExactly("B"); Check.That(multiDictionary[3]).ContainsExactly("C"); }
private static IEnumerable <ITestCaseData> ObjectComparationTestCases() { yield return(new TestCaseData("string", "string") .SetName("Should return that strings are equals") .Returns(true)); yield return(new TestCaseData("a", "b") .SetName("Should return that strings are not equals") .Returns(false)); var obj = new object(); yield return(new TestCaseData(obj, obj) .SetName("Should return that objects are euals be reference") .Returns(true)); yield return(new TestCaseData(new List <int> { 1, 2, 3 }, new List <int> { 1, 2, 3 }) .SetName("Should return that collections are equals") .Returns(true)); yield return(new TestCaseData(new List <int> { 1, 2, 3 }, new List <int> { 1, 2, 3, 4 }) .SetName("Should return that collections are not equals") .Returns(false)); yield return(new TestCaseData(Lists.AsList(Lists.AsList(1, 2), Lists.AsList(3, 4)), Lists.AsList(Lists.AsList(1, 2), Lists.AsList(3, 4))) .SetName("Should return that nested collections are equals") .Returns(true)); yield return(new TestCaseData(Lists.AsList(Lists.AsList(1, 2), Lists.AsList(3, 4)), Lists.AsList(Lists.AsList(1, 2), Lists.AsList(3, 4, 5))) .SetName("Should return that nested collections are not equals") .Returns(false)); yield return(new TestCaseData(new object[] { "A", 13 }, new object[] { "A", 13 }) .SetName("Should return that arrays are not equals") .Returns(true)); yield return(new TestCaseData(Dictionaries.Create("A", 2, "B", 3), Dictionaries.Create("A", 2, "B", 3)) .SetName("Should return that dictionaries are equals") .Returns(true)); yield return(new TestCaseData(Dictionaries.Create("A", 2, "B", 3), Dictionaries.Create("B", 3, "A", 2)) .SetName("Should return that dictionaries created in reverse order are equals") .Returns(true)); yield return(new TestCaseData(Dictionaries.Create("A", 2, "B", 3), Dictionaries.Create("A", 2, "B", 4)) .SetName("Should return that dictionaries are not equals") .Returns(false)); yield return(new TestCaseData(null, null) .SetName("Should return that nulls are equals") .Returns(true)); yield return(new TestCaseData(null, "123") .SetName("Should return that null are not equals to object") .Returns(false)); yield return(new TestCaseData(1324, null) .SetName("Should return that object is not equals to null") .Returns(false)); yield return(new TestCaseData(Sets.AsSet(1, 2, 3), Sets.AsSet(3, 2, 1)) .SetName("Should return that sets are equals") .Returns(true)); yield return(new TestCaseData(Sets.EmptySet <int>(), Sets.EmptySet <int>()) .SetName("Should return that empty sets are equals") .Returns(true)); yield return(new TestCaseData(Sets.EmptySet <int>(), Sets.EmptySet <string>()) .SetName("Should return that empty sets with different types are not equals") .Returns(false)); yield return(new TestCaseData(Sets.AsSet(1, 2, 3), Sets.AsSet(3, 2)) .SetName("Should return that sets are not equals") .Returns(false)); }
private static IEnumerable <ITestCaseData> HashCodeTestCases() { object first = Lists.AsList(1, 2, 3, 4); object second = Lists.AsList(1, 2, 3, 4); yield return(new TestCaseData(first, second) .SetName("Should generate same hash codes for lists with exact elements") .Returns(true)); first = Lists.AsList(Lists.AsList("A", "B", "C"), Lists.AsList("C", "D"), Lists.EmptyList <string>()); second = Lists.AsList(Lists.AsList("A", "B", "C"), Lists.AsList("C", "D"), Lists.EmptyList <string>()); yield return(new TestCaseData(first, second) .SetName("Should generate same hash codes for nested lists with exact elements") .Returns(true)); first = Lists.AsList(Lists.AsList("A", "B", "C"), Lists.AsList("C", "D"), Lists.EmptyList <string>()); second = Lists.AsList(Lists.AsList("A", "B", "C"), Lists.AsList("C", "D"), Lists.AsList("X")); yield return(new TestCaseData(first, second) .SetName("Should generate different hash codes for nested lists with different elements") .Returns(false)); first = new object[] { "A", 3, new DateTime(2015, 6, 9, 0, 0, 0) }; second = new object[] { "A", 3, new DateTime(2015, 6, 9, 0, 0, 0) }; yield return(new TestCaseData(first, second) .SetName("Should generate same hash codes for arrays with exact elements") .Returns(true)); first = Lists.AsList(1, 2, 3, 4); second = Lists.AsList(1, 2, 4, 3); yield return(new TestCaseData(first, second) .SetName("Should generate different hash codes for lists with different elements order") .Returns(false)); first = Lists.AsList(1, 2, 3, 4); second = Lists.AsList(1, 2, 3); yield return(new TestCaseData(first, second) .SetName("Should generate different hash codes for lists with different elements count") .Returns(false)); first = Lists.EmptyList <string>(); second = Lists.AsList("A"); yield return(new TestCaseData(first, second) .SetName("Should generate different hash codes for empty list and not empty list") .Returns(false)); first = "test"; second = "test"; yield return(new TestCaseData(first, second) .SetName("Should generate same hash codes for same strings") .Returns(true)); first = 5; second = 5; yield return(new TestCaseData(first, second) .SetName("Should generate same hash codes for same ints") .Returns(true)); first = Dictionaries.Create(1, "A", 2, "B"); second = Dictionaries.Create(1, "A", 2, "B"); yield return(new TestCaseData(first, second) .SetName("Should generate same hash codes for same dictionaries") .Returns(true)); first = Dictionaries.Create(1, "A", 2, "B"); second = Dictionaries.Create(3, "C", 4, "D"); yield return(new TestCaseData(first, second) .SetName("Should generate different hash codes for different dictionaries") .Returns(false)); first = Dictionaries.Create(1, "A", 2, "B"); second = Dictionaries.Create(1, "A"); yield return(new TestCaseData(first, second) .SetName("Should generate same hash codes for different dictionaries sizes") .Returns(false)); first = new HashObject(5); second = new HashObject(5); yield return(new TestCaseData(first, second) .SetName("Should generate same hash codes using GetHashCode implementation") .Returns(true)); first = new HashObject(5); second = new HashObject(1); yield return(new TestCaseData(first, second) .SetName("Should generate different hash codes using GetHashCode implementation") .Returns(false)); first = Lists.AsList(new HashObject(1), new HashObject(2), new HashObject(3)); second = Lists.AsList(new HashObject(1), new HashObject(2), new HashObject(3)); yield return(new TestCaseData(first, second) .SetName("Should generate same hash codes using GetHashCode implementation for list items") .Returns(true)); first = Lists.AsList(new HashObject(1), new HashObject(2), new HashObject(3)); second = Lists.AsList(new HashObject(3), new HashObject(2), new HashObject(1)); yield return(new TestCaseData(first, second) .SetName("Should generate different hash codes using GetHashCode implementation for list items") .Returns(false)); yield return(new TestCaseData(null, null) .SetName("Should generate same hash codes for nulls") .Returns(true)); first = new object(); yield return(new TestCaseData(first, null) .SetName("Should generate different hash codes for null and object") .Returns(false)); }
public void ShouldPutValuesIntoCompositeDictionary(CompositeDictionaryCreator creator) { // given var compositeDictionary = creator.Create <string, int, string>(); // when compositeDictionary.Put("A", 1, "A1"); compositeDictionary.Put("A", 2, "A2"); compositeDictionary.Put("A", 3, "A3"); compositeDictionary.Put("B", 1, "B1"); compositeDictionary.PutAll(Dictionaries.Create( "C", Dictionaries.Create(1, "C1", 2, "C2"), "D", Dictionaries.Create(1, "D1", 2, "D2"))); // then Check.That(compositeDictionary.Count).IsEqualTo(8); Check.That(compositeDictionary.PrimaryKeysCount).IsEqualTo(4); Check.That(compositeDictionary.IsEmpty).IsFalse(); Check.That(compositeDictionary.Get("A", 1)).IsEqualTo("A1"); Check.That(compositeDictionary[CompositeKeys.Of("A", 2)]).IsEqualTo("A2"); Check.That(compositeDictionary.Get(CompositeKeys.Of("A", 3))).IsEqualTo("A3"); Check.That(compositeDictionary.Get("B", 1)).IsEqualTo("B1"); Check.That(compositeDictionary.Get("C", 1)).IsEqualTo("C1"); Check.That(compositeDictionary.Get("C", 2)).IsEqualTo("C2"); Check.That(compositeDictionary.Get("D", 1)).IsEqualTo("D1"); Check.That(compositeDictionary.Get("D", 2)).IsEqualTo("D2"); Check.That(compositeDictionary.Values).HasSize(8).And .Contains("A1", "A2", "A3", "B1", "C1", "C2", "D1", "D2"); Check.That(compositeDictionary.Entries).HasSize(8).And.Contains( Dictionaries.Entry("A", Dictionaries.Entry(1, "A1")), Dictionaries.Entry("A", Dictionaries.Entry(2, "A2")), Dictionaries.Entry("A", Dictionaries.Entry(3, "A3")), Dictionaries.Entry("B", Dictionaries.Entry(1, "B1")), Dictionaries.Entry("C", Dictionaries.Entry(1, "C1")), Dictionaries.Entry("C", Dictionaries.Entry(2, "C2")), Dictionaries.Entry("D", Dictionaries.Entry(1, "D1")), Dictionaries.Entry("D", Dictionaries.Entry(2, "D2"))); Check.That(compositeDictionary.PrimaryKeys).HasSize(4).And .Contains("A", "B", "C", "D"); Check.That(compositeDictionary.SecondaryKeys).HasSize(3).And .Contains(1, 2, 3); Check.That(compositeDictionary.PrimaryKeyEntries("A")).HasSize(3).And .Contains(Dictionaries.Entry(1, "A1"), Dictionaries.Entry(2, "A2"), Dictionaries.Entry(3, "A3")); Check.That(compositeDictionary.PrimaryKeyEntries("B")).HasSize(1).And .Contains(Dictionaries.Entry(1, "B1")); Check.That(compositeDictionary.PrimaryKeyEntries("C")).HasSize(2).And .Contains(Dictionaries.Entry(1, "C1"), Dictionaries.Entry(2, "C2")); Check.That(compositeDictionary.PrimaryKeyEntries("D")).HasSize(2).And .Contains(Dictionaries.Entry(1, "D1"), Dictionaries.Entry(2, "D2")); Check.That(compositeDictionary.PrimaryKeyValues("A")).HasSize(3).And.Contains("A1", "A2", "A3"); Check.That(compositeDictionary.PrimaryKeyValues("B")).HasSize(1).And.Contains("B1"); Check.That(compositeDictionary.PrimaryKeyValues("C")).HasSize(2).And.Contains("C1", "C2"); Check.That(compositeDictionary.PrimaryKeyValues("D")).HasSize(2).And.Contains("D1", "D2"); Check.That(compositeDictionary.SecondaryKeyEntries(1)).HasSize(4).And .Contains(Dictionaries.Entry("A", "A1"), Dictionaries.Entry("B", "B1"), Dictionaries.Entry("C", "C1"), Dictionaries.Entry("D", "D1")); Check.That(compositeDictionary.SecondaryKeyEntries(2)).HasSize(3).And .Contains(Dictionaries.Entry("A", "A2"), Dictionaries.Entry("C", "C2"), Dictionaries.Entry("D", "D2")); Check.That(compositeDictionary.SecondaryKeyEntries(3)).HasSize(1).And .Contains(Dictionaries.Entry("A", "A3")); Check.That(compositeDictionary.SecondaryKeyValues(1)).HasSize(4).And.Contains("A1", "B1", "C1", "D1"); Check.That(compositeDictionary.SecondaryKeyValues(2)).HasSize(3).And.Contains("A2", "C2", "D2"); Check.That(compositeDictionary.SecondaryKeyValues(3)).HasSize(1).And.Contains("A3"); }