593 行
22 KiB
C#
593 行
22 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 ConflictsAreForeseenBeforeAnythingIsApplied()
|
|
{
|
|
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(history.Root);
|
|
Histories.Edit(manager, () => document.X = 2);
|
|
|
|
var independent = history.PlanApply([b], ConflictPolicy.Skip);
|
|
var skipped = history.PlanApply([a], ConflictPolicy.Skip);
|
|
var overwritten = history.PlanApply([a], ConflictPolicy.Apply);
|
|
|
|
Assert.False(independent.NeedsReview);
|
|
var skippedConflict = Assert.Single(skipped.Items);
|
|
Assert.Equal(UndoTreePlanKind.Conflict, skippedConflict.Kind);
|
|
Assert.Equal(ConflictPolicy.Skip, skippedConflict.Choice);
|
|
Assert.Equal(ConflictPolicy.Apply, Assert.Single(overwritten.Items).Choice);
|
|
Assert.Equal("X=2 Y=0 Parts=[p1:0]", document.Snapshot());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheForesightMatchesWhatTheApplicationReports()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
var a = history.Cursor;
|
|
await history.JumpAsync(history.Root);
|
|
Histories.Edit(manager, () => document.X = 2);
|
|
|
|
foreach (var policy in new[] { ConflictPolicy.Skip, ConflictPolicy.Apply })
|
|
{
|
|
var plan = history.PlanApply([a], policy);
|
|
var result = await history.ApplyAsync([a], policy, decisions: plan.Decisions);
|
|
|
|
Assert.Equal(
|
|
plan.Items.Where(item => item.IsConflict).Select(item => (item.Text, item.Choice == ConflictPolicy.Apply)),
|
|
result.Conflicts.Select(conflict => (UndoTreeText.Join(conflict.TargetLabel, conflict.Member), conflict.Resolved)));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ADetachedTargetIsForeseenAsSkipped()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
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 planned = Assert.Single(history.PlanApply([b], ConflictPolicy.Apply).Items);
|
|
|
|
Assert.Equal(UndoTreePlanKind.Missing, planned.Kind);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RevertingAChangeThatWasChangedAgainIsForeseenAsAConflict()
|
|
{
|
|
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 planned = Assert.Single(history.PlanApply([a], ConflictPolicy.Skip, inverse: true).Items);
|
|
|
|
Assert.Equal(UndoTreePlanKind.Conflict, planned.Kind);
|
|
Assert.Equal(ConflictKind.SameProperty, Assert.Single((await history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true)).Conflicts).Kind);
|
|
}
|
|
|
|
[Fact]
|
|
public void WithoutAReplayableSourceNothingIsForeseen()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document, withReader: false);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
|
|
Assert.Empty(history.PlanApply([history.Cursor], ConflictPolicy.Skip).Items);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheNodesOutsideThePathAndTheRedoLineAreCountedAsSideBranches()
|
|
{
|
|
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);
|
|
Histories.Edit(manager, () => document.Y = 6);
|
|
await history.JumpAsync(a);
|
|
Histories.Edit(manager, () => document.Parts[0].Value = 3);
|
|
|
|
Assert.Equal(2, history.CountSideBranches());
|
|
|
|
history.DropSideBranches();
|
|
|
|
Assert.Equal(0, history.CountSideBranches());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RevertingAnOperationInTheMiddleKeepsTheLaterOnes()
|
|
{
|
|
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);
|
|
Histories.Edit(manager, () => document.Y = 5);
|
|
|
|
var result = await history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true);
|
|
|
|
Assert.Equal("X=0 Y=5 Parts=[p1:10]", document.Snapshot());
|
|
Assert.Equal(1, result.Applied);
|
|
Assert.Empty(result.Conflicts);
|
|
Assert.Equal(4, history.Cursor.Depth);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ApplyingAnOperationAnnouncesTheChangeAndClearsTheRedoableNodes()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
Histories.Edit(manager, () => document.Y = 5);
|
|
var b = history.Cursor;
|
|
Histories.Edit(manager, () => document.Parts[0].Value = 10);
|
|
var c = history.Cursor;
|
|
await history.UndoAsync();
|
|
await history.UndoAsync();
|
|
Assert.True(b.IsRedoable);
|
|
Assert.True(c.IsRedoable);
|
|
var announced = 0;
|
|
history.Changed += (_, _) => announced++;
|
|
|
|
await history.ApplyAsync([c], ConflictPolicy.Skip);
|
|
|
|
Assert.Equal(1, announced);
|
|
Assert.False(b.IsRedoable);
|
|
Assert.False(c.IsRedoable);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RevertingAnOperationAnnouncesTheChange()
|
|
{
|
|
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 announced = 0;
|
|
history.Changed += (_, _) => announced++;
|
|
|
|
await history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true);
|
|
|
|
Assert.Equal(1, announced);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ApplyingNothingAnnouncesNothing()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
var a = history.Cursor;
|
|
await history.UndoAsync();
|
|
Histories.Edit(manager, () => document.X = 2);
|
|
var announced = 0;
|
|
history.Changed += (_, _) => announced++;
|
|
|
|
var result = await history.ApplyAsync([a], ConflictPolicy.Skip);
|
|
|
|
Assert.Equal(0, result.Applied);
|
|
Assert.Equal(0, announced);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ARevertIsRecordedAsANewEditThatCanBeUndone()
|
|
{
|
|
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);
|
|
await history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true);
|
|
|
|
await history.UndoAsync();
|
|
|
|
Assert.Equal("X=1 Y=5 Parts=[p1:0]", document.Snapshot());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RevertingAChangeThatALaterOperationChangedAgainIsAConflict()
|
|
{
|
|
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 skipped = await history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true);
|
|
|
|
Assert.Equal("X=2 Y=0 Parts=[p1:0]", document.Snapshot());
|
|
Assert.Equal(0, skipped.Applied);
|
|
Assert.Equal(ConflictKind.SameProperty, Assert.Single(skipped.Conflicts).Kind);
|
|
|
|
var overwritten = await history.ApplyAsync([a], ConflictPolicy.Apply, inverse: true);
|
|
|
|
Assert.Equal("X=0 Y=0 Parts=[p1:0]", document.Snapshot());
|
|
Assert.Equal(1, overwritten.Applied);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RevertingSeveralChangesOfOneOperationRestoresThemInReverseOrder()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => { document.X = 1; document.Y = 2; document.Parts[0].Value = 3; });
|
|
var a = history.Cursor;
|
|
|
|
var result = await history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true);
|
|
|
|
Assert.Equal("X=0 Y=0 Parts=[p1:0]", document.Snapshot());
|
|
Assert.Equal(3, result.Applied);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RevertingAListChangeRemovesTheAddedItem()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.Parts.Add(new Part("p2")));
|
|
var a = history.Cursor;
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
|
|
await history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true);
|
|
|
|
Assert.Equal("X=1 Y=0 Parts=[p1:0]", document.Snapshot());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RevertingAListChangeKeepsTheItemsAddedAfterIt()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.Parts.Add(new Part("p2")));
|
|
var a = history.Cursor;
|
|
Histories.Edit(manager, () => document.Parts.Add(new Part("p3")));
|
|
|
|
await history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true);
|
|
|
|
Assert.Equal("X=0 Y=0 Parts=[p1:0,p3:0]", document.Snapshot());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RevertingTheRemovalOfAnItemPutsItBack()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1", "p2");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.Parts.RemoveAt(0));
|
|
var a = history.Cursor;
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
|
|
await history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true);
|
|
|
|
Assert.Equal(["p1", "p2"], document.Parts.Select(part => part.Label).Order());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnOperationWithAnActionCannotBeReverted()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
manager.AddCommand(new UndoRedoActionCommand(() => { }, () => { }));
|
|
manager.Record();
|
|
var a = history.Cursor;
|
|
|
|
var failure = await Assert.ThrowsAsync<UndoTreeOperationException>(() => history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true));
|
|
|
|
Assert.Equal(Texts.RevertUnsupported, failure.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task OnlyAnOperationOnTheCurrentPathCanBeReverted()
|
|
{
|
|
var (manager, document) = Histories.NewDocument("p1");
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
var a = history.Cursor;
|
|
await history.JumpAsync(history.Root);
|
|
|
|
var failure = await Assert.ThrowsAsync<UndoTreeOperationException>(() => history.ApplyAsync([a], ConflictPolicy.Skip, inverse: true));
|
|
|
|
Assert.Equal(Texts.NothingToApply, failure.Message);
|
|
Assert.Equal("X=0 Y=0 Parts=[p1:0]", document.Snapshot());
|
|
}
|
|
|
|
[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);
|
|
}
|
|
}
|