Skip to content

Commit

Permalink
Updated tests to work better for nodatime/newtonsoftjson and wkt
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Jul 9, 2022
1 parent 94f6b61 commit 7f6c5ab
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ private static T DeserializeObject<T>(string geom, JsonSerializerSettings settin
return JsonConvert.DeserializeObject<ValueOf<T>>("{\"value\":\"" + geom + "\"}", settings)!.Value;
}

private static string SerializeObject<T>(T geom, JsonSerializerSettings settings)
{
return JsonConvert.SerializeObject(geom, settings).Trim('"');
}

private readonly JsonSerializerSettings _settings;

public NewtonsoftJsonNetTopologySuiteWellKnownTextTests(ITestOutputHelper outputHelper) : base(outputHelper)
Expand All @@ -26,96 +31,100 @@ public NewtonsoftJsonNetTopologySuiteWellKnownTextTests(ITestOutputHelper output
// typeof(MultiPolygon),
// typeof(GeometryCollection)
[Theory]
[InlineData("POINT(30 10)")]
[InlineData("POINT (30 10)")]
public void Geometry_Tests(string geom)
{
DeserializeObject<Point>(geom, _settings)
.Should()
.Be(new Point(30, 10));
var value = DeserializeObject<Point>(geom, _settings)
.Should()
.Be(new Point(30, 10))
.And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("LINESTRING(30 10, 10 30, 40 40)")]
[InlineData("LINESTRING (30 10, 10 30, 40 40)")]
public void LineString_Tests(string geom)
{
DeserializeObject<LineString>(geom, _settings)
.Should()
.Be(
new LineString(
new[]
{
new Coordinate(30, 10),
new Coordinate(10, 30),
new Coordinate(40, 40),
}
)
);
var value = DeserializeObject<LineString>(geom, _settings)
.Should()
.Be(
new LineString(
new[]
{
new Coordinate(30, 10),
new Coordinate(10, 30),
new Coordinate(40, 40),
}
)
).And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))")]
[InlineData("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))")]
public void Polygon_Tests(string geom)
{
DeserializeObject<Polygon>(geom, _settings)
.Should()
.Be(
new Polygon(
new LinearRing(
new[]
{
new Coordinate(30, 10), new Coordinate(40, 40), new Coordinate(20, 40), new Coordinate(10, 20), new Coordinate(30, 10)
}
)
)
);
var value = DeserializeObject<Polygon>(geom, _settings)
.Should()
.Be(
new Polygon(
new LinearRing(
new[]
{
new Coordinate(30, 10), new Coordinate(40, 40), new Coordinate(20, 40), new Coordinate(10, 20), new Coordinate(30, 10)
}
)
)
).And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("MULTIPOINT(10 40, 40 30, 20 20, 30 10)")]
[InlineData("MULTIPOINT ((10 40), (40 30), (20 20), (30 10))")]
public void MultiPoint_Tests(string geom)
{
new ObjectAssertions(DeserializeObject<MultiPoint>(geom, _settings))
.Be(
new MultiPoint(
new[]
{
new Point(10, 40),
new Point(40, 30),
new Point(20, 20),
new Point(30, 10)
}
)
);
var value = new ObjectAssertions(DeserializeObject<MultiPoint>(geom, _settings))
.Be(
new MultiPoint(
new[]
{
new Point(10, 40),
new Point(40, 30),
new Point(20, 20),
new Point(30, 10)
}
)
).And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("MULTILINESTRING((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))")]
[InlineData("MULTILINESTRING ((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))")]
public void MultiLineString_Tests(string geom)
{
new ObjectAssertions(
DeserializeObject<MultiLineString>(geom, _settings)
)
.BeOfType<MultiLineString>();
var value = new ObjectAssertions(
DeserializeObject<MultiLineString>(geom, _settings)
)
.BeOfType<MultiLineString>().Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("MULTIPOLYGON(((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35)), ((30 20, 20 15, 20 25, 30 20)))")]
[InlineData("MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35)), ((30 20, 20 15, 20 25, 30 20)))")]
public void MultiPolygon_Tests(string geom)
{
new ObjectAssertions(
DeserializeObject<MultiPolygon>(geom, _settings)
)
.BeOfType<MultiPolygon>();
var value = new ObjectAssertions(DeserializeObject<MultiPolygon>(geom, _settings))
.BeOfType<MultiPolygon>().Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("GEOMETRYCOLLECTION(POINT(40 10), LINESTRING(10 10, 20 20, 10 40), POLYGON((40 40, 20 45, 45 30, 40 40)))")]
[InlineData("GEOMETRYCOLLECTION (POINT (40 10), LINESTRING (10 10, 20 20, 10 40), POLYGON ((40 40, 20 45, 45 30, 40 40)))")]
public void GeometryCollection_Tests(string geom)
{
new ObjectAssertions(
DeserializeObject<GeometryCollection>(geom, _settings)
)
.BeOfType<GeometryCollection>();
var value = new ObjectAssertions(DeserializeObject<GeometryCollection>(geom, _settings))
.BeOfType<GeometryCollection>().Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

public class ValueOf<T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ private static T DeserializeObject<T>(string geom, JsonSerializerOptions setting
return JsonSerializer.Deserialize<T>("\"" + geom + "\"", settings);
}

private static string SerializeObject(object geom, JsonSerializerOptions settings)
{
return JsonSerializer.Serialize(geom, settings).Trim('"');
}

private readonly JsonSerializerOptions _settings;

public SystemTextJsonNetTopologySuiteWellKnownTextTests(ITestOutputHelper outputHelper) : base(outputHelper)
Expand All @@ -27,98 +32,106 @@ public SystemTextJsonNetTopologySuiteWellKnownTextTests(ITestOutputHelper output
// typeof(MultiPolygon),
// typeof(GeometryCollection)
[Theory]
[InlineData("POINT(30 10)")]
[InlineData("POINT (30 10)")]
public void Geometry_Tests(string geom)
{
DeserializeObject<Point>(geom, _settings)
.Should()
.Be(new Point(30, 10));
var value = DeserializeObject<Point>(geom, _settings)
.Should()
.Be(new Point(30, 10))
.And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("LINESTRING(30 10, 10 30, 40 40)")]
[InlineData("LINESTRING (30 10, 10 30, 40 40)")]
public void LineString_Tests(string geom)
{
DeserializeObject<LineString>(geom, _settings)
.Should()
.Be(
new LineString(
new[]
{
new Coordinate(30, 10),
new Coordinate(10, 30),
new Coordinate(40, 40),
}
)
);
var value = DeserializeObject<LineString>(geom, _settings)
.Should()
.Be(
new LineString(
new[]
{
new Coordinate(30, 10),
new Coordinate(10, 30),
new Coordinate(40, 40),
}
)
)
.And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("POLYGON((30 10, 40 40, 20 40, 10 20, 30 10))")]
[InlineData("POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))")]
public void Polygon_Tests(string geom)
{
DeserializeObject<Polygon>(geom, _settings)
.Should()
.Be(
new Polygon(
new LinearRing(
new[]
{
new Coordinate(30, 10), new Coordinate(40, 40), new Coordinate(20, 40), new Coordinate(10, 20), new Coordinate(30, 10)
}
var value = DeserializeObject<Polygon>(geom, _settings)
.Should()
.Be(
new Polygon(
new LinearRing(
new[]
{
new Coordinate(30, 10), new Coordinate(40, 40), new Coordinate(20, 40), new Coordinate(10, 20), new Coordinate(30, 10)
}
)
)
)
)
);
.And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("MULTIPOINT(10 40, 40 30, 20 20, 30 10)")]
[InlineData("MULTIPOINT ((10 40), (40 30), (20 20), (30 10))")]
public void MultiPoint_Tests(string geom)
{
new ObjectAssertions(
DeserializeObject<MultiPoint>(geom, _settings)
)
.Be(
new MultiPoint(
new[]
{
new Point(10, 40),
new Point(40, 30),
new Point(20, 20),
new Point(30, 10)
}
)
);
var value = new ObjectAssertions(
DeserializeObject<MultiPoint>(geom, _settings)
)
.Be(
new MultiPoint(
new[]
{
new Point(10, 40),
new Point(40, 30),
new Point(20, 20),
new Point(30, 10)
}
)
)
.And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("MULTILINESTRING((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))")]
[InlineData("MULTILINESTRING ((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))")]
public void MultiLineString_Tests(string geom)
{
new ObjectAssertions(
DeserializeObject<MultiLineString>(geom, _settings)
)
.BeOfType<MultiLineString>();
var value = new ObjectAssertions(DeserializeObject<MultiLineString>(geom, _settings))
.BeOfType<MultiLineString>()
.And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("MULTIPOLYGON(((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35)), ((30 20, 20 15, 20 25, 30 20)))")]
[InlineData("MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35)), ((30 20, 20 15, 20 25, 30 20)))")]
public void MultiPolygon_Tests(string geom)
{
new ObjectAssertions(
DeserializeObject<MultiPolygon>(geom, _settings)
)
.BeOfType<MultiPolygon>();
var value = new ObjectAssertions(DeserializeObject<MultiPolygon>(geom, _settings))
.BeOfType<MultiPolygon>()
.And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

[Theory]
[InlineData("GEOMETRYCOLLECTION(POINT(40 10), LINESTRING(10 10, 20 20, 10 40), POLYGON((40 40, 20 45, 45 30, 40 40)))")]
[InlineData("GEOMETRYCOLLECTION (POINT (40 10), LINESTRING (10 10, 20 20, 10 40), POLYGON ((40 40, 20 45, 45 30, 40 40)))")]
public void GeometryCollection_Tests(string geom)
{
new ObjectAssertions(
DeserializeObject<GeometryCollection>(geom, _settings)
)
.BeOfType<GeometryCollection>();
var value = new ObjectAssertions(DeserializeObject<GeometryCollection>(geom, _settings))
.BeOfType<GeometryCollection>()
.And.Subject;
SerializeObject(value, _settings).Should().Be(geom);
}

public class ValueOf<T>
Expand Down

0 comments on commit 7f6c5ab

Please sign in to comment.