From 7b7e499b0de16325f067def1e3ba8fe61182737f Mon Sep 17 00:00:00 2001 From: Daniel Cronqvist Date: Mon, 12 Aug 2024 22:33:00 +0200 Subject: [PATCH] Add tests for custom type properties in maps --- DotTiled.Tests/Serialization/TestData.cs | 5 +- .../map-with-custom-type-props.cs | 122 ++++++++++++++++++ .../map-with-custom-type-props.tmj | 44 +++++++ .../map-with-custom-type-props.tmx | 21 +++ .../propertytypes.json | 49 +++++++ .../Serialization/Tmj/TmjMapReaderTests.cs | 2 +- .../Serialization/Tmx/TmxMapReaderTests.cs | 2 +- DotTiled/Model/Color.cs | 2 + 8 files changed, 243 insertions(+), 4 deletions(-) create mode 100644 DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.cs create mode 100644 DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.tmj create mode 100644 DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.tmx create mode 100644 DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/propertytypes.json diff --git a/DotTiled.Tests/Serialization/TestData.cs b/DotTiled.Tests/Serialization/TestData.cs index 5cdf444..371e8b7 100644 --- a/DotTiled.Tests/Serialization/TestData.cs +++ b/DotTiled.Tests/Serialization/TestData.cs @@ -28,10 +28,11 @@ public static partial class TestData return stringReader.ReadToEnd(); } - public static IEnumerable MapsThatHaveTmxAndTmj => + public static IEnumerable MapTests => [ ["Serialization.TestData.Map.default_map.default-map", TestData.DefaultMap(), Array.Empty()], - ["Serialization.TestData.Map.map_with_common_props.map-with-common-props", TestData.MapWithCommonProps(), Array.Empty()] + ["Serialization.TestData.Map.map_with_common_props.map-with-common-props", TestData.MapWithCommonProps(), Array.Empty()], + ["Serialization.TestData.Map.map_with_custom_type_props.map-with-custom-type-props", TestData.MapWithCustomTypeProps(), TestData.MapWithCustomTypePropsCustomTypeDefinitions()] ]; private static CustomTypeDefinition[] typedefs = [ diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.cs b/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.cs new file mode 100644 index 0000000..1343f62 --- /dev/null +++ b/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.cs @@ -0,0 +1,122 @@ +using System.Globalization; + +namespace DotTiled.Tests; + +public partial class TestData +{ + public static Map MapWithCustomTypeProps() => 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 = Color.Parse("#00000000", CultureInfo.InvariantCulture), + Version = "1.10", + TiledVersion = "1.11.0", + NextLayerID = 2, + NextObjectID = 1, + Layers = [ + new TileLayer + { + ID = 1, + Name = "Tile Layer 1", + 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 + ] + } + } + ], + Properties = new Dictionary + { + ["customclassprop"] = new ClassProperty + { + Name = "customclassprop", + PropertyType = "CustomClass", + Properties = new Dictionary + { + ["boolinclass"] = new BoolProperty { Name = "boolinclass", Value = true }, + ["colorinclass"] = new ColorProperty { Name = "colorinclass", Value = Color.Parse("#000000ff", CultureInfo.InvariantCulture) }, + ["fileinclass"] = new FileProperty { Name = "fileinclass", Value = "" }, + ["floatinclass"] = new FloatProperty { Name = "floatinclass", Value = 13.37f }, + ["intinclass"] = new IntProperty { Name = "intinclass", Value = 0 }, + ["objectinclass"] = new ObjectProperty { Name = "objectinclass", Value = 0 }, + ["stringinclass"] = new StringProperty { Name = "stringinclass", Value = "This is a set string" } + } + } + } + }; + + // This comes from map-with-custom-type-props/propertytypes.json + public static IReadOnlyCollection MapWithCustomTypePropsCustomTypeDefinitions() => [ + new CustomClassDefinition + { + Name = "CustomClass", + UseAs = CustomClassUseAs.Property, + Members = [ + new BoolProperty + { + Name = "boolinclass", + Value = false + }, + new ColorProperty + { + Name = "colorinclass", + Value = Color.Parse("#000000ff", CultureInfo.InvariantCulture) + }, + new FileProperty + { + Name = "fileinclass", + Value = "" + }, + new FloatProperty + { + Name = "floatinclass", + Value = 0f + }, + new IntProperty + { + Name = "intinclass", + Value = 0 + }, + new ObjectProperty + { + Name = "objectinclass", + Value = 0 + }, + new StringProperty + { + Name = "stringinclass", + Value = "" + } + ] + } + ]; +} diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.tmj b/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.tmj new file mode 100644 index 0000000..a8c7f43 --- /dev/null +++ b/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.tmj @@ -0,0 +1,44 @@ +{ "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":"customclassprop", + "propertytype":"CustomClass", + "type":"class", + "value": + { + "boolinclass":true, + "floatinclass":13.37, + "stringinclass":"This is a set string" + } + }], + "renderorder":"right-down", + "tiledversion":"1.11.0", + "tileheight":32, + "tilesets":[], + "tilewidth":32, + "type":"map", + "version":"1.10", + "width":5 +} \ No newline at end of file diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.tmx b/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.tmx new file mode 100644 index 0000000..c364577 --- /dev/null +++ b/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/map-with-custom-type-props.tmx @@ -0,0 +1,21 @@ + + + + + + + + + + + + + +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 + + + diff --git a/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/propertytypes.json b/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/propertytypes.json new file mode 100644 index 0000000..16c42fb --- /dev/null +++ b/DotTiled.Tests/Serialization/TestData/Map/map-with-custom-type-props/propertytypes.json @@ -0,0 +1,49 @@ +[ + { + "color": "#ffa0a0a4", + "drawFill": true, + "id": 8, + "members": [ + { + "name": "boolinclass", + "type": "bool", + "value": false + }, + { + "name": "colorinclass", + "type": "color", + "value": "" + }, + { + "name": "fileinclass", + "type": "file", + "value": "" + }, + { + "name": "floatinclass", + "type": "float", + "value": 0 + }, + { + "name": "intinclass", + "type": "int", + "value": 0 + }, + { + "name": "objectinclass", + "type": "object", + "value": 0 + }, + { + "name": "stringinclass", + "type": "string", + "value": "" + } + ], + "name": "CustomClass", + "type": "class", + "useAs": [ + "property" + ] + } +] diff --git a/DotTiled.Tests/Serialization/Tmj/TmjMapReaderTests.cs b/DotTiled.Tests/Serialization/Tmj/TmjMapReaderTests.cs index 0cdad90..71e5304 100644 --- a/DotTiled.Tests/Serialization/Tmj/TmjMapReaderTests.cs +++ b/DotTiled.Tests/Serialization/Tmj/TmjMapReaderTests.cs @@ -2,7 +2,7 @@ namespace DotTiled.Tests; public partial class TmjMapReaderTests { - public static IEnumerable Maps => TestData.MapsThatHaveTmxAndTmj; + public static IEnumerable Maps => TestData.MapTests; [Theory] [MemberData(nameof(Maps))] public void TmxMapReaderReadMap_ValidTmjExternalTilesetsAndTemplates_ReturnsMapThatEqualsExpected( diff --git a/DotTiled.Tests/Serialization/Tmx/TmxMapReaderTests.cs b/DotTiled.Tests/Serialization/Tmx/TmxMapReaderTests.cs index 3134863..c0dc083 100644 --- a/DotTiled.Tests/Serialization/Tmx/TmxMapReaderTests.cs +++ b/DotTiled.Tests/Serialization/Tmx/TmxMapReaderTests.cs @@ -4,7 +4,7 @@ namespace DotTiled.Tests; public partial class TmxMapReaderTests { - public static IEnumerable Maps => TestData.MapsThatHaveTmxAndTmj; + public static IEnumerable Maps => TestData.MapTests; [Theory] [MemberData(nameof(Maps))] public void TmxMapReaderReadMap_ValidXmlExternalTilesetsAndTemplates_ReturnsMapThatEqualsExpected( diff --git a/DotTiled/Model/Color.cs b/DotTiled/Model/Color.cs index 29bafe9..ae74d0d 100644 --- a/DotTiled/Model/Color.cs +++ b/DotTiled/Model/Color.cs @@ -66,4 +66,6 @@ public class Color : IParsable, IEquatable public override bool Equals(object? obj) => obj is Color other && Equals(other); public override int GetHashCode() => HashCode.Combine(R, G, B, A); + + public override string ToString() => $"#{A:x2}{R:x2}{G:x2}{B:x2}"; }