ファイル

222 行
7.4 KiB
C#

using YukkuriMovieMaker.Project;
using YukkuriMovieMaker.UndoRedo;
namespace UndoTree.Tests;
public sealed class UndoTreeNavigationTests
{
static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable root, bool withReader = true, bool preferReplay = false)
=> new(manager, new AsyncAwaitStatus(), [root], 100, withReader ? UndoRedoManagerReader.Create(manager) : null) { PreferReplay = preferReplay };
static (UndoRedoManager Manager, Document Document, UndoTreeHistory History, UndoTreeNodeViewModel A, UndoTreeNodeViewModel B, UndoTreeNodeViewModel C) Chain(bool preferReplay = false)
{
var (manager, document) = Histories.NewDocument("p1");
var history = Track(manager, document, preferReplay: preferReplay);
Histories.Edit(manager, () => document.X = 1);
var a = history.Cursor;
Histories.Edit(manager, () => document.Parts[0].Value = 10);
var b = history.Cursor;
Histories.Edit(manager, () => document.Y = 5);
var c = history.Cursor;
return (manager, document, history, a, b, c);
}
[Fact]
public async Task JumpingToAnAncestorUndoesEverythingAfterIt()
{
var (_, document, history, a, _, _) = Chain();
await history.JumpAsync(a);
Assert.Equal("X=1 Y=0 Parts=[p1:0]", document.Snapshot());
Assert.Same(a, history.Cursor);
}
[Fact]
public async Task JumpingToTheRootRestoresTheInitialState()
{
var (_, document, history, _, _, _) = Chain();
await history.JumpAsync(history.Root);
Assert.Equal("X=0 Y=0 Parts=[p1:0]", document.Snapshot());
Assert.Same(history.Root, history.Cursor);
}
[Fact]
public async Task JumpingForwardAlongTheRedoLineRedoes()
{
var (_, document, history, a, _, c) = Chain();
await history.JumpAsync(a);
await history.JumpAsync(c);
Assert.Equal("X=1 Y=5 Parts=[p1:10]", document.Snapshot());
Assert.Same(c, history.Cursor);
}
[Fact]
public async Task JumpingToTheCurrentNodeChangesNothing()
{
var (manager, document, history, _, _, c) = Chain();
await history.JumpAsync(c);
Assert.Equal("X=1 Y=5 Parts=[p1:10]", document.Snapshot());
Assert.False(manager.IsRedoable);
Assert.Same(c, history.Cursor);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task JumpingToASiblingBranchRestoresItsStateAndItsUndoSteps(bool preferReplay)
{
var (manager, document, history, a, b, c) = Chain(preferReplay);
await history.JumpAsync(a);
Histories.Edit(manager, () => document.X = 9);
var d = history.Cursor;
await history.JumpAsync(c);
var atC = document.Snapshot();
await history.UndoAsync();
var atB = document.Snapshot();
await history.UndoAsync();
var atA = document.Snapshot();
await history.JumpAsync(d);
Assert.Equal("X=1 Y=5 Parts=[p1:10]", atC);
Assert.Equal("X=1 Y=0 Parts=[p1:10]", atB);
Assert.Equal("X=1 Y=0 Parts=[p1:0]", atA);
Assert.Equal("X=9 Y=0 Parts=[p1:0]", document.Snapshot());
Assert.Same(d, history.Cursor);
Assert.Equal(5, history.Count);
Assert.Contains(b, a.Children);
Assert.Contains(d, a.Children);
}
[Fact]
public async Task AfterJumpingTheManagerCanStillRedoAlongTheBranch()
{
var (manager, document, history, a, b, c) = Chain();
await history.JumpAsync(a);
Histories.Edit(manager, () => document.X = 9);
await history.JumpAsync(b);
var redoable = manager.IsRedoable;
await history.RedoAsync();
Assert.True(redoable);
Assert.Equal("X=1 Y=5 Parts=[p1:10]", document.Snapshot());
Assert.Same(c, history.Cursor);
}
[Fact]
public async Task ReplayingBranchesKeepsTheRedoLineEmptyAfterTheJump()
{
var (manager, document, history, a, b, _) = Chain(preferReplay: true);
await history.JumpAsync(a);
Histories.Edit(manager, () => document.X = 9);
await history.JumpAsync(b);
Assert.False(manager.IsRedoable);
Assert.Same(b, history.Cursor);
}
[Fact]
public async Task AMirrorOnlyHistoryJumpsAlongTheLine()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document, withReader: false);
Histories.Edit(manager, () => document.X = 1);
var a = history.Cursor;
Histories.Edit(manager, () => document.X = 2);
var b = history.Cursor;
await history.JumpAsync(history.Root);
await history.JumpAsync(b);
Assert.Equal(2, document.X);
Assert.Same(b, history.Cursor);
Assert.Contains(a, b.PathFromRoot());
}
[Fact]
public async Task AMirrorOnlyHistoryCannotCrossToAnotherBranch()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document, withReader: false);
Histories.Edit(manager, () => document.X = 1);
var a = history.Cursor;
Histories.Edit(manager, () => document.X = 2);
var b = history.Cursor;
await history.JumpAsync(a);
Histories.Edit(manager, () => document.X = 3);
var failure = await Assert.ThrowsAsync<UndoTreeOperationException>(() => history.JumpAsync(b));
Assert.Equal(Texts.BranchUnavailable, failure.Message);
Assert.Equal(3, document.X);
}
[Fact]
public async Task AFailedRedoLeavesTheTreeInSyncWithTheManager()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
var a = history.Cursor;
manager.AddCommand(new UndoRedoActionCommand(() => { }, () => throw new InvalidOperationException("redo")));
manager.Record();
var b = history.Cursor;
await history.JumpAsync(a);
var reader = UndoRedoManagerReader.Create(manager)!;
await Assert.ThrowsAsync<InvalidOperationException>(() => history.JumpAsync(b));
Assert.Equal(reader.Undo.Count, history.Cursor.Depth);
}
[Fact]
public async Task JumpingWithoutASupportedSourceIsRefused()
{
var (manager, document) = Histories.NewDocument();
Histories.Edit(manager, () => document.X = 1);
using var history = Track(manager, document, withReader: false);
var failure = await Assert.ThrowsAsync<UndoTreeOperationException>(() => history.JumpAsync(history.Root));
Assert.Equal(Texts.Unsupported, failure.Message);
}
[Fact]
public async Task UndoAndRedoGoThroughTheManager()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
await history.UndoAsync();
var undone = document.X;
await history.RedoAsync();
Assert.Equal(0, undone);
Assert.Equal(1, document.X);
Assert.False(manager.IsRedoable);
}
[Fact]
public async Task UndoAndRedoDoNothingWhenThereIsNothingToUndoOrRedo()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
await history.UndoAsync();
await history.RedoAsync();
Assert.False(manager.IsUndoable);
Assert.False(manager.IsRedoable);
}
}