From 559a8c951baf0d0d75c6e81e105de02e9a21b7f9 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 29 Sep 2015 11:00:33 +0200 Subject: [PATCH] Fix PerspexProperty equality. --- src/Perspex.Base/PerspexProperty.cs | 15 +++++++++++++-- .../PerspexObjectTests_Direct.cs | 16 ++++++++++++++++ .../PerspexPropertyTests.cs | 13 +++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Perspex.Base/PerspexProperty.cs b/src/Perspex.Base/PerspexProperty.cs index d612a3b285..3bda662038 100644 --- a/src/Perspex.Base/PerspexProperty.cs +++ b/src/Perspex.Base/PerspexProperty.cs @@ -288,7 +288,18 @@ namespace Perspex /// True if the properties are equal, otherwise false. public static bool operator ==(PerspexProperty a, PerspexProperty b) { - return a?.Equals(b) ?? false; + if (object.ReferenceEquals(a, b)) + { + return true; + } + else if (((object)a == null) || ((object)b == null)) + { + return false; + } + else + { + return a.Equals(b); + } } /// @@ -299,7 +310,7 @@ namespace Perspex /// True if the properties are equal, otherwise false. public static bool operator !=(PerspexProperty a, PerspexProperty b) { - return !a?.Equals(b) ?? false; + return !(a == b); } /// diff --git a/tests/Perspex.Base.UnitTests/PerspexObjectTests_Direct.cs b/tests/Perspex.Base.UnitTests/PerspexObjectTests_Direct.cs index 59a978117a..c8b4470d13 100644 --- a/tests/Perspex.Base.UnitTests/PerspexObjectTests_Direct.cs +++ b/tests/Perspex.Base.UnitTests/PerspexObjectTests_Direct.cs @@ -26,6 +26,14 @@ namespace Perspex.Base.UnitTests Assert.Equal("initial", target.GetValue((PerspexProperty)Class1.FooProperty)); } + [Fact] + public void GetValue_On_Unregistered_Property_Throws_Exception() + { + var target = new Class2(); + + Assert.Throws(() => target.GetValue(Class1.BarProperty)); + } + [Fact] public void SetValue_Sets_Value() { @@ -80,6 +88,14 @@ namespace Perspex.Base.UnitTests Assert.True(raised); } + [Fact] + public void SetValue_On_Unregistered_Property_Throws_Exception() + { + var target = new Class2(); + + Assert.Throws(() => target.SetValue(Class1.BarProperty, "value")); + } + [Fact] public void GetObservable_Returns_Values() { diff --git a/tests/Perspex.Base.UnitTests/PerspexPropertyTests.cs b/tests/Perspex.Base.UnitTests/PerspexPropertyTests.cs index e1de37a7ca..6c90ac1039 100644 --- a/tests/Perspex.Base.UnitTests/PerspexPropertyTests.cs +++ b/tests/Perspex.Base.UnitTests/PerspexPropertyTests.cs @@ -137,6 +137,19 @@ namespace Perspex.Base.UnitTests Assert.True(target.IsDirect); } + [Fact] + public void Property_Equals_Should_Handle_Null() + { + var p1 = new PerspexProperty("p1", typeof(Class1)); + + Assert.NotEqual(p1, null); + Assert.NotEqual(null, p1); + Assert.False(p1 == null); + Assert.False(null == p1); + Assert.False(p1.Equals(null)); + Assert.True((PerspexProperty)null == (PerspexProperty)null); + } + [Fact] public void AddOwnered_Property_Should_Equal_Original() {