bugfixes, better MusicXML support and 1.1.0
This commit is contained in:
@@ -0,0 +1,484 @@
|
||||
// NoteDisplayTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("NoteDisplay — per-note notation overrides")
|
||||
struct NoteDisplayTests {
|
||||
|
||||
// MARK: - 1. Nil / no-override path — byte-identical output
|
||||
|
||||
@Test func nilDisplayProducesNoByteDifference() throws {
|
||||
// A note with no display parameter must produce byte-identical MusicXML
|
||||
// to the same note with display: nil (the default).
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter)
|
||||
}
|
||||
}
|
||||
let scoreWithNil: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: nil)
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
let xmlNil: String = try scoreWithNil.toMusicXMLString()
|
||||
#expect(xml == xmlNil)
|
||||
// Also confirm none of the display elements are present.
|
||||
#expect(!xml.contains("<notehead>"))
|
||||
#expect(!xml.contains("<stem>"))
|
||||
#expect(!xml.contains("size=\""))
|
||||
}
|
||||
|
||||
@Test func allDefaultsNoteDisplayProducesNoByteDifference() throws {
|
||||
// NoteDisplay() with all defaults is a no-op when hasOverrides == false.
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter)
|
||||
}
|
||||
}
|
||||
let scoreWithDefault: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay())
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
let xmlDefault: String = try scoreWithDefault.toMusicXMLString()
|
||||
#expect(xml == xmlDefault)
|
||||
}
|
||||
|
||||
// MARK: - 2. Notehead shapes → <notehead>VALUE</notehead>
|
||||
|
||||
@Test func noteheadXEmitsCorrectElement() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<notehead>x</notehead>"))
|
||||
}
|
||||
|
||||
@Test func noteheadDiamondEmitsCorrectElement() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .diamond))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<notehead>diamond</notehead>"))
|
||||
}
|
||||
|
||||
@Test func noteheadNoneEmitsHiddenNotehead() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .hidden))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<notehead>none</notehead>"))
|
||||
}
|
||||
|
||||
@Test func noteheadCircleXEmitsHyphenatedValue() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .circleX))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<notehead>circle-x</notehead>"))
|
||||
}
|
||||
|
||||
@Test func noteheadOtherClusterPassesThroughVerbatim() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .other("cluster")))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<notehead>cluster</notehead>"))
|
||||
}
|
||||
|
||||
@Test func noteheadOtherIsXMLEscaped() throws {
|
||||
// .other with reserved XML characters should be escaped.
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .other("<test>")))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<notehead><test></notehead>"))
|
||||
#expect(!xml.contains("<notehead><test></notehead>"))
|
||||
}
|
||||
|
||||
// MARK: - 3. NoteSize → <type size="...">
|
||||
|
||||
@Test func sizeNilProducesNoByteDifference() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter)
|
||||
}
|
||||
}
|
||||
let scoreWithNilSize: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(size: nil))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
let xmlNilSize: String = try scoreWithNilSize.toMusicXMLString()
|
||||
#expect(xml == xmlNilSize)
|
||||
#expect(!xml.contains("size="))
|
||||
}
|
||||
|
||||
@Test func sizeNormalProducesNoByteDifference() throws {
|
||||
// size: .normal is the same as nil — musicXMLTypeSize returns nil, so no size= attribute.
|
||||
// NoteDisplay(size: .normal).hasOverrides must be false.
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter)
|
||||
}
|
||||
}
|
||||
let scoreWithNormal: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(size: .normal))
|
||||
}
|
||||
}
|
||||
let displayNormal: NoteDisplay = NoteDisplay(size: .normal)
|
||||
#expect(!displayNormal.hasOverrides)
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
let xmlNormal: String = try scoreWithNormal.toMusicXMLString()
|
||||
#expect(xml == xmlNormal)
|
||||
}
|
||||
|
||||
@Test func sizeCueEmitsSizeAttributeOnType() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .eighth, display: NoteDisplay(size: .cue))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<type size=\"cue\">eighth</type>"))
|
||||
#expect(!xml.contains("<type>eighth</type>"))
|
||||
}
|
||||
|
||||
@Test func sizeLargeEmitsSizeAttributeOnType() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(size: .large))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<type size=\"large\">quarter</type>"))
|
||||
}
|
||||
|
||||
// MARK: - 4. hideStem → <stem>none</stem>
|
||||
|
||||
@Test func hideStemEmitsStemNone() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(hideStem: true))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<stem>none</stem>"))
|
||||
}
|
||||
|
||||
@Test func hideStemFalseProducesNoByteDifference() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter)
|
||||
}
|
||||
}
|
||||
let scoreNoStem: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(hideStem: false))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
let xmlNoStem: String = try scoreNoStem.toMusicXMLString()
|
||||
#expect(xml == xmlNoStem)
|
||||
#expect(!xml.contains("<stem>"))
|
||||
}
|
||||
|
||||
@Test func hideStemAppearsAfterTimeModificationBeforeStaff() throws {
|
||||
// Verify the MusicXML child-element order: time-modification → stem → notehead → staff.
|
||||
// Use a quarter note (no time-modification) to keep the test simple — we verify
|
||||
// that <stem> appears BEFORE any <notehead> and the two appear within the note block.
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x, hideStem: true))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<stem>none</stem>"))
|
||||
#expect(xml.contains("<notehead>x</notehead>"))
|
||||
// stem must appear before notehead in the output string.
|
||||
let stemRange: Range<String.Index>? = xml.range(of: "<stem>none</stem>")
|
||||
let noteheadRange: Range<String.Index>? = xml.range(of: "<notehead>x</notehead>")
|
||||
#expect(stemRange != nil)
|
||||
#expect(noteheadRange != nil)
|
||||
if let stemR: Range<String.Index> = stemRange,
|
||||
let nhR: Range<String.Index> = noteheadRange {
|
||||
#expect(stemR.lowerBound < nhR.lowerBound)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 5. hideBeam — breaks a beam run at the marked note
|
||||
|
||||
@Test func hideBeamBreaksBeamRun() throws {
|
||||
// Four eighth notes in a row normally form one beam group (begin/continue/continue/end).
|
||||
// Marking the SECOND note with hideBeam should break the run so the second note
|
||||
// carries no <beam> element and the surrounding run is split.
|
||||
let scoreBeamed: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .eighth)
|
||||
Note(.d, 5, dur: .eighth)
|
||||
Note(.e, 5, dur: .eighth)
|
||||
Note(.f, 5, dur: .eighth)
|
||||
}
|
||||
}
|
||||
let scoreHideBeam: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .eighth)
|
||||
Note(.d, 5, dur: .eighth, display: NoteDisplay(hideBeam: true))
|
||||
Note(.e, 5, dur: .eighth)
|
||||
Note(.f, 5, dur: .eighth)
|
||||
}
|
||||
}
|
||||
let xmlBeamed: String = try scoreBeamed.toMusicXMLString()
|
||||
let xmlHide: String = try scoreHideBeam.toMusicXMLString()
|
||||
// The beamed version has exactly 4 <beam> elements (begin/continue/continue/end).
|
||||
let beamedCount: Int = xmlBeamed.components(separatedBy: "<beam number=\"1\">").count - 1
|
||||
#expect(beamedCount == 4)
|
||||
// The hide-beam version has fewer <beam> elements because the run broke.
|
||||
let hideCount: Int = xmlHide.components(separatedBy: "<beam number=\"1\">").count - 1
|
||||
#expect(hideCount < beamedCount)
|
||||
// The hide-beam version should still contain some beams for the intact sub-runs.
|
||||
#expect(hideCount > 0)
|
||||
}
|
||||
|
||||
@Test func hideBeamFalseProducesNoByteDifference() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .eighth)
|
||||
Note(.d, 5, dur: .eighth)
|
||||
}
|
||||
}
|
||||
let scoreNoHide: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .eighth)
|
||||
Note(.d, 5, dur: .eighth, display: NoteDisplay(hideBeam: false))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
let xmlNoHide: String = try scoreNoHide.toMusicXMLString()
|
||||
#expect(xml == xmlNoHide)
|
||||
}
|
||||
|
||||
// MARK: - 6. display composes with spanners
|
||||
|
||||
@Test func displayComposesWithSpanners() throws {
|
||||
// A note with BOTH spanners: and display: should emit both the notehead
|
||||
// override AND the slur markup.
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Note(.c, 5, dur: .quarter, spanners: [.beginSlur], display: NoteDisplay(notehead: .diamond))
|
||||
Note(.e, 5, dur: .quarter, spanners: [.endSlur])
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<notehead>diamond</notehead>"))
|
||||
#expect(xml.contains("<slur number=\"1\" type=\"start\"/>"))
|
||||
#expect(xml.contains("<slur number=\"1\" type=\"stop\"/>"))
|
||||
}
|
||||
|
||||
@Test func displayComposesWithSlurBlock() throws {
|
||||
// A note wrapped in Slur{} AND a display should emit both.
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Slur {
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x))
|
||||
Note(.e, 5, dur: .quarter)
|
||||
}
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<notehead>x</notehead>"))
|
||||
#expect(xml.contains("<slur number=\"1\" type=\"start\"/>"))
|
||||
}
|
||||
|
||||
// MARK: - 7. flatten transparency
|
||||
|
||||
@Test func displayedFlattenIsTransparent() {
|
||||
// A .displayed note must produce the SAME ScoreEvent list as the unwrapped note.
|
||||
let part: ScorePart = ScorePart(
|
||||
name: "test",
|
||||
elements: [
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x, hideStem: true))
|
||||
]
|
||||
)
|
||||
let plainPart: ScorePart = ScorePart(
|
||||
name: "test",
|
||||
elements: [
|
||||
Note(.c, 5, dur: .quarter)
|
||||
]
|
||||
)
|
||||
let events: [ScoreEvent] = part.flatten()
|
||||
let plainEvents: [ScoreEvent] = plainPart.flatten()
|
||||
#expect(events.count == plainEvents.count)
|
||||
#expect(events.count == 1)
|
||||
#expect(events[0].beat == plainEvents[0].beat)
|
||||
#expect(events[0].duration == plainEvents[0].duration)
|
||||
#expect(events[0].pitch.midiNote == plainEvents[0].pitch.midiNote)
|
||||
}
|
||||
|
||||
@Test func displayedChordFlattenIsTransparent() {
|
||||
// A .displayed chord must produce the same ScoreEvent list as the unwrapped chord.
|
||||
let part: ScorePart = ScorePart(
|
||||
name: "test",
|
||||
elements: [
|
||||
Chord([(.c, 5), (.e, 5)], dur: .half, display: NoteDisplay(notehead: .diamond))
|
||||
]
|
||||
)
|
||||
let plainPart: ScorePart = ScorePart(
|
||||
name: "test",
|
||||
elements: [
|
||||
Chord([(.c, 5), (.e, 5)], dur: .half)
|
||||
]
|
||||
)
|
||||
let events: [ScoreEvent] = part.flatten()
|
||||
let plainEvents: [ScoreEvent] = plainPart.flatten()
|
||||
#expect(events.count == plainEvents.count)
|
||||
#expect(events.count == 2)
|
||||
}
|
||||
|
||||
// MARK: - 8. Rests ignore display
|
||||
|
||||
@Test func restIgnoresNoteheadAndStem() throws {
|
||||
// A rest followed by a note with display. Verify the rest carries no <notehead> or <stem>.
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Rest(dur: .quarter)
|
||||
Note(.c, 5, dur: .quarter, display: NoteDisplay(notehead: .x, hideStem: true))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
// The note has the overrides; the rest should not.
|
||||
#expect(xml.contains("<notehead>x</notehead>"))
|
||||
#expect(xml.contains("<stem>none</stem>"))
|
||||
// Verify: a plain Rest does not produce <notehead> even when the part contains
|
||||
// an atom with a notehead override. The rest's own XML block must not contain these.
|
||||
// Split by <note> blocks and check the rest block specifically.
|
||||
let noteBlocks: [Substring] = xml.split(separator: "\n").filter { $0.contains("<rest") }
|
||||
for block: Substring in noteBlocks {
|
||||
// None of the rest-only lines should contain notehead or stem overrides.
|
||||
#expect(!block.contains("notehead"))
|
||||
#expect(!block.contains("stem"))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - 9. Notehead enum values
|
||||
|
||||
@Test func noteheadMusicXMLValues() {
|
||||
#expect(Notehead.normal.musicXMLValue == "normal")
|
||||
#expect(Notehead.x.musicXMLValue == "x")
|
||||
#expect(Notehead.diamond.musicXMLValue == "diamond")
|
||||
#expect(Notehead.triangle.musicXMLValue == "triangle")
|
||||
#expect(Notehead.slash.musicXMLValue == "slash")
|
||||
#expect(Notehead.cross.musicXMLValue == "cross")
|
||||
#expect(Notehead.circleX.musicXMLValue == "circle-x")
|
||||
#expect(Notehead.square.musicXMLValue == "square")
|
||||
#expect(Notehead.hidden.musicXMLValue == "none")
|
||||
#expect(Notehead.other("xyz").musicXMLValue == "xyz")
|
||||
}
|
||||
|
||||
@Test func noteSizeMusicXMLTypeSize() {
|
||||
#expect(NoteSize.cue.musicXMLTypeSize == "cue")
|
||||
#expect(NoteSize.normal.musicXMLTypeSize == nil)
|
||||
#expect(NoteSize.large.musicXMLTypeSize == "large")
|
||||
}
|
||||
|
||||
@Test func noteDisplayHasOverrides() {
|
||||
#expect(!NoteDisplay().hasOverrides)
|
||||
// size: .normal is equivalent to nil — no size attribute emitted, so not an override.
|
||||
#expect(!NoteDisplay(size: .normal).hasOverrides)
|
||||
#expect(!NoteDisplay(size: nil).hasOverrides)
|
||||
#expect(NoteDisplay(notehead: .x).hasOverrides)
|
||||
#expect(NoteDisplay(notehead: .hidden).hasOverrides)
|
||||
#expect(NoteDisplay(size: .cue).hasOverrides)
|
||||
#expect(NoteDisplay(size: .large).hasOverrides)
|
||||
#expect(NoteDisplay(hideStem: true).hasOverrides)
|
||||
#expect(NoteDisplay(hideBeam: true).hasOverrides)
|
||||
}
|
||||
|
||||
// MARK: - 10. Chord display overrides
|
||||
|
||||
@Test func chordDisplayEmitsNoteheadOnAllNoteheads() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Chord([(.c, 5), (.e, 5)], dur: .quarter, display: NoteDisplay(notehead: .x))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
// Both noteheads in the chord should carry the notehead override.
|
||||
let count: Int = xml.components(separatedBy: "<notehead>x</notehead>").count - 1
|
||||
#expect(count == 2)
|
||||
}
|
||||
|
||||
@Test func chordHideStemEmitsStemNone() throws {
|
||||
let score: Score = Score(bpm: 120) {
|
||||
Part("test") {
|
||||
Chord([(.c, 5), (.e, 5)], dur: .quarter, display: NoteDisplay(hideStem: true))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
#expect(xml.contains("<stem>none</stem>"))
|
||||
}
|
||||
|
||||
// MARK: - 11. Barline-split inherits display
|
||||
|
||||
@Test func barlineSplitNoteInheritsDisplay() throws {
|
||||
// A note that crosses a barline is split into two tied pieces; both pieces
|
||||
// should carry the display override (e.g. <notehead>x</notehead>).
|
||||
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(3, 4)) {
|
||||
Part("test") {
|
||||
// A dotted-half that fits in one bar, plus a quarter that wraps into bar 2.
|
||||
// Use a whole note which crosses the 3/4 barline:
|
||||
// 4 beats in a 3/4 bar → splits into dotted-half + quarter.
|
||||
Note(.c, 5, dur: .whole, display: NoteDisplay(notehead: .diamond))
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
// The whole note splits into exactly two tied pieces (dotted-half + quarter);
|
||||
// both must carry the diamond notehead.
|
||||
let count: Int = xml.components(separatedBy: "<notehead>diamond</notehead>").count - 1
|
||||
#expect(count == 2)
|
||||
}
|
||||
|
||||
@Test func barlineSplitDisplayInheritedOnMultiVoicePath() throws {
|
||||
// The multi-voice encoder path reconstructs a leftover continuation atom
|
||||
// for the part of a note past the barline. That reconstruction must carry
|
||||
// the display override forward (regression: it previously reset to default,
|
||||
// dropping the notehead on the continuation piece).
|
||||
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(3, 4)) {
|
||||
Part("test") {
|
||||
Voice(1) {
|
||||
Note(.c, 5, dur: .whole, display: NoteDisplay(notehead: .diamond))
|
||||
}
|
||||
Voice(2) {
|
||||
Note(.e, 4, dur: .whole)
|
||||
}
|
||||
}
|
||||
}
|
||||
let xml: String = try score.toMusicXMLString()
|
||||
// Voice 1's whole note splits into two tied pieces across the 3/4 barline;
|
||||
// both pieces must keep the diamond notehead.
|
||||
let count: Int = xml.components(separatedBy: "<notehead>diamond</notehead>").count - 1
|
||||
#expect(count == 2)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user