using static GitConverter.Lib.Converters.JsonFormatDetector;
namespace GitConverter.TestsApp.Converters
{
///
/// Unit tests for JsonFormatDetector.
///
///
/// Test strategy
/// - For each supported JSON GIS format (GeoJson, EsriJson, GeoJsonSeq, TopoJson) we provide:
/// 1) A minimal, explicit example written to a temp file (Detect_XXX_Simple).
/// 2) A small variant to exercise slightly different structure (Detect_XXX_Variant).
/// 3) Order-independence checks where relevant (type/features/spatialReference may appear in any order).
/// - These tests write ephemeral files in the system temp folder and clean them up in Dispose().
///
public class JsonFormatDetectorTests : IDisposable
{
private readonly string _root;
public JsonFormatDetectorTests()
{
_root = Path.Combine(Path.GetTempPath(), "GitConverter.JsonFormatTests", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_root);
}
public void Dispose()
{
try { if (Directory.Exists(_root)) Directory.Delete(_root, true); } catch { }
}
private string Write(string name, string content)
{
var path = Path.Combine(_root, name);
File.WriteAllText(path, content);
return path;
}
// ---------- GeoJSON tests ----------
[Fact(DisplayName = "Detect_GeoJson_Simple")]
public void Detect_GeoJson_Simple()
{
var json = @"{ ""type"": ""FeatureCollection"", ""features"": [] }";
var path = Write("geo_simple.json", json);
var result = DetectFromFile(path);
Assert.Equal(Format.GeoJson, result);
}
[Fact(DisplayName = "Detect_GeoJson_Variant")]
public void Detect_GeoJson_Variant()
{
var json = @"{
""type"": ""FeatureCollection"",
""features"": [
{ ""type"": ""Feature"", ""geometry"": { ""type"": ""Point"", ""coordinates"": [0,0] }, ""properties"": { ""id"": 1 } }
]
}";
var path = Write("geo_variant.json", json);
var result = DetectFromFile(path);
Assert.Equal(Format.GeoJson, result);
}
[Fact(DisplayName = "Detect_GeoJson_TypeNotFirst")]
public void Detect_GeoJson_TypeNotFirst()
{
// 'features' property before 'type' — detector should find FeatureCollection regardless of order
var json = @"{ ""features"": [], ""type"": ""FeatureCollection"" }";
var path = Write("geo_type_not_first.json", json);
var result = DetectFromFile(path);
Assert.Equal(Format.GeoJson, result);
}
// ---------- EsriJSON tests ----------
[Fact(DisplayName = "Detect_EsriJson_Simple")]
public void Detect_EsriJson_Simple()
{
var json = @"{ ""spatialReference"": { ""wkid"": 4326 }, ""features"": [] }";
var path = Write("esri_simple.json", json);
var result = DetectFromFile(path);
Assert.Equal(Format.EsriJson, result);
}
[Fact(DisplayName = "Detect_EsriJson_Variant")]
public void Detect_EsriJson_Variant()
{
var json = @"{ ""features"": [], ""geometryType"": ""esriGeometryPoint"", ""spatialReference"": { ""wkt"": ""GEOGCS[...]"" } }";
var path = Write("esri_variant.json", json);
var result = DetectFromFile(path);
Assert.Equal(Format.EsriJson, result);
}
[Fact(DisplayName = "Detect_EsriJson_OrderIndependence")]
public void Detect_EsriJson_OrderIndependence()
{
// 'features' appears before 'spatialReference' or vice versa - detector should handle either order
var json = @"{ ""features"": [], ""otherProp"": 1, ""spatialReference"": { ""wkid"": 3857 } }";
var path = Write("esri_order.json", json);
var result = DetectFromFile(path);
Assert.Equal(Format.EsriJson, result);
}
// ---------- GeoJSON Sequence / NDJSON tests ----------
[Fact(DisplayName = "Detect_GeoJsonSeq_Array")]
public void Detect_GeoJsonSeq_Array()
{
var json = @"[ { ""type"": ""Feature"" }, { ""type"": ""Feature"" } ]";
var path = Write("seq_array.json", json);
var result = DetectFromFile(path);
Assert.Equal(Format.GeoJsonSeq, result);
}
[Fact(DisplayName = "Detect_GeoJsonSeq_NDJSON")]
public void Detect_GeoJsonSeq_NDJSON()
{
// NDJSON: newline-delimited JSON objects (we put two lines)
var json = "{ \"type\": \"Feature\" }\n{ \"type\": \"Feature\" }\n";
var path = Write("seq_ndjson.json", json);
// DetectFromFile attempts to parse and falls back to first-line parsing for NDJSON
var result = DetectFromFile(path);
Assert.Equal(Format.GeoJsonSeq, result);
}
// ---------- TopoJSON tests ----------
[Fact(DisplayName = "Detect_TopoJson_Simple")]
public void Detect_TopoJson_Simple()
{
var json = @"{ ""type"": ""Topology"", ""objects"": {} }";
var path = Write("topo_simple.json", json);
var result = DetectFromFile(path);
Assert.Equal(Format.TopoJson, result);
}
[Fact(DisplayName = "Detect_TopoJson_Variant")]
public void Detect_TopoJson_Variant()
{
var json = @"{
""type"": ""Topology"",
""objects"": {
""example"": { ""type"": ""GeometryCollection"", ""geometries"": [] }
}
}";
var path = Write("topo_variant.json", json);
var result = DetectFromFile(path);
Assert.Equal(Format.TopoJson, result);
}
}
}