289 行
11 KiB
C#
289 行
11 KiB
C#
using YukkuriMovieMaker.Project;
|
|
using YukkuriMovieMaker.UndoRedo;
|
|
|
|
namespace UndoTree.Tests;
|
|
|
|
public sealed class UndoTreeApplyTests
|
|
{
|
|
static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable root, bool withReader = true)
|
|
=> new(manager, new AsyncAwaitStatus(), [root], 100, withReader ? UndoRedoManagerReader.Create(manager) : null);
|
|
|
|
[Fact]
|
|
public async Task IndependentOperationsAreReplayedOnTopOfTheNewEdit()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
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;
|
|
await history.JumpAsync(a);
|
|
Histories.Edit(manager, () => document.Parts.Add(new Part("p2")));
|
|
|
|
var result = await history.ApplyAsync([b, c], ConflictPolicy.Skip);
|
|
|
|
Assert.Equal("X=1 Y=5 Parts=[p1:10,p2:0]", document.Snapshot());
|
|
Assert.Equal(2, result.Applied);
|
|
Assert.Empty(result.Conflicts);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReplayedOperationsBecomeNewNodesBelowTheCursorAndTheSourceBranchStays()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
var a = history.Cursor;
|
|
Histories.Edit(manager, () => document.Y = 5);
|
|
var b = history.Cursor;
|
|
await history.JumpAsync(a);
|
|
Histories.Edit(manager, () => document.Parts.Add(new Part("p2")));
|
|
var inserted = history.Cursor;
|
|
|
|
await history.ApplyAsync([b], ConflictPolicy.Skip);
|
|
|
|
Assert.Contains(b, a.Children);
|
|
Assert.Single(inserted.Children);
|
|
Assert.Same(inserted.Children[0], history.Cursor);
|
|
Assert.Equal(b.Label, history.Cursor.Label);
|
|
Assert.NotSame(b, history.Cursor);
|
|
Assert.Equal(5, history.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReplayedOperationsUndoStepByStepBackToTheNewEdit()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
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;
|
|
await history.JumpAsync(a);
|
|
Histories.Edit(manager, () => document.Parts.Add(new Part("p2")));
|
|
await history.ApplyAsync([b, c], ConflictPolicy.Skip);
|
|
|
|
await history.UndoAsync();
|
|
var withoutC = document.Snapshot();
|
|
await history.UndoAsync();
|
|
var withoutB = document.Snapshot();
|
|
await history.UndoAsync();
|
|
var withoutInserted = document.Snapshot();
|
|
|
|
Assert.Equal("X=1 Y=0 Parts=[p1:10,p2:0]", withoutC);
|
|
Assert.Equal("X=1 Y=0 Parts=[p1:0,p2:0]", withoutB);
|
|
Assert.Equal("X=1 Y=0 Parts=[p1:0]", withoutInserted);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ASamePropertyChangeIsSkippedByDefaultAndKeepsTheNewEdit()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
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 result = await history.ApplyAsync([b], ConflictPolicy.Skip);
|
|
|
|
Assert.Equal(3, document.X);
|
|
Assert.Equal(1, result.Skipped);
|
|
Assert.Equal(ConflictKind.SameProperty, result.Conflicts[0].Kind);
|
|
Assert.False(result.Conflicts[0].Resolved);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ASamePropertyChangeOverwritesWhenThePolicyIsApplyAndUndoesToTheNewEdit()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
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 result = await history.ApplyAsync([b], ConflictPolicy.Apply);
|
|
var applied = document.X;
|
|
await history.UndoAsync();
|
|
|
|
Assert.Equal(2, applied);
|
|
Assert.Equal(0, result.Skipped);
|
|
Assert.True(result.Conflicts[0].Resolved);
|
|
Assert.Equal(3, document.X);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnOperationOnADeletedTargetIsSkippedAndLeavesTheTargetUntouched()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1", "p2");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
var a = history.Cursor;
|
|
var part = document.Parts[0];
|
|
Histories.Edit(manager, () => part.Value = 10);
|
|
var b = history.Cursor;
|
|
await history.JumpAsync(a);
|
|
Histories.Edit(manager, () => document.Parts.Remove(part));
|
|
|
|
var result = await history.ApplyAsync([b], ConflictPolicy.Apply);
|
|
while (manager.IsUndoable)
|
|
await manager.UndoAsync();
|
|
|
|
Assert.Equal(0, part.Value);
|
|
Assert.Equal(ConflictKind.DeadTarget, result.Conflicts[0].Kind);
|
|
Assert.Equal(1, result.Skipped);
|
|
Assert.Equal("X=0 Y=0 Parts=[p1:0,p2:0]", document.Snapshot());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnOperationWhoseEveryCommandIsSkippedAddsNoNode()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1", "p2");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
var a = history.Cursor;
|
|
var part = document.Parts[0];
|
|
Histories.Edit(manager, () => part.Value = 10);
|
|
var b = history.Cursor;
|
|
await history.JumpAsync(a);
|
|
Histories.Edit(manager, () => document.Parts.Remove(part));
|
|
var count = history.Count;
|
|
|
|
await history.ApplyAsync([b], ConflictPolicy.Skip);
|
|
|
|
Assert.Equal(count, history.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AListReplacementKeepsTheNewEditsAdditionByApplyingOnlyTheDifference()
|
|
{
|
|
var (manager, sheet) = Histories.NewSheet("p1");
|
|
using var history = Track(manager, sheet);
|
|
Histories.Edit(manager, () => sheet.X = 1);
|
|
var a = history.Cursor;
|
|
Histories.Edit(manager, () => sheet.Parts = sheet.Parts.Add(new Part("pb")));
|
|
var b = history.Cursor;
|
|
await history.JumpAsync(a);
|
|
Histories.Edit(manager, () => sheet.Parts = sheet.Parts.Add(new Part("pi")));
|
|
|
|
var result = await history.ApplyAsync([b], ConflictPolicy.Skip);
|
|
var merged = sheet.Snapshot();
|
|
await history.UndoAsync();
|
|
|
|
Assert.Equal("X=1 Parts=[p1,pi,pb]", merged);
|
|
Assert.Equal(ConflictKind.CollectionMerged, result.Conflicts[0].Kind);
|
|
Assert.Equal(0, result.Skipped);
|
|
Assert.Equal("X=1 Parts=[p1,pi]", sheet.Snapshot());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AListReplacementThatNothingElseTouchedIsReplayedAsIs()
|
|
{
|
|
var (manager, sheet) = Histories.NewSheet("p1");
|
|
using var history = Track(manager, sheet);
|
|
Histories.Edit(manager, () => sheet.X = 1);
|
|
var a = history.Cursor;
|
|
Histories.Edit(manager, () => sheet.Parts = sheet.Parts.Add(new Part("pb")));
|
|
var b = history.Cursor;
|
|
await history.JumpAsync(a);
|
|
Histories.Edit(manager, () => sheet.X = 4);
|
|
|
|
var result = await history.ApplyAsync([b], ConflictPolicy.Skip);
|
|
|
|
Assert.Equal("X=4 Parts=[p1,pb]", sheet.Snapshot());
|
|
Assert.Empty(result.Conflicts);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnInjectedActionIsRegisteredAgainSoThatTheReplayCanBeUndone()
|
|
{
|
|
var (manager, document) = Histories.NewDocument();
|
|
using var history = Track(manager, document);
|
|
var counter = 0;
|
|
counter++;
|
|
manager.AddCommand(new UndoRedoActionCommand(() => counter--, () => counter++));
|
|
manager.Record();
|
|
var injected = history.Cursor;
|
|
Histories.Edit(manager, () => document.X = 2);
|
|
var second = history.Cursor;
|
|
await history.JumpAsync(history.Root);
|
|
Histories.Edit(manager, () => document.Y = 9);
|
|
|
|
await history.ApplyAsync([injected, second], ConflictPolicy.Skip);
|
|
var applied = (counter, document.X);
|
|
while (manager.IsUndoable)
|
|
await manager.UndoAsync();
|
|
|
|
Assert.Equal((1, 2), applied);
|
|
Assert.Equal(0, counter);
|
|
Assert.Equal(0, document.X);
|
|
Assert.Equal(0, document.Y);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ApplyingNothingReturnsAnEmptyResult()
|
|
{
|
|
var (manager, document) = Histories.NewDocument();
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
|
|
var result = await history.ApplyAsync([], ConflictPolicy.Skip);
|
|
|
|
Assert.Equal(0, result.Applied);
|
|
Assert.Empty(result.Conflicts);
|
|
Assert.Equal(2, history.Count);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ApplyingNeedsAHistoryThatCanReplay()
|
|
{
|
|
var (manager, document) = Histories.NewDocument();
|
|
using var history = Track(manager, document, withReader: false);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
var a = history.Cursor;
|
|
|
|
var failure = await Assert.ThrowsAsync<UndoTreeOperationException>(() => history.ApplyAsync([a], ConflictPolicy.Skip));
|
|
|
|
Assert.Equal(Texts.BranchUnavailable, failure.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ApplyingWithoutAnyReadableHistoryIsReportedAsUnsupported()
|
|
{
|
|
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.ApplyAsync([history.Root], ConflictPolicy.Skip));
|
|
|
|
Assert.Equal(Texts.Unsupported, failure.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AReplayThatFailsHalfWayStillRecordsWhatItDid()
|
|
{
|
|
var (manager, document) = Histories.NewDocument();
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
var a = history.Cursor;
|
|
Histories.Edit(manager, () => { document.Y = 4; manager.AddCommand(new UndoRedoActionCommand(() => { }, () => throw new InvalidOperationException("replay"))); });
|
|
var b = history.Cursor;
|
|
await history.JumpAsync(a);
|
|
Histories.Edit(manager, () => document.Parts.Add(new Part("p")));
|
|
|
|
await Assert.ThrowsAsync<InvalidOperationException>(() => history.ApplyAsync([b], ConflictPolicy.Skip));
|
|
|
|
Assert.Equal(4, document.Y);
|
|
Assert.Equal(3, history.Cursor.Depth);
|
|
}
|
|
}
|