|
| 1 | +using DynamicData.Aggregation; |
| 2 | +using DynamicData.Binding; |
| 3 | +using FluentAssertions; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.ObjectModel; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using Xunit; |
| 10 | + |
| 11 | +namespace DynamicData.Tests.List |
| 12 | +{ |
| 13 | + public class TransformManyProjectionFixture : IDisposable |
| 14 | + { |
| 15 | + private readonly ISourceList<ClassWithNestedObservableCollection> _source; |
| 16 | + private readonly IObservableList<ProjectedNestedChild> _results; |
| 17 | + |
| 18 | + public TransformManyProjectionFixture() |
| 19 | + { |
| 20 | + _source = new SourceList<ClassWithNestedObservableCollection>(); |
| 21 | + |
| 22 | + _results = _source.Connect() |
| 23 | + .AutoRefreshOnObservable(self => self.Children.ToObservableChangeSet()) |
| 24 | + .TransformMany(parent => parent.Children.Select(c => new ProjectedNestedChild(parent, c)), new ProjectNestedChildEqualityComparer()) |
| 25 | + .AsObservableList(); |
| 26 | + } |
| 27 | + |
| 28 | + public void Dispose() |
| 29 | + { |
| 30 | + _source.Dispose(); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void AddRange() |
| 35 | + { |
| 36 | + var children = new[] |
| 37 | + { |
| 38 | + new NestedChild("A", "ValueA"), |
| 39 | + new NestedChild("B", "ValueB"), |
| 40 | + new NestedChild("C", "ValueC"), |
| 41 | + new NestedChild("D", "ValueD"), |
| 42 | + new NestedChild("E", "ValueE"), |
| 43 | + new NestedChild("F", "ValueF") |
| 44 | + }; |
| 45 | + |
| 46 | + var parents = new[] |
| 47 | + { |
| 48 | + new ClassWithNestedObservableCollection(1, new[] { children[0], children[1] }), |
| 49 | + new ClassWithNestedObservableCollection(2, new[] { children[2], children[3] }), |
| 50 | + new ClassWithNestedObservableCollection(3, new[] { children[4] }) |
| 51 | + }; |
| 52 | + |
| 53 | + _source.AddRange(parents); |
| 54 | + |
| 55 | + _results.Count.Should().Be(5); |
| 56 | + _results.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children.Take(5).Select(c => new ProjectedNestedChild(p, c)))); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public void RemoveParent() |
| 61 | + { |
| 62 | + var children = new[] |
| 63 | + { |
| 64 | + new NestedChild("A", "ValueA"), |
| 65 | + new NestedChild("B", "ValueB"), |
| 66 | + new NestedChild("C", "ValueC"), |
| 67 | + new NestedChild("D", "ValueD"), |
| 68 | + new NestedChild("E", "ValueE"), |
| 69 | + new NestedChild("F", "ValueF") |
| 70 | + }; |
| 71 | + |
| 72 | + var parents = new[] |
| 73 | + { |
| 74 | + new ClassWithNestedObservableCollection(1, new[] { children[0], children[1] }), |
| 75 | + new ClassWithNestedObservableCollection(2, new[] { children[2], children[3] }), |
| 76 | + new ClassWithNestedObservableCollection(3, new[] { children[4] }) |
| 77 | + }; |
| 78 | + |
| 79 | + _source.AddRange(parents); |
| 80 | + |
| 81 | + //remove a parent and check children have moved |
| 82 | + _source.Remove(parents[0]); |
| 83 | + _results.Count.Should().Be(3); |
| 84 | + _results.Items.ShouldBeEquivalentTo(parents.Skip(1).SelectMany(p => p.Children.Select(c => new ProjectedNestedChild(p, c)))); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void RemoveChild() |
| 89 | + { |
| 90 | + var children = new[] |
| 91 | + { |
| 92 | + new NestedChild("A", "ValueA"), |
| 93 | + new NestedChild("B", "ValueB"), |
| 94 | + new NestedChild("C", "ValueC"), |
| 95 | + new NestedChild("D", "ValueD"), |
| 96 | + new NestedChild("E", "ValueE"), |
| 97 | + new NestedChild("F", "ValueF") |
| 98 | + }; |
| 99 | + |
| 100 | + var parents = new[] |
| 101 | + { |
| 102 | + new ClassWithNestedObservableCollection(1, new[] { children[0], children[1] }), |
| 103 | + new ClassWithNestedObservableCollection(2, new[] { children[2], children[3] }), |
| 104 | + new ClassWithNestedObservableCollection(3, new[] { children[4] }) |
| 105 | + }; |
| 106 | + |
| 107 | + _source.AddRange(parents); |
| 108 | + |
| 109 | + //remove a child |
| 110 | + parents[1].Children.Remove(children[3]); |
| 111 | + _results.Count.Should().Be(4); |
| 112 | + _results.Items.ShouldBeEquivalentTo(parents.SelectMany(p => p.Children.Where(child => child.Name != "D").Select(c => new ProjectedNestedChild(p, c)))); |
| 113 | + } |
| 114 | + |
| 115 | + private class ProjectedNestedChild |
| 116 | + { |
| 117 | + public ClassWithNestedObservableCollection Parent { get; } |
| 118 | + |
| 119 | + public NestedChild Child { get; } |
| 120 | + |
| 121 | + public ProjectedNestedChild(ClassWithNestedObservableCollection parent, NestedChild child) |
| 122 | + { |
| 123 | + Parent = parent; |
| 124 | + Child = child; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private class ProjectNestedChildEqualityComparer : IEqualityComparer<ProjectedNestedChild> |
| 129 | + { |
| 130 | + public bool Equals(ProjectedNestedChild x, ProjectedNestedChild y) |
| 131 | + { |
| 132 | + if (x == null || y == null) |
| 133 | + return false; |
| 134 | + |
| 135 | + return x.Child.Name == y.Child.Name; |
| 136 | + } |
| 137 | + |
| 138 | + public int GetHashCode(ProjectedNestedChild obj) |
| 139 | + { |
| 140 | + return obj.Child.Name.GetHashCode(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private class NestedChild |
| 145 | + { |
| 146 | + public string Name { get; } |
| 147 | + public string Value { get; } |
| 148 | + |
| 149 | + public NestedChild(string name, string value) |
| 150 | + { |
| 151 | + Name = name; |
| 152 | + Value = value; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private class ClassWithNestedObservableCollection |
| 157 | + { |
| 158 | + public int Id { get; } |
| 159 | + public ObservableCollection<NestedChild> Children { get; } |
| 160 | + |
| 161 | + public ClassWithNestedObservableCollection(int id, IEnumerable<NestedChild> animals) |
| 162 | + { |
| 163 | + Id = id; |
| 164 | + Children = new ObservableCollection<NestedChild>(animals); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments