-
-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove from transform many with projection #265
Merged
RolandPheasant
merged 5 commits into
reactivemarbles:master
from
dchaib:RemoveFromTransformManyWithProjection
Aug 13, 2019
+220
−9
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e1baf5
Add failing unit tests
dchaib 5d281c2
Add an equality comparer in IChangeSet for lists
dchaib e4e0327
Pass the equality comparer to the Clone method directly from Transfor…
dchaib 0f3072c
Add equality comparer to other calls to Clone method in TransformMany
dchaib dea6cc3
Bump minor version
dchaib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
src/DynamicData.Tests/List/TransformManyProjectionFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
using DynamicData.Aggregation; | ||
using DynamicData.Binding; | ||
using FluentAssertions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace DynamicData.Tests.List | ||
{ | ||
public class TransformManyProjectionFixture : IDisposable | ||
{ | ||
private readonly ISourceList<ClassWithNestedObservableCollection> _source; | ||
private readonly IObservableList<ProjectedNestedChild> _results; | ||
|
||
public TransformManyProjectionFixture() | ||
{ | ||
_source = new SourceList<ClassWithNestedObservableCollection>(); | ||
|
||
_results = _source.Connect() | ||
.AutoRefreshOnObservable(self => self.Children.ToObservableChangeSet()) | ||
.TransformMany(parent => parent.Children.Select(c => new ProjectedNestedChild(parent, c)), new ProjectNestedChildEqualityComparer()) | ||
.AsObservableList(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_source.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void AddRange() | ||
{ | ||
var children = new[] | ||
{ | ||
new NestedChild("A", "ValueA"), | ||
new NestedChild("B", "ValueB"), | ||
new NestedChild("C", "ValueC"), | ||
new NestedChild("D", "ValueD"), | ||
new NestedChild("E", "ValueE"), | ||
new NestedChild("F", "ValueF") | ||
}; | ||
|
||
var parents = new[] | ||
{ | ||
new ClassWithNestedObservableCollection(1, new[] { children[0], children[1] }), | ||
new ClassWithNestedObservableCollection(2, new[] { children[2], children[3] }), | ||
new ClassWithNestedObservableCollection(3, new[] { children[4] }) | ||
}; | ||
|
||
_source.AddRange(parents); | ||
|
||
_results.Count.Should().Be(5); | ||
_results.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children.Take(5).Select(c => new ProjectedNestedChild(p, c)))); | ||
} | ||
|
||
[Fact] | ||
public void RemoveParent() | ||
{ | ||
var children = new[] | ||
{ | ||
new NestedChild("A", "ValueA"), | ||
new NestedChild("B", "ValueB"), | ||
new NestedChild("C", "ValueC"), | ||
new NestedChild("D", "ValueD"), | ||
new NestedChild("E", "ValueE"), | ||
new NestedChild("F", "ValueF") | ||
}; | ||
|
||
var parents = new[] | ||
{ | ||
new ClassWithNestedObservableCollection(1, new[] { children[0], children[1] }), | ||
new ClassWithNestedObservableCollection(2, new[] { children[2], children[3] }), | ||
new ClassWithNestedObservableCollection(3, new[] { children[4] }) | ||
}; | ||
|
||
_source.AddRange(parents); | ||
|
||
//remove a parent and check children have moved | ||
_source.Remove(parents[0]); | ||
_results.Count.Should().Be(3); | ||
_results.Items.ShouldBeEquivalentTo(parents.Skip(1).SelectMany(p => p.Children.Select(c => new ProjectedNestedChild(p, c)))); | ||
} | ||
|
||
[Fact] | ||
public void RemoveChild() | ||
{ | ||
var children = new[] | ||
{ | ||
new NestedChild("A", "ValueA"), | ||
new NestedChild("B", "ValueB"), | ||
new NestedChild("C", "ValueC"), | ||
new NestedChild("D", "ValueD"), | ||
new NestedChild("E", "ValueE"), | ||
new NestedChild("F", "ValueF") | ||
}; | ||
|
||
var parents = new[] | ||
{ | ||
new ClassWithNestedObservableCollection(1, new[] { children[0], children[1] }), | ||
new ClassWithNestedObservableCollection(2, new[] { children[2], children[3] }), | ||
new ClassWithNestedObservableCollection(3, new[] { children[4] }) | ||
}; | ||
|
||
_source.AddRange(parents); | ||
|
||
//remove a child | ||
parents[1].Children.Remove(children[3]); | ||
_results.Count.Should().Be(4); | ||
_results.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children.Where(child => child.Name != "D").Select(c => new ProjectedNestedChild(p, c)))); | ||
} | ||
|
||
private class ProjectedNestedChild | ||
{ | ||
public ClassWithNestedObservableCollection Parent { get; } | ||
|
||
public NestedChild Child { get; } | ||
|
||
public ProjectedNestedChild(ClassWithNestedObservableCollection parent, NestedChild child) | ||
{ | ||
Parent = parent; | ||
Child = child; | ||
} | ||
} | ||
|
||
private class ProjectNestedChildEqualityComparer : IEqualityComparer<ProjectedNestedChild> | ||
{ | ||
public bool Equals(ProjectedNestedChild x, ProjectedNestedChild y) | ||
{ | ||
if (x == null || y == null) | ||
return false; | ||
|
||
return x.Child.Name == y.Child.Name; | ||
} | ||
|
||
public int GetHashCode(ProjectedNestedChild obj) | ||
{ | ||
return obj.Child.Name.GetHashCode(); | ||
} | ||
} | ||
|
||
private class NestedChild | ||
{ | ||
public string Name { get; } | ||
public string Value { get; } | ||
|
||
public NestedChild(string name, string value) | ||
{ | ||
Name = name; | ||
Value = value; | ||
} | ||
} | ||
|
||
private class ClassWithNestedObservableCollection | ||
{ | ||
public int Id { get; } | ||
public ObservableCollection<NestedChild> Children { get; } | ||
|
||
public ClassWithNestedObservableCollection(int id, IEnumerable<NestedChild> animals) | ||
{ | ||
Id = id; | ||
Children = new ObservableCollection<NestedChild>(animals); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added an overload instead of an optional parameter because of https://github.com/dchaib/DynamicData/blob/e4e0327fd38c909f293e1bd9731049fdd78a73e0/src/DynamicData/List/ObservableListEx.cs#L1246
Let me know if I should fix this differently!