Files
h-school/tests/HSchool.Content.Tests/InheritanceTests.cs
T

58 lines
2.2 KiB
C#

namespace HSchool.Content.Tests;
public class InheritanceTests
{
private readonly CatalogLoader _loader = new();
[Fact]
public void ChildField_ReplacesParentArrayWholesale()
{
var catalog = _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(CatalogLoader.CorePackId, "actions", "sit", """{ "defName": "Sit" }"""),
PackDocuments.Def(CatalogLoader.CorePackId, "actions", "inspect", """{ "defName": "Inspect" }"""),
PackDocuments.Def(
CatalogLoader.CorePackId,
"things",
"base",
"""{ "defName": "FurnitureBase", "abstract": true, "actions": ["Sit", "Inspect"] }"""),
PackDocuments.Def(
CatalogLoader.CorePackId,
"things",
"chair",
"""{ "defName": "Chair", "parent": "FurnitureBase", "actions": ["Sit"] }"""),
]);
Assert.Equal(["Sit"], catalog.Things["Chair"].Actions);
Assert.False(catalog.Things["Chair"].Abstract);
Assert.True(catalog.Things["FurnitureBase"].Abstract);
}
[Fact]
public void CyclicParent_FailsTheCatalog()
{
var ex = Assert.Throws<ContentLoadException>(() => _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(CatalogLoader.CorePackId, "things", "a", """{ "defName": "A", "parent": "B" }"""),
PackDocuments.Def(CatalogLoader.CorePackId, "things", "b", """{ "defName": "B", "parent": "A" }"""),
]));
Assert.Contains("cyclic", ex.Message, StringComparison.OrdinalIgnoreCase);
}
[Fact]
public void ParentOfAnotherKind_FailsTheCatalog()
{
var ex = Assert.Throws<ContentLoadException>(() => _loader.Load(
[CatalogLoader.CorePackId],
[
PackDocuments.Def(CatalogLoader.CorePackId, "rooms", "office", """{ "defName": "Office" }"""),
PackDocuments.Def(CatalogLoader.CorePackId, "things", "chair", """{ "defName": "Chair", "parent": "Office" }"""),
]));
Assert.Contains("different kind", ex.Message, StringComparison.OrdinalIgnoreCase);
}
}