Add final tests

This commit is contained in:
Daniel Cronqvist 2024-08-13 23:18:42 +02:00
parent 292dc9b1b9
commit 653e5b5326
16 changed files with 549 additions and 17 deletions

View file

@ -1,3 +1,6 @@
using System.Collections;
using System.Numerics;
namespace DotTiled.Tests;
public static partial class DotTiledAssert
@ -10,6 +13,29 @@ public static partial class DotTiledAssert
return;
}
if (typeof(T) == typeof(float))
{
var expectedFloat = (float)(object)expected;
var actualFloat = (float)(object)actual!;
var expecRounded = MathF.Round(expectedFloat, 3);
var actRounded = MathF.Round(actualFloat, 3);
Assert.True(expecRounded == actRounded, $"Expected {nameof} '{expecRounded}' but got '{actRounded}'");
return;
}
if (expected is Vector2)
{
var expectedVector = (Vector2)(object)expected;
var actualVector = (Vector2)(object)actual!;
AssertEqual(expectedVector.X, actualVector.X, $"{nameof}.X");
AssertEqual(expectedVector.Y, actualVector.Y, $"{nameof}.Y");
return;
}
if (typeof(T).IsArray)
{
var expectedArray = (Array)(object)expected;
@ -24,6 +50,20 @@ public static partial class DotTiledAssert
return;
}
if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>))
{
var expectedList = (IList)(object)expected;
var actualList = (IList)(object)actual!;
Assert.NotNull(actualList);
AssertEqual(expectedList.Count, actualList.Count, $"{nameof}.Count");
for (var i = 0; i < expectedList.Count; i++)
AssertEqual(expectedList[i], actualList[i], $"{nameof}[{i}]");
return;
}
Assert.True(expected.Equals(actual), $"Expected {nameof} '{expected}' but got '{actual}'");
}

View file

@ -13,7 +13,6 @@ public static partial class DotTiledAssert
AssertEqual(expected.Width, actual.Width, nameof(Object.Width));
AssertEqual(expected.Height, actual.Height, nameof(Object.Height));
AssertEqual(expected.Rotation, actual.Rotation, nameof(Object.Rotation));
AssertEqual(expected.GID, actual.GID, nameof(Object.GID));
AssertEqual(expected.Visible, actual.Visible, nameof(Object.Visible));
AssertEqual(expected.Template, actual.Template, nameof(Object.Template));
@ -63,4 +62,10 @@ public static partial class DotTiledAssert
AssertEqual(expected.Text, actual.Text, nameof(TextObject.Text));
}
private static void AssertObject(TileObject expected, TileObject actual)
{
// Attributes
AssertEqual(expected.GID, actual.GID, nameof(TileObject.GID));
}
}

View file

@ -39,6 +39,7 @@ public static partial class TestData
["Serialization/TestData/Map/map_with_flippingflags/map-with-flippingflags", (string f) => TestData.MapWithFlippingFlags(f), Array.Empty<CustomTypeDefinition>()],
["Serialization/TestData/Map/map_external_tileset_multi/map-external-tileset-multi", (string f) => TestData.MapExternalTilesetMulti(f), Array.Empty<CustomTypeDefinition>()],
["Serialization/TestData/Map/map_external_tileset_wangset/map-external-tileset-wangset", (string f) => TestData.MapExternalTilesetWangset(f), Array.Empty<CustomTypeDefinition>()],
["Serialization/TestData/Map/map_with_many_layers/map-with-many-layers", (string f) => TestData.MapWithManyLayers(f), Array.Empty<CustomTypeDefinition>()],
];
private static CustomTypeDefinition[] typedefs = [

View file

@ -0,0 +1,214 @@
using System.Numerics;
namespace DotTiled.Tests;
public partial class TestData
{
public static Map MapWithManyLayers(string fileExt) => new Map
{
Class = "",
Orientation = MapOrientation.Orthogonal,
Width = 5,
Height = 5,
TileWidth = 32,
TileHeight = 32,
Infinite = false,
HexSideLength = null,
StaggerAxis = null,
StaggerIndex = null,
ParallaxOriginX = 0,
ParallaxOriginY = 0,
RenderOrder = RenderOrder.RightDown,
CompressionLevel = -1,
BackgroundColor = new Color { R = 0, G = 0, B = 0, A = 0 },
Version = "1.10",
TiledVersion = "1.11.0",
NextLayerID = 8,
NextObjectID = 7,
Tilesets = [
new Tileset
{
Version = "1.10",
TiledVersion = "1.11.0",
FirstGID = 1,
Name = "tileset",
TileWidth = 32,
TileHeight = 32,
TileCount = 24,
Columns = 8,
Source = $"tileset.{(fileExt == "tmx" ? "tsx" : "tsj")}",
Image = new Image
{
Format = ImageFormat.Png,
Source = "tileset.png",
Width = 256,
Height = 96,
}
}
],
Layers = [
new Group
{
ID = 2,
Name = "Root",
Layers = [
new ObjectLayer
{
ID = 3,
Name = "Objects",
Objects = [
new RectangleObject
{
ID = 1,
Name = "Object 1",
X = 25.6667f,
Y = 28.6667f,
Width = 31.3333f,
Height = 31.3333f
},
new PointObject
{
ID = 3,
Name = "P1",
X = 117.667f,
Y = 48.6667f
},
new EllipseObject
{
ID = 4,
Name = "Circle1",
X = 77f,
Y = 72.3333f,
Width = 34.6667f,
Height = 34.6667f
},
new PolygonObject
{
ID = 5,
Name = "Poly",
X = 20.6667f,
Y = 114.667f,
Points = [
new Vector2(0, 0),
new Vector2(104,20),
new Vector2(35.6667f, 32.3333f)
]
},
new TileObject
{
ID = 6,
Name = "TileObj",
GID = 7,
X = -35,
Y = 110.333f,
Width = 64,
Height = 146
}
]
},
new Group
{
ID = 5,
Name = "Sub",
Layers = [
new TileLayer
{
ID = 7,
Name = "Tile 3",
Width = 5,
Height = 5,
Data = new Data
{
Encoding = DataEncoding.Csv,
Chunks = null,
Compression = null,
GlobalTileIDs = [
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
],
FlippingFlags = [
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None
]
}
},
new TileLayer
{
ID = 6,
Name = "Tile 2",
Width = 5,
Height = 5,
Data = new Data
{
Encoding = DataEncoding.Csv,
Chunks = null,
Compression = null,
GlobalTileIDs = [
0, 15, 15, 0, 0,
0, 15, 15, 0, 0,
0, 15, 15, 15, 0,
15, 15, 15, 0, 0,
0, 0, 0, 0, 0
],
FlippingFlags = [
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None
]
}
}
]
},
new ImageLayer
{
ID = 4,
Name = "ImageLayer",
Image = new Image
{
Format = ImageFormat.Png,
Source = "tileset.png",
Width = fileExt == "tmx" ? 256u : 0, // Currently, json format does not
Height = fileExt == "tmx" ? 96u : 0 // include image dimensions in image layer https://github.com/mapeditor/tiled/issues/4028
},
RepeatX = true
},
new TileLayer
{
ID = 1,
Name = "Tile Layer 1",
Width = 5,
Height = 5,
Data = new Data
{
Encoding = DataEncoding.Csv,
Chunks = null,
Compression = null,
GlobalTileIDs = [
1, 1, 1, 1, 1,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0
],
FlippingFlags = [
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None,
FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None, FlippingFlags.None
]
}
}
]
}
]
};
}

View file

@ -0,0 +1,181 @@
{ "compressionlevel":-1,
"height":5,
"infinite":false,
"layers":[
{
"id":2,
"layers":[
{
"draworder":"topdown",
"id":3,
"name":"Objects",
"objects":[
{
"height":31.3333333333333,
"id":1,
"name":"Object 1",
"rotation":0,
"type":"",
"visible":true,
"width":31.3333333333333,
"x":25.6666666666667,
"y":28.6666666666667
},
{
"height":0,
"id":3,
"name":"P1",
"point":true,
"rotation":0,
"type":"",
"visible":true,
"width":0,
"x":117.666666666667,
"y":48.6666666666667
},
{
"ellipse":true,
"height":34.6666666666667,
"id":4,
"name":"Circle1",
"rotation":0,
"type":"",
"visible":true,
"width":34.6666666666667,
"x":77,
"y":72.3333333333333
},
{
"height":0,
"id":5,
"name":"Poly",
"polygon":[
{
"x":0,
"y":0
},
{
"x":104,
"y":20
},
{
"x":35.6666666666667,
"y":32.3333333333333
}],
"rotation":0,
"type":"",
"visible":true,
"width":0,
"x":20.6666666666667,
"y":114.666666666667
},
{
"gid":7,
"height":146,
"id":6,
"name":"TileObj",
"rotation":0,
"type":"",
"visible":true,
"width":64,
"x":-35,
"y":110.333333333333
}],
"opacity":1,
"type":"objectgroup",
"visible":true,
"x":0,
"y":0
},
{
"id":5,
"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":7,
"name":"Tile 3",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
},
{
"data":[0, 15, 15, 0, 0,
0, 15, 15, 0, 0,
0, 15, 15, 15, 0,
15, 15, 15, 0, 0,
0, 0, 0, 0, 0],
"height":5,
"id":6,
"name":"Tile 2",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":5,
"x":0,
"y":0
}],
"name":"Sub",
"opacity":1,
"type":"group",
"visible":true,
"x":0,
"y":0
},
{
"id":4,
"image":"tileset.png",
"name":"ImageLayer",
"opacity":1,
"repeatx":true,
"type":"imagelayer",
"visible":true,
"x":0,
"y":0
},
{
"data":[1, 1, 1, 1, 1,
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
}],
"name":"Root",
"opacity":1,
"type":"group",
"visible":true,
"x":0,
"y":0
}],
"nextlayerid":8,
"nextobjectid":7,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.11.0",
"tileheight":32,
"tilesets":[
{
"firstgid":1,
"source":"tileset.tsj"
}],
"tilewidth":32,
"type":"map",
"version":"1.10",
"width":5
}

View file

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.11.0" orientation="orthogonal" renderorder="right-down" width="5" height="5" tilewidth="32" tileheight="32" infinite="0" nextlayerid="8" nextobjectid="7">
<tileset firstgid="1" source="tileset.tsx"/>
<group id="2" name="Root">
<objectgroup id="3" name="Objects">
<object id="1" name="Object 1" x="25.6667" y="28.6667" width="31.3333" height="31.3333"/>
<object id="3" name="P1" x="117.667" y="48.6667">
<point/>
</object>
<object id="4" name="Circle1" x="77" y="72.3333" width="34.6667" height="34.6667">
<ellipse/>
</object>
<object id="5" name="Poly" x="20.6667" y="114.667">
<polygon points="0,0 104,20 35.6667,32.3333"/>
</object>
<object id="6" name="TileObj" gid="7" x="-35" y="110.333" width="64" height="146"/>
</objectgroup>
<group id="5" name="Sub">
<layer id="7" name="Tile 3" width="5" height="5">
<data encoding="csv">
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
</data>
</layer>
<layer id="6" name="Tile 2" width="5" height="5">
<data encoding="csv">
0,15,15,0,0,
0,15,15,0,0,
0,15,15,15,0,
15,15,15,0,0,
0,0,0,0,0
</data>
</layer>
</group>
<imagelayer id="4" name="ImageLayer" repeatx="1">
<image source="tileset.png" width="256" height="96"/>
</imagelayer>
<layer id="1" name="Tile Layer 1" width="5" height="5">
<data encoding="csv">
1,1,1,1,1,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0
</data>
</layer>
</group>
</map>

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View file

@ -0,0 +1,14 @@
{ "columns":8,
"image":"tileset.png",
"imageheight":96,
"imagewidth":256,
"margin":0,
"name":"tileset",
"spacing":0,
"tilecount":24,
"tiledversion":"1.11.0",
"tileheight":32,
"tilewidth":32,
"type":"tileset",
"version":"1.10"
}

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.10" tiledversion="1.11.0" name="tileset" tilewidth="32" tileheight="32" tilecount="24" columns="8">
<image source="tileset.png" width="256" height="96"/>
</tileset>

View file

@ -5,8 +5,8 @@ public class ImageLayer : BaseLayer
// Attributes
public uint X { get; set; } = 0;
public uint Y { get; set; } = 0;
public required bool RepeatX { get; set; }
public required bool RepeatY { get; set; }
public bool RepeatX { get; set; } = false;
public bool RepeatY { get; set; } = false;
// At most one of
public Image? Image { get; set; }

View file

@ -13,7 +13,6 @@ public abstract class Object
public float Width { get; set; } = 0f;
public float Height { get; set; } = 0f;
public float Rotation { get; set; } = 0f;
public uint? GID { get; set; }
public bool Visible { get; set; } = true;
public string? Template { get; set; }

View file

@ -0,0 +1,6 @@
namespace DotTiled;
public class TileObject : Object
{
public uint GID { get; set; }
}

View file

@ -25,8 +25,8 @@ internal partial class Tmj
var properties = element.GetOptionalPropertyCustom<Dictionary<string, IProperty>?>("properties", e => ReadProperties(e, customTypeDefinitions), null);
var image = element.GetRequiredProperty<string>("image");
var repeatX = element.GetRequiredProperty<bool>("repeatx");
var repeatY = element.GetRequiredProperty<bool>("repeaty");
var repeatX = element.GetOptionalProperty<bool>("repeatx", false);
var repeatY = element.GetOptionalProperty<bool>("repeaty", false);
var transparentColor = element.GetOptionalPropertyParseable<Color?>("transparentcolor", s => Color.Parse(s, CultureInfo.InvariantCulture), null);
var x = element.GetOptionalProperty<uint>("x", 0);
var y = element.GetOptionalProperty<uint>("y", 0);

View file

@ -97,7 +97,6 @@ internal partial class Tmj
widthDefault = templObj.Width;
heightDefault = templObj.Height;
rotationDefault = templObj.Rotation;
gidDefault = templObj.GID;
visibleDefault = templObj.Visible;
propertiesDefault = templObj.Properties;
ellipseDefault = templObj is EllipseObject;
@ -123,6 +122,25 @@ internal partial class Tmj
var x = element.GetOptionalProperty<float>("x", xDefault);
var y = element.GetOptionalProperty<float>("y", yDefault);
if (gid is not null)
{
return new TileObject
{
ID = id,
Name = name,
Type = type,
X = x,
Y = y,
Width = width,
Height = height,
Rotation = rotation,
Visible = visible,
Template = template,
Properties = properties,
GID = gid.Value
};
}
if (ellipse)
{
return new EllipseObject
@ -135,7 +153,6 @@ internal partial class Tmj
Width = width,
Height = height,
Rotation = rotation,
GID = gid,
Visible = visible,
Template = template,
Properties = properties
@ -154,7 +171,6 @@ internal partial class Tmj
Width = width,
Height = height,
Rotation = rotation,
GID = gid,
Visible = visible,
Template = template,
Properties = properties
@ -173,7 +189,6 @@ internal partial class Tmj
Width = width,
Height = height,
Rotation = rotation,
GID = gid,
Visible = visible,
Template = template,
Properties = properties,
@ -193,7 +208,6 @@ internal partial class Tmj
Width = width,
Height = height,
Rotation = rotation,
GID = gid,
Visible = visible,
Template = template,
Properties = properties,
@ -211,7 +225,6 @@ internal partial class Tmj
text.Width = width;
text.Height = height;
text.Rotation = rotation;
text.GID = gid;
text.Visible = visible;
text.Template = template;
text.Properties = properties;
@ -228,7 +241,6 @@ internal partial class Tmj
Width = width,
Height = height,
Rotation = rotation,
GID = gid,
Visible = visible,
Template = template,
Properties = properties

View file

@ -105,7 +105,6 @@ internal partial class Tmx
widthDefault = templObj.Width;
heightDefault = templObj.Height;
rotationDefault = templObj.Rotation;
gidDefault = templObj.GID;
visibleDefault = templObj.Visible;
propertiesDefault = templObj.Properties;
}
@ -137,12 +136,19 @@ internal partial class Tmx
_ => throw new Exception($"Unknown object marker '{elementName}'")
});
if (gid is not null)
{
obj = new TileObject { ID = id, GID = gid.Value };
reader.Skip();
}
if (obj is null)
{
obj = new RectangleObject { ID = id };
reader.Skip();
}
obj.ID = id;
obj.Name = name;
obj.Type = type;
obj.X = x;
@ -150,7 +156,6 @@ internal partial class Tmx
obj.Width = width;
obj.Height = height;
obj.Rotation = rotation;
obj.GID = gid;
obj.Visible = visible;
obj.Template = template;
obj.Properties = properties;

View file

@ -74,8 +74,8 @@ internal partial class Tmx
var offsetY = reader.GetOptionalAttributeParseable<float>("offsety") ?? 0.0f;
var parallaxX = reader.GetOptionalAttributeParseable<float>("parallaxx") ?? 1.0f;
var parallaxY = reader.GetOptionalAttributeParseable<float>("parallaxy") ?? 1.0f;
var repeatX = reader.GetRequiredAttributeParseable<bool>("repeatx");
var repeatY = reader.GetRequiredAttributeParseable<bool>("repeaty");
var repeatX = (reader.GetOptionalAttributeParseable<uint>("repeatx") ?? 0) == 1;
var repeatY = (reader.GetOptionalAttributeParseable<uint>("repeaty") ?? 0) == 1;
Dictionary<string, IProperty>? properties = null;
Image? image = null;