Skip to content

Commit 6b79dbc

Browse files
author
Matt Hunt
committed
Adding TypedFeatureCollection
1 parent 38aced9 commit 6b79dbc

File tree

6 files changed

+433
-216
lines changed

6 files changed

+433
-216
lines changed

src/GeoJSON.Text.Test.Benchmark/GeoJSON.Text.Test.Benchmark.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>netstandard2.0;net5;net6</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net7.0;netstandard2.0</TargetFrameworks>
65
<ImplicitUsings>disable</ImplicitUsings>
76
<Nullable>enable</Nullable>
87
<SignAssembly>False</SignAssembly>
9-
<LangVersion>Latest</LangVersion>
8+
<LangVersion>10</LangVersion>
109
</PropertyGroup>
1110

1211
<ItemGroup>

src/GeoJSON.Text.Test.Unit/Feature/FeatureCollectionTests.cs

Lines changed: 141 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -6,169 +6,189 @@
66
using GeoJSON.Text.Geometry;
77
using NUnit.Framework;
88

9-
namespace GeoJSON.Text.Tests.Feature
9+
namespace GeoJSON.Text.Tests.Feature;
10+
11+
[TestFixture]
12+
public class FeatureCollectionTests : TestBase
1013
{
11-
[TestFixture]
12-
public class FeatureCollectionTests : TestBase
14+
[Test]
15+
public void Ctor_Throws_ArgumentNullException_When_Features_Is_Null()
1316
{
14-
[Test]
15-
public void Ctor_Throws_ArgumentNullException_When_Features_Is_Null()
17+
Assert.Throws<ArgumentNullException>(() =>
1618
{
17-
Assert.Throws<ArgumentNullException>(() =>
18-
{
19-
var featureCollection = new FeatureCollection(null);
20-
});
21-
}
19+
var featureCollection = new FeatureCollection(null);
20+
});
21+
}
2222

23-
[Test]
24-
public void Can_Deserialize()
25-
{
26-
string json = GetExpectedJson();
23+
[Test]
24+
public void Can_Deserialize()
25+
{
26+
string json = GetExpectedJson();
2727

28-
var featureCollection = JsonSerializer.Deserialize<FeatureCollection>(json);
28+
var featureCollection = JsonSerializer.Deserialize<FeatureCollection>(json);
2929

30-
Assert.IsNotNull(featureCollection.Features);
31-
Assert.AreEqual(featureCollection.Features.Count, 3);
32-
Assert.AreEqual(featureCollection.Features.Count(x => x.Geometry.Type == GeoJSONObjectType.Point), 1);
33-
Assert.AreEqual(featureCollection.Features.Count(x => x.Geometry.Type == GeoJSONObjectType.MultiPolygon), 1);
34-
Assert.AreEqual(featureCollection.Features.Count(x => x.Geometry.Type == GeoJSONObjectType.Polygon), 1);
35-
}
30+
Assert.IsNotNull(featureCollection);
31+
Assert.IsNotNull(featureCollection.Features);
32+
Assert.AreEqual(featureCollection.Features.Count, 3);
33+
Assert.AreEqual(featureCollection.Features.Count(x => x.Geometry.Type == GeoJSONObjectType.Point), 1);
34+
Assert.AreEqual(featureCollection.Features.Count(x => x.Geometry.Type == GeoJSONObjectType.MultiPolygon), 1);
35+
Assert.AreEqual(featureCollection.Features.Count(x => x.Geometry.Type == GeoJSONObjectType.Polygon), 1);
36+
}
3637

37-
[Test]
38-
public void FeatureCollectionSerialization()
39-
{
40-
var model = new FeatureCollection();
41-
for (var i = 10; i-- > 0;)
42-
{
43-
var geom = new LineString(new[]
44-
{
45-
new Position(51.010, -1.034),
46-
new Position(51.010, -0.034)
47-
});
38+
[Test]
39+
public void Can_DeserializeGeneric()
40+
{
41+
string json = GetExpectedJson();
4842

49-
var props = new Dictionary<string, object>
50-
{
51-
{ "test1", "1" },
52-
{ "test2", 2 }
53-
};
43+
var featureCollection =
44+
JsonSerializer.Deserialize<FeatureCollection<FeatureCollectionTestPropertyObject>>(json);
5445

55-
var feature = new Text.Feature.Feature(geom, props);
56-
model.Features.Add(feature);
57-
}
46+
Assert.IsNotNull(featureCollection);
47+
Assert.IsNotNull(featureCollection.Features);
48+
Assert.AreEqual(featureCollection.Features.Count, 3);
49+
Assert.AreEqual("DD", featureCollection.Features.First().Properties.Name);
50+
Assert.AreEqual(123, featureCollection.Features.First().Properties.Size);
51+
}
5852

59-
var actualJson = JsonSerializer.Serialize(model);
53+
[Test]
54+
public void FeatureCollectionSerialization()
55+
{
56+
var model = new FeatureCollection();
57+
for (var i = 10; i-- > 0;)
58+
{
59+
var geom = new LineString(new[]
60+
{
61+
new Position(51.010, -1.034),
62+
new Position(51.010, -0.034)
63+
});
6064

61-
Assert.IsNotNull(actualJson);
65+
var props = new Dictionary<string, object>
66+
{
67+
{ "test1", "1" },
68+
{ "test2", 2 }
69+
};
6270

63-
Assert.IsFalse(string.IsNullOrEmpty(actualJson));
71+
var feature = new Text.Feature.Feature(geom, props);
72+
model.Features.Add(feature);
6473
}
6574

66-
[Test]
67-
public void FeatureCollection_Equals_GetHashCode_Contract()
68-
{
69-
var left = GetFeatureCollection();
70-
var right = GetFeatureCollection();
75+
var actualJson = JsonSerializer.Serialize(model);
7176

72-
Assert_Are_Equal(left, right);
73-
}
77+
Assert.IsNotNull(actualJson);
7478

75-
[Test]
76-
public void Serialized_And_Deserialized_FeatureCollection_Equals_And_Share_HashCode()
77-
{
78-
var leftFc = GetFeatureCollection();
79-
var leftJson = JsonSerializer.Serialize(leftFc);
80-
var left = JsonSerializer.Deserialize<FeatureCollection>(leftJson);
79+
Assert.IsFalse(string.IsNullOrEmpty(actualJson));
80+
}
8181

82-
var rightFc = GetFeatureCollection();
83-
var rightJson = JsonSerializer.Serialize(rightFc);
84-
var right = JsonSerializer.Deserialize<FeatureCollection>(rightJson);
82+
[Test]
83+
public void FeatureCollection_Equals_GetHashCode_Contract()
84+
{
85+
var left = GetFeatureCollection();
86+
var right = GetFeatureCollection();
8587

86-
Assert_Are_Equal(left, right);
87-
}
88+
Assert_Are_Equal(left, right);
89+
}
8890

89-
[Test]
90-
public void FeatureCollection_Test_IndexOf()
91-
{
92-
var model = new FeatureCollection();
93-
var expectedIds = new List<string>();
94-
var expectedIndexes = new List<int>();
91+
[Test]
92+
public void Serialized_And_Deserialized_FeatureCollection_Equals_And_Share_HashCode()
93+
{
94+
var leftFc = GetFeatureCollection();
95+
var leftJson = JsonSerializer.Serialize(leftFc);
96+
var left = JsonSerializer.Deserialize<FeatureCollection>(leftJson);
9597

96-
for (var i = 0; i < 10; i++)
97-
{
98-
var id = "id" + i;
98+
var rightFc = GetFeatureCollection();
99+
var rightJson = JsonSerializer.Serialize(rightFc);
100+
var right = JsonSerializer.Deserialize<FeatureCollection>(rightJson);
99101

100-
expectedIds.Add(id);
101-
expectedIndexes.Add(i);
102+
Assert_Are_Equal(left, right);
103+
}
102104

103-
var geom = new LineString(new[]
104-
{
105-
new Position(51.010, -1.034),
106-
new Position(51.010, -0.034)
107-
});
105+
[Test]
106+
public void FeatureCollection_Test_IndexOf()
107+
{
108+
var model = new FeatureCollection();
109+
var expectedIds = new List<string>();
110+
var expectedIndexes = new List<int>();
108111

109-
var props = FeatureTests.GetPropertiesInRandomOrder();
112+
for (var i = 0; i < 10; i++)
113+
{
114+
var id = "id" + i;
110115

111-
var feature = new Text.Feature.Feature(geom, props, id);
112-
model.Features.Add(feature);
113-
}
116+
expectedIds.Add(id);
117+
expectedIndexes.Add(i);
114118

115-
for (var i = 0; i < 10; i++)
119+
var geom = new LineString(new[]
116120
{
117-
var actualFeature = model.Features[i];
118-
var actualId = actualFeature.Id;
119-
var actualIndex = model.Features.IndexOf(actualFeature);
121+
new Position(51.010, -1.034),
122+
new Position(51.010, -0.034)
123+
});
124+
125+
var props = FeatureTests.GetPropertiesInRandomOrder();
120126

121-
var expectedId = expectedIds[i];
122-
var expectedIndex = expectedIndexes[i];
127+
var feature = new Text.Feature.Feature(geom, props, id);
128+
model.Features.Add(feature);
129+
}
123130

124-
Assert.AreEqual(expectedId, actualId);
125-
Assert.AreEqual(expectedIndex, actualIndex);
131+
for (var i = 0; i < 10; i++)
132+
{
133+
var actualFeature = model.Features[i];
134+
var actualId = actualFeature.Id;
135+
var actualIndex = model.Features.IndexOf(actualFeature);
126136

127-
Assert.Inconclusive("not supported. the Feature.Id is optional. " +
128-
" create a new class that inherits from" +
129-
" Feature and then override Equals and GetHashCode");
137+
var expectedId = expectedIds[i];
138+
var expectedIndex = expectedIndexes[i];
130139

131-
}
140+
Assert.AreEqual(expectedId, actualId);
141+
Assert.AreEqual(expectedIndex, actualIndex);
132142

143+
Assert.Inconclusive("not supported. the Feature.Id is optional. " +
144+
" create a new class that inherits from" +
145+
" Feature and then override Equals and GetHashCode");
133146
}
147+
}
134148

135149

136-
private FeatureCollection GetFeatureCollection()
150+
private FeatureCollection GetFeatureCollection()
151+
{
152+
var model = new FeatureCollection();
153+
for (var i = 10; i-- > 0;)
137154
{
138-
var model = new FeatureCollection();
139-
for (var i = 10; i-- > 0;)
155+
var geom = new LineString(new[]
140156
{
141-
var geom = new LineString(new[]
142-
{
143-
new Position(51.010, -1.034),
144-
new Position(51.010, -0.034)
145-
});
146-
147-
var props = FeatureTests.GetPropertiesInRandomOrder();
148-
149-
var feature = new Text.Feature.Feature(geom, props);
150-
model.Features.Add(feature);
151-
}
152-
return model;
157+
new Position(51.010, -1.034),
158+
new Position(51.010, -0.034)
159+
});
160+
161+
var props = FeatureTests.GetPropertiesInRandomOrder();
162+
163+
var feature = new Text.Feature.Feature(geom, props);
164+
model.Features.Add(feature);
153165
}
154166

155-
private void Assert_Are_Equal(FeatureCollection left, FeatureCollection right)
156-
{
157-
Assert.AreEqual(left, right);
167+
return model;
168+
}
158169

159-
Assert.IsTrue(left.Equals(right));
160-
Assert.IsTrue(right.Equals(left));
170+
private void Assert_Are_Equal(FeatureCollection left, FeatureCollection right)
171+
{
172+
Assert.AreEqual(left, right);
161173

162-
Assert.IsTrue(left.Equals(left));
163-
Assert.IsTrue(right.Equals(right));
174+
Assert.IsTrue(left.Equals(right));
175+
Assert.IsTrue(right.Equals(left));
164176

165-
Assert.IsTrue(left == right);
166-
Assert.IsTrue(right == left);
177+
Assert.IsTrue(left.Equals(left));
178+
Assert.IsTrue(right.Equals(right));
167179

168-
Assert.IsFalse(left != right);
169-
Assert.IsFalse(right != left);
180+
Assert.IsTrue(left == right);
181+
Assert.IsTrue(right == left);
170182

171-
Assert.AreEqual(left.GetHashCode(), right.GetHashCode());
172-
}
183+
Assert.IsFalse(left != right);
184+
Assert.IsFalse(right != left);
185+
186+
Assert.AreEqual(left.GetHashCode(), right.GetHashCode());
187+
}
188+
189+
private class FeatureCollectionTestPropertyObject
190+
{
191+
public string Name { get; set; }
192+
public int Size { get; set; }
173193
}
174194
}

0 commit comments

Comments
 (0)