|
|
|
@ -52,6 +52,22 @@ namespace Avalonia.Base.UnitTests.Collections |
|
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => target.InsertRange(1, new List<int>() { 1 })); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Move_Should_Move_One_Item() |
|
|
|
{ |
|
|
|
AvaloniaList<int> target = [1, 2, 3]; |
|
|
|
|
|
|
|
AssertEvent(target, () => target.Move(0, 1), [ |
|
|
|
new NotifyCollectionChangedEventArgs( |
|
|
|
NotifyCollectionChangedAction.Move, |
|
|
|
new[] { 1 }, |
|
|
|
1, |
|
|
|
0) |
|
|
|
]); |
|
|
|
|
|
|
|
Assert.Equal(new[] { 2, 1, 3 }, target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Move_Should_Update_Collection() |
|
|
|
{ |
|
|
|
@ -75,9 +91,9 @@ namespace Avalonia.Base.UnitTests.Collections |
|
|
|
[Fact] |
|
|
|
public void MoveRange_Can_Move_To_End() |
|
|
|
{ |
|
|
|
var target = new AvaloniaList<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); |
|
|
|
AvaloniaList<int> target = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
|
|
|
|
|
|
|
target.MoveRange(0, 5, 10); |
|
|
|
target.MoveRange(0, 5, 9); |
|
|
|
|
|
|
|
Assert.Equal(new[] { 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 }, target); |
|
|
|
} |
|
|
|
@ -85,25 +101,35 @@ namespace Avalonia.Base.UnitTests.Collections |
|
|
|
[Fact] |
|
|
|
public void MoveRange_Raises_Correct_CollectionChanged_Event() |
|
|
|
{ |
|
|
|
var target = new AvaloniaList<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); |
|
|
|
var raised = false; |
|
|
|
AvaloniaList<int> target = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; |
|
|
|
|
|
|
|
target.CollectionChanged += (s, e) => |
|
|
|
{ |
|
|
|
Assert.Equal(NotifyCollectionChangedAction.Move, e.Action); |
|
|
|
Assert.Equal(0, e.OldStartingIndex); |
|
|
|
Assert.Equal(10, e.NewStartingIndex); |
|
|
|
Assert.Equal(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, e.OldItems); |
|
|
|
Assert.Equal(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, e.NewItems); |
|
|
|
raised = true; |
|
|
|
}; |
|
|
|
AssertEvent(target, () => target.MoveRange(0, 9, 9), [ |
|
|
|
new NotifyCollectionChangedEventArgs( |
|
|
|
NotifyCollectionChangedAction.Move, |
|
|
|
new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, |
|
|
|
9, |
|
|
|
0) |
|
|
|
]); |
|
|
|
|
|
|
|
target.MoveRange(0, 9, 10); |
|
|
|
|
|
|
|
Assert.True(raised); |
|
|
|
Assert.Equal(new[] { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void MoveRange_Should_Move_One_Item() |
|
|
|
{ |
|
|
|
AvaloniaList<int> target = [1, 2, 3]; |
|
|
|
|
|
|
|
AssertEvent(target, () => target.MoveRange(0, 1, 1), [ |
|
|
|
new NotifyCollectionChangedEventArgs( |
|
|
|
NotifyCollectionChangedAction.Move, |
|
|
|
new[] { 1 }, |
|
|
|
1, |
|
|
|
0) |
|
|
|
]); |
|
|
|
|
|
|
|
Assert.Equal(new[] { 2, 1, 3 }, target); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public void Adding_Item_Should_Raise_CollectionChanged() |
|
|
|
{ |
|
|
|
@ -526,5 +552,48 @@ namespace Avalonia.Base.UnitTests.Collections |
|
|
|
|
|
|
|
Assert.Equal(0, raised); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Assert that <see cref="items"/> emits <see cref="expectedEvents"/> when performing <see cref="action"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="items">The event source.</param>
|
|
|
|
/// <param name="action">The action to perform.</param>
|
|
|
|
/// <param name="expectedEvents">The expected events.</param>
|
|
|
|
private static void AssertEvent(INotifyCollectionChanged items, Action action, NotifyCollectionChangedEventArgs[] expectedEvents) |
|
|
|
{ |
|
|
|
var callCount = 0; |
|
|
|
items.CollectionChanged += OnCollectionChanged; |
|
|
|
|
|
|
|
Assert.Multiple(() => |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
action(); |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
items.CollectionChanged -= OnCollectionChanged; |
|
|
|
} |
|
|
|
|
|
|
|
Assert.Equal(expectedEvents.Length, callCount); |
|
|
|
}); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs actualEvent) |
|
|
|
{ |
|
|
|
Assert.Multiple(() => |
|
|
|
{ |
|
|
|
Assert.True(callCount < expectedEvents.Length); |
|
|
|
Assert.Equal(expectedEvents[callCount].Action, actualEvent.Action); |
|
|
|
Assert.Equal(expectedEvents[callCount].NewItems, actualEvent.NewItems); |
|
|
|
Assert.Equal(expectedEvents[callCount].NewStartingIndex, actualEvent.NewStartingIndex); |
|
|
|
Assert.Equal(expectedEvents[callCount].OldItems, actualEvent.OldItems); |
|
|
|
Assert.Equal(expectedEvents[callCount].OldStartingIndex, actualEvent.OldStartingIndex); |
|
|
|
}); |
|
|
|
|
|
|
|
++callCount; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|