Skip to content

Commit

Permalink
Fixing issue 94, exception when checking for null
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-lethargic committed Aug 14, 2017
1 parent c3220ef commit 2dc82d7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
49 changes: 48 additions & 1 deletion src/GeoJSON.Net.Tests/Feature/FeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<LineString>
Expand Down
28 changes: 27 additions & 1 deletion src/GeoJSON.Net.Tests/Feature/GenericFeatureTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string, string>
{
{"test1", "test1val"},
{"test2", "test2val"}
};

var feature = new Feature<Point, Dictionary<string, string>>(point, properties, "testid");

Assert.DoesNotThrow(() =>
{
equal1 = feature == null;
equal2 = feature.Equals(null);
});

Assert.IsFalse(equal1);
Assert.IsFalse(equal2);
}

private class TypedFeatureProps
{
[JsonProperty("name")]
Expand Down
3 changes: 3 additions & 0 deletions src/GeoJSON.Net/Feature/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ private static Dictionary<string, object> GetDictionaryOfPublicProperties(object

public bool Equals(Feature<TGeometry> other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;

return Geometry.Equals(other.Geometry);
}

Expand Down

0 comments on commit 2dc82d7

Please sign in to comment.