mirror of
https://github.com/dcronqvist/DotTiled.git
synced 2025-02-05 08:52:50 +02:00
Add some more tests to HasPropertiesBase
This commit is contained in:
parent
53907b9c36
commit
e285f97825
2 changed files with 69 additions and 2 deletions
|
@ -9,6 +9,74 @@ public class HasPropertiesBaseTests
|
|||
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;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
|
||||
namespace DotTiled;
|
||||
|
@ -72,7 +71,7 @@ public abstract class HasPropertiesBase : IHasProperties
|
|||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool TryGetProperty<T>(string name, [NotNullWhen(true)] out T property) where T : IProperty
|
||||
public bool TryGetProperty<T>(string name, out T property) where T : IProperty
|
||||
{
|
||||
var properties = GetProperties();
|
||||
if (properties.FirstOrDefault(_properties => _properties.Name == name) is T prop)
|
||||
|
|
Loading…
Add table
Reference in a new issue