Browse Source

Started adding PerspexList tests.

pull/39/head
Steven Kirk 11 years ago
parent
commit
1befdd41c5
  1. 6
      Perspex.Base/Collections/PerspexList.cs
  2. 185
      Tests/Perspex.Base.UnitTests/Collections/PerspexListTests.cs
  3. 1
      Tests/Perspex.Base.UnitTests/Perspex.Base.UnitTests.csproj

6
Perspex.Base/Collections/PerspexList.cs

@ -115,6 +115,8 @@ namespace Perspex.Collections
public void AddRange(IEnumerable<T> items)
{
Contract.Requires<ArgumentNullException>(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<T> items)
{
Contract.Requires<ArgumentNullException>(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<T> items)
{
Contract.Requires<ArgumentNullException>(items != null);
List<T> removed = new List<T>();
foreach (var i in items)

185
Tests/Perspex.Base.UnitTests/Collections/PerspexListTests.cs

@ -0,0 +1,185 @@
// -----------------------------------------------------------------------
// <copyright file="PerspexListTests.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
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<int>(items);
Assert.Equal(items, target);
}
[Fact]
public void AddRange_With_Null_Should_Throw_Exception()
{
var target = new PerspexList<int>();
Assert.Throws<ArgumentNullException>(() => target.AddRange(null));
}
[Fact]
public void RemoveAll_With_Null_Should_Throw_Exception()
{
var target = new PerspexList<int>();
Assert.Throws<ArgumentNullException>(() => target.RemoveAll(null));
}
[Fact]
public void InsertRange_With_Null_Should_Throw_Exception()
{
var target = new PerspexList<int>();
Assert.Throws<ArgumentNullException>(() => target.InsertRange(1, null));
}
[Fact]
public void InsertRange_Past_End_Should_Throw_Exception()
{
var target = new PerspexList<int>();
Assert.Throws<ArgumentOutOfRangeException>(() => target.InsertRange(1, new List<int>() { 1 }));
}
[Fact]
public void Adding_Item_Should_Raise_CollectionChanged()
{
var target = new PerspexList<int>(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<int>());
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<int>(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<int>());
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<int>(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<int>());
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<int>(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<int>());
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<int>(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<int>());
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<int>(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<int>());
Assert.Equal(0, e.OldStartingIndex);
raised = true;
};
target.Clear();
Assert.True(raised);
}
}
}

1
Tests/Perspex.Base.UnitTests/Perspex.Base.UnitTests.csproj

@ -70,6 +70,7 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="Collections\PerspexListTests.cs" />
<Compile Include="PerspexPropertyTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="PerspexObjectTests.cs" />

Loading…
Cancel
Save