public void CreateAndDisposeExplicitSetting(ReferenceHandling referenceHandling) { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var propertyChanges = new List<string>(); var expectedChanges = new List<string>(); using (var tracker = Track.IsDirty(x, y, PropertiesSettings.GetOrCreate(referenceHandling))) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff); CollectionAssert.IsEmpty(propertyChanges); x.Value++; Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Value x: 2 y: 1", tracker.Diff.ToString("", " ")); expectedChanges.AddRange(new[] { "Diff", "IsDirty" }); CollectionAssert.AreEqual(expectedChanges, propertyChanges); } x.Value++; CollectionAssert.AreEqual(expectedChanges, propertyChanges); #if (!DEBUG) // debug build keeps instances alive longer for nicer debugging experience var wrx = new System.WeakReference(x); var wry = new System.WeakReference(y); x = null; y = null; System.GC.Collect(); Assert.AreEqual(false, wrx.IsAlive); Assert.AreEqual(false, wry.IsAlive); #endif }
public void DoesNotNotifyWhenMissingProperty() { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var propertyChanges = new List <string>(); using (var tracker = Track.IsDirty(x, y)) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff); CollectionAssert.IsEmpty(propertyChanges); x.OnPropertyChanged("Missing"); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff?.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); y.OnPropertyChanged("Missing"); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff?.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); } }
public void Excludes(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties { IntValue = 1, StringValue = "2" }; var target = new WithSimpleProperties { IntValue = 3, StringValue = "4" }; var settings = PropertiesSettings.Build() .IgnoreProperty<WithSimpleProperties>(x => x.StringValue) .CreateSettings(referenceHandling); using (Synchronize.PropertyValues(source, target, settings)) { Assert.AreEqual(1, source.IntValue); Assert.AreEqual(1, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("4", target.StringValue); source.IntValue = 5; Assert.AreEqual(5, source.IntValue); Assert.AreEqual(5, target.IntValue); source.StringValue = "7"; Assert.AreEqual("7", source.StringValue); Assert.AreEqual("4", target.StringValue); } source.IntValue = 6; Assert.AreEqual(6, source.IntValue); Assert.AreEqual(5, target.IntValue); Assert.AreEqual("7", source.StringValue); Assert.AreEqual("4", target.StringValue); }
public void CreateAndDisposeExplicitSetting(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties { Value1 = 1, Time = DateTime.MinValue }; var propertyChanges = new List<string>(); var expectedPropertyChanges = 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.Value1++; Assert.AreEqual(1, tracker.Changes); expectedPropertyChanges.AddRange(new[] { "Changes" }); CollectionAssert.AreEqual(expectedPropertyChanges, propertyChanges); var expected = new[] { RootChangeEventArgs.Create(ChangeTrackerNode.GetOrCreate(source, tracker.Settings, false).Value, new PropertyChangeEventArgs(source, source.GetProperty(nameof(source.Value1)))) }; CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default); } source.Value1++; CollectionAssert.AreEqual(expectedPropertyChanges, propertyChanges); #if (!DEBUG) // debug build keeps instances alive longer for nicer debugging experience var wrx = new System.WeakReference(source); source = null; System.GC.Collect(); Assert.AreEqual(false, wrx.IsAlive); #endif }
public void CreateAndDisposeExplicitSetting(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties { Value1 = 1, Time = DateTime.MinValue }; var propertyChanges = new List <string>(); var expectedPropertyChanges = 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.Value1++; Assert.AreEqual(1, tracker.Changes); expectedPropertyChanges.AddRange(new[] { "Changes" }); CollectionAssert.AreEqual(expectedPropertyChanges, propertyChanges); var expected = new[] { RootChangeEventArgs.Create(ChangeTrackerNode.GetOrCreate(source, tracker.Settings, isRoot: false).Value, new PropertyChangeEventArgs(source, source.GetProperty(nameof(source.Value1)))) }; CollectionAssert.AreEqual(expected, changes, EventArgsComparer.Default); } source.Value1++; CollectionAssert.AreEqual(expectedPropertyChanges, propertyChanges); #if !DEBUG // debug build keeps instances alive longer for nicer debugging experience var wrx = new System.WeakReference(source); source = null; System.GC.Collect(); Assert.AreEqual(false, wrx.IsAlive); #endif }
public void HandlesPropertyChangedEmptyAndNull(string prop) { var source = new WithSimpleProperties { IntValue = 1, StringValue = "2" }; var target = new WithSimpleProperties { IntValue = 3, StringValue = "4" }; foreach (var referenceHandling in Enum.GetValues(typeof(ReferenceHandling)).Cast <ReferenceHandling>()) { source.IntValue = 1; source.StringValue = "2"; target.IntValue = 3; target.StringValue = "4"; using (Synchronize.PropertyValues(source, target, referenceHandling)) { Assert.AreEqual(1, source.IntValue); Assert.AreEqual(1, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("2", target.StringValue); source.SetFields(5, "6"); source.OnPropertyChanged(prop); Assert.AreEqual(5, source.IntValue); Assert.AreEqual(5, target.IntValue); Assert.AreEqual("6", source.StringValue); Assert.AreEqual("6", target.StringValue); } } }
public void HappyPath(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties { IntValue = 1, NullableIntValue = 2, StringValue = "3", EnumValue = StringSplitOptions.RemoveEmptyEntries }; var target = new WithSimpleProperties { IntValue = 3, NullableIntValue = 4 }; using (Synchronize.PropertyValues(source, target, referenceHandling)) { Assert.AreEqual(1, source.IntValue); Assert.AreEqual(1, target.IntValue); Assert.AreEqual(2, source.NullableIntValue); Assert.AreEqual(2, target.NullableIntValue); Assert.AreEqual("3", source.StringValue); Assert.AreEqual("3", target.StringValue); Assert.AreEqual(StringSplitOptions.RemoveEmptyEntries, source.EnumValue); Assert.AreEqual(StringSplitOptions.RemoveEmptyEntries, target.EnumValue); source.IntValue = 5; Assert.AreEqual(5, source.IntValue); Assert.AreEqual(5, target.IntValue); } source.IntValue = 6; Assert.AreEqual(6, source.IntValue); Assert.AreEqual(5, target.IntValue); }
public void Excludes(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties { IntValue = 1, StringValue = "2" }; var target = new WithSimpleProperties { IntValue = 3, StringValue = "4" }; var settings = PropertiesSettings.Build() .IgnoreProperty <WithSimpleProperties>(x => x.StringValue) .CreateSettings(referenceHandling); using (Synchronize.PropertyValues(source, target, settings)) { Assert.AreEqual(1, source.IntValue); Assert.AreEqual(1, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("4", target.StringValue); source.IntValue = 5; Assert.AreEqual(5, source.IntValue); Assert.AreEqual(5, target.IntValue); source.StringValue = "7"; Assert.AreEqual("7", source.StringValue); Assert.AreEqual("4", target.StringValue); } source.IntValue = 6; Assert.AreEqual(6, source.IntValue); Assert.AreEqual(5, target.IntValue); Assert.AreEqual("7", source.StringValue); Assert.AreEqual("4", target.StringValue); }
public void HandlesMissingProperty(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties { IntValue = 1, StringValue = "2" }; var target = new WithSimpleProperties { IntValue = 3, StringValue = "4" }; using (Synchronize.PropertyValues(source, target, referenceHandling)) { Assert.AreEqual(1, source.IntValue); Assert.AreEqual(1, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("2", target.StringValue); #pragma warning disable INPC009 // Don't raise PropertyChanged for missing property. source.OnPropertyChanged("Missing"); #pragma warning restore INPC009 // Don't raise PropertyChanged for missing property. source.IntValue = 5; Assert.AreEqual(5, source.IntValue); Assert.AreEqual(5, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("2", target.StringValue); } source.IntValue = 6; Assert.AreEqual(6, source.IntValue); Assert.AreEqual(5, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("2", target.StringValue); }
public void HandlesMissingProperty(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties { IntValue = 1, StringValue = "2" }; var target = new WithSimpleProperties { IntValue = 3, StringValue = "4" }; using (Synchronize.PropertyValues(source, target, referenceHandling)) { Assert.AreEqual(1, source.IntValue); Assert.AreEqual(1, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("2", target.StringValue); source.OnPropertyChanged("Missing"); source.IntValue = 5; Assert.AreEqual(5, source.IntValue); Assert.AreEqual(5, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("2", target.StringValue); } source.IntValue = 6; Assert.AreEqual(6, source.IntValue); Assert.AreEqual(5, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("2", target.StringValue); }
public void ReturnsDifferentForDifferentSettings() { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var t1 = DirtyTrackerNode.GetOrCreate(x, y, PropertiesSettings.GetOrCreate(ReferenceHandling.Structural), true); var t2 = DirtyTrackerNode.GetOrCreate(x, y, PropertiesSettings.GetOrCreate(ReferenceHandling.References), true); Assert.AreNotSame(t1, t2); }
public void ThrowsIfTargetChanges(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties(); var target = new WithSimpleProperties(); using (Synchronize.PropertyValues(source, target, referenceHandling)) { var exception = Assert.Throws<InvalidOperationException>(() => target.IntValue++); var expected = "Target cannot be modified when a synchronizer is applied to it\r\n" + "The change would just trigger a dirty notification and the value would be updated with the value from source."; Assert.AreEqual(expected, exception.Message); } }
public void ThrowsIfTargetChanges(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties(); var target = new WithSimpleProperties(); using (Synchronize.PropertyValues(source, target, referenceHandling)) { var exception = Assert.Throws <InvalidOperationException>(() => target.IntValue++); var expected = "Target cannot be modified when a synchronizer is applied to it\r\n" + "The change would just trigger a dirty notification and the value would be updated with the value from source."; Assert.AreEqual(expected, exception.Message); } }
public void ReturnsSameWhileAlive() { var x = new WithSimpleProperties(); var y = new WithSimpleProperties(); var settings = PropertiesSettings.GetOrCreate(ReferenceHandling.Structural); var t1 = DirtyTrackerNode.GetOrCreate(x, y, settings, true); var t2 = DirtyTrackerNode.GetOrCreate(x, y, settings, true); Assert.AreSame(t1, t2); t1.Dispose(); var t3 = DirtyTrackerNode.GetOrCreate(x, y, settings, true); Assert.AreSame(t1, t3); t2.Dispose(); t3.Dispose(); var t4 = DirtyTrackerNode.GetOrCreate(x, y, settings, true); Assert.AreNotSame(t1, t4); }
public void WithSimpleHappyPath(ReferenceHandling referenceHandling) { var source = new WithSimpleProperties(1, 2, "3", StringSplitOptions.RemoveEmptyEntries); var target = new WithSimpleProperties { IntValue = 3, NullableIntValue = 4 }; this.CopyMethod(source, target, referenceHandling); Assert.AreEqual(1, source.IntValue); Assert.AreEqual(1, target.IntValue); Assert.AreEqual(2, source.NullableIntValue); Assert.AreEqual(2, target.NullableIntValue); Assert.AreEqual("3", source.StringValue); Assert.AreEqual("3", target.StringValue); Assert.AreEqual(StringSplitOptions.RemoveEmptyEntries, source.EnumValue); Assert.AreEqual(StringSplitOptions.RemoveEmptyEntries, target.EnumValue); }
public void WithSimpleHappyPath(ReferenceHandling referenceHandling) { var x = new WithSimpleProperties(); var y = new WithSimpleProperties(); foreach (var expected in new[] { true, false }) { var comparerMock = new Mock <IEqualityComparer <WithSimpleProperties> >(MockBehavior.Strict); comparerMock.Setup(c => c.Equals(x, y)) .Returns(expected); var result = this.EqualByMethod(x, y, comparerMock.Object, referenceHandling); Assert.AreEqual(expected, result); comparerMock.Verify( c => c.Equals(It.IsAny <WithSimpleProperties>(), It.IsAny <WithSimpleProperties>()), Times.Once); } }
public void WithSimpleHappyPath(string xn, string yn, bool expected) { var x = new WithSimpleProperties(1, 2, xn, StringSplitOptions.RemoveEmptyEntries); var y = new WithSimpleProperties(1, 2, yn, StringSplitOptions.RemoveEmptyEntries); var result = this.EqualMethod(x, y); Assert.AreEqual(expected, result); result = this.EqualMethod(x, y, ReferenceHandling.Throw); Assert.AreEqual(expected, result); result = this.EqualMethod(x, y, ReferenceHandling.Structural); Assert.AreEqual(expected, result); result = this.EqualMethod(x, y, ReferenceHandling.References); Assert.AreEqual(expected, result); }
public void ReturnsDifferentForDifferentPairs() { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var settings = PropertiesSettings.GetOrCreate(ReferenceHandling.Structural); using (var t1 = DirtyTrackerNode.GetOrCreate(x, y, settings, isRoot: true)) { using (var t2 = DirtyTrackerNode.GetOrCreate(y, x, settings, isRoot: true)) { Assert.AreNotSame(t1, t2); } } }
public void TracksY() { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 3, Time = DateTime.MinValue.AddSeconds(1) }; var propertyChanges = new List <string>(); var expectedChanges = new List <string>(); using (var tracker = Track.IsDirty(x, y)) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000 Value x: 1 y: 3", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); y.Value = 5; Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000 Value x: 1 y: 5", tracker.Diff.ToString("", " ")); expectedChanges.Add("Diff"); CollectionAssert.AreEqual(expectedChanges, propertyChanges); y.Value = 1; Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); expectedChanges.Add("Diff"); CollectionAssert.AreEqual(expectedChanges, propertyChanges); y.Time = DateTime.MinValue; Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff?.ToString("", " ")); expectedChanges.AddRange(new[] { "Diff", "IsDirty" }); CollectionAssert.AreEqual(expectedChanges, propertyChanges); y.Time = DateTime.MinValue.AddSeconds(2); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:02.0000000", tracker.Diff.ToString("", " ")); expectedChanges.AddRange(new[] { "Diff", "IsDirty" }); CollectionAssert.AreEqual(expectedChanges, propertyChanges); } }
public void IgnoresMember(int xv, int yv, ReferenceHandling?referenceHandling, bool expected) { var x = new WithSimpleProperties(xv, null, "3", StringSplitOptions.RemoveEmptyEntries); var y = new WithSimpleProperties(yv, 2, "3", StringSplitOptions.RemoveEmptyEntries); var excluded = this is FieldValues.Classes ? "nullableIntValue" : nameof(WithSimpleProperties.NullableIntValue); if (referenceHandling == null) { var result = this.EqualMethod(x, y, excludedMembers: excluded); Assert.AreEqual(expected, result); } else { var result = this.EqualMethod(x, y, referenceHandling.Value, excluded); Assert.AreEqual(expected, result); } }
public void HandlesPropertyChangedEmptyAndNull(string prop) { var x = new WithSimpleProperties(1, DateTime.MinValue); var y = new WithSimpleProperties(1, DateTime.MinValue); var propertyChanges = new List <string>(); var expectedChanges = new List <string>(); using (var tracker = Track.IsDirty(x, y)) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff); CollectionAssert.IsEmpty(propertyChanges); x.SetFields(1, DateTime.MinValue.AddSeconds(1)); x.OnPropertyChanged(prop); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:01.0000000 y: 0001-01-01T00:00:00.0000000", tracker.Diff.ToString("", " ")); expectedChanges.AddRange(new[] { "Diff", "IsDirty" }); CollectionAssert.AreEqual(expectedChanges, propertyChanges); } }
public void WithSimpleHappyPath(string xn, string yn, string expected) { expected = expected?.Replace( "<member>", this is FieldValues.Classes ? "stringValue" : "StringValue"); var x = new WithSimpleProperties(1, 2, xn, StringSplitOptions.RemoveEmptyEntries); var y = new WithSimpleProperties(1, 2, yn, StringSplitOptions.RemoveEmptyEntries); var result = this.DiffMethod(x, y); Assert.AreEqual(expected, result.ToString(string.Empty, " ")); result = this.DiffMethod(x, y, ReferenceHandling.Throw); Assert.AreEqual(expected, result.ToString(string.Empty, " ")); result = this.DiffMethod(x, y, ReferenceHandling.Structural); Assert.AreEqual(expected, result.ToString(string.Empty, " ")); result = this.DiffMethod(x, y, ReferenceHandling.References); Assert.AreEqual(expected, result.ToString(string.Empty, " ")); }
public void Ignores() { var source = new WithSimpleProperties { IntValue = 1, NullableIntValue = 2, StringValue = "3", EnumValue = StringSplitOptions.RemoveEmptyEntries }; var target = new WithSimpleProperties { IntValue = 2, NullableIntValue = 4, StringValue = "4", EnumValue = StringSplitOptions.None }; var excluded = this is FieldValues.Classes ? "nullableIntValue" : nameof(WithSimpleProperties.NullableIntValue); this.CopyMethod(source, target, excluded: excluded); Assert.AreEqual(1, source.IntValue); Assert.AreEqual(1, target.IntValue); Assert.AreEqual(2, source.NullableIntValue); Assert.AreEqual(4, target.NullableIntValue); Assert.AreEqual("3", source.StringValue); Assert.AreEqual("3", target.StringValue); Assert.AreEqual(StringSplitOptions.RemoveEmptyEntries, source.EnumValue); Assert.AreEqual(StringSplitOptions.RemoveEmptyEntries, target.EnumValue); }
public void CreateAndDisposeExplicitSetting(ReferenceHandling referenceHandling) { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var propertyChanges = new List <string>(); var expectedChanges = new List <string>(); using (var tracker = Track.IsDirty(x, y, PropertiesSettings.GetOrCreate(referenceHandling))) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff); CollectionAssert.IsEmpty(propertyChanges); x.Value++; Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Value x: 2 y: 1", tracker.Diff.ToString("", " ")); expectedChanges.AddRange(new[] { "Diff", "IsDirty" }); CollectionAssert.AreEqual(expectedChanges, propertyChanges); } x.Value++; CollectionAssert.AreEqual(expectedChanges, propertyChanges); #if (!DEBUG) // debug build keeps instances alive longer for nicer debugging experience var wrx = new System.WeakReference(x); var wry = new System.WeakReference(y); x = null; y = null; System.GC.Collect(); Assert.AreEqual(false, wrx.IsAlive); Assert.AreEqual(false, wry.IsAlive); #endif }
public void DoesNotNotifyWhenNoChangeWhenDirty() { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue.AddSeconds(1) }; var propertyChanges = new List <string>(); using (var tracker = Track.IsDirty(x, y)) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); x.OnPropertyChanged(nameof(WithSimpleProperties.Value)); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); x.OnPropertyChanged(nameof(WithSimpleProperties.Time)); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); y.OnPropertyChanged(nameof(WithSimpleProperties.Value)); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); y.OnPropertyChanged(nameof(WithSimpleProperties.Time)); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); } }
public void DoesNotNotifyWhenMissingProperty() { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var propertyChanges = new List<string>(); using (var tracker = Track.IsDirty(x, y)) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff); CollectionAssert.IsEmpty(propertyChanges); x.OnPropertyChanged("Missing"); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff?.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); y.OnPropertyChanged("Missing"); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff?.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); } }
public void HandlesPropertyChangedEmptyAndNull(string prop) { var x = new WithSimpleProperties(1, DateTime.MinValue); var y = new WithSimpleProperties(1, DateTime.MinValue); var propertyChanges = new List<string>(); var expectedChanges = new List<string>(); using (var tracker = Track.IsDirty(x, y)) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff); CollectionAssert.IsEmpty(propertyChanges); x.SetFields(1, DateTime.MinValue.AddSeconds(1)); x.OnPropertyChanged(prop); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:01.0000000 y: 0001-01-01T00:00:00.0000000", tracker.Diff.ToString("", " ")); expectedChanges.AddRange(new[] { "Diff", "IsDirty" }); CollectionAssert.AreEqual(expectedChanges, propertyChanges); } }
public void TracksY() { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 3, Time = DateTime.MinValue.AddSeconds(1) }; var propertyChanges = new List<string>(); var expectedChanges = new List<string>(); using (var tracker = Track.IsDirty(x, y)) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000 Value x: 1 y: 3", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); y.Value = 5; Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000 Value x: 1 y: 5", tracker.Diff.ToString("", " ")); expectedChanges.Add("Diff"); CollectionAssert.AreEqual(expectedChanges, propertyChanges); y.Value = 1; Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); expectedChanges.Add("Diff"); CollectionAssert.AreEqual(expectedChanges, propertyChanges); y.Time = DateTime.MinValue; Assert.AreEqual(false, tracker.IsDirty); Assert.AreEqual(null, tracker.Diff?.ToString("", " ")); expectedChanges.AddRange(new[] { "Diff", "IsDirty" }); CollectionAssert.AreEqual(expectedChanges, propertyChanges); y.Time = DateTime.MinValue.AddSeconds(2); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:02.0000000", tracker.Diff.ToString("", " ")); expectedChanges.AddRange(new[] { "Diff", "IsDirty" }); CollectionAssert.AreEqual(expectedChanges, propertyChanges); } }
public void DoesNotNotifyWhenNoChangeWhenDirty() { var x = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue }; var y = new WithSimpleProperties { Value = 1, Time = DateTime.MinValue.AddSeconds(1) }; var propertyChanges = new List<string>(); using (var tracker = Track.IsDirty(x, y)) { tracker.PropertyChanged += (_, e) => propertyChanges.Add(e.PropertyName); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); x.OnPropertyChanged(nameof(WithSimpleProperties.Value)); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); x.OnPropertyChanged(nameof(WithSimpleProperties.Time)); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); y.OnPropertyChanged(nameof(WithSimpleProperties.Value)); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); y.OnPropertyChanged(nameof(WithSimpleProperties.Time)); Assert.AreEqual(true, tracker.IsDirty); Assert.AreEqual("WithSimpleProperties Time x: 0001-01-01T00:00:00.0000000 y: 0001-01-01T00:00:01.0000000", tracker.Diff.ToString("", " ")); CollectionAssert.IsEmpty(propertyChanges); } }
public void HandlesPropertyChangedEmptyAndNull(string prop) { var source = new WithSimpleProperties { IntValue = 1, StringValue = "2" }; var target = new WithSimpleProperties { IntValue = 3, StringValue = "4" }; foreach (var referenceHandling in Enum.GetValues(typeof(ReferenceHandling)).Cast<ReferenceHandling>()) { source.IntValue = 1; source.StringValue = "2"; target.IntValue = 3; target.StringValue = "4"; using (Synchronize.PropertyValues(source, target, referenceHandling)) { Assert.AreEqual(1, source.IntValue); Assert.AreEqual(1, target.IntValue); Assert.AreEqual("2", source.StringValue); Assert.AreEqual("2", target.StringValue); source.SetFields(5, "6"); source.OnPropertyChanged(prop); Assert.AreEqual(5, source.IntValue); Assert.AreEqual(5, target.IntValue); Assert.AreEqual("6", source.StringValue); Assert.AreEqual("6", target.StringValue); } } }