Files
SabreTools.Serialization/SabreTools.Metadata.Test/ModelBackedItemTests.cs

318 lines
10 KiB
C#
Raw Normal View History

2026-03-24 18:03:01 -04:00
using SabreTools.Data.Models.Metadata;
using Xunit;
namespace SabreTools.Metadata.Test
{
public class ModelBackedItemTests
{
#region Private Testing Classes
/// <summary>
/// Testing implementation of DictionaryBase
/// </summary>
private class TestDictionaryBase : DictionaryBase
{
public const string NameKey = "__NAME__";
}
/// <summary>
/// Testing implementation of ModelBackedItem
/// </summary>
private class TestModelBackedItem : ModelBackedItem<TestDictionaryBase>
{
#region Comparision Methods
/// <inheritdoc/>
public override bool Equals(ModelBackedItem? other)
{
// If other is null
if (other is null)
return false;
// If the type is mismatched
if (other is not TestModelBackedItem otherItem)
return false;
// Compare internal models
return _internal.EqualTo(otherItem._internal);
}
/// <inheritdoc/>
public override bool Equals(ModelBackedItem<TestDictionaryBase>? other)
{
// If other is null
if (other is null)
return false;
// If the type is mismatched
if (other is not TestModelBackedItem otherItem)
return false;
// Compare internal models
return _internal.EqualTo(otherItem._internal);
}
#endregion
}
/// <summary>
/// Alternate testing implementation of ModelBackedItem
/// </summary>
private class TestModelAltBackedItem : ModelBackedItem<TestDictionaryBase>
{
#region Comparision Methods
/// <inheritdoc/>
public override bool Equals(ModelBackedItem? other)
{
// If other is null
if (other is null)
return false;
// If the type is mismatched
if (other is not TestModelAltBackedItem otherItem)
return false;
// Compare internal models
return _internal.EqualTo(otherItem._internal);
}
/// <inheritdoc/>
public override bool Equals(ModelBackedItem<TestDictionaryBase>? other)
{
// If other is null
if (other is null)
return false;
// If the type is mismatched
if (other is not TestModelAltBackedItem otherItem)
return false;
// Compare internal models
return _internal.EqualTo(otherItem._internal);
}
#endregion
}
#endregion
#region Equals
[Fact]
public void Equals_NullOther_False()
{
ModelBackedItem self = new TestModelBackedItem();
ModelBackedItem? other = null;
bool actual = self.Equals(other);
Assert.False(actual);
}
[Fact]
public void Equals_MismatchedType_False()
{
ModelBackedItem self = new TestModelBackedItem();
ModelBackedItem? other = new TestModelAltBackedItem();
bool actual = self.Equals(other);
Assert.False(actual);
}
[Fact]
public void Equals_MismatchedTypeAlt_False()
{
ModelBackedItem self = new TestModelAltBackedItem();
ModelBackedItem? other = new TestModelBackedItem();
bool actual = self.Equals(other);
Assert.False(actual);
}
[Fact]
public void Equals_DifferentModels_False()
{
ModelBackedItem<TestDictionaryBase> self = new TestModelBackedItem();
2026-03-26 13:52:00 -04:00
self.Write(TestDictionaryBase.NameKey, "self");
2026-03-24 18:03:01 -04:00
ModelBackedItem<TestDictionaryBase>? other = new TestModelBackedItem();
2026-03-26 13:52:00 -04:00
other.Write(TestDictionaryBase.NameKey, "other");
2026-03-24 18:03:01 -04:00
bool actual = self.Equals(other);
Assert.False(actual);
}
[Fact]
public void Equals_EqualModels_True()
{
ModelBackedItem<TestDictionaryBase> self = new TestModelBackedItem();
2026-03-26 13:52:00 -04:00
self.Write(TestDictionaryBase.NameKey, "name");
2026-03-24 18:03:01 -04:00
ModelBackedItem<TestDictionaryBase>? other = new TestModelBackedItem();
2026-03-26 13:52:00 -04:00
other.Write(TestDictionaryBase.NameKey, "name");
2026-03-24 18:03:01 -04:00
bool actual = self.Equals(other);
Assert.True(actual);
}
#endregion
2026-03-26 13:52:00 -04:00
#region Remove
2026-03-24 18:03:01 -04:00
[Fact]
2026-03-26 13:52:00 -04:00
public void Remove_NullItem_False()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? modelBackedItem = null;
string? fieldName = TestDictionaryBase.NameKey;
2026-03-26 13:52:00 -04:00
bool? actual = modelBackedItem?.Remove(fieldName);
2026-03-24 18:03:01 -04:00
Assert.Null(actual);
}
[Fact]
2026-03-26 13:52:00 -04:00
public void Remove_EmptyFieldName_False()
2026-03-24 18:03:01 -04:00
{
var modelBackedItem = new TestModelBackedItem();
string? fieldName = string.Empty;
2026-03-26 13:52:00 -04:00
bool actual = modelBackedItem.Remove(fieldName);
2026-03-24 18:03:01 -04:00
Assert.False(actual);
}
[Fact]
2026-03-26 13:52:00 -04:00
public void Remove_MissingKey_True()
2026-03-24 18:03:01 -04:00
{
var modelBackedItem = new TestModelBackedItem();
string? fieldName = TestDictionaryBase.NameKey;
2026-03-26 13:52:00 -04:00
bool actual = modelBackedItem.Remove(fieldName);
2026-03-24 18:03:01 -04:00
Assert.True(actual);
2026-03-26 13:52:00 -04:00
Assert.Null(modelBackedItem.ReadString(fieldName));
2026-03-24 18:03:01 -04:00
}
[Fact]
2026-03-26 13:52:00 -04:00
public void Remove_ValidKey_True()
2026-03-24 18:03:01 -04:00
{
var modelBackedItem = new TestModelBackedItem();
2026-03-26 13:52:00 -04:00
modelBackedItem.Write(TestDictionaryBase.NameKey, "value");
2026-03-24 18:03:01 -04:00
string? fieldName = TestDictionaryBase.NameKey;
2026-03-26 13:52:00 -04:00
bool actual = modelBackedItem.Remove(fieldName);
2026-03-24 18:03:01 -04:00
Assert.True(actual);
2026-03-26 13:52:00 -04:00
Assert.Null(modelBackedItem.ReadString(fieldName));
2026-03-24 18:03:01 -04:00
}
#endregion
2026-03-26 13:52:00 -04:00
#region Replace
2026-03-24 18:03:01 -04:00
[Fact]
2026-03-26 13:52:00 -04:00
public void Replace_NullFrom_False()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? from = null;
var to = new TestModelBackedItem();
string? fieldName = TestDictionaryBase.NameKey;
2026-03-26 13:52:00 -04:00
bool actual = to.Replace(from, fieldName);
2026-03-24 18:03:01 -04:00
Assert.False(actual);
}
[Fact]
2026-03-26 13:52:00 -04:00
public void Replace_NullTo_False()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? from = null;
TestModelBackedItem? to = new TestModelBackedItem();
string? fieldName = TestDictionaryBase.NameKey;
2026-03-26 13:52:00 -04:00
bool actual = to.Replace(from, fieldName);
2026-03-24 18:03:01 -04:00
Assert.False(actual);
}
[Fact]
2026-03-26 13:52:00 -04:00
public void Replace_EmptyFieldName_False()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? from = new TestModelBackedItem();
TestModelBackedItem? to = new TestModelBackedItem();
string? fieldName = string.Empty;
2026-03-26 13:52:00 -04:00
bool actual = to.Replace(from, fieldName);
2026-03-24 18:03:01 -04:00
Assert.False(actual);
}
[Fact]
2026-03-26 13:52:00 -04:00
public void Replace_MissingKey_False()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? from = new TestModelBackedItem();
TestModelBackedItem? to = new TestModelBackedItem();
string? fieldName = TestDictionaryBase.NameKey;
2026-03-26 13:52:00 -04:00
bool actual = to.Replace(from, fieldName);
2026-03-24 18:03:01 -04:00
Assert.False(actual);
}
[Fact]
2026-03-26 13:52:00 -04:00
public void Replace_ValidKey_True()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? from = new TestModelBackedItem();
2026-03-26 13:52:00 -04:00
from.Write(TestDictionaryBase.NameKey, "value");
2026-03-24 18:03:01 -04:00
TestModelBackedItem? to = new TestModelBackedItem();
string? fieldName = TestDictionaryBase.NameKey;
2026-03-26 13:52:00 -04:00
bool actual = to.Replace(from, fieldName);
2026-03-24 18:03:01 -04:00
Assert.True(actual);
2026-03-26 13:52:00 -04:00
Assert.Equal("value", to.ReadString(TestDictionaryBase.NameKey));
2026-03-24 18:03:01 -04:00
}
#endregion
2026-03-26 13:52:00 -04:00
#region WriteWithValidation
2026-03-24 18:03:01 -04:00
[Fact]
2026-03-26 13:52:00 -04:00
public void WriteWithValidation_NullItem_False()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? modelBackedItem = null;
string? fieldName = TestDictionaryBase.NameKey;
object value = "value";
2026-03-26 13:52:00 -04:00
bool? actual = modelBackedItem?.WriteWithValidation(fieldName, value);
2026-03-24 18:03:01 -04:00
Assert.Null(actual);
}
[Fact]
2026-03-26 13:52:00 -04:00
public void WriteWithValidation_EmptyFieldName_False()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? modelBackedItem = new TestModelBackedItem();
string? fieldName = string.Empty;
object value = "value";
2026-03-26 13:52:00 -04:00
bool actual = modelBackedItem.WriteWithValidation(fieldName, value);
2026-03-24 18:03:01 -04:00
Assert.False(actual);
}
[Fact]
2026-03-26 13:52:00 -04:00
public void WriteWithValidation_MissingKey_False()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? modelBackedItem = new TestModelBackedItem();
string? fieldName = Rom.SHA1Key;
object value = "value";
2026-03-26 13:52:00 -04:00
bool actual = modelBackedItem.WriteWithValidation(fieldName, value);
2026-03-24 18:03:01 -04:00
Assert.False(actual);
}
[Fact]
2026-03-26 13:52:00 -04:00
public void WriteWithValidation_InvalidKey_True()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? modelBackedItem = new TestModelBackedItem();
2026-03-26 13:52:00 -04:00
modelBackedItem.Write(TestDictionaryBase.NameKey, "old");
2026-03-24 18:03:01 -04:00
string? fieldName = "INVALID";
object value = "value";
2026-03-26 13:52:00 -04:00
bool actual = modelBackedItem.WriteWithValidation(fieldName, value);
2026-03-24 18:03:01 -04:00
Assert.False(actual);
2026-03-26 13:52:00 -04:00
Assert.Null(modelBackedItem.ReadString(fieldName));
2026-03-24 18:03:01 -04:00
}
[Fact]
2026-03-26 13:52:00 -04:00
public void WriteWithValidation_ValidKey_True()
2026-03-24 18:03:01 -04:00
{
TestModelBackedItem? modelBackedItem = new TestModelBackedItem();
2026-03-26 13:52:00 -04:00
modelBackedItem.Write(TestDictionaryBase.NameKey, "old");
2026-03-24 18:03:01 -04:00
string? fieldName = TestDictionaryBase.NameKey;
object value = "value";
2026-03-26 13:52:00 -04:00
bool actual = modelBackedItem.WriteWithValidation(fieldName, value);
2026-03-24 18:03:01 -04:00
Assert.True(actual);
2026-03-26 13:52:00 -04:00
Assert.Equal(value, modelBackedItem.ReadString(fieldName));
2026-03-24 18:03:01 -04:00
}
#endregion
}
}