diff --git a/src/GeoJSON.Net.Tests/Feature/FeatureTests.cs b/src/GeoJSON.Net.Tests/Feature/FeatureTests.cs index 30d8ca0d..b5c3ae57 100644 --- a/src/GeoJSON.Net.Tests/Feature/FeatureTests.cs +++ b/src/GeoJSON.Net.Tests/Feature/FeatureTests.cs @@ -196,7 +196,7 @@ public void Ctor_Creates_Properties_Collection_When_Passed_Null_Proper_Object() Assert.IsNotNull(feature.Properties); CollectionAssert.IsEmpty(feature.Properties); } - + [Test] public void Feature_Equals_GetHashCode_Contract_Properties_Of_Objects() { @@ -309,7 +309,54 @@ public void Serialized_And_Deserialized_Feature_Equals_And_Share_HashCode() Assert_Are_Equal(left, right); // assert id's + properties doesn't influence comparison and hashcode } + [Test] + public void Feature_Equals_Null_Issue94() + { + bool equal1 = true; + bool equal2 = true; + + var feature = new Net.Feature.Feature(new Point(new Position(123, 12))); + Assert.DoesNotThrow(() => + { + equal1 = feature.Equals(null); + equal2 = feature == null; + }); + Assert.IsFalse(equal1); + Assert.IsFalse(equal2); + } + + [Test] + public void Feature_Null_Instance_Equals_Null_Issue94() + { + var equal1 = true; + + Net.Feature.Feature feature = null; + Assert.DoesNotThrow(() => + { + equal1 = feature != null; + }); + + Assert.IsFalse(equal1); + } + + [Test] + public void Feature_Equals_Itself_Issue94() + { + bool equal1 = false; + bool equal2 = false; + + var feature = new Net.Feature.Feature(new Point(new Position(123, 12))); + Assert.DoesNotThrow(() => + { + equal1 = feature == feature; + equal2 = feature.Equals(feature); + }); + + Assert.IsTrue(equal1); + Assert.IsTrue(equal2); + } + private IGeometryObject GetGeometry() { var coordinates = new List diff --git a/src/GeoJSON.Net.Tests/Feature/GenericFeatureTests.cs b/src/GeoJSON.Net.Tests/Feature/GenericFeatureTests.cs index fffebbb2..4fd84209 100644 --- a/src/GeoJSON.Net.Tests/Feature/GenericFeatureTests.cs +++ b/src/GeoJSON.Net.Tests/Feature/GenericFeatureTests.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using GeoJSON.Net.Feature; using GeoJSON.Net.Geometry; using Newtonsoft.Json; @@ -56,6 +57,31 @@ public void Can_Deserialize_LineString_Feature() //Assert.AreEqual(456, feature.Geometry.Coordinates.Altitude); } + [Test] + public void Feature_Generic_Equals_Null_Issure94() + { + bool equal1 = true; + bool equal2 = true; + + var point = new Point(new Position(123, 34)); + var properties = new Dictionary + { + {"test1", "test1val"}, + {"test2", "test2val"} + }; + + var feature = new Feature>(point, properties, "testid"); + + Assert.DoesNotThrow(() => + { + equal1 = feature == null; + equal2 = feature.Equals(null); + }); + + Assert.IsFalse(equal1); + Assert.IsFalse(equal2); + } + private class TypedFeatureProps { [JsonProperty("name")] diff --git a/src/GeoJSON.Net/Feature/Feature.cs b/src/GeoJSON.Net/Feature/Feature.cs index a6ccc37e..2595ecbb 100644 --- a/src/GeoJSON.Net/Feature/Feature.cs +++ b/src/GeoJSON.Net/Feature/Feature.cs @@ -172,6 +172,9 @@ private static Dictionary GetDictionaryOfPublicProperties(object public bool Equals(Feature other) { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Geometry.Equals(other.Geometry); }