Skip to content

Commit

Permalink
add some tests that pass with the fix and fail without the fix
Browse files Browse the repository at this point in the history
  • Loading branch information
airbreather committed Apr 24, 2024
1 parent 4892284 commit c581d9b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
Expand Down Expand Up @@ -68,5 +70,20 @@ public void TestSanD(OgcGeometryType type, int num, bool threeD)
for (int i = 0; i < fc.Count; i++)
FeatureConverterTest.CheckEquality(fc[i], d[i]);
}

[Test]
[GeoJsonIssueNumber(143)]
public void SkippedPropertiesShouldNotThrowWithCompleteObjectInPartialBuffer()
{
var options = DefaultOptions;
var node = JsonSerializer.SerializeToNode(new FeatureCollection(), options)!;
node["_skippedProperty"] = "irrelevant";
var nodes = Enumerable.Repeat(node, 500).ToArray();
using SingleByteReadingMemoryStream stream = new();
JsonSerializer.Serialize(stream, nodes, options);
stream.Position = 0;
var roundtrip = JsonSerializer.Deserialize<FeatureCollection[]>(stream, options);
Assert.That(roundtrip, Has.Length.EqualTo(500));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -128,6 +129,21 @@ public void WriteJsonWithIdTest(string idPropertyName, object id)
CheckEquality(value, deserialized, idPropertyName);
}

[Test]
[GeoJsonIssueNumber(143)]
public void SkippedPropertiesShouldNotThrowWithCompleteObjectInPartialBuffer()
{
var options = DefaultOptions;
var node = JsonSerializer.SerializeToNode(new Feature(), options)!;
node["_skippedProperty"] = "irrelevant";
var nodes = Enumerable.Repeat(node, 500).ToArray();
using SingleByteReadingMemoryStream stream = new();
JsonSerializer.Serialize(stream, nodes, options);
stream.Position = 0;
var roundtrip = JsonSerializer.Deserialize<IFeature[]>(stream, options);
Assert.That(roundtrip, Has.Length.EqualTo(500));
}

public static IEnumerable<object[]> FeatureIdTestCases
{
get
Expand Down Expand Up @@ -244,7 +260,7 @@ public void TestNumericFeatureIdMustBeValidDecimal()
}}
}}
";

Assert.That(() => JsonSerializer.Deserialize<Feature>(serialized, DefaultOptions), Throws.InstanceOf<JsonException>());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using System.Text.Json;
using NetTopologySuite.Geometries;
using NUnit.Framework;
Expand Down Expand Up @@ -184,5 +185,20 @@ public void TestWriteReadWkt(string wkt)

Assert.That(geomS.IsEmpty ? geomD.IsEmpty : geomS.EqualsTopologically(geomD));
}

[Test]
[GeoJsonIssueNumber(143)]
public void SkippedPropertiesShouldNotThrowWithCompleteObjectInPartialBuffer()
{
var options = DefaultOptions;
var node = JsonSerializer.SerializeToNode(Point.Empty, options)!;
node["_skippedProperty"] = "irrelevant";
var nodes = Enumerable.Repeat(node, 500).ToArray();
using SingleByteReadingMemoryStream stream = new();
JsonSerializer.Serialize(stream, nodes, options);
stream.Position = 0;
var roundtrip = JsonSerializer.Deserialize<Geometry[]>(stream, options);
Assert.That(roundtrip, Has.Length.EqualTo(500));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace NetTopologySuite.IO.GeoJSON4STJ.Test;

/// <summary>
/// A <see cref="MemoryStream"/> implementation that will never read more than one byte at a time.
/// </summary>
internal sealed class SingleByteReadingMemoryStream : MemoryStream
{
public override int Read(byte[] buffer, int offset, int count)
{
return base.Read(buffer, offset, Math.Min(1, count));
}

public override int Read(Span<byte> buffer)
{
return base.Read(buffer[.. Math.Min(1, buffer.Length)]);
}

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
return base.ReadAsync(buffer, offset, Math.Min(1, count), cancellationToken);
}

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
return base.ReadAsync(buffer[.. Math.Min(1, buffer.Length)], cancellationToken);
}
}

0 comments on commit c581d9b

Please sign in to comment.