582 lines
27 KiB
Swift
582 lines
27 KiB
Swift
// SlurSpannerTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("SlurSpanner — authoring, encoding, and validation")
|
|
struct SlurSpannerTests {
|
|
|
|
// MARK: - Slur {} block
|
|
|
|
@Test func slurBlockEmitsStartOnFirstNoteStopOnLast() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Slur {
|
|
Note(.c, 5, dur: .quarter)
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter)
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("<slur number=\"1\" type=\"start\"/>"))
|
|
#expect(xml.contains("<slur number=\"1\" type=\"stop\"/>"))
|
|
// The middle notes should NOT carry slur annotations.
|
|
// Count occurrences: exactly one start and one stop.
|
|
// Note: tie annotations also use type="start"/"stop", so count only slur lines.
|
|
let slurStart: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"start\"/>").count - 1
|
|
let slurStop: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"stop\"/>").count - 1
|
|
#expect(slurStart == 1)
|
|
#expect(slurStop == 1)
|
|
// Verify start and stop appear in document order (start index < stop index).
|
|
let startRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"start\"/>")
|
|
let stopRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"stop\"/>")
|
|
if let s: Range<String.Index> = startRange, let t: Range<String.Index> = stopRange {
|
|
#expect(s.lowerBound < t.lowerBound)
|
|
} else {
|
|
Issue.record("Expected both slur start and stop to appear in XML")
|
|
}
|
|
}
|
|
|
|
@Test func slurBlockSingleNoteEmitsStartAndStopOnSameNote() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Slur {
|
|
Note(.c, 5, dur: .quarter)
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Single atom: both start and stop land on the same <note> block.
|
|
// Both should appear in a single <notations> block.
|
|
let slurStart: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"start\"/>").count - 1
|
|
let slurStop: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"stop\"/>").count - 1
|
|
#expect(slurStart == 1)
|
|
#expect(slurStop == 1)
|
|
// Both must appear within the same <notations>…</notations> block.
|
|
// Find the single <note> block and check both are inside it.
|
|
let noteBlocks: [Substring] = xml.split(separator: Character("\n"), omittingEmptySubsequences: false)
|
|
var inNotations: Bool = false
|
|
var foundStart: Bool = false
|
|
var foundStop: Bool = false
|
|
for line: Substring in noteBlocks {
|
|
if line.contains("<notations>") { inNotations = true }
|
|
if inNotations && line.contains("<slur number=\"1\" type=\"start\"/>") { foundStart = true }
|
|
if inNotations && line.contains("<slur number=\"1\" type=\"stop\"/>") { foundStop = true }
|
|
if line.contains("</notations>") { inNotations = false }
|
|
}
|
|
#expect(foundStart)
|
|
#expect(foundStop)
|
|
}
|
|
|
|
@Test func slurBlockSkipsRestsAsEndpoint() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Slur {
|
|
Rest(dur: .quarter)
|
|
Note(.c, 5, dur: .quarter)
|
|
Note(.d, 5, dur: .quarter)
|
|
Rest(dur: .quarter)
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Start on the first non-rest note (C5), stop on the last non-rest note (D5).
|
|
let slurStart: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"start\"/>").count - 1
|
|
let slurStop: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"stop\"/>").count - 1
|
|
#expect(slurStart == 1)
|
|
#expect(slurStop == 1)
|
|
}
|
|
|
|
// MARK: - Per-note spanners
|
|
|
|
@Test func perNoteBeginSlurEmitsStartOnThatNote() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginSlur])
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter, spanners: [.endSlur])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
let slurStart: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"start\"/>").count - 1
|
|
let slurStop: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"stop\"/>").count - 1
|
|
#expect(slurStart == 1)
|
|
#expect(slurStop == 1)
|
|
// Start before stop in document order.
|
|
let startRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"start\"/>")
|
|
let stopRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"stop\"/>")
|
|
if let s: Range<String.Index> = startRange, let t: Range<String.Index> = stopRange {
|
|
#expect(s.lowerBound < t.lowerBound)
|
|
} else {
|
|
Issue.record("Expected both slur start and stop in XML")
|
|
}
|
|
}
|
|
|
|
@Test func perNoteBeginCrescendoEmitsWedge() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginCrescendo])
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter, spanners: [.endHairpin])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("<wedge type=\"crescendo\"/>"))
|
|
#expect(xml.contains("<wedge type=\"stop\"/>"))
|
|
}
|
|
|
|
@Test func perNoteBeginDiminuendoEmitsWedge() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.f, 5, dur: .quarter, spanners: [.beginDiminuendo])
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.d, 5, dur: .quarter, spanners: [.endHairpin])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("<wedge type=\"diminuendo\"/>"))
|
|
#expect(xml.contains("<wedge type=\"stop\"/>"))
|
|
}
|
|
|
|
// MARK: - Shared-boundary / overlap case
|
|
|
|
@Test func sharedBoundarySlurStartAndHairpinStopOnSameNote() {
|
|
// A note that simultaneously ends a hairpin AND starts a slur.
|
|
// Both annotations must appear in the XML.
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
// Bar 1: crescendo opens here
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginCrescendo])
|
|
Note(.d, 5, dur: .quarter)
|
|
// This note: crescendo ends AND slur starts (shared boundary)
|
|
Note(.e, 5, dur: .quarter, spanners: [.endHairpin, .beginSlur])
|
|
// Bar 1 continues with the slur
|
|
Note(.f, 5, dur: .quarter, spanners: [.endSlur])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Wedge stop after E5
|
|
#expect(xml.contains("<wedge type=\"crescendo\"/>"))
|
|
#expect(xml.contains("<wedge type=\"stop\"/>"))
|
|
// Slur start on E5, slur stop on F5
|
|
#expect(xml.contains("<slur number=\"1\" type=\"start\"/>"))
|
|
#expect(xml.contains("<slur number=\"1\" type=\"stop\"/>"))
|
|
// In document order: crescendo start < wedge stop and slur start < slur stop
|
|
let crescRange: Range<String.Index>? = xml.range(of: "<wedge type=\"crescendo\"/>")
|
|
let wedgeStopRange: Range<String.Index>? = xml.range(of: "<wedge type=\"stop\"/>")
|
|
let slurStartRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"start\"/>")
|
|
let slurStopRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"stop\"/>")
|
|
if let cr: Range<String.Index> = crescRange,
|
|
let ws: Range<String.Index> = wedgeStopRange,
|
|
let ss: Range<String.Index> = slurStartRange,
|
|
let st: Range<String.Index> = slurStopRange
|
|
{
|
|
#expect(cr.lowerBound < ws.lowerBound)
|
|
#expect(ss.lowerBound < st.lowerBound)
|
|
} else {
|
|
Issue.record("Expected all four spanner markers in XML")
|
|
}
|
|
}
|
|
|
|
@Test func crescendoBlockAndSlurBlockCanOverlap() {
|
|
// Slur begins inside a Crescendo block — the slur-start and wedge-start
|
|
// both land on the first note of the Slur, while the crescendo was opened
|
|
// by the Crescendo block's first note (C5). The slur-stop lands on the last
|
|
// Slur note; the wedge-stop lands on the last Crescendo note (F5).
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Crescendo {
|
|
Note(.c, 5, dur: .quarter, dyn: .p)
|
|
Note(.d, 5, dur: .quarter)
|
|
Slur {
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter, dyn: .f)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Crescendo (hairpin) must appear.
|
|
#expect(xml.contains("<wedge type=\"crescendo\"/>"))
|
|
#expect(xml.contains("<wedge type=\"stop\"/>"))
|
|
// Slur must appear.
|
|
#expect(xml.contains("<slur number=\"1\" type=\"start\"/>"))
|
|
#expect(xml.contains("<slur number=\"1\" type=\"stop\"/>"))
|
|
}
|
|
|
|
// MARK: - Flatten transparency
|
|
|
|
@Test func slurBlockFlattenIsIdenticalToUnwrapped() {
|
|
// The ScoreEvent list from a Slur-wrapped span must equal the list from
|
|
// the same notes without the wrapper.
|
|
let partWithSlur: ScorePart = ScorePart(
|
|
name: "p",
|
|
elements: [
|
|
ScoreElement.slur([
|
|
ScoreElement.note(.named(.c, 5), duration: .quarter, dynamic: .mf,
|
|
articulation: .normal, voice: 0, tie: false),
|
|
ScoreElement.note(.named(.d, 5), duration: .quarter, dynamic: .mf,
|
|
articulation: .normal, voice: 0, tie: false)
|
|
])
|
|
]
|
|
)
|
|
let partWithout: ScorePart = ScorePart(
|
|
name: "p",
|
|
elements: [
|
|
ScoreElement.note(.named(.c, 5), duration: .quarter, dynamic: .mf,
|
|
articulation: .normal, voice: 0, tie: false),
|
|
ScoreElement.note(.named(.d, 5), duration: .quarter, dynamic: .mf,
|
|
articulation: .normal, voice: 0, tie: false)
|
|
]
|
|
)
|
|
let eventsWithSlur: [ScoreEvent] = partWithSlur.flatten()
|
|
let eventsWithout: [ScoreEvent] = partWithout.flatten()
|
|
#expect(eventsWithSlur.count == eventsWithout.count)
|
|
for (a, b): (ScoreEvent, ScoreEvent) in zip(eventsWithSlur, eventsWithout) {
|
|
#expect(a.beat == b.beat)
|
|
#expect(a.duration == b.duration)
|
|
#expect(a.pitch == b.pitch)
|
|
}
|
|
}
|
|
|
|
@Test func spannedElementFlattenIsIdenticalToUnwrapped() {
|
|
let partWithSpanned: ScorePart = ScorePart(
|
|
name: "p",
|
|
elements: [
|
|
ScoreElement.spanned(
|
|
[.beginSlur],
|
|
ScoreElement.note(.named(.c, 5), duration: .quarter, dynamic: .mf,
|
|
articulation: .normal, voice: 0, tie: false)
|
|
)
|
|
]
|
|
)
|
|
let partWithout: ScorePart = ScorePart(
|
|
name: "p",
|
|
elements: [
|
|
ScoreElement.note(.named(.c, 5), duration: .quarter, dynamic: .mf,
|
|
articulation: .normal, voice: 0, tie: false)
|
|
]
|
|
)
|
|
let eventsWithSpanned: [ScoreEvent] = partWithSpanned.flatten()
|
|
let eventsWithout: [ScoreEvent] = partWithout.flatten()
|
|
#expect(eventsWithSpanned.count == eventsWithout.count)
|
|
if let a: ScoreEvent = eventsWithSpanned.first, let b: ScoreEvent = eventsWithout.first {
|
|
#expect(a.beat == b.beat)
|
|
#expect(a.duration == b.duration)
|
|
#expect(a.pitch == b.pitch)
|
|
}
|
|
}
|
|
|
|
// MARK: - Validation: unbalanced spanners
|
|
|
|
@Test func unbalancedEndSlurWithNoOpenIsDropped() {
|
|
// An .endSlur with no preceding .beginSlur should be silently dropped
|
|
// (no <slur type="stop"/> in the output) and a diagnostic posted.
|
|
LyrebirdMessageCenter.shared.clear()
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.endSlur])
|
|
Note(.d, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Dropped: no slur stop in output.
|
|
#expect(!xml.contains("<slur number=\"1\" type=\"stop\"/>"))
|
|
// No slur start either (none was authored).
|
|
#expect(!xml.contains("<slur number=\"1\" type=\"start\"/>"))
|
|
}
|
|
|
|
@Test func unbalancedEndHairpinWithNoOpenIsDropped() {
|
|
LyrebirdMessageCenter.shared.clear()
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.endHairpin])
|
|
Note(.d, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// No wedge-stop should appear.
|
|
#expect(!xml.contains("<wedge type=\"stop\"/>"))
|
|
}
|
|
|
|
@Test func doubleOpenHairpinDropsRedundantStartWithoutSynthesizingStop() {
|
|
// Two hairpin starts, one stop: the redundant second start is dropped.
|
|
// The validator must NOT synthesize a spurious wedge-stop on the dropped
|
|
// start — exactly one crescendo start and one stop should survive.
|
|
LyrebirdMessageCenter.shared.clear()
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginCrescendo])
|
|
Note(.d, 5, dur: .quarter, spanners: [.beginCrescendo])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endHairpin])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
let crescStarts: Int = xml.components(separatedBy: "<wedge type=\"crescendo\"/>").count - 1
|
|
let wedgeStops: Int = xml.components(separatedBy: "<wedge type=\"stop\"/>").count - 1
|
|
#expect(crescStarts == 1, "redundant second crescendo start must be dropped")
|
|
#expect(wedgeStops == 1, "exactly one stop — no spurious stop synthesized on the dropped start")
|
|
}
|
|
|
|
@Test func diagnosticIsPostedForUnbalancedSpanner() async {
|
|
// An orphan endSlur causes a warning to be posted via
|
|
// `Task { @MainActor in LyrebirdMessageCenter.shared.warning(...) }` inside
|
|
// validateSpannerBalance. Poll in a bounded loop (up to ~50 yields) rather
|
|
// than relying on a fixed yield count, so the test is deterministic under
|
|
// load without being fragile to scheduler jitter.
|
|
LyrebirdMessageCenter.shared.clear()
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.endSlur])
|
|
}
|
|
}
|
|
// Trigger encoding (this fires the Task { @MainActor in ... } inside validateSpannerBalance).
|
|
let _: String = score.toMusicXMLString()
|
|
// Poll until a warning appears or the bound is hit.
|
|
let maxPolls: Int = 50
|
|
var pollCount: Int = 0
|
|
while pollCount < maxPolls {
|
|
await Task.yield()
|
|
let warnings: [LyrebirdMessage] = LyrebirdMessageCenter.shared.messages.filter {
|
|
$0.tier == .warning
|
|
}
|
|
if !warnings.isEmpty { break }
|
|
pollCount += 1
|
|
}
|
|
// A warning should now be in the message center.
|
|
let warnings: [LyrebirdMessage] = LyrebirdMessageCenter.shared.messages.filter {
|
|
$0.tier == .warning
|
|
}
|
|
#expect(!warnings.isEmpty)
|
|
}
|
|
|
|
// MARK: - Note DSL spanners: / param does not affect output when empty
|
|
|
|
@Test func noteWithEmptySpannersIsIdenticalToPlainNote() {
|
|
let scoreWithEmpty: Score = Score(bpm: 120) {
|
|
Part("p") { Note(.c, 5, dur: .quarter, spanners: []) }
|
|
}
|
|
let scorePlain: Score = Score(bpm: 120) {
|
|
Part("p") { Note(.c, 5, dur: .quarter) }
|
|
}
|
|
#expect(scoreWithEmpty.toMusicXMLString() == scorePlain.toMusicXMLString())
|
|
}
|
|
|
|
// MARK: - Chord spanners
|
|
|
|
@Test func chordSpannersEmitSlurOnChordAtom() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Chord([.named(.c, 5), .named(.e, 5)], dur: .quarter, spanners: [.beginSlur])
|
|
Note(.d, 5, dur: .quarter, spanners: [.endSlur])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("<slur number=\"1\" type=\"start\"/>"))
|
|
#expect(xml.contains("<slur number=\"1\" type=\"stop\"/>"))
|
|
}
|
|
|
|
// MARK: - Existing slur round-trip unaffected
|
|
|
|
@Test func legatoSlurRoundTripUnaffected() {
|
|
// The existing legato-articulation slur path (annotateSlurs over .legato notes)
|
|
// must still work correctly — the new Slur {} block does not interfere.
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, art: .legato)
|
|
Note(.d, 5, dur: .quarter, art: .legato)
|
|
Note(.e, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Legato run of C5-D5 should produce one slur start (on C5) and
|
|
// one slur stop (on E5 — the non-legato follower).
|
|
let slurStart: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"start\"/>").count - 1
|
|
let slurStop: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"stop\"/>").count - 1
|
|
#expect(slurStart == 1)
|
|
#expect(slurStop == 1)
|
|
}
|
|
|
|
// MARK: - Existing wedge round-trip unaffected
|
|
|
|
@Test func crescendoBlockRoundTripUnaffected() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Crescendo {
|
|
Note(.c, 5, dur: .quarter, dyn: .p)
|
|
Note(.d, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
Note(.f, 5, dur: .quarter, dyn: .f)
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("<wedge type=\"crescendo\"/>"))
|
|
#expect(xml.contains("<wedge type=\"stop\"/>"))
|
|
// No unexpected slur markup.
|
|
#expect(!xml.contains("<slur"))
|
|
}
|
|
|
|
// MARK: - Authored-slur / legato-auto-slur interaction (blocking bug regression)
|
|
|
|
/// A note that is BOTH a `.legato` note AND a `Slur {}` endpoint must keep
|
|
/// its authored slur annotation. The legato auto-slur must not clobber the
|
|
/// authored `emitStart`/`emitStop` flags, and the output must contain exactly
|
|
/// one balanced `<slur start>` / `<slur stop>` pair with no dangling open.
|
|
@Test func legatoNoteAtSlurBlockBoundaryKeepsAuthoredSlur() {
|
|
// C5 is legato AND is the last note inside a Slur {} block (authored stop).
|
|
// D5 (the non-legato follower) would normally receive the auto-slur stop,
|
|
// but the authored stop on C5 takes precedence — the auto-slur must be
|
|
// suppressed so only one balanced pair is emitted.
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Slur {
|
|
Note(.a, 4, dur: .quarter)
|
|
Note(.b, 4, dur: .quarter)
|
|
Note(.c, 5, dur: .quarter, art: .legato)
|
|
}
|
|
Note(.d, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Exactly one start and one stop — no dangling extra span.
|
|
let slurStart: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"start\"/>").count - 1
|
|
let slurStop: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"stop\"/>").count - 1
|
|
#expect(slurStart == 1, "authored Slur {} block must emit exactly one slur start")
|
|
#expect(slurStop == 1, "authored Slur {} block must emit exactly one slur stop")
|
|
// Start must precede stop in document order.
|
|
let startRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"start\"/>")
|
|
let stopRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"stop\"/>")
|
|
if let s: Range<String.Index> = startRange, let t: Range<String.Index> = stopRange {
|
|
#expect(s.lowerBound < t.lowerBound, "slur start must appear before slur stop")
|
|
} else {
|
|
Issue.record("Expected exactly one slur start and one slur stop in XML")
|
|
}
|
|
}
|
|
|
|
/// An authored `Slur {}` block overlapping a `.legato` run in the same voice
|
|
/// must NOT emit two concurrent `number="1"` spans. The auto-slur for the
|
|
/// legato run must be suppressed; only the authored slur survives.
|
|
@Test func authoredSlurOverlappingLegatoRunDoesNotEmitTwoConcurrentSpans() {
|
|
// Slur {} wraps C5-D5-E5. D5 is also legato, which would normally trigger
|
|
// an auto-slur run of length 1 (D5 only, E5 is the follower). The authored
|
|
// slur on this voice prevents the auto-slur from firing.
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Slur {
|
|
Note(.c, 5, dur: .quarter)
|
|
Note(.d, 5, dur: .quarter, art: .legato)
|
|
Note(.e, 5, dur: .quarter)
|
|
}
|
|
Note(.f, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Only one pair of slur start/stop — no two concurrent number="1" spans.
|
|
let slurStart: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"start\"/>").count - 1
|
|
let slurStop: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"stop\"/>").count - 1
|
|
#expect(slurStart == 1, "exactly one slur start: authored wins, auto-slur suppressed")
|
|
#expect(slurStop == 1, "exactly one slur stop: authored wins, auto-slur suppressed")
|
|
// Verify the authored slur spans the full block (start on first note, stop on last).
|
|
// Both markers must appear in document order.
|
|
let startRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"start\"/>")
|
|
let stopRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"stop\"/>")
|
|
if let s: Range<String.Index> = startRange, let t: Range<String.Index> = stopRange {
|
|
#expect(s.lowerBound < t.lowerBound, "slur start must appear before slur stop")
|
|
} else {
|
|
Issue.record("Expected exactly one slur start and one slur stop in XML")
|
|
}
|
|
}
|
|
|
|
// MARK: - Orphan-open-at-end-of-part cleanup
|
|
|
|
@Test func orphanOpenSlurAtEndOfPartIsDropped() {
|
|
// A slur that opens but never closes: the opener start is dropped, so no
|
|
// slur markup is emitted.
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginSlur])
|
|
Note(.d, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(!xml.contains("<slur number=\"1\" type=\"start\"/>"))
|
|
#expect(!xml.contains("<slur number=\"1\" type=\"stop\"/>"))
|
|
}
|
|
|
|
@Test func orphanOpenHairpinAtEndOfPartIsDropped() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginCrescendo])
|
|
Note(.d, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(!xml.contains("<wedge type=\"crescendo\"/>"))
|
|
#expect(!xml.contains("<wedge type=\"stop\"/>"))
|
|
}
|
|
|
|
@Test func balancedSlurSurvivesWhenLaterSlurInSameVoiceIsOrphaned() {
|
|
// Two slur spans in one voice: the first is balanced, the second opens
|
|
// and never closes. The cleanup must drop ONLY the second (orphan) start,
|
|
// leaving the first span's start+stop intact. This guards against the
|
|
// "find first emitStart" misattribution bug (which would drop the first,
|
|
// balanced span and leave the orphan emitted).
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
// Balanced span: C5 → D5.
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginSlur])
|
|
Note(.d, 5, dur: .quarter, spanners: [.endSlur])
|
|
// Orphan span: E5 opens, never closes.
|
|
Note(.e, 5, dur: .quarter, spanners: [.beginSlur])
|
|
Note(.f, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Exactly one balanced start/stop pair survives (the first span); the
|
|
// orphan second start is dropped.
|
|
let slurStart: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"start\"/>").count - 1
|
|
let slurStop: Int = xml.components(separatedBy: "<slur number=\"1\" type=\"stop\"/>").count - 1
|
|
#expect(slurStart == 1, "only the balanced span's start should survive")
|
|
#expect(slurStop == 1, "the balanced span's stop must remain")
|
|
// The surviving start must precede the stop (it is the first span, not the orphan).
|
|
let startRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"start\"/>")
|
|
let stopRange: Range<String.Index>? = xml.range(of: "<slur number=\"1\" type=\"stop\"/>")
|
|
if let s: Range<String.Index> = startRange, let t: Range<String.Index> = stopRange {
|
|
#expect(s.lowerBound < t.lowerBound)
|
|
} else {
|
|
Issue.record("Expected the balanced span's start and stop to survive")
|
|
}
|
|
}
|
|
|
|
@Test func balancedHairpinSurvivesWhenLaterHairpinInSameVoiceIsOrphaned() {
|
|
// Hairpin analog of the slur multi-span survival test: a balanced
|
|
// crescendo followed by an orphan-open crescendo in the same voice.
|
|
// Cleanup must drop ONLY the orphan second start, keeping the first
|
|
// balanced hairpin's start + stop.
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
// Balanced hairpin: C5 → D5.
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginCrescendo])
|
|
Note(.d, 5, dur: .quarter, spanners: [.endHairpin])
|
|
// Orphan hairpin: E5 opens, never closes.
|
|
Note(.e, 5, dur: .quarter, spanners: [.beginCrescendo])
|
|
Note(.f, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
let crescStarts: Int = xml.components(separatedBy: "<wedge type=\"crescendo\"/>").count - 1
|
|
let wedgeStops: Int = xml.components(separatedBy: "<wedge type=\"stop\"/>").count - 1
|
|
#expect(crescStarts == 1, "only the balanced hairpin's start should survive")
|
|
#expect(wedgeStops == 1, "the balanced hairpin's stop must remain")
|
|
}
|
|
}
|