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
17 changes: 17 additions & 0 deletions UndoRedo.Test/UndoRedoStackTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@
Assert.IsTrue(saveBoundaryCreatedFired, "SaveBoundaryCreated event should fire when marking as saved");
}

[TestMethod]
public void GetCommandsInRange_CountOfIntMaxValueFromNonZeroStart_ReturnsRemainingCommands()
{
StackManager stack = new();
DelegateCommand first = new("A", () => { }, () => { });
DelegateCommand second = new("B", () => { }, () => { });
DelegateCommand third = new("C", () => { }, () => { });
stack.AddCommand(first);
stack.AddCommand(second);
stack.AddCommand(third);

// startIndex + count overflowed to a negative length before #105
CollectionAssert.AreEqual(new[] { second, third }, stack.GetCommandsInRange(1, int.MaxValue).ToList());

Check warning on line 255 in UndoRedo.Test/UndoRedoStackTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_UndoRedo&issues=AaDjg2qVavg0Jf5TfxbX&open=AaDjg2qVavg0Jf5TfxbX&pullRequest=106
CollectionAssert.AreEqual(new[] { third }, stack.GetCommandsInRange(2, int.MaxValue).ToList());

Check warning on line 256 in UndoRedo.Test/UndoRedoStackTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_UndoRedo&issues=AaDjg2qVavg0Jf5TfxbY&open=AaDjg2qVavg0Jf5TfxbY&pullRequest=106
CollectionAssert.AreEqual(new[] { second }, stack.GetCommandsInRange(1, 1).ToList());

Check warning on line 257 in UndoRedo.Test/UndoRedoStackTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_UndoRedo&issues=AaDjg2qVavg0Jf5TfxbZ&open=AaDjg2qVavg0Jf5TfxbZ&pullRequest=106
}

private sealed class MockNavigationProvider : INavigationProvider
{
public string? LastNavigatedContext { get; private set; }
Expand Down
5 changes: 3 additions & 2 deletions UndoRedo/Services/StackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ public IEnumerable<ICommand> GetCommandsInRange(int startIndex, int count)
return [];
}

int endIndex = Math.Min(startIndex + count, _commands.Count);
return _commands.GetRange(startIndex, endIndex - startIndex);
// Clamp against what remains rather than summing startIndex + count, which overflows for large counts
int available = _commands.Count - startIndex;
return _commands.GetRange(startIndex, Math.Min(count, available));
}
}
Loading