Modelling soon done, starting tests structuring

This commit is contained in:
Daniel Cronqvist 2024-07-26 00:37:47 +02:00
parent d2be83972d
commit 70fc74f43b
46 changed files with 1883 additions and 1230 deletions

View file

@ -24,4 +24,9 @@
<Using Include="Xunit" />
</ItemGroup>
<ItemGroup>
<!-- TmxSerializer test data -->
<EmbeddedResource Include="TmxSerializer/TestData/**/*.tmx" />
</ItemGroup>
</Project>

View file

@ -1,259 +0,0 @@
using System.Text;
using System.Xml.Serialization;
using DotTiled;
namespace DotTiled.Tests;
public class MapTests
{
[Fact]
public void ReadXml_Always_SetsRequiredAttributes()
{
// Arrange
var xml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<map
version="1.2"
class="class"
orientation="orthogonal"
renderorder="right-down"
compressionlevel="5"
width="10"
height="10"
tilewidth="32"
tileheight="32"
parallaxoriginx="0.5"
parallaxoriginy="0.5"
nextlayerid="1"
nextobjectid="1"
infinite="1"
>
</map>
""";
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<object[]> ColorData =>
new List<object[]>
{
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 =
$"""
<?xml version="1.0" encoding="UTF-8"?>
<map
version="1.2"
class="class"
orientation="orthogonal"
renderorder="right-down"
compressionlevel="5"
width="10"
height="10"
tilewidth="32"
tileheight="32"
hexsidelength="10"
staggeraxis="y"
staggerindex="odd"
parallaxoriginx="0.5"
parallaxoriginy="0.5"
backgroundcolor="{color}"
nextlayerid="1"
nextobjectid="1"
infinite="1"
>
</map>
""";
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 =
"""
<?xml version="1.0" encoding="UTF-8"?>
<map
version="1.2"
class="class"
orientation="orthogonal"
renderorder="right-down"
compressionlevel="5"
width="10"
height="10"
tilewidth="32"
tileheight="32"
parallaxoriginx="0.5"
parallaxoriginy="0.5"
nextlayerid="1"
nextobjectid="1"
infinite="1"
>
<properties>
<property name="string" type="string" value="string"/>
<property name="int" type="int" value="42"/>
<property name="float" type="float" value="42.42"/>
<property name="bool" type="bool" value="true"/>
<property name="color" type="color" value="#ff0000"/>
<property name="file" type="file" value="file"/>
<property name="object" type="object" value="5"/>
<property name="class" type="class" propertytype="TestClass">
<properties>
<property name="TestClassString" type="string" value="string"/>
<property name="TestClassInt" type="int" value="43"/>
</properties>
</property>
</properties>
</map>
""";
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<StringProperty>("string").Value);
Assert.Equal(PropertyType.Int, map.Properties["int"].Type);
Assert.Equal(42, map.GetProperty<IntProperty>("int").Value);
Assert.Equal(PropertyType.Float, map.Properties["float"].Type);
Assert.Equal(42.42f, map.GetProperty<FloatProperty>("float").Value);
Assert.Equal(PropertyType.Bool, map.Properties["bool"].Type);
Assert.True(map.GetProperty<BooleanProperty>("bool").Value);
Assert.Equal(PropertyType.Color, map.Properties["color"].Type);
Assert.Equal(new TiledColor { R = 255, G = 0, B = 0, A = 255 }, map.GetProperty<ColorProperty>("color").Value);
Assert.Equal(PropertyType.File, map.Properties["file"].Type);
Assert.Equal("file", map.GetProperty<FileProperty>("file").Value);
Assert.Equal(PropertyType.Object, map.Properties["object"].Type);
Assert.Equal(5, map.GetProperty<ObjectProperty>("object").Value);
Assert.Equal(PropertyType.Class, map.Properties["class"].Type);
var classProperty = map.GetProperty<ClassProperty>("class");
Assert.Equal("TestClass", classProperty.PropertyType);
Assert.Equal(2, classProperty.Value.Count);
Assert.Equal("string", classProperty.GetProperty<StringProperty>("TestClassString").Value);
Assert.Equal(43, classProperty.GetProperty<IntProperty>("TestClassInt").Value);
}
[Fact]
public void ReadXml_Always_1()
{
// Arrange
var xml =
"""
<?xml version="1.0" encoding="UTF-8"?>
<map
version="1.2"
class="class"
orientation="orthogonal"
renderorder="right-down"
compressionlevel="5"
width="10"
height="10"
tilewidth="32"
tileheight="32"
parallaxoriginx="0.5"
parallaxoriginy="0.5"
nextlayerid="1"
nextobjectid="1"
infinite="1"
>
<properties>
<property name="string" type="string" value="string"/>
<property name="int" type="int" value="42"/>
<property name="float" type="float" value="42.42"/>
<property name="bool" type="bool" value="true"/>
<property name="color" type="color" value="#ff0000"/>
<property name="file" type="file" value="file"/>
<property name="object" type="object" value="5"/>
<property name="class" type="class" propertytype="TestClass">
<properties>
<property name="TestClassString" type="string" value="string"/>
<property name="TestClassInt" type="int" value="43"/>
</properties>
</property>
</properties>
<tileset firstgid="1" source="textures/tiles.tsx"/>
</map>
""";
var xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
// Act
var map = Map.LoadFromStream(xmlStream);
}
}

View file

@ -0,0 +1,46 @@
namespace DotTiled.Tests;
public partial class TmxSerializerMapTests
{
private readonly static Map EmptyMap = new Map
{
Version = "1.10",
TiledVersion = "1.11.0",
Orientation = MapOrientation.Orthogonal,
RenderOrder = RenderOrder.RightDown,
Width = 5,
Height = 5,
TileWidth = 32,
TileHeight = 32,
Infinite = false,
NextLayerID = 2,
NextObjectID = 1,
Layers = [
new TileLayer
{
ID = 1,
Name = "Tile Layer 1",
Width = 5,
Height = 5,
Data = new Data
{
Encoding = DataEncoding.Csv,
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
]
}
}
]
};
}

View file

@ -0,0 +1,12 @@
<?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="2" nextobjectid="1">
<layer id="1" name="Tile Layer 1" 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>
</map>

View file

@ -0,0 +1,18 @@
using System.Xml;
namespace DotTiled.Tests;
public static class TmxSerializerTestData
{
public static XmlReader GetReaderFor(string testDataFile)
{
var fullyQualifiedTestDataFile = $"DotTiled.Tests.{testDataFile}";
using var stream = typeof(TmxSerializerTestData).Assembly.GetManifestResourceStream(fullyQualifiedTestDataFile)
?? throw new ArgumentException($"Test data file '{fullyQualifiedTestDataFile}' not found");
using var stringReader = new StreamReader(stream);
var xml = stringReader.ReadToEnd();
var xmlStringReader = new StringReader(xml);
return XmlReader.Create(xmlStringReader);
}
}

View file

@ -0,0 +1,50 @@
namespace DotTiled.Tests;
public partial class TmxSerializerMapTests
{
private static void AssertMap(Map actual, Map expected)
{
// Attributes
Assert.Equal(expected.Version, actual.Version);
Assert.Equal(expected.TiledVersion, actual.TiledVersion);
Assert.Equal(expected.Class, actual.Class);
Assert.Equal(expected.Orientation, actual.Orientation);
Assert.Equal(expected.RenderOrder, actual.RenderOrder);
Assert.Equal(expected.CompressionLevel, actual.CompressionLevel);
Assert.Equal(expected.Width, actual.Width);
Assert.Equal(expected.Height, actual.Height);
Assert.Equal(expected.TileWidth, actual.TileWidth);
Assert.Equal(expected.TileHeight, actual.TileHeight);
Assert.Equal(expected.HexSideLength, actual.HexSideLength);
Assert.Equal(expected.StaggerAxis, actual.StaggerAxis);
Assert.Equal(expected.StaggerIndex, actual.StaggerIndex);
Assert.Equal(expected.ParallaxOriginX, actual.ParallaxOriginX);
Assert.Equal(expected.ParallaxOriginY, actual.ParallaxOriginY);
Assert.Equal(expected.BackgroundColor, actual.BackgroundColor);
Assert.Equal(expected.NextLayerID, actual.NextLayerID);
Assert.Equal(expected.NextObjectID, actual.NextObjectID);
Assert.Equal(expected.Infinite, actual.Infinite);
}
public static IEnumerable<object[]> DeserializeMap_ValidXmlNoExternalTilesets_ReturnsMapWithoutThrowing_Data =>
[
["TmxSerializer.TestData.Map.empty-map.tmx", EmptyMap]
];
[Theory]
[MemberData(nameof(DeserializeMap_ValidXmlNoExternalTilesets_ReturnsMapWithoutThrowing_Data))]
public void DeserializeMap_ValidXmlNoExternalTilesets_ReturnsMapWithoutThrowing(string testDataFile, Map expectedMap)
{
// Arrange
using var reader = TmxSerializerTestData.GetReaderFor(testDataFile);
Func<string, Tileset> externalTilesetResolver = (string s) => throw new NotSupportedException("External tilesets are not supported in this test");
var tmxSerializer = new TmxSerializer(externalTilesetResolver);
// Act
var map = tmxSerializer.DeserializeMap(reader);
// Assert
Assert.NotNull(map);
AssertMap(map, expectedMap);
}
}

View file

@ -0,0 +1,30 @@
namespace DotTiled.Tests;
public class TmxSerializerTests
{
[Fact]
public void TmxSerializerConstructor_ExternalTilesetResolverIsNull_ThrowsArgumentNullException()
{
// Arrange
Func<string, Tileset> externalTilesetResolver = null!;
// Act
Action act = () => _ = new TmxSerializer(externalTilesetResolver);
// Assert
Assert.Throws<ArgumentNullException>(act);
}
[Fact]
public void TmxSerializerConstructor_ExternalTilesetResolverIsNotNull_DoesNotThrow()
{
// Arrange
Func<string, Tileset> externalTilesetResolver = _ => new Tileset();
// Act
var tmxSerializer = new TmxSerializer(externalTilesetResolver);
// Assert
Assert.NotNull(tmxSerializer);
}
}