public void ShouldExntedReadOnlyDictionaryByToArrayListMultiDictionaryMethod() { // given var dictionary = Dictionaries.CreateImmutable("A", "A1", "B", "B1", "C", "C1"); // when var multiDictionary = dictionary.ToArrayListMultiDictionary(); // then Check.That(multiDictionary) .IsEqualTo(ArrayListMultiDictionary <string, string> .Of("A", "A1", "B", "B1", "C", "C1")); }
public void ShouldExtendEnumberableByToMultiDictionaryMethodWithoutValueConverter() { // given IEnumerable <int> enumerable = Lists.AsList(1, 2, 3); Converter <int, string> keyConverter = element => element.ToString(); // when var multiDictionary = enumerable.ToMultiDictionary(keyConverter); // then Check.That(multiDictionary).IsEqualTo(ArrayListMultiDictionary <string, int> .Of("1", 1, "2", 2, "3", 3)); }
/// <summary> /// Converts collection to MultiDictionary /// </summary> /// <typeparam name="T">Type of collection item</typeparam> /// <typeparam name="TKey">Type of dictionary key</typeparam> /// <typeparam name="TValue">Type of dictionary value</typeparam> /// <param name="source">collection</param> /// <param name="keyConverter">Key converter</param> /// <param name="valueConverter">Value converter</param> /// <returns>MultiDictionary</returns> public static MultiDictionary <TKey, TValue> ToMultiDictionary <T, TKey, TValue>(this IEnumerable <T> source, Converter <T, TKey> keyConverter, Converter <T, TValue> valueConverter) { Preconditions.IsNotNull(source, () => new ArgumentNullException("source")); var result = new ArrayListMultiDictionary <TKey, TValue>(); foreach (var element in source) { var key = keyConverter(element); var value = valueConverter(element); result.Put(key, value); } return(result); }
public void ShouldPreventImmutableMultiDictionaryPutAllMultiEntries(ImmutableMultiDictionaryCreator creator) { // given var immutableMultiDictionary = creator.Create <int, string>(); var entries = ArrayListMultiDictionary <int, string> .Of(1, "A", 1, "B", 2, "A"); // 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>()); }
private static IEnumerable <ITestCaseData> MultiDictionaryComparationCases() { MultiDictionary <int, string> first = new ArrayListMultiDictionary <int, string>(); first.Put(1, "A"); first.Put(2, "B"); first.PutAll(3, Lists.AsList("A", "B", "C")); MultiDictionary <int, string> second = new ArrayListMultiDictionary <int, string>(); second.Put(1, "A"); second.Put(2, "B"); second.PutAll(3, Lists.AsList("A", "B", "C")); yield return(new TestCaseData(first, second) .SetName("Should return that multi dictionaries are equals") .Returns(true)); first = new HashSetMultiDictionary <int, string>(); first.Put(1, "A"); first.Put(2, "B"); first.PutAll(3, Lists.AsList("A", "B", "C")); second = new ArrayListMultiDictionary <int, string>(); second.Put(1, "A"); second.Put(2, "B"); second.PutAll(3, Lists.AsList("A", "B", "C")); yield return(new TestCaseData(first, second) .SetName("Should return that different implementations of multi dictionaries with same elements are equals") .Returns(true)); first = new ArrayListMultiDictionary <int, string>(); first.Put(1, "A"); first.Put(2, "B"); first.PutAll(3, Lists.AsList("A", "B", "C")); second = new ArrayListMultiDictionary <int, string>(); second.Put(1, "A"); second.Put(2, "B"); second.PutAll(3, Lists.AsList("B", "C")); yield return(new TestCaseData(first, second) .SetName("Should return that multi dictionaries with different elements are not equals") .Returns(false)); }
public void ShouldAllowDuplicatedValuesForKey() { // given var multiDictionary = new ArrayListMultiDictionary<int, string>(); // when multiDictionary.Put(1, "A"); multiDictionary.Put(1, "A"); multiDictionary.PutAll(2, Lists.AsList("A", "B", "B")); // then Check.That(multiDictionary.Count).IsEqualTo(5); Check.That(multiDictionary.Values).ContainsExactly("A", "A", "A", "B", "B"); Check.That(multiDictionary[1]).ContainsExactly("A", "A"); Check.That(multiDictionary[1]).IsInstanceOf<List<string>>(); Check.That(multiDictionary[2]).ContainsExactly("A", "B", "B"); Check.That(multiDictionary[2]).IsInstanceOf<List<string>>(); }
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 ShouldConvertArrayListMultiDictionaryToImmutable() { // given var multiDictionary = new ArrayListMultiDictionary <int, string>(); multiDictionary.Put(1, "A"); multiDictionary.PutAll(2, Lists.AsList("A", "B", "B")); multiDictionary.PutAll(3, Lists.AsList("X", "Y")); // when var immutableMultiDictionary = multiDictionary.ToImmutableMultiDictionary(); // then Check.That(immutableMultiDictionary).IsInstanceOf <ImmutableListMultiDictionary <int, string> >(); Check.That(immutableMultiDictionary).IsEqualTo(ImmutableListMultiDictionary <int, string> .Of( 1, "A", 2, "A", 2, "B", 2, "B", 3, "X", 3, "Y")); }
public void ShouldConvertMultiDictionaryToDictionaryWithArrayLists() { // given var multiDictionary = new ArrayListMultiDictionary<int, string>(); multiDictionary.Put(1, "A"); multiDictionary.Put(2, "B"); multiDictionary.PutAll(3, Lists.AsList("A", "B", "C", "C")); // when var dictionary = multiDictionary.ToDictionary(); // then Check.That(dictionary[1]).IsInstanceOf<List<string>>() .And.ContainsExactly("A"); Check.That(dictionary[2]).IsInstanceOf<List<string>>() .And.ContainsExactly("B"); Check.That(dictionary[3]).IsInstanceOf<List<string>>() .And.ContainsExactly("A", "B", "C", "C"); }