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
19 changes: 19 additions & 0 deletions Keybinding.Test/ModifierAliasNormalizationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public void EveryConstructionPath_NormalizesModifierAliases(string alias, string
Chord.Parse($"{alias}+S"),
new Chord([new Note(alias), new Note("S")]),
new Chord([new Note(NoteName.Create(alias.ToUpperInvariant())), new Note("S")]),
new Chord([new Note(NoteName.Create(alias)), new Note(NoteName.Create("s"))]),
new Chord([new Note(NoteName.Create(alias.ToLowerInvariant())), new Note("S")]),
];

foreach (Chord chord in built)
Expand All @@ -64,6 +66,23 @@ public void EveryConstructionPath_NormalizesModifierAliases(string alias, string
}
}

[TestMethod]
[DataRow("control")]
[DataRow("Control")]
[DataRow("ctrl")]
[DataRow("s")]
[DataRow("f5")]
[DataRow(" cmd ")]
public void NoteNameConstructor_MatchesStringConstructor(string key)
{
Note fromString = new(key);
Note fromNoteName = new(NoteName.Create(key));

Assert.AreEqual(fromString, fromNoteName);
Assert.AreEqual(fromString.GetHashCode(), fromNoteName.GetHashCode());
Assert.AreEqual(fromString.Key.ToString(), fromNoteName.Key.ToString());
}

[TestMethod]
public void ParseChord_AliasAndCanonicalName_CollapseToOneNote()
{
Expand Down
12 changes: 10 additions & 2 deletions Keybinding/Models/MusicalTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed class Note : IEquatable<Note>
public Note(NoteName key)
{
Ensure.NotNull(key);
string canonical = CanonicalizeKey(key.ToString());
string canonical = NormalizeKey(key.ToString());
Key = canonical == key.ToString() ? key : NoteName.Create(canonical);
}

Expand All @@ -34,9 +34,17 @@ public Note(string key)
throw new ArgumentException("Key cannot be null or whitespace", nameof(key));
}

Key = NoteName.Create(CanonicalizeKey(key.Trim().ToUpperInvariant()));
Key = NoteName.Create(NormalizeKey(key));
}

/// <summary>
/// Trims, uppercases and canonicalizes a key name. Both constructors go through this, so a note
/// compares and hashes the same however it was built.
/// </summary>
/// <param name="key">A key name in any case</param>
/// <returns>The canonical key name</returns>
private static string NormalizeKey(string key) => CanonicalizeKey(key.Trim().ToUpperInvariant());

/// <summary>
/// Maps a modifier alias to its canonical key name, so that every way of building a note
/// (parsing, constructing directly, or loading a stored profile) compares and hashes the same.
Expand Down
Loading