Move test data and current tests into UnitTests

This commit is contained in:
Daniel Cronqvist 2024-09-07 13:23:32 +02:00
parent 0a77a9fec7
commit d23eec4433
67 changed files with 55 additions and 65 deletions

View file

@ -0,0 +1,132 @@
namespace DotTiled.Tests;
public class CustomClassDefinitionTests
{
private sealed class TestClass1
{
public string Name { get; set; } = "John Doe";
public int Age { get; set; } = 42;
}
private static CustomClassDefinition ExpectedTestClass1Definition => new CustomClassDefinition
{
Name = "TestClass1",
UseAs = CustomClassUseAs.All,
Members = new List<IProperty>
{
new StringProperty { Name = "Name", Value = "John Doe" },
new IntProperty { Name = "Age", Value = 42 }
}
};
private sealed class TestClass2WithNestedClass
{
public string Name { get; set; } = "John Doe";
public int Age { get; set; } = 42;
public TestClass1 Nested { get; set; } = new TestClass1();
}
private static CustomClassDefinition ExpectedTestClass2WithNestedClassDefinition => new CustomClassDefinition
{
Name = "TestClass2WithNestedClass",
UseAs = CustomClassUseAs.All,
Members = [
new StringProperty { Name = "Name", Value = "John Doe" },
new IntProperty { Name = "Age", Value = 42 },
new ClassProperty
{
Name = "Nested",
PropertyType = "TestClass1",
Value = []
}
]
};
private sealed class TestClass3WithOverridenNestedClass
{
public string Name { get; set; } = "John Doe";
public int Age { get; set; } = 42;
public TestClass1 Nested { get; set; } = new TestClass1
{
Name = "Jane Doe"
};
}
private static CustomClassDefinition ExpectedTestClass3WithOverridenNestedClassDefinition => new CustomClassDefinition
{
Name = "TestClass3WithOverridenNestedClass",
UseAs = CustomClassUseAs.All,
Members = [
new StringProperty { Name = "Name", Value = "John Doe" },
new IntProperty { Name = "Age", Value = 42 },
new ClassProperty
{
Name = "Nested",
PropertyType = "TestClass1",
Value = [
new StringProperty { Name = "Name", Value = "Jane Doe" },
]
}
]
};
private static IEnumerable<(Type, CustomClassDefinition)> GetCustomClassDefinitionTestData()
{
yield return (typeof(TestClass1), ExpectedTestClass1Definition);
yield return (typeof(TestClass2WithNestedClass), ExpectedTestClass2WithNestedClassDefinition);
yield return (typeof(TestClass3WithOverridenNestedClass), ExpectedTestClass3WithOverridenNestedClassDefinition);
}
private static void AssertCustomClassDefinitionEqual(CustomClassDefinition expected, CustomClassDefinition actual)
{
DotTiledAssert.AssertEqual(expected.ID, actual.ID, nameof(CustomClassDefinition.ID));
DotTiledAssert.AssertEqual(expected.Name, actual.Name, nameof(CustomClassDefinition.Name));
DotTiledAssert.AssertEqual(expected.Color, actual.Color, nameof(CustomClassDefinition.Color));
DotTiledAssert.AssertEqual(expected.DrawFill, actual.DrawFill, nameof(CustomClassDefinition.DrawFill));
DotTiledAssert.AssertEqual(expected.UseAs, actual.UseAs, nameof(CustomClassDefinition.UseAs));
DotTiledAssert.AssertProperties(expected.Members, actual.Members);
}
public static IEnumerable<object[]> CustomClassDefinitionTestData =>
GetCustomClassDefinitionTestData().Select(data => new object[] { data.Item1, data.Item2 });
[Theory]
[MemberData(nameof(CustomClassDefinitionTestData))]
public void FromClass_Type_WhenTypeIsCustomClass_ReturnsCustomClassDefinition(Type type, CustomClassDefinition expected)
{
// Arrange & Act
var result = CustomClassDefinition.FromClass(type);
// Assert
AssertCustomClassDefinitionEqual(expected, result);
}
[Fact]
public void FromClass_Type_WhenTypeIsNull_ThrowsArgumentNullException()
{
// Arrange
Type type = null;
// Act & Assert
Assert.Throws<ArgumentNullException>(() => CustomClassDefinition.FromClass(type));
}
[Fact]
public void FromClass_Type_WhenTypeIsString_ThrowsArgumentException()
{
// Arrange
Type type = typeof(string);
// Act & Assert
Assert.Throws<ArgumentException>(() => CustomClassDefinition.FromClass(type));
}
[Fact]
public void FromClass_Type_WhenTypeIsNotClass_ThrowsArgumentException()
{
// Arrange
Type type = typeof(int);
// Act & Assert
Assert.Throws<ArgumentException>(() => CustomClassDefinition.FromClass(type));
}
}

View file

@ -0,0 +1,110 @@
namespace DotTiled.Tests;
public class CustomEnumDefinitionTests
{
private static void AssertCustomEnumDefinitionEqual(CustomEnumDefinition expected, CustomEnumDefinition actual)
{
DotTiledAssert.AssertEqual(expected.ID, actual.ID, nameof(CustomEnumDefinition.ID));
DotTiledAssert.AssertEqual(expected.Name, actual.Name, nameof(CustomEnumDefinition.Name));
DotTiledAssert.AssertEqual(expected.StorageType, actual.StorageType, nameof(CustomEnumDefinition.StorageType));
DotTiledAssert.AssertEqual(expected.ValueAsFlags, actual.ValueAsFlags, nameof(CustomEnumDefinition.ValueAsFlags));
DotTiledAssert.AssertListOrdered(expected.Values, actual.Values, nameof(CustomEnumDefinition.Values));
}
[Fact]
public void FromEnum_Type_WhenTypeIsNotEnum_ThrowsArgumentException()
{
// Arrange
var type = typeof(string);
// Act & Assert
Assert.Throws<ArgumentException>(() => CustomEnumDefinition.FromEnum(type));
}
private enum TestEnum1 { Value1, Value2, Value3 }
[Fact]
public void FromEnum_Type_WhenTypeIsEnum_ReturnsCustomEnumDefinition()
{
// Arrange
var type = typeof(TestEnum1);
var expected = new CustomEnumDefinition
{
ID = 0,
Name = "TestEnum1",
StorageType = CustomEnumStorageType.Int,
Values = ["Value1", "Value2", "Value3"],
ValueAsFlags = false
};
// Act
var result = CustomEnumDefinition.FromEnum(type);
// Assert
AssertCustomEnumDefinitionEqual(expected, result);
}
[Flags]
private enum TestEnum2 { Value1, Value2, Value3 }
[Fact]
public void FromEnum_Type_WhenEnumIsFlags_ReturnsCustomEnumDefinition()
{
// Arrange
var type = typeof(TestEnum2);
var expected = new CustomEnumDefinition
{
ID = 0,
Name = "TestEnum2",
StorageType = CustomEnumStorageType.Int,
Values = ["Value1", "Value2", "Value3"],
ValueAsFlags = true
};
// Act
var result = CustomEnumDefinition.FromEnum(type);
// Assert
AssertCustomEnumDefinitionEqual(expected, result);
}
[Fact]
public void FromEnum_T_WhenTypeIsEnum_ReturnsCustomEnumDefinition()
{
// Arrange
var expected = new CustomEnumDefinition
{
ID = 0,
Name = "TestEnum1",
StorageType = CustomEnumStorageType.Int,
Values = ["Value1", "Value2", "Value3"],
ValueAsFlags = false
};
// Act
var result = CustomEnumDefinition.FromEnum<TestEnum1>();
// Assert
AssertCustomEnumDefinitionEqual(expected, result);
}
[Fact]
public void FromEnum_T_WhenEnumIsFlags_ReturnsCustomEnumDefinition()
{
// Arrange
var expected = new CustomEnumDefinition
{
ID = 0,
Name = "TestEnum2",
StorageType = CustomEnumStorageType.Int,
Values = ["Value1", "Value2", "Value3"],
ValueAsFlags = true
};
// Act
var result = CustomEnumDefinition.FromEnum<TestEnum2>();
// Assert
AssertCustomEnumDefinitionEqual(expected, result);
}
}

View file

@ -0,0 +1,300 @@
using System.Globalization;
namespace DotTiled.Tests;
public class HasPropertiesBaseTests
{
private sealed class TestHasProperties(IList<IProperty> props) : HasPropertiesBase
{
public override IList<IProperty> GetProperties() => props;
}
[Fact]
public void TryGetProperty_PropertyNotFound_ReturnsFalseAndOutIsNull()
{
// Arrange
var hasProperties = new TestHasProperties([]);
// Act
var result = hasProperties.TryGetProperty<BoolProperty>("Test", out var property);
// Assert
Assert.False(result);
Assert.Null(property);
}
[Fact]
public void TryGetProperty_PropertyFound_ReturnsTrueAndOutIsProperty()
{
// Arrange
List<IProperty> props = [new BoolProperty { Name = "Test", Value = true }];
var hasProperties = new TestHasProperties(props);
// Act
var result = hasProperties.TryGetProperty<BoolProperty>("Test", out var property);
// Assert
Assert.True(result);
Assert.NotNull(property);
Assert.Equal("Test", property.Name);
Assert.True(property.Value);
}
[Fact]
public void GetProperty_PropertyNotFound_ThrowsKeyNotFoundException()
{
// Arrange
var hasProperties = new TestHasProperties([]);
// Act & Assert
_ = Assert.Throws<KeyNotFoundException>(() => hasProperties.GetProperty<BoolProperty>("Test"));
}
[Fact]
public void GetProperty_PropertyFound_ReturnsProperty()
{
// Arrange
List<IProperty> props = [new BoolProperty { Name = "Test", Value = true }];
var hasProperties = new TestHasProperties(props);
// Act
var property = hasProperties.GetProperty<BoolProperty>("Test");
// Assert
Assert.NotNull(property);
Assert.Equal("Test", property.Name);
Assert.True(property.Value);
}
[Fact]
public void GetProperty_PropertyIsWrongType_ThrowsInvalidCastException()
{
// Arrange
List<IProperty> props = [new BoolProperty { Name = "Test", Value = true }];
var hasProperties = new TestHasProperties(props);
// Act & Assert
_ = Assert.Throws<InvalidCastException>(() => hasProperties.GetProperty<IntProperty>("Test"));
}
private sealed class MapTo
{
public bool MapToBool { get; set; } = false;
public Color MapToColor { get; set; } = Color.Parse("#00000000", CultureInfo.InvariantCulture);
public float MapToFloat { get; set; } = 0.0f;
public string MapToFile { get; set; } = "";
public int MapToInt { get; set; } = 0;
public int MapToObject { get; set; } = 0;
public string MapToString { get; set; } = "";
}
[Fact]
public void MapPropertiesTo_NestedPropertyNotFound_ThrowsKeyNotFoundException()
{
// Arrange
List<IProperty> props = [
new ClassProperty {
Name = "ClassInObject",
PropertyType = "MapTo",
Value = [
new StringProperty { Name = "PropertyThatDoesNotExistInMapTo", Value = "Test" }
],
}
];
var hasProperties = new TestHasProperties(props);
// Act & Assert
_ = Assert.Throws<KeyNotFoundException>(() => hasProperties.GetProperty<ClassProperty>("ClassInObject").MapPropertiesTo<MapTo>());
}
[Fact]
public void MapPropertiesTo_NestedPropertyIsNotClassProperty_ThrowsInvalidCastException()
{
// Arrange
List<IProperty> props = [
new StringProperty { Name = "ClassInObject", Value = "Test" }
];
var hasProperties = new TestHasProperties(props);
// Act & Assert
_ = Assert.Throws<InvalidCastException>(() => hasProperties.GetProperty<ClassProperty>("ClassInObject").MapPropertiesTo<MapTo>());
}
[Fact]
public void MapPropertiesTo_NestedAllBasicValidProperties_ReturnsMappedProperty()
{
// Arrange
List<IProperty> props = [
new ClassProperty {
Name = "ClassInObject",
PropertyType = "MapTo",
Value = [
new BoolProperty { Name = "MapToBool", Value = true },
new ColorProperty { Name = "MapToColor", Value = Color.Parse("#FF0000FF", CultureInfo.InvariantCulture) },
new FloatProperty { Name = "MapToFloat", Value = 1.0f },
new StringProperty { Name = "MapToFile", Value = "Test" },
new IntProperty { Name = "MapToInt", Value = 1 },
new IntProperty { Name = "MapToObject", Value = 1 },
new StringProperty { Name = "MapToString", Value = "Test" },
],
}
];
var hasProperties = new TestHasProperties(props);
// Act
var mappedProperty = hasProperties.GetProperty<ClassProperty>("ClassInObject").MapPropertiesTo<MapTo>();
// Assert
Assert.True(mappedProperty.MapToBool);
Assert.Equal(Color.Parse("#FF0000FF", CultureInfo.InvariantCulture), mappedProperty.MapToColor);
Assert.Equal(1.0f, mappedProperty.MapToFloat);
Assert.Equal("Test", mappedProperty.MapToFile);
Assert.Equal(1, mappedProperty.MapToInt);
Assert.Equal(1, mappedProperty.MapToObject);
Assert.Equal("Test", mappedProperty.MapToString);
}
private sealed class NestedMapTo
{
public string NestedMapToString { get; set; } = "";
public MapTo MapToInNested { get; set; } = new MapTo();
}
[Fact]
public void MapPropertiesTo_NestedNestedMapTo_ReturnsMappedProperty()
{
// Arrange
List<IProperty> props = [
new ClassProperty {
Name = "ClassInObject",
PropertyType = "NestedMapTo",
Value = [
new StringProperty { Name = "NestedMapToString", Value = "Test" },
new ClassProperty {
Name = "MapToInNested",
PropertyType = "MapTo",
Value = [
new BoolProperty { Name = "MapToBool", Value = true },
new ColorProperty { Name = "MapToColor", Value = Color.Parse("#FF0000FF", CultureInfo.InvariantCulture) },
new FloatProperty { Name = "MapToFloat", Value = 1.0f },
new StringProperty { Name = "MapToFile", Value = "Test" },
new IntProperty { Name = "MapToInt", Value = 1 },
new IntProperty { Name = "MapToObject", Value = 1 },
new StringProperty { Name = "MapToString", Value = "Test" },
],
},
],
}
];
var hasProperties = new TestHasProperties(props);
// Act
var mappedProperty = hasProperties.GetProperty<ClassProperty>("ClassInObject").MapPropertiesTo<NestedMapTo>();
// Assert
Assert.Equal("Test", mappedProperty.NestedMapToString);
Assert.True(mappedProperty.MapToInNested.MapToBool);
Assert.Equal(Color.Parse("#FF0000FF", CultureInfo.InvariantCulture), mappedProperty.MapToInNested.MapToColor);
Assert.Equal(1.0f, mappedProperty.MapToInNested.MapToFloat);
Assert.Equal("Test", mappedProperty.MapToInNested.MapToFile);
Assert.Equal(1, mappedProperty.MapToInNested.MapToInt);
Assert.Equal(1, mappedProperty.MapToInNested.MapToObject);
Assert.Equal("Test", mappedProperty.MapToInNested.MapToString);
}
private enum TestEnum
{
TestValue1,
TestValue2,
TestValue3
}
private sealed class EnumMapTo
{
public TestEnum EnumMapToEnum { get; set; } = TestEnum.TestValue1;
}
[Fact]
public void MapPropertiesTo_NestedEnumProperty_ReturnsMappedProperty()
{
// Arrange
List<IProperty> props = [
new ClassProperty {
Name = "ClassInObject",
PropertyType = "EnumMapTo",
Value = [
new EnumProperty { Name = "EnumMapToEnum", PropertyType = "TestEnum", Value = new HashSet<string> { "TestValue1" } },
],
}
];
var hasProperties = new TestHasProperties(props);
// Act
var mappedProperty = hasProperties.GetProperty<ClassProperty>("ClassInObject").MapPropertiesTo<EnumMapTo>();
// Assert
Assert.Equal(TestEnum.TestValue1, mappedProperty.EnumMapToEnum);
}
private enum TestEnumWithFlags
{
TestValue1 = 1,
TestValue2 = 2,
TestValue3 = 4
}
private sealed class EnumWithFlagsMapTo
{
public TestEnumWithFlags EnumWithFlagsMapToEnum { get; set; } = TestEnumWithFlags.TestValue1;
}
[Fact]
public void MapPropertiesTo_NestedEnumWithFlagsProperty_ReturnsMappedProperty()
{
// Arrange
List<IProperty> props = [
new ClassProperty {
Name = "ClassInObject",
PropertyType = "EnumWithFlagsMapTo",
Value = [
new EnumProperty { Name = "EnumWithFlagsMapToEnum", PropertyType = "TestEnumWithFlags", Value = new HashSet<string> { "TestValue1", "TestValue2" } },
],
}
];
var hasProperties = new TestHasProperties(props);
// Act
var mappedProperty = hasProperties.GetProperty<ClassProperty>("ClassInObject").MapPropertiesTo<EnumWithFlagsMapTo>();
// Assert
Assert.Equal(TestEnumWithFlags.TestValue1 | TestEnumWithFlags.TestValue2, mappedProperty.EnumWithFlagsMapToEnum);
}
[Fact]
public void MapPropertiesTo_WithProperties_ReturnsMappedProperty()
{
// Arrange
List<IProperty> props = [
new BoolProperty { Name = "MapToBool", Value = true },
new ColorProperty { Name = "MapToColor", Value = Color.Parse("#FF0000FF", CultureInfo.InvariantCulture) },
new FloatProperty { Name = "MapToFloat", Value = 1.0f },
new StringProperty { Name = "MapToFile", Value = "Test" },
new IntProperty { Name = "MapToInt", Value = 1 },
new IntProperty { Name = "MapToObject", Value = 1 },
new StringProperty { Name = "MapToString", Value = "Test" },
];
var hasProperties = new TestHasProperties(props);
// Act
var mappedProperty = hasProperties.MapPropertiesTo<MapTo>();
// Assert
Assert.True(mappedProperty.MapToBool);
Assert.Equal(Color.Parse("#FF0000FF", CultureInfo.InvariantCulture), mappedProperty.MapToColor);
Assert.Equal(1.0f, mappedProperty.MapToFloat);
Assert.Equal("Test", mappedProperty.MapToFile);
Assert.Equal(1, mappedProperty.MapToInt);
Assert.Equal(1, mappedProperty.MapToObject);
Assert.Equal("Test", mappedProperty.MapToString);
}
}