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
71 changes: 71 additions & 0 deletions Keybinding.Test/GetAllChordsSnapshotTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace ktsu.Keybinding.Test;

using ktsu.Keybinding.Core.Models;
using ktsu.Keybinding.Core.Services;

[TestClass]
public class GetAllChordsSnapshotTests
{
private KeybindingService _service = null!;
private ProfileManager _profiles = null!;

[TestInitialize]
public void Setup()
{
CommandRegistry registry = new();
_profiles = new ProfileManager();
_profiles.CreateProfile("p", "Profile");
_profiles.SetActiveProfile("p");
_service = new KeybindingService(registry, _profiles);

registry.RegisterCommand(new Command("a", "A"));
registry.RegisterCommand(new Command("b", "B"));
}

[TestMethod]
public void GetAllChords_LaterBindAndUnbind_DoNotChangeTheReturnedDictionary()
{
_service.BindChord("a", Chord.Parse("Ctrl+A"));

IReadOnlyDictionary<string, Chord> active = _service.GetAllChords();
IReadOnlyDictionary<string, Chord> byId = _service.GetAllChords("p");

_service.BindChord("b", Chord.Parse("Ctrl+B"));
_service.UnbindChord("a");

Assert.HasCount(1, active, "The active-profile snapshot should not see later bindings.");
Assert.IsTrue(active.ContainsKey("a"), "The active-profile snapshot should not lose a later-unbound command.");
Assert.HasCount(1, byId);
Assert.IsTrue(byId.ContainsKey("a"));
}

[TestMethod]
public void GetAllChords_BindingWhileEnumerating_DoesNotThrow()
{
_service.BindChord("a", Chord.Parse("Ctrl+A"));

foreach (KeyValuePair<string, Chord> binding in _service.GetAllChords())
{
if (!_service.HasChordBinding("b"))
{
_service.BindChord("b", binding.Value);
}
}

Assert.IsTrue(_service.HasChordBinding("b"));
}

[TestMethod]
public void BoundCommands_LaterBind_DoesNotChangeTheReturnedCollection()
{
_service.BindChord("a", Chord.Parse("Ctrl+A"));
Profile profile = _profiles.GetProfile("p")!;

IReadOnlyCollection<string> bound = profile.BoundCommands;
_service.BindChord("b", Chord.Parse("Ctrl+B"));

Assert.HasCount(1, bound);
}
}
4 changes: 2 additions & 2 deletions Keybinding/Models/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
{
if (string.IsNullOrWhiteSpace(commandId))
{
throw new ArgumentException("Command ID cannot be null or whitespace", nameof(commandId));

Check warning on line 86 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Define a constant instead of using this literal 'Command ID cannot be null or whitespace' 4 times.

Check warning on line 86 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Define a constant instead of using this literal 'Command ID cannot be null or whitespace' 4 times.

Check warning on line 86 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Define a constant instead of using this literal 'Command ID cannot be null or whitespace' 4 times.

Check warning on line 86 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Define a constant instead of using this literal 'Command ID cannot be null or whitespace' 4 times.

Check warning on line 86 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Define a constant instead of using this literal 'Command ID cannot be null or whitespace' 4 times.

Check warning on line 86 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Define a constant instead of using this literal 'Command ID cannot be null or whitespace' 4 times.
}

Ensure.NotNull(chord);
Expand All @@ -101,7 +101,7 @@
{
return string.IsNullOrWhiteSpace(commandId)
? throw new ArgumentException("Command ID cannot be null or whitespace", nameof(commandId))
: Chords.TryGetValue(commandId.Trim(), out Chord? chord)

Check warning on line 104 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 104 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 104 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 104 in Keybinding/Models/Profile.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.
? chord
: null;
}
Expand All @@ -110,7 +110,7 @@
/// Gets all chord bindings for this profile
/// </summary>
/// <returns>Dictionary of command ID to chord mappings</returns>
public IReadOnlyDictionary<string, Chord> GetAllChords() => Chords.AsReadOnly();
public IReadOnlyDictionary<string, Chord> GetAllChords() => new Dictionary<string, Chord>(Chords).AsReadOnly();

/// <summary>
/// Checks if a command has a chord binding in this profile
Expand Down Expand Up @@ -142,7 +142,7 @@
/// Gets all command IDs that have chord bindings in this profile
/// </summary>
/// <returns>Collection of command IDs</returns>
public IReadOnlyCollection<string> BoundCommands => Chords.Keys;
public IReadOnlyCollection<string> BoundCommands => [.. Chords.Keys];

/// <summary>
/// Clears all chord bindings from this profile
Expand Down
Loading