Skip to content

Commit

Permalink
Merge pull request #117 from justinhachemeister/master
Browse files Browse the repository at this point in the history
Update Feature constructor definition to use interface instead of con…
  • Loading branch information
xfischer authored Feb 26, 2019
2 parents 53e12d6 + 80e2d79 commit f92340b
Show file tree
Hide file tree
Showing 4 changed files with 298 additions and 4 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 @@ -167,6 +167,28 @@ public void Can_Serialize_MultiPolygon_Feature()
JsonAssert.AreEqual(expectedJson, actualJson);
}

[Test]
public void Can_Serialize_Dictionary_Subclass()
{
var properties =
new TestFeaturePropertyDictionary()
{
BooleanProperty = true,
DoubleProperty = 1.2345d,
EnumProperty = TestFeatureEnum.Value1,
IntProperty = -1,
StringProperty = "Hello, GeoJSON !"
};

Net.Feature.Feature feature = new Net.Feature.Feature(new Point(new Position(10, 10)), properties);

var expectedJson = this.GetExpectedJson();
var actualJson = JsonConvert.SerializeObject(feature);

Assert.False(string.IsNullOrEmpty(expectedJson));
JsonAssert.AreEqual(expectedJson, actualJson);
}

[Test]
public void Ctor_Can_Add_Properties_Using_Object()
{
Expand All @@ -187,6 +209,31 @@ public void Ctor_Can_Add_Properties_Using_Object()
Assert.AreEqual(feature.Properties.Count, 6);
}

[Test]
public void Ctor_Can_Add_Properties_Using_Object_Inheriting_Dictionary()
{
int expectedProperties = 6;

var properties = new TestFeaturePropertyDictionary()
{
BooleanProperty = true,
DateTimeProperty = DateTime.Now,
DoubleProperty = 1.2345d,
EnumProperty = TestFeatureEnum.Value1,
IntProperty = -1,
StringProperty = "Hello, GeoJSON !"
};

Net.Feature.Feature feature = new Net.Feature.Feature(new Point(new Position(10, 10)), properties);

Assert.IsNotNull(feature.Properties);
Assert.IsTrue(feature.Properties.Count > 1);
Assert.AreEqual(
feature.Properties.Count,
expectedProperties,
$"Expected: {expectedProperties} Actual: {feature.Properties.Count}");
}

[Test]
public void Ctor_Creates_Properties_Collection_When_Passed_Null_Proper_Object()
{
Expand Down Expand Up @@ -437,7 +484,7 @@ private IGeometryObject GetGeometry()
return multiLine;
}

public static Dictionary<string, object> GetPropertiesInRandomOrder()
public static IDictionary<string, object> GetPropertiesInRandomOrder()
{
var properties = new Dictionary<string, object>()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[
10.0,
10.0
]
},
"properties":{
"BooleanProperty":true,
"DoubleProperty":1.2345,
"EnumProperty":1,
"IntProperty":-1,
"StringProperty":"Hello, GeoJSON !"
}
}
230 changes: 230 additions & 0 deletions src/GeoJSON.Net.Tests/Feature/TestFeaturePropertyDictionary.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
namespace GeoJSON.Net.Tests.Feature
{
using System;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// The Test Property Dictionary object.
/// </summary>
internal class TestFeaturePropertyDictionary : IDictionary<string, object>
{
/// <summary>
/// The internal dictionary this implementation is wrapping for testing purposes.
/// </summary>
private readonly IDictionary<string, object> internalDictionary;

/// <summary>
/// Initializes a new instance of the <see cref="TestFeaturePropertyDictionary"/> class.
/// </summary>
public TestFeaturePropertyDictionary()
{
this.internalDictionary = new Dictionary<string, object>();
}

public bool BooleanProperty
{
get
{
return this.GetKeyOrDefault<bool>(nameof(this.BooleanProperty));
}

set
{
this.internalDictionary[nameof(this.BooleanProperty)] = value;
}
}

public DateTime DateTimeProperty
{
get
{
return this.GetKeyOrDefault<DateTime>(nameof(this.DateTimeProperty));
}

set
{
this.internalDictionary[nameof(this.DateTimeProperty)] = value;
}
}

public double DoubleProperty
{
get
{
return this.GetKeyOrDefault<double>(nameof(this.DoubleProperty));
}

set
{
this.internalDictionary[nameof(this.DoubleProperty)] = value;
}
}

public TestFeatureEnum EnumProperty
{
get
{
return this.GetKeyOrDefault<TestFeatureEnum>(nameof(this.EnumProperty));
}

set
{
this.internalDictionary[nameof(this.EnumProperty)] = value;
}
}

public int IntProperty
{
get
{
return this.GetKeyOrDefault<int>(nameof(this.IntProperty));
}

set
{
this.internalDictionary[nameof(this.IntProperty)] = value;
}
}

public string StringProperty
{
get
{
return this.GetKeyOrDefault<string>(nameof(this.StringProperty));
}

set
{
this.internalDictionary[nameof(this.StringProperty)] = value;
}
}

/// <inheritdoc/>
public int Count
{
get
{
return this.internalDictionary.Count;
}
}

/// <inheritdoc/>
public bool IsReadOnly
{
get
{
return this.internalDictionary.IsReadOnly;
}
}

/// <inheritdoc/>
public ICollection<string> Keys
{
get
{
return this.internalDictionary.Keys;
}
}

/// <inheritdoc/>
public ICollection<object> Values
{
get
{
return this.internalDictionary.Values;
}
}

/// <inheritdoc/>
public object this[string key]
{
get
{
return this.internalDictionary[key];
}

set
{
this.internalDictionary[key] = value;
}
}

/// <inheritdoc/>
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return this.internalDictionary.GetEnumerator();
}

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}

/// <inheritdoc/>
public void Add(KeyValuePair<string, object> item)
{
this.internalDictionary.Add(item);
}

/// <inheritdoc/>
public void Clear()
{
this.internalDictionary.Clear();
}

/// <inheritdoc/>
public bool Contains(KeyValuePair<string, object> item)
{
return this.internalDictionary.Contains(item);
}

/// <inheritdoc/>
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
this.internalDictionary.CopyTo(array, arrayIndex);
}

/// <inheritdoc/>
public bool Remove(KeyValuePair<string, object> item)
{
return this.internalDictionary.Remove(item);
}

/// <inheritdoc/>
public bool ContainsKey(string key)
{
return this.internalDictionary.ContainsKey(key);
}

/// <inheritdoc/>
public void Add(string key, object value)
{
this.internalDictionary.Add(key, value);
}

/// <inheritdoc/>
public bool Remove(string key)
{
return this.internalDictionary.Remove(key);
}

/// <inheritdoc/>
public bool TryGetValue(string key, out object value)
{
return this.internalDictionary.TryGetValue(key, out value);
}

private T GetKeyOrDefault<T>(string keyName)
{
object value;
if (this.TryGetValue(keyName, out value))
{
return (T)value;
}

return default(T);
}
}
}
6 changes: 3 additions & 3 deletions src/GeoJSON.Net/Feature/Feature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override int GetHashCode()
public class Feature : Feature<IGeometryObject>
{
[JsonConstructor]
public Feature(IGeometryObject geometry, Dictionary<string, object> properties = null, string id = null)
public Feature(IGeometryObject geometry, IDictionary<string, object> properties = null, string id = null)
: base(geometry, properties, id)
{
}
Expand All @@ -122,7 +122,7 @@ public Feature(IGeometryObject geometry, object properties, string id = null)
/// </summary>
/// <remarks>Returns correctly typed Geometry property</remarks>
/// <typeparam name="TGeometry"></typeparam>
public class Feature<TGeometry> : Feature<TGeometry, Dictionary<string, object>>, IEquatable<Feature<TGeometry>> where TGeometry : IGeometryObject
public class Feature<TGeometry> : Feature<TGeometry, IDictionary<string, object>>, IEquatable<Feature<TGeometry>> where TGeometry : IGeometryObject
{

/// <summary>
Expand All @@ -132,7 +132,7 @@ public class Feature<TGeometry> : Feature<TGeometry, Dictionary<string, object>>
/// <param name="properties">The properties.</param>
/// <param name="id">The (optional) identifier.</param>
[JsonConstructor]
public Feature(TGeometry geometry, Dictionary<string, object> properties = null, string id = null)
public Feature(TGeometry geometry, IDictionary<string, object> properties = null, string id = null)
: base(geometry, properties ?? new Dictionary<string, object>(), id)
{
}
Expand Down

0 comments on commit f92340b

Please sign in to comment.