Skip to content

Commit

Permalink
Track.IsDirty with inheritance
Browse files Browse the repository at this point in the history
- Tests for EqualBy, Diff And Track.IsDirty
  • Loading branch information
JohanLarsson committed Aug 25, 2016
1 parent b0b8b7c commit 36f76d4
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 18 deletions.
38 changes: 38 additions & 0 deletions Gu.State.Tests/DiffTests/ClassesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,44 @@ this is FieldValues.Classes
Assert.AreEqual(expected, result.ToString("", " "));
}

[Test]
public void EqualWhenSameType()
{
var x = new With<BaseClass>(new Derived1 { BaseValue = 1, Derived1Value = 2 });
var y = new With<BaseClass>(new Derived1 { BaseValue = 1, Derived1Value = 2 });
var result = this.DiffMethod(x, y, ReferenceHandling.Structural);
Assert.AreEqual(true, result.IsEmpty);
Assert.AreEqual("Empty", result.ToString("", " "));
}

[Test]
public void NotEqualWhenSameType()
{
var x = new With<BaseClass>(new Derived1 { BaseValue = 1, Derived1Value = 2 });
var y = new With<BaseClass>(new Derived1 { BaseValue = 1, Derived1Value = 3 });
var result = this.DiffMethod(x, y, ReferenceHandling.Structural);
Assert.AreEqual(false, result.IsEmpty);
var expected = this is PropertyValues.Classes
? "With<BaseClass> Value Derived1Value x: 2 y: 3"
: "With<BaseClass> <Value>k__BackingField <Derived1Value>k__BackingField x: 2 y: 3";
var actual = result.ToString("", " ");
Assert.AreEqual(expected, actual);
}

[Test]
public void NotEqualWhenDifferentTypes()
{
var x = new With<BaseClass>(new Derived1());
var y = new With<BaseClass>(new Derived2());
var result = this.DiffMethod(x, y, ReferenceHandling.Structural);
Assert.AreEqual(false, result.IsEmpty);
var expected = this is PropertyValues.Classes
? "With<BaseClass> Value x: Gu.State.Tests.DiffTests.DiffTypes+Derived1 y: Gu.State.Tests.DiffTests.DiffTypes+Derived2"
: "With<BaseClass> <Value>k__BackingField x: Gu.State.Tests.DiffTests.DiffTypes+Derived1 y: Gu.State.Tests.DiffTests.DiffTypes+Derived2";
var actual = result.ToString("", " ");
Assert.AreEqual(expected, actual);
}

[TestCase(1, 1, null, "Empty")]
[TestCase(1, 2, null, "WithReadonlyProperty<int> <member> x: 1 y: 2")]
[TestCase(1, 1, ReferenceHandling.Throw, "Empty")]
Expand Down
25 changes: 25 additions & 0 deletions Gu.State.Tests/DiffTests/DiffTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@

public static class DiffTypes
{
public class With<T>
{
public With(T value)
{
this.Value = value;
}

public T Value { get; }
}

public class WithComplexValue
{
private string name;
Expand Down Expand Up @@ -503,5 +513,20 @@ public override int GetHashCode()
return this.HashValue;
}
}

public abstract class BaseClass
{
public double BaseValue { get; set; }
}

public class Derived1 : BaseClass
{
public double Derived1Value { get; set; }
}

public class Derived2 : BaseClass
{
public double Derived2Value { get; set; }
}
}
}
63 changes: 45 additions & 18 deletions Gu.State.Tests/EqualByTests/ClassesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,33 @@ public void WithComplexStructural(string xn, string yn, bool expected)
Assert.AreEqual(expected, result);
}

[Test]
public void EqualWhenSameType()
{
var x = new With<BaseClass>(new Derived1 { BaseValue = 1, Derived1Value = 2 });
var y = new With<BaseClass>(new Derived1 { BaseValue = 1, Derived1Value = 2 });
var result = this.EqualMethod(x, y, ReferenceHandling.Structural);
Assert.AreEqual(true, result);
}

[Test]
public void NotEqualWhenSameType()
{
var x = new With<BaseClass>(new Derived1 { BaseValue = 1, Derived1Value = 2 });
var y = new With<BaseClass>(new Derived1 { BaseValue = 1, Derived1Value = 3 });
var result = this.EqualMethod(x, y, ReferenceHandling.Structural);
Assert.AreEqual(false, result);
}

[Test]
public void NotEqualWhenDifferentTypes()
{
var x = new With<BaseClass>(new Derived1());
var y = new With<BaseClass>(new Derived2());
var result = this.EqualMethod(x, y, ReferenceHandling.Structural);
Assert.AreEqual(false, result);
}

[Test]
public void WithComplexStructuralWhenNull()
{
Expand Down Expand Up @@ -98,11 +125,11 @@ public void WithComplexStructuralWhenYIsNull()
public void WithComplexReferenceWhenSame()
{
var x = new WithComplexProperty
{
Name = "a",
Value = 1,
ComplexType = new ComplexType { Name = "b", Value = 2 }
};
{
Name = "a",
Value = 1,
ComplexType = new ComplexType { Name = "b", Value = 2 }
};
var y = new WithComplexProperty { Name = "a", Value = 1, ComplexType = x.ComplexType };
var result = this.EqualMethod(x, y, ReferenceHandling.Structural);
Assert.AreEqual(true, result);
Expand All @@ -115,17 +142,17 @@ public void WithComplexReferenceWhenSame()
public void WithComplexReferenceWhenNotSame()
{
var x = new WithComplexProperty
{
Name = "a",
Value = 1,
ComplexType = new ComplexType { Name = "b", Value = 2 }
};
{
Name = "a",
Value = 1,
ComplexType = new ComplexType { Name = "b", Value = 2 }
};
var y = new WithComplexProperty
{
Name = "a",
Value = 1,
ComplexType = new ComplexType { Name = "b", Value = 2 }
};
{
Name = "a",
Value = 1,
ComplexType = new ComplexType { Name = "b", Value = 2 }
};
var result = this.EqualMethod(x, y, ReferenceHandling.Structural);
Assert.AreEqual(true, result);

Expand Down Expand Up @@ -240,13 +267,13 @@ public void WithListOfComplexPropertyToLonger()
{
var source = new WithListProperty<ComplexType> { Items = { new ComplexType("a", 1) } };
var target = new WithListProperty<ComplexType>
{
Items =
{
Items =
{
new ComplexType("a", 1),
new ComplexType("a", 1)
}
};
};
var result = this.EqualMethod(source, target, ReferenceHandling.Structural);
Assert.AreEqual(false, result);
}
Expand Down
15 changes: 15 additions & 0 deletions Gu.State.Tests/EqualByTests/EqualByTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,5 +599,20 @@ public override int GetHashCode()
return this.HashValue;
}
}

public abstract class BaseClass
{
public double BaseValue { get; set; }
}

public class Derived1 : BaseClass
{
public double Derived1Value { get; set; }
}

public class Derived2 : BaseClass
{
public double Derived2Value { get; set; }
}
}
}
1 change: 1 addition & 0 deletions Gu.State.Tests/Gu.State.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
<Compile Include="Track\Dirty\DirtyTrackerTests.ReferenceLoops.cs" />
<Compile Include="Track\Dirty\DirtyTrackerTests.MemoryLeaks.cs" />
<Compile Include="Track\Dirty\DirtyTrackerTests.Throws.cs" />
<Compile Include="Track\Dirty\DirtyTrackerTests.WithInheritance.cs" />
<Compile Include="Track\Dirty\DirtyTrackerTypes.cs" />
<Compile Include="EqualByTests\ClassesTests.cs" />
<Compile Include="EqualByTests\CollectionTests.cs" />
Expand Down
38 changes: 38 additions & 0 deletions Gu.State.Tests/Track/Dirty/DirtyTrackerTests.WithInheritance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace Gu.State.Tests
{
using NUnit.Framework;

using static DirtyTrackerTypes;

public partial class DirtyTrackerTests
{
public class WithInheritance
{
[Test]
public void NotDirtyWhenSameType()
{
var x = new With<BaseClass>();
var y = new With<BaseClass>();
using (var tracker = Track.IsDirty(x, y))
{
x.Value = new Derived2();
y.Value = new Derived2();
Assert.AreEqual(false, tracker.IsDirty);
}
}

[Test]
public void DirtyWhenDifferentTypes()
{
var x = new With<BaseClass>();
var y = new With<BaseClass>();
using (var tracker = Track.IsDirty(x, y))
{
x.Value = new Derived2();
y.Value = new Derived1();
Assert.AreEqual(true, tracker.IsDirty);
}
}
}
}
}
55 changes: 55 additions & 0 deletions Gu.State.Tests/Track/Dirty/DirtyTrackerTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -634,5 +634,60 @@ public virtual void OnPropertyChanged([CallerMemberName] string propertyName = n
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

public abstract class BaseClass : INotifyPropertyChanged
{
private double baseDouble;
public event PropertyChangedEventHandler PropertyChanged;

public double BaseValue
{
get { return this.baseDouble; }
set
{
if (value.Equals(this.baseDouble)) return;
this.baseDouble = value;
this.OnPropertyChanged();
}
}

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

public class Derived1 : BaseClass
{
private double derived1Value;

public double Derived1Value
{
get { return this.derived1Value; }
set
{
if (value.Equals(this.derived1Value)) return;
this.derived1Value = value;
this.OnPropertyChanged();
}
}
}

public class Derived2 : BaseClass
{
private double derived2Value;

public double Derived2Value
{
get { return this.derived2Value; }
set
{
if (value.Equals(this.derived2Value)) return;
this.derived2Value = value;
this.OnPropertyChanged();
}
}
}
}
}
5 changes: 5 additions & 0 deletions Gu.State/Track/Dirty/DirtyTrackerNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ private static bool IsTrackablePair(object x, object y, PropertiesSettings setti
return false;
}

if (x.GetType() != y.GetType())
{
return false;
}

return !settings.IsImmutable(x.GetType()) && !settings.IsImmutable(y.GetType());
}

Expand Down

0 comments on commit 36f76d4

Please sign in to comment.