Getting further with the json support

This commit is contained in:
Daniel Cronqvist 2024-08-09 23:12:45 +02:00
parent 5626614acd
commit 1168917c23
38 changed files with 1072 additions and 269 deletions

View file

@ -30,11 +30,11 @@ public static partial class DotTiledAssert
Assert.NotNull(actual.Tilesets);
Assert.Equal(expected.Tilesets.Count, actual.Tilesets.Count);
for (var i = 0; i < expected.Tilesets.Count; i++)
AssertTileset(actual.Tilesets[i], expected.Tilesets[i]);
AssertTileset(expected.Tilesets[i], actual.Tilesets[i]);
Assert.NotNull(actual.Layers);
Assert.Equal(expected.Layers.Count, actual.Layers.Count);
for (var i = 0; i < expected.Layers.Count; i++)
AssertLayer(actual.Layers[i], expected.Layers[i]);
AssertLayer(expected.Layers[i], actual.Layers[i]);
}
}

View file

@ -25,8 +25,8 @@
</ItemGroup>
<ItemGroup>
<!-- TmxSerializer test data -->
<EmbeddedResource Include="Serialization/Tmx/TestData/**/*" />
<!-- Test data -->
<EmbeddedResource Include="Serialization/TestData/**/*" />
</ItemGroup>
</Project>

View file

@ -2,12 +2,12 @@ using System.Xml;
namespace DotTiled.Tests;
public static class TmxMapReaderTestData
public static partial class TestData
{
public static XmlReader GetXmlReaderFor(string testDataFile)
{
var fullyQualifiedTestDataFile = $"DotTiled.Tests.{testDataFile}";
using var stream = typeof(TmxMapReaderTestData).Assembly.GetManifestResourceStream(fullyQualifiedTestDataFile)
using var stream = typeof(TestData).Assembly.GetManifestResourceStream(fullyQualifiedTestDataFile)
?? throw new ArgumentException($"Test data file '{fullyQualifiedTestDataFile}' not found");
using var stringReader = new StreamReader(stream);
@ -19,7 +19,7 @@ public static class TmxMapReaderTestData
public static string GetRawStringFor(string testDataFile)
{
var fullyQualifiedTestDataFile = $"DotTiled.Tests.{testDataFile}";
using var stream = typeof(TmxMapReaderTestData).Assembly.GetManifestResourceStream(fullyQualifiedTestDataFile)
using var stream = typeof(TestData).Assembly.GetManifestResourceStream(fullyQualifiedTestDataFile)
?? throw new ArgumentException($"Test data file '{fullyQualifiedTestDataFile}' not found");
using var stringReader = new StreamReader(stream);

View file

@ -0,0 +1,30 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"compression":"gzip",
"data":"H4sIAAAAAAAACmNgoD0AAMrGiJlkAAAA",
"encoding":"base64",
"height":5,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -0,0 +1,30 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"compression":"zlib",
"data":"eJxjYKA9AAAAZAAB",
"encoding":"base64",
"height":5,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -0,0 +1,30 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"compression":"",
"data":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
"encoding":"base64",
"height":5,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -0,0 +1,32 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"data":[0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0],
"height":5,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -1,8 +1,8 @@
namespace DotTiled.Tests;
public partial class TmxMapReaderTests
public partial class TestData
{
private static Map EmptyMapWithProperties() => new Map
public static Map EmptyMapWithProperties() => new Map
{
Version = "1.10",
TiledVersion = "1.11.0",

View file

@ -0,0 +1,69 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"data":[0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0],
"height":5,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"properties":[
{
"name":"MapBool",
"type":"bool",
"value":true
},
{
"name":"MapColor",
"type":"color",
"value":"#ffff0000"
},
{
"name":"MapFile",
"type":"file",
"value":"file.png"
},
{
"name":"MapFloat",
"type":"float",
"value":5.2
},
{
"name":"MapInt",
"type":"int",
"value":42
},
{
"name":"MapObject",
"type":"object",
"value":5
},
{
"name":"MapString",
"type":"string",
"value":"string in map"
}],
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -1,8 +1,8 @@
namespace DotTiled.Tests;
public partial class TmxMapReaderTests
public partial class TestData
{
private static Map EmptyMapWithEncodingAndCompression(DataEncoding dataEncoding, DataCompression? compression) => new Map
public static Map EmptyMapWithEncodingAndCompression(DataEncoding dataEncoding, DataCompression? compression) => new Map
{
Version = "1.10",
TiledVersion = "1.11.0",

View file

@ -1,8 +1,8 @@
namespace DotTiled.Tests;
public partial class TmxMapReaderTests
public partial class TestData
{
private static Map MapWithGroup() => new Map
public static Map MapWithGroup() => new Map
{
Version = "1.10",
TiledVersion = "1.11.0",

View file

@ -0,0 +1,80 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"data":[0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0],
"height":5,
"id":4,
"name":"Tile Layer 2",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
},
{
"id":3,
"layers":[
{
"data":[0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0],
"height":5,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
},
{
"draworder":"topdown",
"id":2,
"name":"Object Layer 1",
"objects":[
{
"height":64.5,
"id":1,
"name":"Name",
"rotation":0,
"type":"",
"visible":true,
"width":64.5,
"x":35.5,
"y":26
}],
"opacity":1,
"type":"objectgroup",
"visible":true,
"x":0,
"y":0
}],
"name":"Group 1",
"opacity":1,
"type":"group",
"visible":true,
"x":0,
"y":0
}],
"nextlayerid":5,
"nextobjectid":2,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -1,8 +1,8 @@
namespace DotTiled.Tests;
public partial class TmxMapReaderTests
public partial class TestData
{
private static Map MapWithObjectTemplate() => new Map
public static Map MapWithObjectTemplate() => new Map
{
Version = "1.10",
TiledVersion = "1.11.0",

View file

@ -0,0 +1,104 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"data":[0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0],
"height":5,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
},
{
"draworder":"topdown",
"id":2,
"name":"Object Layer 1",
"objects":[
{
"height":37.0156,
"id":1,
"template":"map-with-object-template.tj",
"name":"Thingy 2",
"properties":[
{
"name":"Bool",
"type":"bool",
"value":true
},
{
"name":"TestClassInTemplate",
"propertytype":"TestClass",
"type":"class",
"value":
{
"Amount":37,
"Name":"I am here"
}
}],
"rotation":0,
"type":"",
"visible":true,
"width":37.0156,
"x":94.5749,
"y":33.6842
},
{
"id":2,
"template":"map-with-object-template.tj",
"x":29.7976,
"y":33.8693
},
{
"height":37.0156,
"id":3,
"template":"map-with-object-template.tj",
"name":"Thingy 3",
"properties":[
{
"name":"Bool",
"type":"bool",
"value":true
},
{
"name":"TestClassInTemplate",
"propertytype":"TestClass",
"type":"class",
"value":
{
"Name":"I am here 3"
}
}],
"rotation":0,
"type":"",
"visible":true,
"width":37.0156,
"x":5,
"y":5
}],
"opacity":1,
"type":"objectgroup",
"visible":true,
"x":0,
"y":0
}],
"nextlayerid":3,
"nextobjectid":3,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -1,8 +1,8 @@
namespace DotTiled.Tests;
public partial class TmxMapReaderTests
public partial class TestData
{
private static Map SimpleMapWithEmbeddedTileset() => new Map
public static Map SimpleMapWithEmbeddedTileset() => new Map
{
Version = "1.10",
TiledVersion = "1.11.0",
@ -26,6 +26,7 @@ public partial class TmxMapReaderTests
Columns = 4,
Image = new Image
{
Format = ImageFormat.Png,
Source = "tiles.png",
Width = 128,
Height = 64

View file

@ -0,0 +1,45 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"data":[1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
2, 2, 2, 2, 2,
2, 2, 2, 2, 2],
"height":5,
"id":1,
"name":"Tile Layer 1",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[
{
"columns":4,
"firstgid":1,
"image":"tiles.png",
"imageheight":64,
"imagewidth":128,
"margin":0,
"name":"Tileset 1",
"spacing":0,
"tilecount":8,
"tileheight":32,
"tilewidth":32
}],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -0,0 +1,28 @@
{ "object":
{
"height":37.0156,
"id":2,
"name":"Thingy",
"properties":[
{
"name":"Bool",
"type":"bool",
"value":true
},
{
"name":"TestClassInTemplate",
"propertytype":"TestClass",
"type":"class",
"value":
{
"Amount":4.2,
"Name":"Hello there"
}
}],
"rotation":0,
"type":"",
"visible":true,
"width":37.0156
},
"type":"template"
}

View file

@ -2,64 +2,75 @@ namespace DotTiled.Tests;
public partial class TmjMapReaderTests
{
[Fact]
public void Test1()
public static IEnumerable<object[]> DeserializeMap_ValidTmjNoExternalTilesets_ReturnsMapWithoutThrowing_Data =>
[
["Serialization.TestData.Map.empty-map-csv.tmj", TestData.EmptyMapWithEncodingAndCompression(DataEncoding.Csv, null)],
["Serialization.TestData.Map.empty-map-base64.tmj", TestData.EmptyMapWithEncodingAndCompression(DataEncoding.Base64, null)],
["Serialization.TestData.Map.empty-map-base64-gzip.tmj", TestData.EmptyMapWithEncodingAndCompression(DataEncoding.Base64, DataCompression.GZip)],
["Serialization.TestData.Map.empty-map-base64-zlib.tmj", TestData.EmptyMapWithEncodingAndCompression(DataEncoding.Base64, DataCompression.ZLib)],
["Serialization.TestData.Map.simple-tileset-embed.tmj", TestData.SimpleMapWithEmbeddedTileset()],
["Serialization.TestData.Map.empty-map-properties.tmj", TestData.EmptyMapWithProperties()],
];
[Theory]
[MemberData(nameof(DeserializeMap_ValidTmjNoExternalTilesets_ReturnsMapWithoutThrowing_Data))]
public void TmxMapReaderReadMap_ValidTmjNoExternalTilesets_ReturnsMapThatEqualsExpected(string testDataFile, Map expectedMap)
{
// Arrange
var jsonString =
"""
var json = TestData.GetRawStringFor(testDataFile);
static Template ResolveTemplate(string source)
{
"backgroundcolor":"#656667",
"height":4,
"nextobjectid":1,
"nextlayerid":1,
"orientation":"orthogonal",
"properties": [
{
"name":"mapProperty1",
"type":"string",
"value":"one"
},
{
"name":"mapProperty3",
"type":"string",
"value":"twoeee"
}
],
"renderorder":"right-down",
"tileheight":32,
"tilewidth":32,
"version":"1",
"tiledversion":"1.0.3",
"width":4,
"tilesets": [
{
"columns":19,
"firstgid":1,
"image":"image/fishbaddie_parts.png",
"imageheight":480,
"imagewidth":640,
"margin":3,
"name":"",
"properties":[
{
"name":"myProperty1",
"type":"string",
"value":"myProperty1_value"
}],
"spacing":1,
"tilecount":266,
"tileheight":32,
"tilewidth":32
}
]
var templateJson = TestData.GetRawStringFor($"Serialization.TestData.Template.{source}");
//var templateReader = new TmjTemplateReader(templateJson, ResolveTemplate);
return null;
}
""";
static Tileset ResolveTileset(string source)
{
var tilesetJson = TestData.GetXmlReaderFor($"Serialization.TestData.Tileset.{source}");
//var tilesetReader = new TmjTilesetReader(tilesetJson, ResolveTileset, ResolveTemplate);
return null;
}
using var mapReader = new TmjMapReader(json, ResolveTileset, ResolveTemplate);
// Act
using var tmjMapReader = new TmjMapReader(jsonString);
var map = mapReader.ReadMap();
// Assert
var map = tmjMapReader.ReadMap();
Assert.NotNull(map);
DotTiledAssert.AssertMap(expectedMap, map);
}
public static IEnumerable<object[]> DeserializeMap_ValidTmjExternalTilesetsAndTemplates_ReturnsMapThatEqualsExpected_Data =>
[
["Serialization.TestData.Map.map-with-object-template.tmj", TestData.MapWithObjectTemplate()],
["Serialization.TestData.Map.map-with-group.tmj", TestData.MapWithGroup()],
];
[Theory]
[MemberData(nameof(DeserializeMap_ValidTmjExternalTilesetsAndTemplates_ReturnsMapThatEqualsExpected_Data))]
public void TmxMapReaderReadMap_ValidTmjExternalTilesetsAndTemplates_ReturnsMapThatEqualsExpected(string testDataFile, Map expectedMap)
{
// Arrange
var json = TestData.GetRawStringFor(testDataFile);
static Template ResolveTemplate(string source)
{
var templateJson = TestData.GetRawStringFor($"Serialization.TestData.Template.{source}");
//var templateReader = new TmjTemplateReader(templateJson, ResolveTemplate);
return null;
}
static Tileset ResolveTileset(string source)
{
var tilesetJson = TestData.GetXmlReaderFor($"Serialization.TestData.Tileset.{source}");
//var tilesetReader = new TmjTilesetReader(tilesetJson, ResolveTileset, ResolveTemplate);
return null;
}
using var mapReader = new TmjMapReader(json, ResolveTileset, ResolveTemplate);
// Act
var map = mapReader.ReadMap();
// Assert
Assert.NotNull(map);
DotTiledAssert.AssertMap(expectedMap, map);
}
}

View file

@ -78,12 +78,12 @@ public partial class TmxMapReaderTests
public static IEnumerable<object[]> DeserializeMap_ValidXmlNoExternalTilesets_ReturnsMapWithoutThrowing_Data =>
[
["Serialization.Tmx.TestData.Map.empty-map-csv.tmx", EmptyMapWithEncodingAndCompression(DataEncoding.Csv, null)],
["Serialization.Tmx.TestData.Map.empty-map-base64.tmx", EmptyMapWithEncodingAndCompression(DataEncoding.Base64, null)],
["Serialization.Tmx.TestData.Map.empty-map-base64-gzip.tmx", EmptyMapWithEncodingAndCompression(DataEncoding.Base64, DataCompression.GZip)],
["Serialization.Tmx.TestData.Map.empty-map-base64-zlib.tmx", EmptyMapWithEncodingAndCompression(DataEncoding.Base64, DataCompression.ZLib)],
["Serialization.Tmx.TestData.Map.simple-tileset-embed.tmx", SimpleMapWithEmbeddedTileset()],
["Serialization.Tmx.TestData.Map.empty-map-properties.tmx", EmptyMapWithProperties()],
["Serialization.TestData.Map.empty-map-csv.tmx", TestData.EmptyMapWithEncodingAndCompression(DataEncoding.Csv, null)],
["Serialization.TestData.Map.empty-map-base64.tmx", TestData.EmptyMapWithEncodingAndCompression(DataEncoding.Base64, null)],
["Serialization.TestData.Map.empty-map-base64-gzip.tmx", TestData.EmptyMapWithEncodingAndCompression(DataEncoding.Base64, DataCompression.GZip)],
["Serialization.TestData.Map.empty-map-base64-zlib.tmx", TestData.EmptyMapWithEncodingAndCompression(DataEncoding.Base64, DataCompression.ZLib)],
["Serialization.TestData.Map.simple-tileset-embed.tmx", TestData.SimpleMapWithEmbeddedTileset()],
["Serialization.TestData.Map.empty-map-properties.tmx", TestData.EmptyMapWithProperties()],
];
[Theory]
@ -91,16 +91,16 @@ public partial class TmxMapReaderTests
public void TmxMapReaderReadMap_ValidXmlNoExternalTilesets_ReturnsMapThatEqualsExpected(string testDataFile, Map expectedMap)
{
// Arrange
using var reader = TmxMapReaderTestData.GetXmlReaderFor(testDataFile);
using var reader = TestData.GetXmlReaderFor(testDataFile);
static Template ResolveTemplate(string source)
{
using var xmlTemplateReader = TmxMapReaderTestData.GetXmlReaderFor($"Serialization.Tmx.TestData.Template.{source}");
using var xmlTemplateReader = TestData.GetXmlReaderFor($"Serialization.TestData.Template.{source}");
using var templateReader = new TxTemplateReader(xmlTemplateReader, ResolveTileset, ResolveTemplate);
return templateReader.ReadTemplate();
}
static Tileset ResolveTileset(string source)
{
using var xmlTilesetReader = TmxMapReaderTestData.GetXmlReaderFor($"Serialization.Tmx.TestData.Tileset.{source}");
using var xmlTilesetReader = TestData.GetXmlReaderFor($"Serialization.TestData.Tileset.{source}");
using var tilesetReader = new TsxTilesetReader(xmlTilesetReader, ResolveTemplate);
return tilesetReader.ReadTileset();
}
@ -116,8 +116,8 @@ public partial class TmxMapReaderTests
public static IEnumerable<object[]> DeserializeMap_ValidXmlExternalTilesetsAndTemplates_ReturnsMapThatEqualsExpected_Data =>
[
["Serialization.Tmx.TestData.Map.map-with-object-template.tmx", MapWithObjectTemplate()],
["Serialization.Tmx.TestData.Map.map-with-group.tmx", MapWithGroup()],
["Serialization.TestData.Map.map-with-object-template.tmx", TestData.MapWithObjectTemplate()],
["Serialization.TestData.Map.map-with-group.tmx", TestData.MapWithGroup()],
];
[Theory]
@ -125,16 +125,16 @@ public partial class TmxMapReaderTests
public void TmxMapReaderReadMap_ValidXmlExternalTilesetsAndTemplates_ReturnsMapThatEqualsExpected(string testDataFile, Map expectedMap)
{
// Arrange
using var reader = TmxMapReaderTestData.GetXmlReaderFor(testDataFile);
using var reader = TestData.GetXmlReaderFor(testDataFile);
static Template ResolveTemplate(string source)
{
using var xmlTemplateReader = TmxMapReaderTestData.GetXmlReaderFor($"Serialization.Tmx.TestData.Template.{source}");
using var xmlTemplateReader = TestData.GetXmlReaderFor($"Serialization.TestData.Template.{source}");
using var templateReader = new TxTemplateReader(xmlTemplateReader, ResolveTileset, ResolveTemplate);
return templateReader.ReadTemplate();
}
static Tileset ResolveTileset(string source)
{
using var xmlTilesetReader = TmxMapReaderTestData.GetXmlReaderFor($"Serialization.Tmx.TestData.Tileset.{source}");
using var xmlTilesetReader = TestData.GetXmlReaderFor($"Serialization.TestData.Tileset.{source}");
using var tilesetReader = new TsxTilesetReader(xmlTilesetReader, ResolveTemplate);
return tilesetReader.ReadTileset();
}