Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions UndoRedo.Test/UndoRedoStackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,46 @@ public async Task UndoToSaveBoundary_BoundaryRemovedByBranching_ReturnsFalse()
Assert.IsEmpty(stack.GetCommandsToUndo(removedBoundary));
}

[TestMethod]
public void GetCommandsToUndo_EnumeratedAfterBranching_ReflectsStateAtCall()
{
// Arrange
UndoRedoService stack = CreateService();
stack.MarkAsSaved();
stack.Execute(new DelegateCommand("A", () => { }, () => { }));
stack.Execute(new DelegateCommand("B", () => { }, () => { }));
SaveBoundary boundary = stack.SaveBoundaries[0];

// Act
IEnumerable<ICommand> commandsToUndo = stack.GetCommandsToUndo(boundary);
stack.Undo();
stack.Execute(new DelegateCommand("C", () => { }, () => { })); // Replaces B in the live stack

// Assert
string descriptions = string.Join(",", commandsToUndo.Select(c => c.Description));
Assert.AreEqual("A,B", descriptions, "The result should be a snapshot of the stack when it was requested");
}

[TestMethod]
public void GetChangeVisualizations_EnumeratedAfterExecute_ReflectsStateAtCall()
{
// Arrange
UndoRedoService stack = CreateService();
stack.Execute(new DelegateCommand("A", () => { }, () => { }));

// Act
IEnumerable<ChangeVisualization> visualizations = stack.GetChangeVisualizations();
stack.MarkAsSaved();
stack.Execute(new DelegateCommand("C", () => { }, () => { }));

// Assert
List<ChangeVisualization> snapshot = [.. visualizations];
Assert.HasCount(1, snapshot, "Commands executed after the call should not appear");
Assert.AreEqual("A", snapshot[0].Command.Description);
Assert.IsTrue(snapshot[0].IsExecuted);
Assert.IsFalse(snapshot[0].HasSaveBoundary, "A save boundary created after the call should not appear");
}

[TestMethod]
public async Task UndoToSaveBoundary_WhenAlreadyAtPosition_ReturnsFalse()
{
Expand Down
3 changes: 2 additions & 1 deletion UndoRedo/Services/SaveBoundaryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ public IEnumerable<ICommand> GetCommandsToUndo(SaveBoundary saveBoundary, int cu
Ensure.NotNull(saveBoundary);
Ensure.NotNull(commands);

// Materialize so the result is a snapshot, not a view that changes with the live stack
return currentPosition <= saveBoundary.Position
? []
: commands.Skip(saveBoundary.Position + 1).Take(currentPosition - saveBoundary.Position);
: [.. commands.Skip(saveBoundary.Position + 1).Take(currentPosition - saveBoundary.Position)];
}

/// <inheritdoc />
Expand Down
Loading