ファイル

218 行
9.0 KiB
C#

using YukkuriMovieMaker.Plugin;
using YukkuriMovieMaker.Project;
using YukkuriMovieMaker.UndoRedo;
namespace UndoTree.Tests;
public sealed class UndoTreeMergeTests
{
static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable root)
=> new(manager, new AsyncAwaitStatus(), [root], 100, UndoRedoManagerReader.Create(manager));
[Fact]
public async Task TheOperationsFromTheCommonAncestorToTheOtherStateAreAddedOnTopOfTheCurrentOnes()
{
var (manager, document) = Histories.NewDocument("p");
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
var ancestor = history.Cursor;
Histories.Edit(manager, () => document.Y = 5);
Histories.Edit(manager, () => document.Parts[0].Value = 7);
var theirs = history.Cursor;
await history.JumpAsync(ancestor);
Histories.Edit(manager, () => document.Parts.Add(new Part("q")));
var path = history.PathToMerge(theirs);
var result = await history.ApplyAsync(path, ConflictPolicy.Skip, byValue: true);
Assert.Equal(2, path.Count);
Assert.Equal("X=1 Y=5 Parts=[p:7,q:0]", document.Snapshot());
Assert.Equal(2, result.Applied);
Assert.Empty(result.Conflicts);
Assert.Equal(4, history.Cursor.Depth);
}
[Fact]
public async Task AChangeToTheSameValueOnBothSidesIsNotAConflict()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
var theirs = history.Cursor;
await history.JumpAsync(history.Root);
Histories.Edit(manager, () => document.X = 1);
var plan = history.PlanApply(history.PathToMerge(theirs), ConflictPolicy.Skip, byValue: true);
var result = await history.ApplyAsync(history.PathToMerge(theirs), ConflictPolicy.Skip, byValue: true);
Assert.False(plan.NeedsReview);
Assert.Equal(UndoTreePlanKind.Same, Assert.Single(plan.Items).Kind);
Assert.Empty(result.Conflicts);
Assert.Equal(0, result.Applied);
Assert.Equal(1, document.X);
}
[Fact]
public async Task AChangeOnOurSideThatEndedUpWhereItStartedIsNotAConflict()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
var theirs = history.Cursor;
await history.JumpAsync(history.Root);
Histories.Edit(manager, () => document.X = 5);
Histories.Edit(manager, () => document.X = 0);
Histories.Edit(manager, () => document.Y = 2);
var skipping = await history.ApplyAsync(history.PathToMerge(theirs), ConflictPolicy.Skip, byValue: true);
Assert.Equal(1, document.X);
Assert.Empty(skipping.Conflicts);
}
[Fact]
public async Task ADifferentChangeOnEachSideIsAConflictThatThePolicyResolves()
{
var results = new Dictionary<ConflictPolicy, (int X, int Skipped, int Predicted)>();
foreach (var policy in new[] { ConflictPolicy.Skip, ConflictPolicy.Apply, ConflictPolicy.Offset })
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 10);
var theirs = history.Cursor;
await history.JumpAsync(history.Root);
Histories.Edit(manager, () => document.X = 50);
var plan = history.PlanApply(history.PathToMerge(theirs), policy, byValue: true);
var result = await history.ApplyAsync(history.PathToMerge(theirs), policy, byValue: true, decisions: plan.Decisions);
results[policy] = (document.X, result.Skipped, plan.ConflictCount);
}
Assert.Equal((50, 1, 1), results[ConflictPolicy.Skip]);
Assert.Equal((10, 0, 1), results[ConflictPolicy.Apply]);
Assert.Equal((60, 0, 1), results[ConflictPolicy.Offset]);
}
[Fact]
public async Task ASequenceOfChangesToOneValueIsJudgedAgainstTheValueTheEarlierOnesLeave()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 10);
Histories.Edit(manager, () => document.X = 20);
Histories.Edit(manager, () => document.X = 30);
var theirs = history.Cursor;
await history.JumpAsync(history.Root);
Histories.Edit(manager, () => document.Y = 1);
var plan = history.PlanApply(history.PathToMerge(theirs), ConflictPolicy.Skip, byValue: true);
var result = await history.ApplyAsync(history.PathToMerge(theirs), ConflictPolicy.Skip, byValue: true);
Assert.False(plan.NeedsReview);
var planned = Assert.Single(plan.Items);
Assert.Equal(UndoTreePlanKind.Change, planned.Kind);
Assert.Equal(3, planned.Count);
Assert.Equal("30", planned.ResultText);
Assert.Empty(result.Conflicts);
Assert.Equal(30, document.X);
}
[Fact]
public async Task WhenTheFirstChangeOfASequenceConflictsThePredictionKeepsFollowingTheValueThatStays()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 10);
Histories.Edit(manager, () => document.X = 20);
var theirs = history.Cursor;
await history.JumpAsync(history.Root);
Histories.Edit(manager, () => document.X = 50);
var plan = history.PlanApply(history.PathToMerge(theirs), ConflictPolicy.Skip, byValue: true);
var result = await history.ApplyAsync(history.PathToMerge(theirs), ConflictPolicy.Skip, byValue: true);
var planned = Assert.Single(plan.Items);
Assert.Equal(UndoTreePlanKind.Conflict, planned.Kind);
Assert.Equal(2, planned.Count);
Assert.Equal("50", planned.ResultText);
Assert.Equal(2, result.Skipped);
Assert.Equal(50, document.X);
}
[Fact]
public async Task TheFirstOfTwoChangesThatConflictsIsOverwrittenAndTheSecondFollowsFromIt()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 10);
Histories.Edit(manager, () => document.X = 20);
var theirs = history.Cursor;
await history.JumpAsync(history.Root);
Histories.Edit(manager, () => document.X = 50);
var plan = history.PlanApply(history.PathToMerge(theirs), ConflictPolicy.Apply, byValue: true);
await history.ApplyAsync(history.PathToMerge(theirs), ConflictPolicy.Apply, byValue: true, decisions: plan.Decisions);
var planned = Assert.Single(plan.Items);
Assert.Equal(ConflictPolicy.Apply, planned.Choice);
Assert.Equal("20", planned.ResultText);
Assert.Equal(20, document.X);
}
[Fact]
public async Task TheSameStateMergedTwiceChangesNothingTheSecondTime()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 10);
var theirs = history.Cursor;
await history.JumpAsync(history.Root);
Histories.Edit(manager, () => document.Y = 4);
await history.ApplyAsync(history.PathToMerge(theirs), ConflictPolicy.Skip, byValue: true);
var before = history.Count;
var second = await history.ApplyAsync(history.PathToMerge(theirs), ConflictPolicy.Skip, byValue: true);
Assert.Equal(0, second.Applied);
Assert.Empty(second.Conflicts);
Assert.Equal(before, history.Count);
Assert.Equal((10, 4), (document.X, document.Y));
}
[Fact]
public async Task AStateAheadOnTheSameLineIsReachedByAddingItsOperations()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
var start = history.Cursor;
Histories.Edit(manager, () => document.X = 2);
var ahead = history.Cursor;
await history.JumpAsync(start);
var path = history.PathToMerge(ahead);
await history.ApplyAsync(path, ConflictPolicy.Skip, byValue: true);
Assert.Single(path);
Assert.Equal(2, document.X);
}
[Fact]
public async Task TheLegacyJudgementByTheSameItemStaysTheDefaultForCherryPicks()
{
var (manager, document) = Histories.NewDocument();
using var history = Track(manager, document);
Histories.Edit(manager, () => document.X = 1);
var theirs = history.Cursor;
await history.JumpAsync(history.Root);
Histories.Edit(manager, () => document.X = 5);
Histories.Edit(manager, () => document.X = 0);
var result = await history.ApplyAsync([theirs], ConflictPolicy.Skip);
Assert.Equal(ConflictKind.SameProperty, Assert.Single(result.Conflicts).Kind);
Assert.Equal(0, document.X);
}
}