Skip to content

Reject undefined enum values in the Pitch, PitchClass and Section factories [patch] - #535

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/533-music-enum-validation
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/533-music-enum-validation

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #533

What was wrong

  • Pitch.Create(NoteLetter, Accidental, int) and PitchClass.Create(NoteLetter, Accidental) added the enums' integer values straight into the note number. Pitch.Create((NoteLetter)1, Accidental.Natural, 4) gave C#4, and Pitch.Create(NoteLetter.C, (Accidental)7, 4) gave G4.
  • Section.Create stored any SectionType. (SectionType)42 printed as [42], which Section.Parse refuses, so the Parse(x.ToString()) == x round trip broke.

Change

  • Notation.EnsureDefined(letter, accidental) throws ArgumentOutOfRangeException with ParamName set to letter or accidental. Both pitch factories call it.
  • Section.Create throws ArgumentOutOfRangeException (ParamName = type) for an undefined SectionType.
  • Both use the Enum.IsDefined check that ChordSymbolWriter.HasSupportedValues already applies to chord enums.

Tests

  • PitchTests: an undefined letter, and undefined accidentals 7, 3 and -3
  • PitchClassTests: an undefined letter and an undefined accidental
  • SectionRoundTripTests.CreateRejectsAnUndefinedSectionType
  • SectionRoundTripTests.RoundTripsEveryDefinedSectionType guards the round trip for every defined value, as the acceptance criteria ask

All seven rejection cases fail on main (I reverted the fix to confirm) and pass with it. The round-trip guard passes on both. Semantics.Test passes in full: 1970 passed, 14 skipped, 0 failed. Semantics.Music builds clean on every target framework (net8/9/10, netstandard2.0/2.1).


Generated by Claude Code

…tories [patch]

Pitch.Create and PitchClass.Create added the enum's integer value straight into
the note number, so (NoteLetter)1 gave C# and (Accidental)7 turned C4 into G4.
Section.Create stored any SectionType, and an undefined one printed as a number
that Section.Parse refuses, breaking the round trip. Each factory now throws
ArgumentOutOfRangeException naming the parameter, using the Enum.IsDefined
check ChordSymbolWriter already applies to chord enums.

Fixes #533

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW4QsgMfJRhwEBdboKd
@sonarqubecloud

sonarqubecloud Bot commented Nov 3, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants