Example #1
0
 public static ICollection <T> SetElements <T>(this ICollection <T> mutable, IEnumerable <T> seq)
 {
     seq = seq.ToArray();
     mutable.Clear();
     mutable.AddElements(seq);
     return(mutable);
 }
        public void AddElements_requires_the_list()
        {
            // Arrange
            ICollection <int> list = null;
            var    newElements     = Enumerable.Range(3, 3).ToArray();
            Action action          = () => list.AddElements(newElements);

            // Act
            // Assert
            action.Should().Throw <ArgumentNullException>();
        }
        public void AddElements_returns_the_collection()
        {
            // Arrange
            ICollection <int> list = Enumerable.Range(0, 3).ToList();

            // Act
            var result = list.AddElements(1, 2);

            // Assert
            result.Should().BeSameAs(list);
        }
        public void AddElements_adds_multiple_elements()
        {
            // Arrange
            ICollection <int> list = Enumerable.Range(0, 3).ToList();
            var expected           = Enumerable.Range(0, 6).ToList();

            // Act
            list.AddElements(3, 4, 5);

            // Assert
            list.Should().BeEquivalentTo(expected);
        }
Example #5
0
        public static void Run_AddElements_Example()
        {
            var element1 = new Element {
                Key = 2, Name = "Second"
            };
            var element2 = new Element {
                Key = 3, Name = "Third"
            };
            ICollection <Element> collection = GetSomeCollection();

            collection.AddElements(element1, element2);

            WriteResult(collection);
        }
Example #6
0
        public void Add_WithNullCollection_ShouldNotAddAllElements()
        {
            // Arrange

            string a = null;
            string b = "";
            string c = "x";
            ICollection <string> list = null;

            // Act

            list.AddElements(a, b, c);

            // Assert

            Assert.Null(list);
        }
        /// <summary>
        /// Joins two collections based on a key selector, and applies an action on each couple from the join operation.
        /// Then it adds elements that do not exist in the outer collection and removes elements that do not exist in the inner collection.
        /// </summary>
        /// <typeparam name="TOuter">Type of the outer collection.</typeparam>
        /// <typeparam name="TInner">Type of the inner collection.</typeparam>
        /// <typeparam name="TKey">Type of the key selector.</typeparam>
        /// <param name="list1">The outer collection.</param>
        /// <param name="list2">The inner collection.</param>
        /// <param name="selector1">The selector for the outer collection.</param>
        /// <param name="selector2">The selector for the inner collection.</param>
        /// <param name="updateAction">The action to apply on each joined couple.</param>
        /// <param name="addAction">The function to create and item to add from an item of the inner list.</param>
        public static void Merge <TOuter, TInner, TKey>(this ICollection <TOuter> list1, IEnumerable <TInner> list2, Func <TOuter, TKey> selector1, Func <TInner, TKey> selector2, Action <TOuter, TInner> updateAction, Func <TInner, TOuter> addAction)
        {
            // Update matching elements
            var matchingElements = list1
                                   .Apply(
                list2,
                selector1,
                selector2,
                updateAction)
                                   .ToList();

            // Remove elements not existing in the inner list
            var toRemove = list1.Except(matchingElements).ToList();

            list1.RemoveRange(toRemove);

            // Add elements not existing in the outer list
            var toAdd = list2.Where(x => !matchingElements.Any(y => selector2(x).Equals(selector1(y)))).ToList();

            list1.AddElements(toAdd.Select(addAction));
        }
Example #8
0
 public static ICollection <T> AddElements <T>(this ICollection <T> mutable, params T[] addendum)
 {
     return(mutable.AddElements((IEnumerable <T>)addendum));
 }