public void DuplicatedResultsResultInNoAdditionalMessage()
        {
            _source.Edit(
                list =>
            {
                list.Add(new Person("Person1", 20));
                list.Add(new Person("Person1", 20));
                list.Add(new Person("Person1", 20));
            });

            _results.Messages.Count.Should().Be(1, "Should be 1 update message");
            _results.Data.Count.Should().Be(1, "Should be 1 items in the cache");
            _results.Data.Items.First().Should().Be(20, "Should 20");
        }
Beispiel #2
0
        public void FiresBatchResultOnce()
        {
            _source.Edit(list =>
            {
                list.Add(new Person("Person1", 20));
                list.Add(new Person("Person2", 21));
                list.Add(new Person("Person3", 22));
            });

            Assert.AreEqual(1, _results.Messages.Count, "Should be 1 updates");
            Assert.AreEqual(3, _results.Data.Count, "Should be 3 items in the cache");

            CollectionAssert.AreEquivalent(new[] { 20, 21, 22 }, _results.Data.Items);
            Assert.AreEqual(20, _results.Data.Items.First(), "Should 20");
        }
Beispiel #3
0
        public void FiresBatchResultOnce()
        {
            _source.Edit(list =>
            {
                list.Add(new Person("Person1", 20));
                list.Add(new Person("Person2", 21));
                list.Add(new Person("Person3", 22));
            });

            _results.Messages.Count.Should().Be(1, "Should be 1 updates");
            _results.Data.Count.Should().Be(3, "Should be 3 items in the cache");

            _results.Data.Items.ShouldAllBeEquivalentTo(new[] { 20, 21, 22 });
            _results.Data.Items.First().Should().Be(20, "Should 20");
        }
Beispiel #4
0
        public void AddNotMatchedAndUpdateMatched()
        {
            const string key        = "Adult1";
            var          notmatched = new Person(key, 19);
            var          matched    = new Person(key, 21);

            _source.Edit(list =>
            {
                list.Add(notmatched);
                list.Add(matched);
            });

            _results.Messages.Count.Should().Be(1, "Should be 1 updates");
            _results.Messages[0].First().Range.First().Should().Be(matched, "Should be same person");
            _results.Data.Items.First().Should().Be(matched, "Should be same person");
        }
Beispiel #5
0
        public void HandleError()
        {
            Exception exception = null;

            _source.Edit(innerList => innerList.RemoveAt(1), ex => exception = ex);
            exception.Should().NotBeNull();
        }
        private void HandleSelectionChanged(SelectionChangedEventArgs args)
        {
            if (_isSelecting)
            {
                return;
            }
            try
            {
                _isSelecting = true;

                _sourceList.Edit(list =>
                {
                    var added = args.AddedItems.OfType <T>().ToList();
                    list.AddRange(added);

                    //cannot remove batch as we do not know whether they are in order
                    args.RemovedItems.OfType <T>()
                    .ForEach(t => list.Remove(t));
                });
            }
            finally
            {
                _isSelecting = false;
            }
        }
Beispiel #7
0
        public void HandleError()
        {
            Exception exception = null;

            _source.Edit(innerList => innerList.RemoveAt(1), ex => exception = ex);
            Assert.IsNotNull(exception);
        }
Beispiel #8
0
    public static void SetTo <T>(this ISourceList <T> list, IEnumerable <T> items, bool checkEquality = false)
    {
        list.Edit(l =>
        {
            int i = 0;
            foreach (var item in items)
            {
                if (i >= l.Count)
                {
                    l.Add(item);
                }
                else if (checkEquality)
                {
                    if (!EqualityComparer <T> .Default.Equals(l[i], item))
                    {
                        l[i] = item;
                    }
                }
                else
                {
                    l[i] = item;
                }
                i++;
            }

            l.RemoveToCount(i);
        });
    }
        public void AddNotMatchedAndUpdateMatched()
        {
            const string key        = "Adult1";
            var          notmatched = new Person(key, 19);
            var          matched    = new Person(key, 21);

            _source.Edit(updater =>
            {
                updater.Add(notmatched);
                updater.Add(matched);
            });

            Assert.AreEqual(1, _results.Messages.Count, "Should be 1 updates");
            Assert.AreEqual(matched, _results.Messages[0].First().Range.First(), "Should be same person");
            Assert.AreEqual(matched, _results.Data.Items.First(), "Should be same person");
        }
Beispiel #10
0
 /// <summary>
 /// Removes the items from source in an optimised manner
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source">The source.</param>
 /// <param name="itemsToRemove">The items to remove.</param>
 /// <exception cref="System.ArgumentNullException"></exception>
 public static void RemoveMany <T>([NotNull] this ISourceList <T> source, IEnumerable <T> itemsToRemove)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Edit(list => list.RemoveMany(itemsToRemove));
 }
Beispiel #11
0
 /// <summary>
 /// Replaces the item at
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source">The source.</param>
 /// <param name="index">The index.</param>
 /// <param name="item">The item.</param>
 public static void Replace <T>([NotNull] this ISourceList <T> source, int index, T item)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Edit(list => list[index] = item);
 }
Beispiel #12
0
 /// <summary>
 /// Clears all items from the specified source list
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source">The source.</param>
 public static void Clear <T>([NotNull] this ISourceList <T> source)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Edit(list => list.Clear());
 }
Beispiel #13
0
 /// <summary>
 /// Replaces the specified original with the destinaton object
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source">The source.</param>
 /// <param name="original">The original.</param>
 /// <param name="destination">The destination.</param>
 public static void Replace <T>([NotNull] this ISourceList <T> source, T original, T destination)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Edit(list => list.Replace(original, destination));
 }
Beispiel #14
0
 /// <summary>
 /// Inserts the elements of a collection into the <see cref="T:System.Collections.Generic.List`1" /> at the specified index.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source">The source.</param>
 /// <param name="items">The items.</param>
 /// <param name="index">The zero-based index at which the new elements should be inserted.</param>
 public static void InsertRange <T>([NotNull] this ISourceList <T> source, IEnumerable <T> items, int index)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Edit(list => list.AddRange(items, index));
 }
Beispiel #15
0
 /// <summary>
 /// Removes a range of elements from the <see cref="T:System.Collections.Generic.List`1" />.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source">The source.</param>
 /// <param name="index">The zero-based starting index of the range of elements to remove.</param>
 /// <param name="count">The number of elements to remove.</param>
 /// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index" /> is less than 0.-or-<paramref name="count" /> is less than 0.</exception>
 /// <exception cref="T:System.ArgumentException"><paramref name="index" /> and <paramref name="count" /> do not denote a valid range of elements in the <see cref="T:System.Collections.Generic.List`1" />.</exception>
 public static void RemoveRange <T>([NotNull] this ISourceList <T> source, int index, int count)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Edit(list => list.RemoveRange(index, count));
 }
Beispiel #16
0
 /// <summary>
 /// Removes the specified item from the source list
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source">The source.</param>
 /// <param name="item">The item.</param>
 public static void Remove <T>([NotNull] this ISourceList <T> source, T item)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Edit(list => list.Remove(item));
 }
Beispiel #17
0
 /// <summary>
 /// Adds the specified item to the source list
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="source">The source.</param>
 /// <param name="item">The item.</param>
 /// <param name="index">The index.</param>
 public static void Insert <T>([NotNull] this ISourceList <T> source, int index, T item)
 {
     if (source == null)
     {
         throw new ArgumentNullException(nameof(source));
     }
     source.Edit(list => list.Insert(index, item));
 }
        /// <summary>
        /// Removes the element at the specified index.
        /// </summary>
        /// <typeparam name="T">The item type.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="index">The index.</param>
        public static void RemoveAt <T>(this ISourceList <T> source, int index)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            source.Edit(list => list.RemoveAt(index));
        }
        /// <summary>
        /// Adds the specified item to the source list.
        /// </summary>
        /// <typeparam name="T">The item type.</typeparam>
        /// <param name="source">The source list.</param>
        /// <param name="item">The item to add.</param>
        public static void Add <T>(this ISourceList <T> source, T item)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            source.Edit(list => list.Add(item));
        }
        /// <summary>
        /// Adds the specified items to the source list.
        /// </summary>
        /// <typeparam name="T">The item type.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="items">The items.</param>
        public static void AddRange <T>(this ISourceList <T> source, IEnumerable <T> items)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            source.Edit(list => list.AddRange(items));
        }
Beispiel #21
0
        public void FiresManyValueForBatchOfDifferentAdds()
        {
            _source.Edit(updater =>
            {
                updater.Add(new Person("Person1", 20));
                updater.Add(new Person("Person2", 21));
                updater.Add(new Person("Person3", 22));
                updater.Add(new Person("Person4", 23));
            });

            _results.Data.Count.Should().Be(4);
            _results.Messages.Count.Should().Be(1);
            _results.Messages.First().Count.Should().Be(1);
            foreach (var update in _results.Messages.First())
            {
                update.Reason.Should().Be(ListChangeReason.AddRange);
            }
        }
Beispiel #22
0
        public void FiresManyValueForBatchOfDifferentAdds()
        {
            _source.Edit(updater =>
            {
                updater.Add(new Person("Person1", 20));
                updater.Add(new Person("Person2", 21));
                updater.Add(new Person("Person3", 22));
                updater.Add(new Person("Person4", 23));
            });

            Assert.AreEqual(4, _results.Data.Count);
            Assert.AreEqual(1, _results.Messages.Count);
            Assert.AreEqual(1, _results.Messages.First().Count);
            foreach (var update in _results.Messages.First())
            {
                Assert.AreEqual(ListChangeReason.AddRange, update.Reason);
            }
        }
        /// <summary>
        /// Moves an item from the original to the destination index.
        /// </summary>
        /// <typeparam name="T">The item type.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="original">The original.</param>
        /// <param name="destination">The destination.</param>
        public static void Move <T>(this ISourceList <T> source, int original, int destination)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            source.Edit(list => list.Move(original, destination));
        }
 public static void Edit <T>(this ISourceList <T> source, IChangeSet <T> changes)
 {
     source.Edit(a =>
     {
         foreach (var c in changes)
         {
             a.Edit(c);
         }
     });
 }
Beispiel #25
0
 public static void RemoveToCount <T>(this ISourceList <T> list, int count)
 {
     list.Edit(l =>
     {
         var toRemove = l.Count - count;
         for (; toRemove > 0; toRemove--)
         {
             l.RemoveAt(l.Count - 1);
         }
     });
 }
        /// <summary>
        /// Removes the specified item from the source list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The source.</param>
        /// <param name="item">The item.</param>
        public static bool Remove <T>([NotNull] this ISourceList <T> source, T item)
        {
            bool removed = false;

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            source.Edit(list => removed = list.Remove(item));
            return(removed);
        }
Beispiel #27
0
        public void SetSelection(IEnumerable <Tag> tags)
        {
            if (tags == null)
            {
                return;
            }

            selection.Edit(eList =>
            {
                eList.Clear();
                eList.AddRange(tags);
            });
        }
Beispiel #28
0
 private static Task CreateTaskInitList <TValue>(Task <List <TValue> > loadTask, ISourceList <TValue> cache)
 {
     return(loadTask
            .ContinueWith(task =>
     {
         cache.Edit(innerCache =>
         {
             innerCache.Clear();
             innerCache.AddRange(task.Result);
         });
         return task.Result;
     }, TaskContinuationOptions.OnlyOnRanToCompletion));
 }
Beispiel #29
0
        public void Edit(IEnumerable <T> items)
        {
            _source.Edit(innerList =>
            {
                var originalItems = innerList.AsArray();
                var newItems      = items.AsArray();

                var removes = originalItems.Except(newItems, _equalityComparer);
                var adds    = newItems.Except(originalItems, _equalityComparer);

                innerList.Remove(removes);
                innerList.AddRange(adds);
            });
        }
Beispiel #30
0
        public void RecursiveEditsWork()
        {
            _source.Edit(l =>
            {
                _source.Edit(l2 => l2.Add(1));
                Assert.True(_source.Items.SequenceEqual(new[] { 1 }));
                Assert.True(l.SequenceEqual(new[] { 1 }));
            });

            Assert.True(_source.Items.SequenceEqual(new [] { 1 }));
        }