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

namespace ktsu.Keybinding.Test;

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

[TestClass]
public class CommandIdWhitespaceTests
{
private static Command CreatePaddedCommand() =>
new(CommandId.Create(" file.save "), CommandName.Create("Save"));

[TestMethod]
public void Constructor_CommandIdWithWhitespace_TrimsId()
{
Assert.AreEqual("file.save", CreatePaddedCommand().Id.ToString());
}

[TestMethod]
public void Constructor_CommandIdWithWhitespace_EqualsStringConstructedCommand()
{
Assert.AreEqual(new Command(" file.save ", "Save"), CreatePaddedCommand());
}

[TestMethod]
public void Constructor_WhitespaceOnlyCommandId_Throws()
{
Assert.ThrowsExactly<ArgumentException>(() => new Command(CommandId.Create(" "), CommandName.Create("Save")));
}

[TestMethod]
public void Register_CommandIdWithWhitespace_CanBeFoundBoundAndUnregistered()
{
CommandRegistry registry = new();
Assert.IsTrue(registry.RegisterCommand(CreatePaddedCommand()));

Assert.IsTrue(registry.IsCommandRegistered("file.save"));
Assert.IsTrue(registry.IsCommandRegistered(" file.save "));
Assert.IsNotNull(registry.GetCommand("file.save"));

ProfileManager profiles = new();
profiles.CreateProfile("p", "Profile");
profiles.SetActiveProfile("p");
KeybindingService service = new(registry, profiles);
Assert.IsTrue(service.BindChord(" file.save ", Chord.Parse("Ctrl+S")));

Assert.IsTrue(registry.UnregisterCommand(" file.save "));
Assert.IsFalse(registry.IsCommandRegistered("file.save"));
}
}
11 changes: 10 additions & 1 deletion Keybinding/Models/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ public Command(CommandId id, CommandName name, CommandDescription? description =
Ensure.NotNull(id);
Ensure.NotNull(name);

Id = id;
// Trim as the string constructor does: the registry's lookups trim the id they are given, so an
// untrimmed id would be stored under a key no lookup can produce.
string rawId = id.ToString();
string trimmedId = rawId.Trim();
if (trimmedId.Length == 0)
{
throw new ArgumentException("Command ID cannot be null or whitespace", nameof(id));
}

Id = trimmedId == rawId ? id : CommandId.Create(trimmedId);
Name = name;
Description = description;
Category = category;
Expand Down
Loading