public void Remove()
            {
                var source = new With <ObservableCollection <ComplexType> > {
                    Value = new ObservableCollection <ComplexType> {
                        new ComplexType(), null
                    }
                };
                var propertyChanges = new List <string>();
                var changes         = new List <EventArgs>();
                var expectedChanges = new List <EventArgs>();

                using (var tracker = Track.Changes(source, ReferenceHandling.Structural))
                {
                    var sourceNode = ChangeTrackerNode.GetOrCreate(source, tracker.Settings, false)
                                     .Value;
                    tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName);
                    tracker.Changed         += (_, e) => changes.Add(e);
                    Assert.AreEqual(0, tracker.Changes);
                    CollectionAssert.IsEmpty(propertyChanges);
                    CollectionAssert.IsEmpty(changes);

                    source.Value.RemoveAt(1);
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    expectedChanges.Add(
                        new PropertyGraphChangedEventArgs <ChangeTrackerNode>(
                            sourceNode,
                            source.GetProperty("Value"),
                            RootChangeEventArgs.Create(
                                ChangeTrackerNode.GetOrCreate(
                                    (INotifyCollectionChanged)source.Value,
                                    tracker.Settings,
                                    false)
                                .Value,
                                new RemoveEventArgs(source.Value, 1))));
                    CollectionAssert.AreEqual(expectedChanges, changes, EventArgsComparer.Default);

                    var complexType = source.Value[0];
                    source.Value.RemoveAt(0);
                    Assert.AreEqual(2, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes", "Changes" }, propertyChanges);
                    expectedChanges.Add(
                        new PropertyGraphChangedEventArgs <ChangeTrackerNode>(
                            sourceNode,
                            source.GetProperty("Value"),
                            RootChangeEventArgs.Create(
                                ChangeTrackerNode.GetOrCreate(
                                    (INotifyCollectionChanged)source.Value,
                                    tracker.Settings,
                                    false)
                                .Value,
                                new RemoveEventArgs(source.Value, 0))));
                    CollectionAssert.AreEqual(expectedChanges, changes, EventArgsComparer.Default);

                    complexType.Value++;
                    Assert.AreEqual(2, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes", "Changes" }, propertyChanges);
                    CollectionAssert.AreEqual(expectedChanges, changes, EventArgsComparer.Default);
                }
            }
            public void ReplaceCollection()
            {
                var source = new With<ObservableCollection<ComplexType>> { Value = new ObservableCollection<ComplexType>() };
                var propertyChanges = new List<string>();
                var changes = new List<EventArgs>();
                var expectedChanges = new List<EventArgs>();
                using (var tracker = Track.Changes(source, ReferenceHandling.Structural))
                {
                    var sourceNode = ChangeTrackerNode.GetOrCreate(source, tracker.Settings, false)
                                                      .Value;
                    tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName);
                    tracker.Changed += (_, e) => changes.Add(e);
                    Assert.AreEqual(0, tracker.Changes);
                    CollectionAssert.IsEmpty(propertyChanges);
                    CollectionAssert.IsEmpty(changes);

                    var observableCollection = source.Value;
                    source.Value = new ObservableCollection<ComplexType> { new ComplexType() };
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    expectedChanges.Add(RootChangeEventArgs.Create(sourceNode, new PropertyChangeEventArgs(source, source.GetProperty("Value"))));
                    CollectionAssert.AreEqual(expectedChanges, changes, EventArgsComparer.Default);

                    source.Value[0].Value++;
                    Assert.AreEqual(2, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes", "Changes" }, propertyChanges);
                    expectedChanges.Add(
                        new PropertyGraphChangedEventArgs<ChangeTrackerNode>(
                            sourceNode,
                            source.GetProperty("Value"),
                            new ItemGraphChangedEventArgs<ChangeTrackerNode>(
                                ChangeTrackerNode.GetOrCreate(
                                    (INotifyCollectionChanged)source.Value,
                                    tracker.Settings,
                                    false)
                                                 .Value,
                                0,
                                RootChangeEventArgs.Create(
                                    ChangeTrackerNode.GetOrCreate(source.Value[0], tracker.Settings, false)
                                                     .Value,
                                    new PropertyChangeEventArgs(source.Value[0], source.Value[0].GetProperty("Value"))))));
                    CollectionAssert.AreEqual(expectedChanges, changes, EventArgsComparer.Default);

                    observableCollection.Add(new ComplexType());
                    Assert.AreEqual(2, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes", "Changes" }, propertyChanges);
                    CollectionAssert.AreEqual(expectedChanges, changes, EventArgsComparer.Default);
                }
            }
            public void WithImmutable(ReferenceHandling referenceHandling)
            {
                var source = new With<Immutable>();

                var propertyChanges = new List<string>();
                var changes = new List<EventArgs>();

                using (var tracker = Track.Changes(source, referenceHandling))
                {
                    tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName);
                    tracker.Changed += (_, e) => changes.Add(e);
                    CollectionAssert.IsEmpty(propertyChanges);
                    CollectionAssert.IsEmpty(changes);

                    source.Value = new Immutable();
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    var node = ChangeTrackerNode.GetOrCreate(source, tracker.Settings, false).Value;
                    var expected = new[] { RootChangeEventArgs.Create(node, new PropertyChangeEventArgs(source, source.GetProperty(nameof(source.Value)))) };
                    CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default);
                }
            }
Exemple #4
0
            public void StopsSubscribing()
            {
                var source = new With <ComplexType> {
                    Value = new ComplexType()
                };
                var propertyChanges = new List <string>();
                var changes         = new List <EventArgs>();

                using (var tracker = Track.Changes(source, ReferenceHandling.Structural))
                {
                    tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName);
                    tracker.Changed         += (_, e) => changes.Add(e);
                    Assert.AreEqual(0, tracker.Changes);
                    CollectionAssert.IsEmpty(propertyChanges);
                    CollectionAssert.IsEmpty(changes);

                    var oldValue = source.Value;
                    source.Value = null;
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    var expected = new[] { RootChangeEventArgs.Create(ChangeTrackerNode.GetOrCreate(source, tracker.Settings, false).Value, new PropertyChangeEventArgs(source, source.GetProperty(nameof(source.Value)))) };
                    CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default);

                    oldValue.Value++;
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default);
                }
            }
            public void WithImmutable(ReferenceHandling referenceHandling)
            {
                var source = new With <Immutable>();

                var propertyChanges = new List <string>();
                var changes         = new List <EventArgs>();

                using (var tracker = Track.Changes(source, referenceHandling))
                {
                    tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName);
                    tracker.Changed         += (_, e) => changes.Add(e);
                    CollectionAssert.IsEmpty(propertyChanges);
                    CollectionAssert.IsEmpty(changes);

                    source.Value = new Immutable();
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    var node     = ChangeTrackerNode.GetOrCreate(source, tracker.Settings, isRoot: false).Value;
                    var expected = new[] { RootChangeEventArgs.Create(node, new PropertyChangeEventArgs(source, source.GetProperty(nameof(source.Value)))) };
                    CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default);
                }
            }
            public void IgnoresType()
            {
                var source   = new With <ComplexType>();
                var settings = PropertiesSettings.Build()
                               .IgnoreType <ComplexType>()
                               .CreateSettings(ReferenceHandling.Structural);

                var propertyChanges = new List <string>();
                var changes         = new List <EventArgs>();

                using (var tracker = Track.Changes(source, settings))
                {
                    tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName);
                    tracker.Changed         += (_, e) => changes.Add(e);
                    Assert.AreEqual(0, tracker.Changes);
                    CollectionAssert.IsEmpty(propertyChanges);
                    CollectionAssert.IsEmpty(changes);

                    source.Value = new ComplexType();
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    var expected = new[] { RootChangeEventArgs.Create(ChangeTrackerNode.GetOrCreate(source, tracker.Settings, isRoot: false).Value, new PropertyChangeEventArgs(source, source.GetProperty("Value"))) };
                    CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default);

                    source.Value.Value++;
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default);
                }
            }
            public void IgnoresType()
            {
                var source = new With<ComplexType>();
                var settings = PropertiesSettings.Build()
                                                 .IgnoreType<ComplexType>()
                                                 .CreateSettings(ReferenceHandling.Structural);

                var propertyChanges = new List<string>();
                var changes = new List<EventArgs>();
                using (var tracker = Track.Changes(source, settings))
                {
                    tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName);
                    tracker.Changed += (_, e) => changes.Add(e);
                    Assert.AreEqual(0, tracker.Changes);
                    CollectionAssert.IsEmpty(propertyChanges);
                    CollectionAssert.IsEmpty(changes);

                    source.Value = new ComplexType();
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    var expected = new[] { RootChangeEventArgs.Create(ChangeTrackerNode.GetOrCreate(source, tracker.Settings, false).Value, new PropertyChangeEventArgs(source, source.GetProperty("Value"))) };
                    CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default);

                    source.Value.Value++;
                    Assert.AreEqual(1, tracker.Changes);
                    CollectionAssert.AreEqual(new[] { "Changes" }, propertyChanges);
                    CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default);
                }
            }