276 行
11 KiB
C#
276 行
11 KiB
C#
using YukkuriMovieMaker.Plugin;
|
|
using YukkuriMovieMaker.Project;
|
|
using YukkuriMovieMaker.UndoRedo;
|
|
|
|
namespace UndoTree.Tests;
|
|
|
|
public sealed class UndoTreePlanTests
|
|
{
|
|
static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable document)
|
|
=> new(manager, new AsyncAwaitStatus(), [document], 100, UndoRedoManagerReader.Create(manager));
|
|
|
|
static async Task<(UndoTreeHistory History, Document Document, UndoTreeNodeViewModel[] Theirs)> Diverged(int x = 2, int y = 0)
|
|
{
|
|
var (manager, document) = Histories.NewDocument();
|
|
var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 1);
|
|
var first = history.Cursor;
|
|
Histories.Edit(manager, () => document.Y = 5);
|
|
var second = history.Cursor;
|
|
await history.JumpAsync(history.Root);
|
|
Histories.Edit(manager, () => document.X = x);
|
|
if (y != 0)
|
|
Histories.Edit(manager, () => document.Y = y);
|
|
|
|
return (history, document, [first, second]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task EachItemShowsItsCurrentValueAndWhatItBecomes()
|
|
{
|
|
var (history, _, theirs) = await Diverged();
|
|
using var _ = history;
|
|
|
|
var plan = history.PlanApply(theirs, ConflictPolicy.Skip);
|
|
|
|
Assert.Equal(["Document / X", "Document / Y"], plan.Items.Select(item => item.Text));
|
|
Assert.Equal([UndoTreePlanKind.Conflict, UndoTreePlanKind.Change], plan.Items.Select(item => item.Kind));
|
|
Assert.Equal(["2", "0"], plan.Items.Select(item => item.CurrentText));
|
|
Assert.Equal(["2", "5"], plan.Items.Select(item => item.ResultText));
|
|
Assert.True(plan.NeedsReview);
|
|
Assert.Equal(1, plan.ConflictCount);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AConflictOffersWhatEachWayOfResolvingItLeaves()
|
|
{
|
|
var (history, _, theirs) = await Diverged();
|
|
using var _ = history;
|
|
var conflict = history.PlanApply(theirs, ConflictPolicy.Skip).Items[0];
|
|
|
|
Assert.Equal([ConflictPolicy.Skip, ConflictPolicy.Apply, ConflictPolicy.Offset], conflict.Choices.Select(choice => choice.Policy));
|
|
Assert.Equal([Texts.ConflictSkip, Texts.ConflictApply, Texts.ConflictOffset], conflict.Choices.Select(choice => choice.Text));
|
|
Assert.Equal("2", conflict.ResultText);
|
|
|
|
conflict.Choice = ConflictPolicy.Apply;
|
|
Assert.Equal("1", conflict.ResultText);
|
|
|
|
conflict.Choice = ConflictPolicy.Offset;
|
|
Assert.Equal("3", conflict.ResultText);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ChangingTheChoiceTellsTheResultToTheView()
|
|
{
|
|
var (history, _, theirs) = await Diverged();
|
|
using var _ = history;
|
|
var conflict = history.PlanApply(theirs, ConflictPolicy.Skip).Items[0];
|
|
var changed = new List<string?>();
|
|
conflict.PropertyChanged += (_, e) => changed.Add(e.PropertyName);
|
|
|
|
conflict.Choice = ConflictPolicy.Apply;
|
|
|
|
Assert.Contains(nameof(UndoTreePlanItem.Choice), changed);
|
|
Assert.Contains(nameof(UndoTreePlanItem.ResultText), changed);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheSettingIsTheChoiceAtFirst()
|
|
{
|
|
var (history, _, theirs) = await Diverged();
|
|
using var _ = history;
|
|
|
|
foreach (var policy in new[] { ConflictPolicy.Skip, ConflictPolicy.Apply, ConflictPolicy.Offset })
|
|
Assert.Equal(policy, history.PlanApply(theirs, policy).Items[0].Choice);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheChoicesOfTheConflictsAreAppliedItemByItem()
|
|
{
|
|
var (history, document, theirs) = await Diverged(2, 7);
|
|
using var _ = history;
|
|
var plan = history.PlanApply(theirs, ConflictPolicy.Skip);
|
|
Assert.Equal([UndoTreePlanKind.Conflict, UndoTreePlanKind.Conflict], plan.Items.Select(item => item.Kind));
|
|
plan.Items[0].Choice = ConflictPolicy.Apply;
|
|
plan.Items[1].Choice = ConflictPolicy.Offset;
|
|
|
|
var result = await history.ApplyAsync(theirs, ConflictPolicy.Skip, decisions: plan.Decisions);
|
|
|
|
Assert.Equal(1, document.X);
|
|
Assert.Equal(12, document.Y);
|
|
Assert.Equal(0, result.Skipped);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnItemLeftOnSkipStaysWhileTheOtherOneIsOverwritten()
|
|
{
|
|
var (history, document, theirs) = await Diverged(2, 7);
|
|
using var _ = history;
|
|
var plan = history.PlanApply(theirs, ConflictPolicy.Skip);
|
|
plan.Items[1].Choice = ConflictPolicy.Apply;
|
|
|
|
var result = await history.ApplyAsync(theirs, ConflictPolicy.Skip, decisions: plan.Decisions);
|
|
|
|
Assert.Equal(2, document.X);
|
|
Assert.Equal(5, document.Y);
|
|
Assert.Equal(1, result.Skipped);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ChoosingForAllSetsEveryConflictAndKeepsSkipWhereTheAmountCannotBeAdded()
|
|
{
|
|
var (history, document, theirs) = await Diverged(2, 7);
|
|
using var _ = history;
|
|
var plan = history.PlanApply(theirs, ConflictPolicy.Skip);
|
|
|
|
plan.ChooseAll(ConflictPolicy.Offset);
|
|
Assert.All(plan.Items, item => Assert.Equal(ConflictPolicy.Offset, item.Choice));
|
|
|
|
plan.ChooseAll(ConflictPolicy.Apply);
|
|
Assert.All(plan.Items, item => Assert.Equal(ConflictPolicy.Apply, item.Choice));
|
|
Assert.Equal(2, plan.Decisions.Count);
|
|
Assert.Equal(2, document.X);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheChainOfChangesToOneItemIsOneRowWhoseResultFollowsTheChoice()
|
|
{
|
|
var (manager, document) = Histories.NewDocument();
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.X = 10);
|
|
var first = history.Cursor;
|
|
Histories.Edit(manager, () => document.X = 20);
|
|
var second = history.Cursor;
|
|
await history.JumpAsync(history.Root);
|
|
Histories.Edit(manager, () => document.X = 50);
|
|
|
|
var plan = history.PlanApply([first, second], ConflictPolicy.Skip, byValue: true);
|
|
|
|
var row = Assert.Single(plan.Items);
|
|
Assert.Equal(2, row.Count);
|
|
Assert.Equal(string.Format(Texts.FoldedRun, "Document / X", 2), row.Text);
|
|
Assert.Equal("50", row.ResultText);
|
|
row.Choice = ConflictPolicy.Apply;
|
|
Assert.Equal("20", row.ResultText);
|
|
row.Choice = ConflictPolicy.Offset;
|
|
Assert.Equal("70", row.ResultText);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AnItemWhoseTargetIsGoneCannotBeChosen()
|
|
{
|
|
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 plan = history.PlanApply([b], ConflictPolicy.Apply);
|
|
|
|
var row = Assert.Single(plan.Items);
|
|
Assert.Equal(UndoTreePlanKind.Missing, row.Kind);
|
|
Assert.False(row.IsConflict);
|
|
Assert.Equal(1, plan.MissingCount);
|
|
Assert.True(plan.NeedsReview);
|
|
Assert.Empty(plan.Decisions);
|
|
Assert.Equal(Texts.ReviewMissing, row.StatusText);
|
|
|
|
var review = new UndoTreeReviewViewModel(plan);
|
|
Assert.True(review.HasMissing);
|
|
Assert.False(review.HasConflicts);
|
|
Assert.Equal(string.Format(Texts.ReviewSummary, 0, 0), review.SummaryText);
|
|
Assert.Equal(string.Format(Texts.ReviewMissingNote, 1), review.MissingText);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ChoosingToAddTheAmountOfChangeForAllLeavesTheItemsThatAreNotNumbersOnSkip()
|
|
{
|
|
var holder = new Holder();
|
|
var manager = new UndoRedoManager();
|
|
manager.Subscribe(holder);
|
|
using var history = Track(manager, holder);
|
|
var first = new Slot();
|
|
var second = new Slot();
|
|
Histories.Edit(manager, () => holder.Level = 5);
|
|
var level = history.Cursor;
|
|
Histories.Edit(manager, () => holder.Slot = first);
|
|
var slot = history.Cursor;
|
|
await history.JumpAsync(history.Root);
|
|
Histories.Edit(manager, () => holder.Level = 9);
|
|
Histories.Edit(manager, () => holder.Slot = second);
|
|
|
|
var plan = history.PlanApply([level, slot], ConflictPolicy.Skip);
|
|
plan.ChooseAll(ConflictPolicy.Offset);
|
|
|
|
Assert.Equal([UndoTreePlanKind.Conflict, UndoTreePlanKind.Conflict], plan.Items.Select(item => item.Kind));
|
|
Assert.Equal([true, false], plan.Items.Select(item => item.CanOffset));
|
|
Assert.Equal([ConflictPolicy.Offset, ConflictPolicy.Skip], plan.Items.Select(item => item.Choice));
|
|
Assert.Equal([ConflictPolicy.Skip, ConflictPolicy.Apply], plan.Items[1].Choices.Select(choice => choice.Policy));
|
|
Assert.True(new UndoTreeReviewViewModel(plan).CanOffsetAny);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AdditionsToAListAreRowsOfTheirOwn()
|
|
{
|
|
var (manager, document) = Histories.NewDocument();
|
|
using var history = Track(manager, document);
|
|
Histories.Edit(manager, () => document.Parts.Add(new Part("p2")));
|
|
var added = history.Cursor;
|
|
await history.JumpAsync(history.Root);
|
|
|
|
var plan = history.PlanApply([added], ConflictPolicy.Skip);
|
|
|
|
var row = Assert.Single(plan.Items);
|
|
Assert.Equal(UndoTreePlanKind.Change, row.Kind);
|
|
Assert.Equal(Texts.ReviewChange, row.StatusText);
|
|
Assert.False(plan.NeedsReview);
|
|
}
|
|
|
|
[Fact]
|
|
public void WithoutAnythingToApplyThereIsNothingToReview()
|
|
{
|
|
var (manager, document) = Histories.NewDocument();
|
|
using var history = Track(manager, document);
|
|
|
|
var plan = history.PlanApply([], ConflictPolicy.Skip);
|
|
|
|
Assert.Empty(plan.Items);
|
|
Assert.False(plan.NeedsReview);
|
|
Assert.Empty(plan.Decisions);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task TheReviewWindowCountsTheItemsAndTheConflicts()
|
|
{
|
|
var (history, _, theirs) = await Diverged();
|
|
using var _ = history;
|
|
var review = new UndoTreeReviewViewModel(history.PlanApply(theirs, ConflictPolicy.Skip));
|
|
|
|
Assert.Equal(string.Format(Texts.ReviewSummary, 2, 1), review.SummaryText);
|
|
Assert.True(review.HasConflicts);
|
|
Assert.False(review.HasMissing);
|
|
Assert.True(review.CanOffsetAny);
|
|
Assert.True(review.ChooseAllCommand.CanExecute("Apply"));
|
|
|
|
review.ChooseAllCommand.Execute("Apply");
|
|
|
|
Assert.Equal(ConflictPolicy.Apply, review.Items[0].Choice);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task WithoutConflictsTheChooseAllButtonsHaveNothingToDo()
|
|
{
|
|
var (history, _, theirs) = await Diverged();
|
|
using var _ = history;
|
|
var review = new UndoTreeReviewViewModel(history.PlanApply([theirs[1]], ConflictPolicy.Skip));
|
|
|
|
Assert.False(review.HasConflicts);
|
|
Assert.False(review.ChooseAllCommand.CanExecute("Skip"));
|
|
Assert.Equal(string.Format(Texts.ReviewSummary, 1, 0), review.SummaryText);
|
|
}
|
|
}
|