95 行
4.1 KiB
C#
95 行
4.1 KiB
C#
namespace UndoTree.Tests;
|
|
|
|
public sealed class UndoTreeRecordDiffTests
|
|
{
|
|
static UndoTreeRecordCommand Set(string path, string member, string before, string after)
|
|
=> new("Property", "target " + path, member, $"{before} → {after}", path, before, after);
|
|
|
|
static UndoTreeRecordCommand Add(string path, int added, int removed = 0)
|
|
=> new("ListItems", "list " + path, "list", string.Empty, path, AddedCount: added, RemovedCount: removed);
|
|
|
|
static IReadOnlyList<UndoTreeNodeViewModel> Tree(params (int Id, int Parent, UndoTreeRecordCommand[] Commands)[] nodes)
|
|
=> UndoTreeArchive.Restore(new UndoTreeRecord(DateTime.Now, [new UndoTreeRecordNode(0, -1, string.Empty, DateTime.MinValue, []), .. nodes.Select(node => new UndoTreeRecordNode(node.Id, node.Parent, "node", DateTime.Now, node.Commands))]));
|
|
|
|
static string[] Lines(IReadOnlyList<UndoTreeCommandInfo> infos) => [.. infos.Select(info => $"{info.TargetLabel} / {info.Member}: {info.Detail}")];
|
|
|
|
[Fact]
|
|
public void TheNetChangeIsTheFirstValueBeforeAndTheLastValueAfter()
|
|
{
|
|
var tree = Tree((1, 0, [Set("#0/X", "X", "0", "1")]), (2, 1, [Set("#0/X", "X", "1", "3")]));
|
|
|
|
var infos = UndoTreeRecordDiff.Between([], [tree[1], tree[2]]);
|
|
|
|
Assert.Equal(["target #0/X / X: 0 → 3"], Lines(infos));
|
|
}
|
|
|
|
[Fact]
|
|
public void ReturningToTheStartingValueIsNotAChange()
|
|
{
|
|
var tree = Tree((1, 0, [Set("#0/X", "X", "0", "1")]), (2, 1, [Set("#0/X", "X", "1", "0")]));
|
|
|
|
Assert.Empty(UndoTreeRecordDiff.Between([], [tree[1], tree[2]]));
|
|
}
|
|
|
|
[Fact]
|
|
public void TwoBranchesAreComparedByWhatEachOneChangedFromTheirCommonNode()
|
|
{
|
|
var tree = Tree((1, 0, [Set("#0/X", "X", "0", "1")]), (2, 1, [Set("#0/Y", "Y", "0", "5")]), (3, 0, [Set("#0/X", "X", "0", "7")]));
|
|
|
|
var infos = UndoTreeRecordDiff.Between([tree[1], tree[2]], [tree[3]]);
|
|
|
|
Assert.Equal(["target #0/X / X: 1 → 7", "target #0/Y / Y: 5 → 0"], Lines(infos));
|
|
}
|
|
|
|
[Fact]
|
|
public void ChangesToDifferentTargetsWithTheSameMemberStaySeparate()
|
|
{
|
|
var tree = Tree((1, 0, [new UndoTreeRecordCommand("Property", "same name", "Value", "0 → 1", "#0/Parts[0]", "0", "1"), new UndoTreeRecordCommand("Property", "same name", "Value", "0 → 2", "#0/Parts[1]", "0", "2")]));
|
|
|
|
var infos = UndoTreeRecordDiff.Between([], [tree[1]]);
|
|
|
|
Assert.Equal(["same name / Value: 0 → 1", "same name / Value: 0 → 2"], Lines(infos));
|
|
}
|
|
|
|
[Fact]
|
|
public void ChangesToAListAreCountedAndAnAdditionTheOtherSideRemovedCancelsOut()
|
|
{
|
|
var tree = Tree((1, 0, [Add("#0/Parts", 2)]), (2, 0, [Add("#0/Parts", 1)]));
|
|
|
|
var infos = UndoTreeRecordDiff.Between([tree[1]], [tree[2]]);
|
|
|
|
Assert.Equal([$"list #0/Parts / {UndoRedoCommandView.ListChangeOf(1, 2)}: "], Lines(infos));
|
|
}
|
|
|
|
[Fact]
|
|
public void ARecordWithoutTheValuesOfItsChangesCountsThemAsOperationsThatCannotBeCompared()
|
|
{
|
|
var tree = Tree((1, 0, [new UndoTreeRecordCommand("Property", "target", "X", "0 → 1"), new UndoTreeRecordCommand("Action", string.Empty, "operation", string.Empty)]));
|
|
|
|
var infos = UndoTreeRecordDiff.Between([], [tree[1]]);
|
|
|
|
Assert.Equal([$" / {string.Format(UndoTree.Texts.DiffOpaque, 2)}: "], Lines(infos));
|
|
}
|
|
|
|
[Fact]
|
|
public void OperationsThatCannotBeComparedAreCountedOnBothSides()
|
|
{
|
|
var opaque = new UndoTreeRecordCommand("Action", string.Empty, "operation", string.Empty);
|
|
var tree = Tree((1, 0, [opaque]), (2, 0, [opaque, opaque]));
|
|
|
|
var infos = UndoTreeRecordDiff.Between([tree[1]], [tree[2]]);
|
|
|
|
Assert.Equal([$" / {string.Format(UndoTree.Texts.DiffOpaque, 3)}: "], Lines(infos));
|
|
}
|
|
|
|
[Fact]
|
|
public void ACommandWithoutAPathIsKeyedByItsTargetName()
|
|
{
|
|
var tree = Tree((1, 0, [new UndoTreeRecordCommand("Property", "name A", "X", "0 → 1", Before: "0", After: "1"), new UndoTreeRecordCommand("Property", "name B", "X", "0 → 2", Before: "0", After: "2")]));
|
|
|
|
var infos = UndoTreeRecordDiff.Between([], [tree[1]]);
|
|
|
|
Assert.Equal(["name A / X: 0 → 1", "name B / X: 0 → 2"], Lines(infos));
|
|
}
|
|
}
|