Skip to content

Commit 8801ab4

Browse files
committed
Add unit tests on geometries
1 parent 3a301ce commit 8801ab4

6 files changed

Lines changed: 342 additions & 0 deletions

File tree

src/AzureMapsControl.Components/Atlas/Pixel.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ internal sealed class PixelJsonConverter : JsonConverter<Pixel>
3939
{
4040
public override Pixel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
4141
{
42+
if(reader.TokenType == JsonTokenType.None)
43+
{
44+
reader.Read();
45+
}
46+
4247
var pixel = new Pixel();
4348
if (reader.TokenType == JsonTokenType.StartArray)
4449
{

src/AzureMapsControl.Components/Atlas/Position.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ internal sealed class PositionJsonConverter : JsonConverter<Position>
5050
{
5151
public override Position Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
5252
{
53+
if (reader.TokenType == JsonTokenType.None)
54+
{
55+
reader.Read();
56+
}
57+
5358
if (reader.TokenType == JsonTokenType.StartArray)
5459
{
5560
var position = new Position();
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
namespace AzureMapsControl.Components.Tests.Atlas
2+
{
3+
using System.Text.Json;
4+
5+
using AzureMapsControl.Components.Atlas;
6+
using AzureMapsControl.Components.Tests.Json;
7+
8+
using Xunit;
9+
10+
public class GeometryJsonConverterTests : JsonConverterTests<Geometry>
11+
{
12+
public GeometryJsonConverterTests() : base(new GeometryJsonConverter()) { }
13+
14+
[Fact]
15+
public void Should_ReadPoint()
16+
{
17+
var point = new Point(new Position(0, 1));
18+
var json = JsonSerializer.Serialize(point);
19+
var result = Read(json);
20+
Assert.IsType<Point>(result);
21+
Assert.Equal(json, JsonSerializer.Serialize(result as Point));
22+
}
23+
24+
[Fact]
25+
public void Should_ReadLineString()
26+
{
27+
var lineString = new LineString(new[] { new Position(0, 1), new Position(2, 3) });
28+
var json = JsonSerializer.Serialize(lineString);
29+
var result = Read(json);
30+
Assert.IsType<LineString>(result);
31+
Assert.Equal(json, JsonSerializer.Serialize(result as LineString));
32+
}
33+
34+
[Fact]
35+
public void Should_ReadMultiLineString()
36+
{
37+
var multiLineString = new MultiLineString(new[] {
38+
new [] {
39+
new Position(0, 1),
40+
new Position(2,3)
41+
},
42+
new [] {
43+
new Position(4,5),
44+
new Position(6,7)
45+
}
46+
});
47+
var json = JsonSerializer.Serialize(multiLineString);
48+
var result = Read(json);
49+
Assert.IsType<MultiLineString>(result);
50+
Assert.Equal(json, JsonSerializer.Serialize(result as MultiLineString));
51+
}
52+
53+
[Fact]
54+
public void Should_ReadMultiPoint()
55+
{
56+
var multiPoint = new MultiPoint(new[] { new Position(0, 1), new Position(2, 3) });
57+
var json = JsonSerializer.Serialize(multiPoint);
58+
var result = Read(json);
59+
Assert.IsType<MultiPoint>(result);
60+
Assert.Equal(json, JsonSerializer.Serialize(result as MultiPoint));
61+
}
62+
63+
[Fact]
64+
public void Should_ReadMultiPolygon()
65+
{
66+
var multiPolygon = new MultiPolygon(new[] {
67+
new [] {
68+
new [] {
69+
new Position(0, 1),
70+
new Position(2, 3)
71+
}
72+
}
73+
});
74+
var json = JsonSerializer.Serialize(multiPolygon);
75+
var result = Read(json);
76+
Assert.IsType<MultiPolygon>(result);
77+
Assert.Equal(json, JsonSerializer.Serialize(result as MultiPolygon));
78+
}
79+
80+
[Fact]
81+
public void Should_ReadPolygon()
82+
{
83+
var polygon = new Polygon(new[] {
84+
new [] {
85+
new Position(0, 1),
86+
new Position(2, 3)
87+
}
88+
});
89+
var json = JsonSerializer.Serialize(polygon);
90+
var result = Read(json);
91+
Assert.IsType<Polygon>(result);
92+
Assert.Equal(json, JsonSerializer.Serialize(result as Polygon));
93+
}
94+
}
95+
96+
public class GeometryPointJsonConverterTests : JsonConverterTests<Point>
97+
{
98+
public GeometryPointJsonConverterTests() : base(new GeometryJsonConverter<Point>()) { }
99+
100+
[Fact]
101+
public void Should_Read()
102+
{
103+
var geometry = new Point(new Position(0, 1));
104+
var json = JsonSerializer.Serialize(geometry);
105+
var result = Read(json);
106+
Assert.Equal(json, JsonSerializer.Serialize(result));
107+
}
108+
109+
[Fact]
110+
public void Should_Write()
111+
{
112+
var geometry = new Point(new Position(0, 1));
113+
TestAndAssertWrite(geometry, JsonSerializer.Serialize(geometry));
114+
}
115+
}
116+
117+
public class GeometryLineStringJsonConverterTests : JsonConverterTests<LineString>
118+
{
119+
public GeometryLineStringJsonConverterTests() : base(new GeometryJsonConverter<LineString>()) { }
120+
121+
[Fact]
122+
public void Should_Read()
123+
{
124+
var geometry = new LineString(new[] { new Position(0, 1), new Position(2, 3) });
125+
var json = JsonSerializer.Serialize(geometry);
126+
var result = Read(json);
127+
Assert.Equal(json, JsonSerializer.Serialize(result));
128+
}
129+
130+
[Fact]
131+
public void Should_Write()
132+
{
133+
var geometry = new LineString(new[] { new Position(0, 1), new Position(2, 3) });
134+
TestAndAssertWrite(geometry, JsonSerializer.Serialize(geometry));
135+
}
136+
}
137+
138+
public class GeometryMultiLineStringJsonConverterTests : JsonConverterTests<MultiLineString>
139+
{
140+
public GeometryMultiLineStringJsonConverterTests() : base(new GeometryJsonConverter<MultiLineString>()) { }
141+
142+
[Fact]
143+
public void Should_Read()
144+
{
145+
var geometry = new MultiLineString(new[] {
146+
new [] {
147+
new Position(0, 1),
148+
new Position(2,3)
149+
},
150+
new [] {
151+
new Position(4,5),
152+
new Position(6,7)
153+
}
154+
});
155+
var json = JsonSerializer.Serialize(geometry);
156+
var result = Read(json);
157+
Assert.Equal(json, JsonSerializer.Serialize(result));
158+
}
159+
160+
[Fact]
161+
public void Should_Write()
162+
{
163+
var geometry = new MultiLineString(new[] {
164+
new [] {
165+
new Position(0, 1),
166+
new Position(2,3)
167+
},
168+
new [] {
169+
new Position(4,5),
170+
new Position(6,7)
171+
}
172+
});
173+
TestAndAssertWrite(geometry, JsonSerializer.Serialize(geometry));
174+
}
175+
}
176+
177+
public class GeometryMultiPointJsonConverterTests : JsonConverterTests<MultiPoint>
178+
{
179+
public GeometryMultiPointJsonConverterTests() : base(new GeometryJsonConverter<MultiPoint>()) { }
180+
181+
[Fact]
182+
public void Should_Read()
183+
{
184+
var geometry = new MultiPoint(new[] { new Position(0, 1), new Position(2, 3) });
185+
var json = JsonSerializer.Serialize(geometry);
186+
var result = Read(json);
187+
Assert.Equal(json, JsonSerializer.Serialize(result));
188+
}
189+
190+
[Fact]
191+
public void Should_Write()
192+
{
193+
var geometry = new MultiPoint(new[] { new Position(0, 1), new Position(2, 3) });
194+
TestAndAssertWrite(geometry, JsonSerializer.Serialize(geometry));
195+
}
196+
}
197+
198+
public class GeometryMultiPolygonJsonConverterTests : JsonConverterTests<MultiPolygon>
199+
{
200+
public GeometryMultiPolygonJsonConverterTests() : base(new GeometryJsonConverter<MultiPolygon>()) { }
201+
202+
[Fact]
203+
public void Should_Read()
204+
{
205+
var geometry = new MultiPolygon(new[] {
206+
new [] {
207+
new [] {
208+
new Position(0, 1),
209+
new Position(2, 3)
210+
}
211+
}
212+
});
213+
var json = JsonSerializer.Serialize(geometry);
214+
var result = Read(json);
215+
Assert.Equal(json, JsonSerializer.Serialize(result));
216+
}
217+
218+
[Fact]
219+
public void Should_Write()
220+
{
221+
var geometry = new MultiPolygon(new[] {
222+
new [] {
223+
new [] {
224+
new Position(0, 1),
225+
new Position(2, 3)
226+
}
227+
}
228+
});
229+
TestAndAssertWrite(geometry, JsonSerializer.Serialize(geometry));
230+
}
231+
}
232+
233+
public class GeometryPolygonJsonConverterTests : JsonConverterTests<Polygon>
234+
{
235+
public GeometryPolygonJsonConverterTests() : base(new GeometryJsonConverter<Polygon>()) { }
236+
237+
[Fact]
238+
public void Should_Read()
239+
{
240+
var geometry = new Polygon(new[] {
241+
new [] {
242+
new Position(0, 1),
243+
new Position(2, 3)
244+
}
245+
});
246+
var json = JsonSerializer.Serialize(geometry);
247+
var result = Read(json);
248+
Assert.Equal(json, JsonSerializer.Serialize(result));
249+
}
250+
251+
[Fact]
252+
public void Should_Write()
253+
{
254+
var geometry = new Polygon(new[] {
255+
new [] {
256+
new Position(0, 1),
257+
new Position(2, 3)
258+
}
259+
});
260+
TestAndAssertWrite(geometry, JsonSerializer.Serialize(geometry));
261+
}
262+
}
263+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace AzureMapsControl.Components.Tests.Atlas
2+
{
3+
using System.Text.Json;
4+
5+
using AzureMapsControl.Components.Atlas;
6+
using AzureMapsControl.Components.Tests.Json;
7+
8+
using Xunit;
9+
10+
public class PixelJsonConverterTests : JsonConverterTests<Pixel>
11+
{
12+
public PixelJsonConverterTests(): base(new PixelJsonConverter()) { }
13+
14+
[Fact]
15+
public void Should_Read()
16+
{
17+
var pixel = new Pixel(1, 2);
18+
var expectedJson = JsonSerializer.Serialize(pixel);
19+
var result = Read(expectedJson);
20+
Assert.Equal(expectedJson, JsonSerializer.Serialize(result));
21+
}
22+
23+
[Fact]
24+
public void Should_Write()
25+
{
26+
var pixel = new Pixel(1, 2);
27+
TestAndAssertWrite(pixel, JsonSerializer.Serialize(pixel));
28+
}
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace AzureMapsControl.Components.Tests.Atlas
2+
{
3+
using System.Text.Json;
4+
5+
using AzureMapsControl.Components.Atlas;
6+
using AzureMapsControl.Components.Tests.Json;
7+
8+
using Xunit;
9+
10+
public class PositionJsonConverterTests : JsonConverterTests<Position>
11+
{
12+
public PositionJsonConverterTests() : base(new PositionJsonConverter()) { }
13+
14+
[Fact]
15+
public void Should_Read()
16+
{
17+
var position = new Position(1, 2, 3);
18+
var expectedJson = JsonSerializer.Serialize(position);
19+
var result = Read(expectedJson);
20+
Assert.Equal(expectedJson, JsonSerializer.Serialize(result));
21+
}
22+
23+
[Fact]
24+
public void Should_Write()
25+
{
26+
var position = new Position(1, 2, 3);
27+
TestAndAssertWrite(position, JsonSerializer.Serialize(position));
28+
}
29+
}
30+
}

tests/AzureMapsControl.Components.Tests/Json/JsonConverterTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,14 @@ protected void TestAndAssertEmptytWrite(TValue value)
4242

4343
Assert.Equal(0, buffer.WrittenCount);
4444
}
45+
46+
protected TValue Read(string expectedJson)
47+
{
48+
var bytes = Encoding.UTF8.GetBytes(expectedJson);
49+
var reader = new Utf8JsonReader(bytes);
50+
51+
var result = _converter.Read(ref reader, typeof(TValue), null);
52+
return result;
53+
}
4554
}
4655
}

0 commit comments

Comments
 (0)