using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using Developpez.Dotnet.Windows.Collections; using NUnit.Framework; namespace Developpez.Dotnet.Windows.Tests.Collections { [TestFixture] public class RangeObservableCollectionTests { [Test] public void Test_AddRange() { var collection = new RangeObservableCollection(); string expectedEvents = "+1+3+2+1"; var expectedCollection = new List(); StringBuilder sbEvents = new StringBuilder(); collection.CollectionChanged += (sender, e) => { Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action); sbEvents.AppendFormat("+{0}", e.NewItems.Count); }; collection.Add(1); expectedCollection.Add(1); collection.AddRange(new[] { 2, 3, 4 }); expectedCollection.AddRange(new[] { 2, 3, 4 }); collection.AddRange(new[] { 5, 6 }); expectedCollection.AddRange(new[] { 5, 6 }); collection.Add(7); expectedCollection.Add(7); string actualEvents = sbEvents.ToString(); Assert.AreEqual(expectedEvents, actualEvents); Assert.IsTrue(expectedCollection.SequenceEqual(collection)); } [Test] public void Test_InsertRange() { var collection = new RangeObservableCollection(); string expectedEvents = "+1+3+2+1"; var expectedCollection = new List(); StringBuilder sbEvents = new StringBuilder(); collection.CollectionChanged += (sender, e) => { Assert.AreEqual(NotifyCollectionChangedAction.Add, e.Action); sbEvents.AppendFormat("+{0}", e.NewItems.Count); }; collection.Insert(0, 1); expectedCollection.Insert(0, 1); collection.InsertRange(0, new[] { 2, 3, 4 }); expectedCollection.InsertRange(0, new[] { 2, 3, 4 }); collection.InsertRange(2, new[] { 5, 6 }); expectedCollection.InsertRange(2, new[] { 5, 6 }); collection.Insert(3, 7); expectedCollection.Insert(3, 7); string actualEvents = sbEvents.ToString(); Assert.AreEqual(expectedEvents, actualEvents); Assert.IsTrue(expectedCollection.SequenceEqual(collection)); } [Test] public void Test_RemoveRange() { var collection = new RangeObservableCollection(Enumerable.Range(0, 10)); string expectedEvents = "-1-3-2-1"; var expectedCollection = Enumerable.Range(0, 10).ToList(); StringBuilder sbEvents = new StringBuilder(); collection.CollectionChanged += (sender, e) => { Assert.AreEqual(NotifyCollectionChangedAction.Remove, e.Action); sbEvents.AppendFormat("-{0}", e.OldItems.Count); }; collection.RemoveAt(0); expectedCollection.RemoveAt(0); collection.RemoveRange(0, 3); expectedCollection.RemoveRange(0, 3); collection.RemoveRange(2, 2); expectedCollection.RemoveRange(2, 2); collection.RemoveAt(3); expectedCollection.RemoveAt(3); string actualEvents = sbEvents.ToString(); Assert.AreEqual(expectedEvents, actualEvents); Assert.IsTrue(expectedCollection.SequenceEqual(collection)); } [Test] public void Test_RemoveAll() { var collection = new RangeObservableCollection(Enumerable.Range(0, 10)); string expectedEvents = "-3-3-1"; var expectedCollection = Enumerable.Range(0, 10).ToList(); StringBuilder sbEvents = new StringBuilder(); collection.CollectionChanged += (sender, e) => { Assert.AreEqual(NotifyCollectionChangedAction.Remove, e.Action); sbEvents.AppendFormat("-{0}", e.OldItems.Count); }; Predicate notDivisibleBy4 = (i) => i % 4 != 0; collection.RemoveAll(notDivisibleBy4); expectedCollection.RemoveAll(notDivisibleBy4); string actualEvents = sbEvents.ToString(); Assert.AreEqual(expectedEvents, actualEvents); Assert.IsTrue(expectedCollection.SequenceEqual(collection)); } [Test] public void Test_ReplaceRange() { var collection = new RangeObservableCollection(Enumerable.Range(0, 10)); string expectedEvents = "-3-2"; var expectedCollection = new[] { 0, 1, 4, 3, 2, 6, 5, 7, 8, 9}; StringBuilder sbEvents = new StringBuilder(); collection.CollectionChanged += (sender, e) => { Assert.AreEqual(NotifyCollectionChangedAction.Replace, e.Action); Assert.AreEqual(e.OldItems.Count, e.NewItems.Count); sbEvents.AppendFormat("-{0}", e.OldItems.Count); }; collection.ReplaceRange(2, new[] { 4, 3, 2 }); collection.ReplaceRange(5, new[] { 6, 5 }); string actualEvents = sbEvents.ToString(); Assert.AreEqual(expectedEvents, actualEvents); Assert.IsTrue(expectedCollection.SequenceEqual(collection)); } } }