Skip to content

Commit

Permalink
Optimize ListEx.Clone by using the is operator (#386)
Browse files Browse the repository at this point in the history
Fix inconsistent indentation
Add unit test for ListEx.Clone for Move on a plain List

Co-authored-by: John Cummings <jcummings2sf@gmail.com>
  • Loading branch information
jcummings2 and John Cummings authored Aug 1, 2020
1 parent 6912429 commit ef688db
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
27 changes: 27 additions & 0 deletions src/DynamicData.Tests/List/CloneChangesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,32 @@ public void MovedItemInObservableCollectionIsMoved()

itemMoved.Should().BeTrue();
}

[Fact]
public void MovedItemInListLowerToHigherIsMoved()
{
_source.AddRange(Enumerable.Range(1, 10));
_source.Move(1, 2);

var changes = _source.CaptureChanges();

_clone.Clone(changes);

_clone.Should().BeEquivalentTo(_source);
}

[Fact]
public void MovedItemInListHigherToLowerIsMoved()
{
_source.AddRange(Enumerable.Range(1, 10));
_source.Move(2, 1);

var changes = _source.CaptureChanges();

_clone.Clone(changes);

_clone.Should().BeEquivalentTo(_source);
}

}
}
12 changes: 5 additions & 7 deletions src/DynamicData/List/ListEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,17 +228,15 @@ private static void Clone<T>(this IList<T> source, Change<T> item, IEqualityCom
var change = item.Item;
bool hasIndex = change.CurrentIndex >= 0;
if (!hasIndex)
{
throw new UnspecifiedIndexException("Cannot move as an index was not specified");
}
{
throw new UnspecifiedIndexException("Cannot move as an index was not specified");
}

var extendedList = source as IExtendedList<T>;
var observableCollection = source as ObservableCollection<T>;
if (extendedList != null)
if (source is IExtendedList<T> extendedList)
{
extendedList.Move(change.PreviousIndex, change.CurrentIndex);
}
else if (observableCollection != null)
else if (source is ObservableCollection<T> observableCollection)
{
observableCollection.Move(change.PreviousIndex, change.CurrentIndex);
}
Expand Down

0 comments on commit ef688db

Please sign in to comment.