303 lines
11 KiB
Swift
303 lines
11 KiB
Swift
// NotationMarkTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("NotationMark — per-note notation marks")
|
|
struct NotationMarkTests {
|
|
|
|
// MARK: - Helpers
|
|
|
|
/// Build a single-quarter-note score and return its MusicXML string.
|
|
private func singleNoteScore(marks: [NotationMark] = [], art: Articulation = .normal) -> String {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("test") {
|
|
Note(.c, 5, dur: .quarter, art: art, marks: marks)
|
|
}
|
|
}
|
|
return score.toMusicXMLString()
|
|
}
|
|
|
|
// MARK: - 1. Empty marks — byte-identical to plain note
|
|
|
|
@Test func emptyMarksProducesByteIdenticalOutput() {
|
|
let plain: String = Score(bpm: 120) {
|
|
Part("test") {
|
|
Note(.c, 5, dur: .quarter)
|
|
}
|
|
}.toMusicXMLString()
|
|
|
|
let withEmpty: String = Score(bpm: 120) {
|
|
Part("test") {
|
|
Note(.c, 5, dur: .quarter, marks: [])
|
|
}
|
|
}.toMusicXMLString()
|
|
|
|
#expect(plain == withEmpty)
|
|
}
|
|
|
|
// MARK: - 2. Multiple articulation marks → one <articulations> block
|
|
|
|
@Test func multipleArticulationMarksEmitSingleBlock() {
|
|
let xml: String = singleNoteScore(marks: [.staccato, .accent])
|
|
// One <articulations> block containing both glyphs.
|
|
#expect(xml.contains("<articulations>"))
|
|
#expect(xml.contains("<staccato/>"))
|
|
#expect(xml.contains("<accent/>"))
|
|
// Only one open/close pair of <articulations>.
|
|
let openCount: Int = xml.components(separatedBy: "<articulations>").count - 1
|
|
#expect(openCount == 1)
|
|
}
|
|
|
|
// MARK: - 3. art: + marks: deduplication
|
|
|
|
@Test func artAndMarksDeduplicateStaccato() {
|
|
let xml: String = singleNoteScore(marks: [.staccato], art: .staccato)
|
|
// <staccato/> must appear exactly once.
|
|
let count: Int = xml.components(separatedBy: "<staccato/>").count - 1
|
|
#expect(count == 1)
|
|
}
|
|
|
|
@Test func artStaccatoAloneProducesOneGlyph() {
|
|
let xml: String = singleNoteScore(art: .staccato)
|
|
let count: Int = xml.components(separatedBy: "<staccato/>").count - 1
|
|
#expect(count == 1)
|
|
}
|
|
|
|
// MARK: - 4. No marks — byte-identical regression (plain art: staccato path unchanged)
|
|
|
|
@Test func noMarksWithArticulationIsByteIdenticalToPreviousBehavior() {
|
|
// A note with art:.staccato and no marks must produce identical XML to
|
|
// the same note authored before marks: was added (regression guard).
|
|
let xml: String = singleNoteScore(art: .staccato)
|
|
#expect(xml.contains("<articulations>"))
|
|
#expect(xml.contains("<staccato/>"))
|
|
// No technical or fermata blocks.
|
|
#expect(!xml.contains("<technical>"))
|
|
#expect(!xml.contains("<fermata"))
|
|
}
|
|
|
|
// MARK: - 5. Technique marks → <technical> block
|
|
|
|
@Test func leftHandPizzicatoEmitsStoppedElement() {
|
|
let xml: String = singleNoteScore(marks: [.leftHandPizzicato])
|
|
#expect(xml.contains("<technical>"))
|
|
#expect(xml.contains("<stopped/>"))
|
|
}
|
|
|
|
@Test func snapPizzicatoEmitsElement() {
|
|
let xml: String = singleNoteScore(marks: [.snapPizzicato])
|
|
#expect(xml.contains("<technical>"))
|
|
#expect(xml.contains("<snap-pizzicato/>"))
|
|
}
|
|
|
|
@Test func upBowEmitsElement() {
|
|
let xml: String = singleNoteScore(marks: [.upBow])
|
|
#expect(xml.contains("<technical>"))
|
|
#expect(xml.contains("<up-bow/>"))
|
|
}
|
|
|
|
@Test func downBowEmitsElement() {
|
|
let xml: String = singleNoteScore(marks: [.downBow])
|
|
#expect(xml.contains("<technical>"))
|
|
#expect(xml.contains("<down-bow/>"))
|
|
}
|
|
|
|
@Test func harmonicEmitsElement() {
|
|
let xml: String = singleNoteScore(marks: [.harmonic])
|
|
#expect(xml.contains("<technical>"))
|
|
#expect(xml.contains("<harmonic/>"))
|
|
}
|
|
|
|
@Test func openStringEmitsElement() {
|
|
let xml: String = singleNoteScore(marks: [.openString])
|
|
#expect(xml.contains("<technical>"))
|
|
#expect(xml.contains("<open-string/>"))
|
|
}
|
|
|
|
// MARK: - 6. Fermata marks
|
|
|
|
@Test func fermataShapeNormalEmitsSelfClosing() {
|
|
let xml: String = singleNoteScore(marks: [.fermata(.normal)])
|
|
// Normal fermata: self-closing <fermata/>.
|
|
#expect(xml.contains("<fermata/>"))
|
|
// Must NOT emit a body text form like <fermata>normal</fermata>.
|
|
#expect(!xml.contains("<fermata>"))
|
|
}
|
|
|
|
@Test func fermataShapeSquareEmitsBodyText() {
|
|
let xml: String = singleNoteScore(marks: [.fermata(.square)])
|
|
#expect(xml.contains("<fermata>square</fermata>"))
|
|
}
|
|
|
|
@Test func fermataShapeDoubleSquareEmitsBodyText() {
|
|
let xml: String = singleNoteScore(marks: [.fermata(.doubleSquare)])
|
|
#expect(xml.contains("<fermata>double-square</fermata>"))
|
|
}
|
|
|
|
@Test func fermataShapeAngledEmitsBodyText() {
|
|
let xml: String = singleNoteScore(marks: [.fermata(.angled)])
|
|
#expect(xml.contains("<fermata>angled</fermata>"))
|
|
}
|
|
|
|
@Test func fermataShapeDoubleAngledEmitsBodyText() {
|
|
let xml: String = singleNoteScore(marks: [.fermata(.doubleAngled)])
|
|
#expect(xml.contains("<fermata>double-angled</fermata>"))
|
|
}
|
|
|
|
@Test func fermataShapeDoubleDotEmitsBodyText() {
|
|
let xml: String = singleNoteScore(marks: [.fermata(.doubleDot)])
|
|
#expect(xml.contains("<fermata>double-dot</fermata>"))
|
|
}
|
|
|
|
@Test func fermataShapeHalfCurveEmitsBodyText() {
|
|
let xml: String = singleNoteScore(marks: [.fermata(.halfCurve)])
|
|
#expect(xml.contains("<fermata>half-curve</fermata>"))
|
|
}
|
|
|
|
@Test func fermataShapeCurlewEmitsBodyText() {
|
|
let xml: String = singleNoteScore(marks: [.fermata(.curlew)])
|
|
#expect(xml.contains("<fermata>curlew</fermata>"))
|
|
}
|
|
|
|
// MARK: - 7. DTD order: <technical> before <articulations> before <fermata>
|
|
|
|
@Test func dtdOrderTechnicalBeforeArticulationsBeforeFermata() {
|
|
let xml: String = singleNoteScore(marks: [.staccato, .upBow, .fermata(.normal)])
|
|
let technicalRange: Range<String.Index>? = xml.range(of: "<technical>")
|
|
let articulationsRange: Range<String.Index>? = xml.range(of: "<articulations>")
|
|
let fermataRange: Range<String.Index>? = xml.range(of: "<fermata")
|
|
// All three must be present.
|
|
#expect(technicalRange != nil)
|
|
#expect(articulationsRange != nil)
|
|
#expect(fermataRange != nil)
|
|
if let tech: Range<String.Index> = technicalRange,
|
|
let art: Range<String.Index> = articulationsRange,
|
|
let ferm: Range<String.Index> = fermataRange
|
|
{
|
|
// <technical> comes before <articulations> comes before <fermata>.
|
|
#expect(tech.lowerBound < art.lowerBound)
|
|
#expect(art.lowerBound < ferm.lowerBound)
|
|
}
|
|
}
|
|
|
|
// MARK: - 8. flatten: .marked note produces same ScoreEvent as plain note
|
|
|
|
@Test func flattenMarkedNoteProducesSameEvents() {
|
|
let markedPart: ScorePart = ScorePart(
|
|
name: "test",
|
|
elements: [Note(.c, 5, dur: .quarter, marks: [.staccato, .fermata(.normal), .upBow])]
|
|
)
|
|
let plainPart: ScorePart = ScorePart(
|
|
name: "test",
|
|
elements: [Note(.c, 5, dur: .quarter)]
|
|
)
|
|
let markedEvents: [ScoreEvent] = markedPart.flatten()
|
|
let plainEvents: [ScoreEvent] = plainPart.flatten()
|
|
#expect(markedEvents.count == plainEvents.count)
|
|
for (m, p): (ScoreEvent, ScoreEvent) in zip(markedEvents, plainEvents) {
|
|
#expect(m.pitch == p.pitch)
|
|
#expect(m.duration == p.duration)
|
|
}
|
|
}
|
|
|
|
// MARK: - 9. Chord marks work
|
|
|
|
@Test func chordMarksEmitOnEachNotehead() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("test") {
|
|
Chord(
|
|
[Pitch.named(.c, 5), Pitch.named(.e, 5)],
|
|
dur: .quarter,
|
|
marks: [.staccato, .upBow]
|
|
)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Both noteheads of the chord should carry the marks.
|
|
let staccatoCount: Int = xml.components(separatedBy: "<staccato/>").count - 1
|
|
#expect(staccatoCount == 2)
|
|
let upBowCount: Int = xml.components(separatedBy: "<up-bow/>").count - 1
|
|
#expect(upBowCount == 2)
|
|
}
|
|
|
|
// MARK: - 10. No-marks note is byte-identical to pre-marks baseline (plain .normal art)
|
|
|
|
@Test func plainNoteWithNoMarksIsByteIdenticalToPlainNote() {
|
|
// A note with no marks: and art:.normal must produce byte-identical XML to
|
|
// the same note built without any marks: parameter at all. This guards that
|
|
// the .marked wrapper is NOT applied when marks is empty.
|
|
let xmlNoParam: String = Score(bpm: 120) {
|
|
Part("test") {
|
|
Note(.d, 4, dur: .half)
|
|
}
|
|
}.toMusicXMLString()
|
|
|
|
let xmlEmptyMarks: String = Score(bpm: 120) {
|
|
Part("test") {
|
|
Note(.d, 4, dur: .half, marks: [])
|
|
}
|
|
}.toMusicXMLString()
|
|
|
|
#expect(xmlNoParam == xmlEmptyMarks)
|
|
// Neither should contain any extra elements.
|
|
#expect(!xmlNoParam.contains("<notations>"))
|
|
}
|
|
|
|
// MARK: - 11. marcato → <strong-accent/>
|
|
|
|
@Test func marcatoMarkEmitsStrongAccent() {
|
|
let xml: String = singleNoteScore(marks: [.marcato])
|
|
#expect(xml.contains("<strong-accent/>"))
|
|
}
|
|
|
|
// MARK: - 12. tenuto → <tenuto/>
|
|
|
|
@Test func tenutoMarkEmitsTenuto() {
|
|
let xml: String = singleNoteScore(marks: [.tenuto])
|
|
#expect(xml.contains("<tenuto/>"))
|
|
}
|
|
|
|
// MARK: - 13. staccatissimo → <staccatissimo/>
|
|
|
|
@Test func staccatissimoMarkEmitsStaccatissimo() {
|
|
let xml: String = singleNoteScore(marks: [.staccatissimo])
|
|
#expect(xml.contains("<staccatissimo/>"))
|
|
}
|
|
|
|
// MARK: - 14. art + marks dedup for marcato (enum name ≠ XML element name)
|
|
|
|
@Test func artAndMarksDeduplicateMarcato() {
|
|
// .marcato is the one glyph whose enum case name differs from its MusicXML
|
|
// element (<strong-accent/>). Both art: and marks: supplying it must still
|
|
// emit exactly one <strong-accent/>.
|
|
let xml: String = singleNoteScore(marks: [.marcato], art: .marcato)
|
|
#expect(xml.components(separatedBy: "<strong-accent/>").count - 1 == 1)
|
|
}
|
|
|
|
// MARK: - 15. marks survive a multi-voice barline-crossing split
|
|
|
|
@Test func marksSurviveMultiVoiceBarlineSplit() {
|
|
// Voice 2's whole note starts on beat 3 and crosses into the next bar; the
|
|
// multi-voice encoder splits it into two tied pieces via the leftover-atom
|
|
// reconstruction. Both pieces must carry the staccato mark.
|
|
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
|
Part("p") {
|
|
Voice(1) { Note(.c, 5, dur: .whole) }
|
|
Voice(2) {
|
|
Rest(dur: .half)
|
|
Note(.g, 4, dur: .whole, marks: [.staccato]) // beats 3-4 + 1-2 next bar
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Two tied pieces, each with <staccato/>.
|
|
#expect(xml.components(separatedBy: "<staccato/>").count - 1 == 2)
|
|
}
|
|
}
|