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

namespace ktsu.Keybinding.Test;

using ktsu.Keybinding.Core.Models;

[TestClass]
public class BlankNoteNameTests
{
[TestMethod]
[DataRow("")]
[DataRow(" ")]
[DataRow("\t")]
public void NoteFromBlankNoteName_ThrowsArgumentException(string key)
{
// The string constructor already refused a blank key; the NoteName one built an empty note that
// displayed as "Ctrl+", executed, and was then dropped when the profile was loaded again (#212).
NoteName name = NoteName.Create(key);
ArgumentException exception = Assert.ThrowsExactly<ArgumentException>(() => new Note(name));
Assert.AreEqual("key", exception.ParamName);
Assert.StartsWith("Key cannot be null or whitespace", exception.Message);
}

[TestMethod]
[DataRow("")]
[DataRow(" ")]
[DataRow("\t")]
public void NoteFromBlankString_ThrowsTheSameArgumentException(string key)
{
ArgumentException exception = Assert.ThrowsExactly<ArgumentException>(() => new Note(key));
Assert.AreEqual("key", exception.ParamName);
Assert.StartsWith("Key cannot be null or whitespace", exception.Message);
}
}
5 changes: 5 additions & 0 deletions Keybinding/Models/MusicalTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
public Note(NoteName key)
{
Ensure.NotNull(key);
if (string.IsNullOrWhiteSpace(key.ToString()))
{
throw new ArgumentException("Key cannot be null or whitespace", nameof(key));
}

string canonical = NormalizeKey(key.ToString());
Key = canonical == key.ToString() ? key : NoteName.Create(canonical);
}
Expand Down Expand Up @@ -64,7 +69,7 @@
/// </summary>
/// <param name="key">An uppercase key name</param>
/// <returns>The canonical key name</returns>
private static string CanonicalizeKey(string key)

Check warning on line 72 in Keybinding/Models/MusicalTypes.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 72 in Keybinding/Models/MusicalTypes.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 72 in Keybinding/Models/MusicalTypes.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 72 in Keybinding/Models/MusicalTypes.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 72 in Keybinding/Models/MusicalTypes.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

Check warning on line 72 in Keybinding/Models/MusicalTypes.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.
{
if (key.Length == 2 && key[0] == 'D' && key[1] is >= '0' and <= '9')
{
Expand Down
Loading