diff --git a/Perspex.Base/Collections/PerspexList.cs b/Perspex.Base/Collections/PerspexList.cs index b0775bd4a9..48e34e5ffc 100644 --- a/Perspex.Base/Collections/PerspexList.cs +++ b/Perspex.Base/Collections/PerspexList.cs @@ -115,6 +115,8 @@ namespace Perspex.Collections public void AddRange(IEnumerable items) { + Contract.Requires(items != null); + int index = this.inner.Count; this.inner.AddRange(items); this.NotifyAdd((items as IList) ?? items.ToList(), index); @@ -155,6 +157,8 @@ namespace Perspex.Collections public void InsertRange(int index, IEnumerable items) { + Contract.Requires(items != null); + this.inner.InsertRange(index, items); this.NotifyAdd((items as IList) ?? items.ToList(), index); } @@ -175,6 +179,8 @@ namespace Perspex.Collections public void RemoveAll(IEnumerable items) { + Contract.Requires(items != null); + List removed = new List(); foreach (var i in items) diff --git a/Tests/Perspex.Base.UnitTests/Collections/PerspexListTests.cs b/Tests/Perspex.Base.UnitTests/Collections/PerspexListTests.cs new file mode 100644 index 0000000000..4518fda7db --- /dev/null +++ b/Tests/Perspex.Base.UnitTests/Collections/PerspexListTests.cs @@ -0,0 +1,185 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Base.UnitTests +{ + using System; + using System.Collections.Generic; + using System.Collections.Specialized; + using System.Linq; + using Perspex.Collections; + using Xunit; + + public class PerspexListTests + { + [Fact] + public void Items_Passed_To_Constructor_Should_Appear_In_List() + { + var items = new[] { 1, 2, 3 }; + var target = new PerspexList(items); + + Assert.Equal(items, target); + } + + [Fact] + public void AddRange_With_Null_Should_Throw_Exception() + { + var target = new PerspexList(); + + Assert.Throws(() => target.AddRange(null)); + } + + [Fact] + public void RemoveAll_With_Null_Should_Throw_Exception() + { + var target = new PerspexList(); + + Assert.Throws(() => target.RemoveAll(null)); + } + + [Fact] + public void InsertRange_With_Null_Should_Throw_Exception() + { + var target = new PerspexList(); + + Assert.Throws(() => target.InsertRange(1, null)); + } + + [Fact] + public void InsertRange_Past_End_Should_Throw_Exception() + { + var target = new PerspexList(); + + Assert.Throws(() => target.InsertRange(1, new List() { 1 })); + } + + [Fact] + public void Adding_Item_Should_Raise_CollectionChanged() + { + var target = new PerspexList(new[] { 1, 2 }); + var raised = false; + + target.CollectionChanged += (s, e) => + { + Assert.Equal(target, s); + Assert.Equal(NotifyCollectionChangedAction.Add, e.Action); + Assert.Equal(new[] { 3 }, e.NewItems.Cast()); + Assert.Equal(2, e.NewStartingIndex); + + raised = true; + }; + + target.Add(3); + + Assert.True(raised); + } + + [Fact] + public void Adding_Items_Should_Raise_CollectionChanged() + { + var target = new PerspexList(new[] { 1, 2 }); + var raised = false; + + target.CollectionChanged += (s, e) => + { + Assert.Equal(target, s); + Assert.Equal(NotifyCollectionChangedAction.Add, e.Action); + Assert.Equal(new[] { 3, 4 }, e.NewItems.Cast()); + Assert.Equal(2, e.NewStartingIndex); + + raised = true; + }; + + target.AddRange(new[] { 3, 4 }); + + Assert.True(raised); + } + + [Fact] + public void Inserting_Item_Should_Raise_CollectionChanged() + { + var target = new PerspexList(new[] { 1, 2 }); + var raised = false; + + target.CollectionChanged += (s, e) => + { + Assert.Equal(target, s); + Assert.Equal(NotifyCollectionChangedAction.Add, e.Action); + Assert.Equal(new[] { 3 }, e.NewItems.Cast()); + Assert.Equal(1, e.NewStartingIndex); + + raised = true; + }; + + target.Insert(1, 3); + + Assert.True(raised); + } + + [Fact] + public void Inserting_Items_Should_Raise_CollectionChanged() + { + var target = new PerspexList(new[] { 1, 2 }); + var raised = false; + + target.CollectionChanged += (s, e) => + { + Assert.Equal(target, s); + Assert.Equal(NotifyCollectionChangedAction.Add, e.Action); + Assert.Equal(new[] { 3, 4 }, e.NewItems.Cast()); + Assert.Equal(1, e.NewStartingIndex); + + raised = true; + }; + + target.InsertRange(1, new[] { 3, 4 }); + + Assert.True(raised); + } + + [Fact] + public void Removing_Item_Should_Raise_CollectionChanged() + { + var target = new PerspexList(new[] { 1, 2, 3 }); + var raised = false; + + target.CollectionChanged += (s, e) => + { + Assert.Equal(target, s); + Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action); + Assert.Equal(new[] { 3 }, e.OldItems.Cast()); + Assert.Equal(2, e.OldStartingIndex); + + raised = true; + }; + + target.Remove(3); + + Assert.True(raised); + } + + [Fact] + public void Clearing_Items_Should_Raise_CollectionChanged_Remove() + { + var target = new PerspexList(new[] { 1, 2, 3 }); + var raised = false; + + target.CollectionChanged += (s, e) => + { + Assert.Equal(target, s); + Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action); + Assert.Equal(new[] { 1, 2, 3 }, e.OldItems.Cast()); + Assert.Equal(0, e.OldStartingIndex); + + raised = true; + }; + + target.Clear(); + + Assert.True(raised); + } + } +} diff --git a/Tests/Perspex.Base.UnitTests/Perspex.Base.UnitTests.csproj b/Tests/Perspex.Base.UnitTests/Perspex.Base.UnitTests.csproj index 60eff76db2..abbb9e8365 100644 --- a/Tests/Perspex.Base.UnitTests/Perspex.Base.UnitTests.csproj +++ b/Tests/Perspex.Base.UnitTests/Perspex.Base.UnitTests.csproj @@ -70,6 +70,7 @@ +