ファイル
YMM4-UndoTree/UndoTree.Tests/UndoTreeAddressTests.cs
T

69 行
2.5 KiB
C#

using YukkuriMovieMaker.UndoRedo;
namespace UndoTree.Tests;
public sealed class UndoTreeAddressTests
{
static (Document Document, IReadOnlyList<IUndoRedoable> Roots, UndoTreeOwners Owners) Open()
{
var document = new Document();
document.Parts.Add(new Part("a"));
document.Parts.Add(new Part("b"));
IReadOnlyList<IUndoRedoable> roots = [document];
return (document, roots, new UndoTreeOwners(roots));
}
[Fact]
public void AnElementIsAddressedByItsCollectionPropertyAndIndex()
{
var (document, roots, owners) = Open();
var hops = UndoTreeAddress.Of(document.Parts[1], roots, owners);
Assert.Equal([new UndoTreeHop(string.Empty, 0), new UndoTreeHop("Parts", 1)], hops);
Assert.Same(document.Parts[1], UndoTreeAddress.Resolve(roots, hops!));
}
[Fact]
public void ARootAndACollectionObjectHaveAnAddressToo()
{
var (document, roots, owners) = Open();
Assert.Equal([new UndoTreeHop(string.Empty, 0)], UndoTreeAddress.Of(document, roots, owners));
var collection = UndoTreeAddress.Of(document.Parts, roots, owners);
Assert.Equal([new UndoTreeHop(string.Empty, 0), new UndoTreeHop("Parts", -1)], collection);
Assert.Same(document.Parts, UndoTreeAddress.Resolve(roots, collection!));
}
[Fact]
public void AnAddressResolvesAgainstAnotherObjectGraphOfTheSameShape()
{
var (_, roots, owners) = Open();
var hops = UndoTreeAddress.Of(((Document)roots[0]).Parts[0], roots, owners)!;
var (other, otherRoots, _) = Open();
Assert.Same(other.Parts[0], UndoTreeAddress.Resolve(otherRoots, hops));
}
[Fact]
public void AnObjectOutsideTheGraphHasNoAddress()
{
var (_, roots, owners) = Open();
Assert.Null(UndoTreeAddress.Of(new Part("stray"), roots, owners));
}
[Fact]
public void AnAddressThatDoesNotFitTheGraphResolvesToNothing()
{
var (_, roots, _) = Open();
Assert.Null(UndoTreeAddress.Resolve(roots, []));
Assert.Null(UndoTreeAddress.Resolve(roots, [new UndoTreeHop(string.Empty, 1)]));
Assert.Null(UndoTreeAddress.Resolve(roots, [new UndoTreeHop("Parts", 0)]));
Assert.Null(UndoTreeAddress.Resolve(roots, [new UndoTreeHop(string.Empty, 0), new UndoTreeHop("Missing", -1)]));
Assert.Null(UndoTreeAddress.Resolve(roots, [new UndoTreeHop(string.Empty, 0), new UndoTreeHop("Parts", 5)]));
Assert.Null(UndoTreeAddress.Resolve(roots, [new UndoTreeHop(string.Empty, 0), new UndoTreeHop("X", 0)]));
}
}