using System.Text; using System.Xml.Serialization; using DotTiled; namespace DotTiled.Tests; public class MapTests { [Fact] public void ReadXml_Always_SetsRequiredAttributes() { // Arrange var xml = """ """; var xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)); // Act var map = Map.LoadFromStream(xmlStream); // Assert // Assert all required properties are set Assert.Equal("1.2", map.Version); Assert.Equal("class", map.Class); Assert.Equal(Orientation.Orthogonal, map.Orientation); Assert.Equal(RenderOrder.RightDown, map.RenderOrder); Assert.Equal(5, map.CompressionLevel); Assert.Equal(10u, map.Width); Assert.Equal(10u, map.Height); Assert.Equal(32u, map.TileWidth); Assert.Equal(32u, map.TileHeight); Assert.Equal(0.5f, map.ParallaxOriginX); Assert.Equal(0.5f, map.ParallaxOriginY); Assert.Equal(1u, map.NextLayerId); Assert.Equal(1u, map.NextObjectId); Assert.True(map.Infinite); // Assert all optional properties are set to their default values Assert.Null(map.TiledVersion); Assert.Null(map.HexSideLength); Assert.Null(map.StaggerAxis); Assert.Null(map.StaggerIndex); Assert.Null(map.BackgroundColor); } public static IEnumerable ColorData => new List { new object[] { "#ff0000", new TiledColor { R = 255, G = 0, B = 0, A = 255 } }, new object[] { "#00ff00", new TiledColor { R = 0, G = 255, B = 0, A = 255 } }, new object[] { "#0000ff", new TiledColor { R = 0, G = 0, B = 255, A = 255 } }, new object[] { "#ffffff", new TiledColor { R = 255, G = 255, B = 255, A = 255 } }, new object[] { "#000000", new TiledColor { R = 0, G = 0, B = 0, A = 255 } }, new object[] { "#ff000000", new TiledColor { R = 0, G = 0, B = 0, A = 255 } }, new object[] { "#fe000000", new TiledColor { R = 0, G = 0, B = 0, A = 254 } }, new object[] { "#fe00ff00", new TiledColor { R = 0, G = 255, B = 0, A = 254 } }, }; [Theory] [MemberData(nameof(ColorData))] public void ReadXml_WhenPresent_SetsOptionalAttributes(string color, TiledColor expectedColor) { // Arrange var xml = $""" """; var xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)); // Act var map = Map.LoadFromStream(xmlStream); // Assert // Assert all required properties are set Assert.Equal("1.2", map.Version); Assert.Equal("class", map.Class); Assert.Equal(Orientation.Orthogonal, map.Orientation); Assert.Equal(RenderOrder.RightDown, map.RenderOrder); Assert.Equal(5, map.CompressionLevel); Assert.Equal(10u, map.Width); Assert.Equal(10u, map.Height); Assert.Equal(32u, map.TileWidth); Assert.Equal(32u, map.TileHeight); Assert.Equal(10u, map.HexSideLength); Assert.Equal(StaggerAxis.Y, map.StaggerAxis); Assert.Equal(StaggerIndex.Odd, map.StaggerIndex); Assert.Equal(0.5f, map.ParallaxOriginX); Assert.Equal(0.5f, map.ParallaxOriginY); Assert.Equal(expectedColor, map.BackgroundColor); Assert.Equal(1u, map.NextLayerId); Assert.Equal(1u, map.NextObjectId); Assert.True(map.Infinite); } [Fact] public void ReadXml_Always_ReadsPropertiesCorrectly() { // Arrange var xml = """ """; var xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)); // Act var map = Map.LoadFromStream(xmlStream); // Assert Assert.NotNull(map.Properties); Assert.Equal(8, map.Properties.Count); Assert.Equal(PropertyType.String, map.Properties["string"].Type); Assert.Equal("string", map.GetProperty("string").Value); Assert.Equal(PropertyType.Int, map.Properties["int"].Type); Assert.Equal(42, map.GetProperty("int").Value); Assert.Equal(PropertyType.Float, map.Properties["float"].Type); Assert.Equal(42.42f, map.GetProperty("float").Value); Assert.Equal(PropertyType.Bool, map.Properties["bool"].Type); Assert.True(map.GetProperty("bool").Value); Assert.Equal(PropertyType.Color, map.Properties["color"].Type); Assert.Equal(new TiledColor { R = 255, G = 0, B = 0, A = 255 }, map.GetProperty("color").Value); Assert.Equal(PropertyType.File, map.Properties["file"].Type); Assert.Equal("file", map.GetProperty("file").Value); Assert.Equal(PropertyType.Object, map.Properties["object"].Type); Assert.Equal(5, map.GetProperty("object").Value); Assert.Equal(PropertyType.Class, map.Properties["class"].Type); var classProperty = map.GetProperty("class"); Assert.Equal("TestClass", classProperty.PropertyType); Assert.Equal(2, classProperty.Value.Count); Assert.Equal("string", classProperty.GetProperty("TestClassString").Value); Assert.Equal(43, classProperty.GetProperty("TestClassInt").Value); } [Fact] public void ReadXml_Always_1() { // Arrange var xml = """ """; var xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)); // Act var map = Map.LoadFromStream(xmlStream); } }