committed by
Dan Walmsley
6 changed files with 184 additions and 2 deletions
@ -0,0 +1,22 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0"> |
|||
<PropertyGroup> |
|||
<TargetFrameworks>netcoreapp2.0;net47</TargetFrameworks> |
|||
<LangVersion>latest</LangVersion> |
|||
<OutputType>Library</OutputType> |
|||
<IsTestProject>true</IsTestProject> |
|||
</PropertyGroup> |
|||
<Import Project="..\..\build\UnitTests.NetCore.targets" /> |
|||
<Import Project="..\..\build\UnitTests.NetFX.props" /> |
|||
<Import Project="..\..\build\Moq.props" /> |
|||
<Import Project="..\..\build\XUnit.props" /> |
|||
<Import Project="..\..\build\Rx.props" /> |
|||
<Import Project="..\..\build\Microsoft.Reactive.Testing.props" /> |
|||
<Import Project="..\..\build\Base.props" /> |
|||
<ItemGroup> |
|||
<ProjectReference Include="..\..\src\Avalonia.Controls.DataGrid\Avalonia.Controls.DataGrid.csproj" /> |
|||
<ProjectReference Include="..\Avalonia.UnitTests\Avalonia.UnitTests.csproj" /> |
|||
</ItemGroup> |
|||
<ItemGroup> |
|||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> |
|||
</ItemGroup> |
|||
</Project> |
|||
@ -0,0 +1,132 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using Avalonia.Collections; |
|||
using Xunit; |
|||
|
|||
namespace Avalonia.Controls.DataGrid.UnitTests.Collections |
|||
{ |
|||
|
|||
public class DataGridSortDescriptionTests |
|||
{ |
|||
[Fact] |
|||
public void OrderBy_Orders_Correctly_When_Ascending() |
|||
{ |
|||
var items = new[] |
|||
{ |
|||
new Item("b", "b"), |
|||
new Item("a", "a"), |
|||
new Item("c", "c"), |
|||
}; |
|||
var expectedResult = items.OrderBy(i => i.Prop1).ToList(); |
|||
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop1), @descending: false); |
|||
|
|||
sortDescription.Initialize(typeof(Item)); |
|||
var result = sortDescription.OrderBy(items).ToList(); |
|||
|
|||
Assert.Equal(expectedResult, result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void OrderBy_Orders_Correctly_When_Descending() |
|||
{ |
|||
var items = new[] |
|||
{ |
|||
new Item("b", "b"), |
|||
new Item("a", "a"), |
|||
new Item("c", "c"), |
|||
}; |
|||
var expectedResult = items.OrderByDescending(i => i.Prop1).ToList(); |
|||
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop1), @descending: true); |
|||
|
|||
sortDescription.Initialize(typeof(Item)); |
|||
var result = sortDescription.OrderBy(items).ToList(); |
|||
|
|||
Assert.Equal(expectedResult, result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ThenBy_Orders_Correctly_When_Ascending() |
|||
{ |
|||
// Casting nonsense below because IOrderedEnumerable<T> isn't covariant in full framework and we need an
|
|||
// object of type IOrderedEnumerable<object> for DataGridSortDescription.ThenBy
|
|||
var items = new[] |
|||
{ |
|||
(object)new Item("a", "b"), |
|||
new Item("a", "a"), |
|||
new Item("a", "c"), |
|||
}.OrderBy(i => ((Item)i).Prop1); |
|||
var expectedResult = new[] |
|||
{ |
|||
new Item("a", "a"), |
|||
new Item("a", "b"), |
|||
new Item("a", "c"), |
|||
}; |
|||
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop2), @descending: false); |
|||
|
|||
sortDescription.Initialize(typeof(Item)); |
|||
var result = sortDescription.ThenBy(items).ToList(); |
|||
|
|||
Assert.Equal(expectedResult, result); |
|||
} |
|||
|
|||
[Fact] |
|||
public void ThenBy_Orders_Correctly_When_Descending() |
|||
{ |
|||
// Casting nonsense below because IOrderedEnumerable<T> isn't covariant in full framework and we need an
|
|||
// object of type IOrderedEnumerable<object> for DataGridSortDescription.ThenBy
|
|||
var items = new[] |
|||
{ |
|||
(object)new Item("a", "b"), |
|||
new Item("a", "a"), |
|||
new Item("a", "c"), |
|||
}.OrderBy(i => ((Item)i).Prop1); |
|||
var expectedResult = new[] |
|||
{ |
|||
new Item("a", "c"), |
|||
new Item("a", "b"), |
|||
new Item("a", "a"), |
|||
}; |
|||
var sortDescription = DataGridSortDescription.FromPath(nameof(Item.Prop2), @descending: true); |
|||
|
|||
sortDescription.Initialize(typeof(Item)); |
|||
var result = sortDescription.ThenBy(items).ToList(); |
|||
|
|||
Assert.Equal(expectedResult, result); |
|||
} |
|||
|
|||
private class Item : IEquatable<Item> |
|||
{ |
|||
public Item(string prop1, string prop2) |
|||
{ |
|||
Prop1 = prop1; |
|||
Prop2 = prop2; |
|||
} |
|||
|
|||
public string Prop1 { get; } |
|||
public string Prop2 { get; } |
|||
|
|||
public bool Equals(Item other) |
|||
{ |
|||
if (ReferenceEquals(null, other)) return false; |
|||
if (ReferenceEquals(this, other)) return true; |
|||
return Prop1 == other.Prop1 && Prop2 == other.Prop2; |
|||
} |
|||
|
|||
public override bool Equals(object obj) |
|||
{ |
|||
if (ReferenceEquals(null, obj)) return false; |
|||
if (ReferenceEquals(this, obj)) return true; |
|||
if (obj.GetType() != this.GetType()) return false; |
|||
return Equals((Item) obj); |
|||
} |
|||
|
|||
public override int GetHashCode() |
|||
{ |
|||
unchecked |
|||
{ |
|||
return ((Prop1 != null ? Prop1.GetHashCode() : 0) * 397) ^ (Prop2 != null ? Prop2.GetHashCode() : 0); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue