482 行
17 KiB
C#
482 行
17 KiB
C#
using YukkuriMovieMaker.Plugin;
|
|
using YukkuriMovieMaker.Project;
|
|
using YukkuriMovieMaker.UndoRedo;
|
|
|
|
namespace UndoTree.Tests;
|
|
|
|
public sealed class UndoTreeBranchTests
|
|
{
|
|
static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable document, bool withReader = true, int capacity = 100)
|
|
=> new(manager, new AsyncAwaitStatus(), [document], capacity, withReader ? UndoRedoManagerReader.Create(manager) : null);
|
|
|
|
static (UndoRedoManager Manager, Document Document, UndoTreeHistory History, UndoTreeNodeViewModel[] Nodes) Chain(int length, int capacity = 100)
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
var history = Track(manager, document, capacity: capacity);
|
|
var nodes = new UndoTreeNodeViewModel[length];
|
|
for (var index = 0; index < length; index++)
|
|
{
|
|
Histories.Edit(manager, () => document.X = index + 1);
|
|
nodes[index] = history.Cursor;
|
|
}
|
|
|
|
return (manager, document, history, nodes);
|
|
}
|
|
|
|
static async Task UndoAll(UndoRedoManager manager)
|
|
{
|
|
while (manager.IsUndoable)
|
|
await manager.UndoAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AStartedBranchIsActiveAndBeginsAtTheNodeItWasStartedFrom()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
|
|
var branch = await history.CreateBranchAsync(history.Cursor);
|
|
|
|
Assert.Same(branch, history.ActiveBranch);
|
|
Assert.Same(branch, Assert.Single(history.NamedBranches));
|
|
Assert.Same(nodes[1], branch.Base);
|
|
Assert.Same(nodes[1], branch.Head);
|
|
Assert.Same(nodes[1], history.UndoOrigin);
|
|
Assert.Equal(string.Format(Texts.BranchDefaultName, 1), branch.Name);
|
|
Assert.False(manager.IsUndoable);
|
|
Assert.Equal(2, document.X);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UndoInsideABranchStopsAtItsStart()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
await history.CreateBranchAsync(history.Cursor);
|
|
Histories.Edit(manager, () => document.X = 3);
|
|
Histories.Edit(manager, () => document.X = 4);
|
|
|
|
await UndoAll(manager);
|
|
|
|
Assert.Equal(2, document.X);
|
|
Assert.Same(nodes[1], history.Cursor);
|
|
Assert.Equal(5, history.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TwoBranchesStartedFromTheSameNodeKeepTheirOwnStates()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
var first = await history.CreateBranchAsync(nodes[1]);
|
|
Histories.Edit(manager, () => document.X = 10);
|
|
var second = await history.CreateBranchAsync(nodes[1]);
|
|
Histories.Edit(manager, () => document.X = 20);
|
|
|
|
var atSecond = document.X;
|
|
await history.SwitchToAsync(first);
|
|
var atFirst = document.X;
|
|
await UndoAll(manager);
|
|
var originOfFirst = document.X;
|
|
await history.SwitchToAsync(second);
|
|
|
|
Assert.Equal(20, atSecond);
|
|
Assert.Equal(10, atFirst);
|
|
Assert.Equal(2, originOfFirst);
|
|
Assert.Equal(20, document.X);
|
|
Assert.Same(second, history.ActiveBranch);
|
|
Assert.Equal(2, history.NamedBranches.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task LeavingABranchReturnsToWhereTheWholeHistoryWasLeft()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
await history.CreateBranchAsync(nodes[0]);
|
|
|
|
await history.SwitchToAsync(null);
|
|
var back = document.X;
|
|
await UndoAll(manager);
|
|
|
|
Assert.Equal(2, back);
|
|
Assert.Equal(0, document.X);
|
|
Assert.Null(history.ActiveBranch);
|
|
Assert.Same(history.Root, history.UndoOrigin);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ABranchRemembersWhereYouLeftIt()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
var branch = await history.CreateBranchAsync(nodes[1]);
|
|
Histories.Edit(manager, () => document.X = 3);
|
|
var third = history.Cursor;
|
|
Histories.Edit(manager, () => document.X = 4);
|
|
await manager.UndoAsync();
|
|
|
|
await history.SwitchToAsync(null);
|
|
var outside = document.X;
|
|
await history.SwitchToAsync(branch);
|
|
var inside = document.X;
|
|
await manager.RedoAsync();
|
|
|
|
Assert.Equal(2, outside);
|
|
Assert.Equal(3, inside);
|
|
Assert.Equal(4, document.X);
|
|
Assert.Same(third, branch.Head);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task JumpingOutOfABranchLeavesItAndRemembersTheHead()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
var branch = await history.CreateBranchAsync(nodes[1]);
|
|
Histories.Edit(manager, () => document.X = 3);
|
|
var third = history.Cursor;
|
|
|
|
await history.JumpAsync(nodes[0]);
|
|
var leftAt = (history.ActiveBranch, history.UndoOrigin, branch.Head);
|
|
await history.SwitchToAsync(branch);
|
|
|
|
Assert.Null(leftAt.ActiveBranch);
|
|
Assert.Same(history.Root, leftAt.UndoOrigin);
|
|
Assert.Same(third, leftAt.Head);
|
|
Assert.Same(third, history.Cursor);
|
|
Assert.Same(branch, history.ActiveBranch);
|
|
Assert.Same(nodes[1], history.UndoOrigin);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SwitchingToTheBranchThatIsAlreadyActiveChangesNothing()
|
|
{
|
|
var (_, _, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
var branch = await history.CreateBranchAsync(nodes[1]);
|
|
var announced = 0;
|
|
history.Changed += (_, _) => announced++;
|
|
|
|
await history.SwitchToAsync(branch);
|
|
|
|
Assert.Equal(0, announced);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RenamingTrimsTheNameAndRefusesAnEmptyOrUnchangedOne()
|
|
{
|
|
var (_, _, history, nodes) = Chain(1);
|
|
using var _ = history;
|
|
var branch = await history.CreateBranchAsync(nodes[0]);
|
|
var announced = 0;
|
|
history.Changed += (_, _) => announced++;
|
|
|
|
var renamed = history.RenameBranch(branch, " Colors ");
|
|
var empty = history.RenameBranch(branch, " ");
|
|
var same = history.RenameBranch(branch, "Colors");
|
|
|
|
Assert.True(renamed);
|
|
Assert.False(empty);
|
|
Assert.False(same);
|
|
Assert.Equal("Colors", branch.Name);
|
|
Assert.Equal(1, announced);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ClosingTheActiveBranchKeepsTheStateAndLiftsTheUndoOrigin()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
var branch = await history.CreateBranchAsync(nodes[1]);
|
|
Histories.Edit(manager, () => document.X = 3);
|
|
var tip = history.Cursor;
|
|
|
|
var closed = history.CloseBranch(branch);
|
|
await UndoAll(manager);
|
|
|
|
Assert.True(closed);
|
|
Assert.Empty(history.NamedBranches);
|
|
Assert.Null(history.ActiveBranch);
|
|
Assert.Same(history.Root, history.UndoOrigin);
|
|
Assert.Equal(0, document.X);
|
|
Assert.Equal(4, history.Count);
|
|
Assert.NotNull(tip);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ClosingTheActiveBranchAtItsBaseReturnsToTheBaseKeepsTheEditsAndLiftsTheUndoOrigin()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
await history.CreateBranchAsync(nodes[1]);
|
|
Histories.Edit(manager, () => document.X = 3);
|
|
var third = history.Cursor;
|
|
Histories.Edit(manager, () => document.X = 4);
|
|
await manager.UndoAsync();
|
|
|
|
await history.CloseBranchAtBaseAsync(history.ActiveBranch!);
|
|
var atBase = document.X;
|
|
await UndoAll(manager);
|
|
|
|
Assert.Equal(2, atBase);
|
|
Assert.Equal(0, document.X);
|
|
Assert.Empty(history.NamedBranches);
|
|
Assert.Null(history.ActiveBranch);
|
|
Assert.Same(history.Root, history.UndoOrigin);
|
|
Assert.Contains(third, history.Nodes);
|
|
Assert.Equal(5, history.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ClosingABranchThatIsNotActiveAtItsBaseOnlyRemovesIt()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
var first = await history.CreateBranchAsync(nodes[1]);
|
|
Histories.Edit(manager, () => document.X = 3);
|
|
var second = await history.CreateBranchAsync(nodes[0]);
|
|
Histories.Edit(manager, () => document.X = 7);
|
|
|
|
await history.CloseBranchAtBaseAsync(first);
|
|
|
|
Assert.Same(second, Assert.Single(history.NamedBranches));
|
|
Assert.Same(second, history.ActiveBranch);
|
|
Assert.Equal(7, document.X);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ClosingAnotherBranchLeavesTheActiveOneAlone()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
var first = await history.CreateBranchAsync(nodes[0]);
|
|
var second = await history.CreateBranchAsync(nodes[1]);
|
|
|
|
var closed = history.CloseBranch(first);
|
|
var again = history.CloseBranch(first);
|
|
await UndoAll(manager);
|
|
|
|
Assert.True(closed);
|
|
Assert.False(again);
|
|
Assert.Same(second, Assert.Single(history.NamedBranches));
|
|
Assert.Same(second, history.ActiveBranch);
|
|
Assert.Same(nodes[1], history.UndoOrigin);
|
|
Assert.Equal(2, document.X);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AClearOfTheHostForgetsEveryBranch()
|
|
{
|
|
var (manager, _, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
await history.CreateBranchAsync(nodes[0]);
|
|
await history.CreateBranchAsync(nodes[1]);
|
|
|
|
manager.Clear();
|
|
|
|
Assert.Empty(history.NamedBranches);
|
|
Assert.Null(history.ActiveBranch);
|
|
Assert.Same(history.Root, history.UndoOrigin);
|
|
Assert.Equal(string.Format(Texts.BranchDefaultName, 1), (await history.CreateBranchAsync(history.Root)).Name);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DroppingSideBranchesRemovesTheBranchesWhoseStartIsGoneAndPullsBackTheirHeads()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(3);
|
|
using var _ = history;
|
|
var kept = await history.CreateBranchAsync(nodes[1]);
|
|
await history.JumpAsync(nodes[2]);
|
|
await history.SwitchToAsync(null);
|
|
await history.JumpAsync(nodes[1]);
|
|
Histories.Edit(manager, () => document.X = 8);
|
|
var newer = history.Cursor;
|
|
await history.JumpAsync(nodes[1]);
|
|
var doomed = await history.CreateBranchAsync(nodes[2]);
|
|
await history.SwitchToAsync(null);
|
|
await history.JumpAsync(newer);
|
|
await history.JumpAsync(nodes[1]);
|
|
|
|
history.DropSideBranches();
|
|
|
|
Assert.Contains(kept, history.NamedBranches);
|
|
Assert.DoesNotContain(doomed, history.NamedBranches);
|
|
Assert.Same(nodes[1], kept.Head);
|
|
Assert.DoesNotContain(nodes[2], history.Nodes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PruningOldBranchesKeepsTheHeadOfABranch()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(1, capacity: 5);
|
|
using var _ = history;
|
|
var branch = await history.CreateBranchAsync(nodes[0]);
|
|
Histories.Edit(manager, () => document.X = 2);
|
|
var head = history.Cursor;
|
|
await history.SwitchToAsync(null);
|
|
|
|
for (var value = 3; value <= 6; value++)
|
|
Histories.Edit(manager, () => document.X = value);
|
|
|
|
Assert.Contains(head, history.Nodes);
|
|
Assert.Same(head, branch.Head);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheReturnPointOfTheWholeHistoryIsPulledBackWhenItsNodeIsDropped()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(3);
|
|
using var _ = history;
|
|
await history.CreateBranchAsync(nodes[1]);
|
|
Histories.Edit(manager, () => document.X = 8);
|
|
var newer = history.Cursor;
|
|
await history.JumpAsync(nodes[1]);
|
|
await history.JumpAsync(newer);
|
|
|
|
history.DropSideBranches();
|
|
await history.SwitchToAsync(null);
|
|
|
|
Assert.DoesNotContain(nodes[2], history.Nodes);
|
|
Assert.Same(nodes[1], history.Cursor);
|
|
Assert.Equal(2, document.X);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PruningOldBranchesKeepsTheReturnPointOfTheWholeHistory()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(2, capacity: 5);
|
|
using var _ = history;
|
|
await history.CreateBranchAsync(nodes[0]);
|
|
|
|
for (var value = 3; value <= 6; value++)
|
|
Histories.Edit(manager, () => document.X = value);
|
|
|
|
Assert.Contains(nodes[1], history.Nodes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WithoutRewritableListsABranchCannotBeStarted()
|
|
{
|
|
var (manager, document) = Histories.NewDocument();
|
|
using var history = Track(manager, document, withReader: false);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
|
|
await Assert.ThrowsAsync<UndoTreeOperationException>(() => history.CreateBranchAsync(history.Cursor));
|
|
|
|
Assert.Empty(history.NamedBranches);
|
|
Assert.Null(history.ActiveBranch);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WhenTheHostTrimsTheListTheActiveBranchStartsLater()
|
|
{
|
|
var (manager, document, history, nodes) = Chain(4);
|
|
using var _ = history;
|
|
var branch = await history.CreateBranchAsync(nodes[3]);
|
|
manager.Capacity = 3;
|
|
|
|
for (var value = 5; value <= 8; value++)
|
|
Histories.Edit(manager, () => document.X = value);
|
|
|
|
Assert.Same(branch, history.ActiveBranch);
|
|
Assert.Same(history.UndoOrigin, branch.Base);
|
|
Assert.NotSame(nodes[3], branch.Base);
|
|
Assert.Equal(9, history.Count);
|
|
Assert.True(history.RootIsOpenState);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheNodeWhereABranchStartsIsMarkedWithItsName()
|
|
{
|
|
var (_, _, history, nodes) = Chain(2);
|
|
using var _ = history;
|
|
|
|
var first = await history.CreateBranchAsync(nodes[1]);
|
|
var second = await history.CreateBranchAsync(nodes[1]);
|
|
history.RenameBranch(first, "Colors");
|
|
|
|
Assert.Equal($"[{string.Format(Texts.BranchMark, "Colors")}][{string.Format(Texts.BranchMark, second.Name)}]", nodes[1].BranchMarks);
|
|
Assert.StartsWith(nodes[1].BranchMarks, nodes[1].DisplayText);
|
|
Assert.Equal(string.Empty, nodes[0].BranchMarks);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheMarkGoesAwayWithTheBranch()
|
|
{
|
|
var (_, _, history, nodes) = Chain(1);
|
|
using var _ = history;
|
|
var branch = await history.CreateBranchAsync(nodes[0]);
|
|
|
|
history.CloseBranch(branch);
|
|
|
|
Assert.Equal(string.Empty, nodes[0].BranchMarks);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheTreeTheListsAndTheBranchesStayInStepWhateverHappens()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
var reader = UndoRedoManagerReader.Create(manager)!;
|
|
var states = new Dictionary<int, int> { [history.Root.Id] = 0 };
|
|
history.Changed += (_, _) => Assert.Same(history.ActiveBranch?.Base ?? history.Root, history.UndoOrigin);
|
|
var random = new Random(20261103);
|
|
var counter = 0;
|
|
var count = 1;
|
|
for (var step = 0; step < 500; step++)
|
|
{
|
|
switch (random.Next(11))
|
|
{
|
|
case 0 or 1 or 2:
|
|
Histories.Edit(manager, () => document.X = ++counter);
|
|
states[history.Cursor.Id] = counter;
|
|
break;
|
|
case 3 when manager.IsUndoable:
|
|
await manager.UndoAsync();
|
|
break;
|
|
case 4 when manager.IsRedoable:
|
|
await manager.RedoAsync();
|
|
break;
|
|
case 5:
|
|
var nodes = history.Nodes.ToArray();
|
|
await history.JumpAsync(nodes[random.Next(nodes.Length)]);
|
|
break;
|
|
case 6:
|
|
var from = history.Nodes.ToArray();
|
|
await history.CreateBranchAsync(from[random.Next(from.Length)]);
|
|
break;
|
|
case 7 when history.NamedBranches.Count > 0:
|
|
await history.SwitchToAsync(history.NamedBranches[random.Next(history.NamedBranches.Count)]);
|
|
break;
|
|
case 8:
|
|
await history.SwitchToAsync(null);
|
|
break;
|
|
case 9 when history.NamedBranches.Count > 0:
|
|
await history.CloseBranchAtBaseAsync(history.NamedBranches[random.Next(history.NamedBranches.Count)]);
|
|
break;
|
|
default:
|
|
if (history.NamedBranches.Count > 0)
|
|
history.CloseBranch(history.NamedBranches[random.Next(history.NamedBranches.Count)]);
|
|
|
|
break;
|
|
}
|
|
|
|
var cursor = history.Cursor;
|
|
Assert.Equal(states[cursor.Id], document.X);
|
|
Assert.Contains(history.UndoOrigin, cursor.PathFromRoot());
|
|
Assert.Equal(cursor.StepsBelow(history.UndoOrigin).Select(node => node.Handle), reader.Undo);
|
|
Assert.Equal(!ReferenceEquals(cursor, history.UndoOrigin), manager.IsUndoable);
|
|
Assert.Same(history.ActiveBranch?.Base ?? history.Root, history.UndoOrigin);
|
|
Assert.True(history.Count >= count);
|
|
count = history.Count;
|
|
foreach (var branch in history.NamedBranches)
|
|
{
|
|
Assert.Contains(branch.Base, history.Nodes);
|
|
Assert.Contains(branch.Head, history.Nodes);
|
|
Assert.Contains(branch.Base, branch.Head.PathFromRoot());
|
|
}
|
|
}
|
|
}
|
|
}
|