using YukkuriMovieMaker.Project; using YukkuriMovieMaker.UndoRedo; namespace UndoTree.Tests; public sealed class UndoTreeDiffTests { static UndoTreeHistory Track(UndoRedoManager manager, IUndoRedoable root) => new(manager, new AsyncAwaitStatus(), [root], 100, UndoRedoManagerReader.Create(manager)); static string[] Lines(IEnumerable infos) => infos.Select(info => info.Detail.Length > 0 ? $"{info.Text}: {info.Detail}" : info.Text).Order().ToArray(); [Fact] public void TheCurrentNodeHasNoDifferenceFromItself() { var (manager, document) = Histories.NewDocument("p1"); using var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); Assert.Empty(history.DiffFromCurrent(history.Cursor)); } [Fact] public async Task AStateAheadOnTheSameLineShowsWhatMovingThereChanges() { 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; document.Y = 5; }); var b = history.Cursor; await history.JumpAsync(a); Assert.Equal(["Document / X: 1 → 2", "Document / Y: 0 → 5"], Lines(history.DiffFromCurrent(b))); } [Fact] public void AStateBehindShowsTheValuesThatWouldComeBack() { 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); Assert.Equal(["Document / X: 2 → 1"], Lines(history.DiffFromCurrent(a))); Assert.Equal(["Document / X: 2 → 0"], Lines(history.DiffFromCurrent(history.Root))); } [Fact] public async Task AnotherBranchIsComparedThroughTheCommonAncestor() { 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; document.Y = 5; }); var b = history.Cursor; await history.JumpAsync(a); Histories.Edit(manager, () => { document.X = 3; document.Y = 5; }); Assert.Equal(["Document / X: 3 → 2"], Lines(history.DiffFromCurrent(b))); } [Fact] public async Task AListChangeIsShownByTheItemsThatWouldAppearOrDisappear() { 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.Add(new Part("p2"))); var b = history.Cursor; await history.JumpAsync(a); Assert.Equal([UndoRedoCommandView.ListChangeOf(1, 0)], Lines(history.DiffFromCurrent(b))); await history.JumpAsync(b); Assert.Equal([UndoRedoCommandView.ListChangeOf(0, 1)], Lines(history.DiffFromCurrent(a))); } [Fact] public async Task OperationsThatCannotBeComparedAreCounted() { var (manager, document) = Histories.NewDocument("p1"); using var history = Track(manager, document); Histories.Edit(manager, () => document.X = 1); var a = history.Cursor; manager.AddCommand(new UndoRedoActionCommand(() => { }, () => { })); manager.Record(); var b = history.Cursor; await history.JumpAsync(a); Assert.Equal([string.Format(Texts.DiffOpaque, 1)], Lines(history.DiffFromCurrent(b))); } [Fact] public void AMirroredHistoryCanStillBeCompared() { var (manager, document) = Histories.NewDocument("p1"); using var history = new UndoTreeHistory(manager, new AsyncAwaitStatus(), [document], 100, null); Histories.Edit(manager, () => document.X = 1); Assert.Equal(["Document / X: 1 → 0"], Lines(history.DiffFromCurrent(history.Root))); } }