Add tests for FromClass for CustomClassDefinition

This commit is contained in:
Daniel Cronqvist 2024-09-07 12:59:59 +02:00
parent 762e610904
commit 64f66421c2
2 changed files with 35 additions and 24 deletions

View file

@ -2,16 +2,6 @@ namespace DotTiled.Tests;
public class CustomClassDefinitionTests public class CustomClassDefinitionTests
{ {
[Fact]
public void FromClassType_WhenTypeIsNotCustomClass_ThrowsArgumentException()
{
// Arrange
var type = typeof(string);
// Act & Assert
Assert.Throws<ArgumentException>(() => CustomClassDefinition.FromClassType(type));
}
private sealed class TestClass1 private sealed class TestClass1
{ {
public string Name { get; set; } = "John Doe"; public string Name { get; set; } = "John Doe";
@ -101,12 +91,42 @@ public class CustomClassDefinitionTests
GetCustomClassDefinitionTestData().Select(data => new object[] { data.Item1, data.Item2 }); GetCustomClassDefinitionTestData().Select(data => new object[] { data.Item1, data.Item2 });
[Theory] [Theory]
[MemberData(nameof(CustomClassDefinitionTestData))] [MemberData(nameof(CustomClassDefinitionTestData))]
public void FromClassType_WhenTypeIsCustomClass_ReturnsCustomClassDefinition(Type type, CustomClassDefinition expected) public void FromClass_Type_WhenTypeIsCustomClass_ReturnsCustomClassDefinition(Type type, CustomClassDefinition expected)
{ {
// Arrange & Act // Arrange & Act
var result = CustomClassDefinition.FromClassType(type); var result = CustomClassDefinition.FromClass(type);
// Assert // Assert
AssertCustomClassDefinitionEqual(expected, result); 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

@ -104,25 +104,16 @@ public class CustomClassDefinition : HasPropertiesBase, ICustomTypeDefinition
/// <param name="type">The type of the class to create a custom class definition from.</param> /// <param name="type">The type of the class to create a custom class definition from.</param>
/// <returns>A new <see cref="CustomClassDefinition"/> instance.</returns> /// <returns>A new <see cref="CustomClassDefinition"/> instance.</returns>
/// <exception cref="ArgumentException">Thrown when the specified type is not a class.</exception> /// <exception cref="ArgumentException">Thrown when the specified type is not a class.</exception>
public static CustomClassDefinition FromClassType(Type type) public static CustomClassDefinition FromClass(Type type)
{ {
ArgumentNullException.ThrowIfNull(type, nameof(type));
if (type == typeof(string) || !type.IsClass) if (type == typeof(string) || !type.IsClass)
throw new ArgumentException("Type must be a class.", nameof(type)); throw new ArgumentException("Type must be a class.", nameof(type));
return FromClass(() => Activator.CreateInstance(type)); return FromClass(() => Activator.CreateInstance(type));
} }
/// <summary>
/// Creates a new <see cref="CustomClassDefinition"/> from the specified instance of a class.
/// </summary>
/// <param name="instance">The instance of the class to create a custom class definition from.</param>
/// <returns>A new <see cref="CustomClassDefinition"/> instance.</returns>
public static CustomClassDefinition FromClassInstance(dynamic instance)
{
ArgumentNullException.ThrowIfNull(instance);
return FromClass(() => instance);
}
/// <summary> /// <summary>
/// Creates a new <see cref="CustomClassDefinition"/> from the specified constructible class type. /// Creates a new <see cref="CustomClassDefinition"/> from the specified constructible class type.
/// </summary> /// </summary>