394 lines
16 KiB
Swift
394 lines
16 KiB
Swift
// GlissandoTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("Glissando — authoring, encoding, and validation")
|
|
struct GlissandoTests {
|
|
|
|
// MARK: - Basic glissando emission
|
|
|
|
@Test func glissandoDefaultEmitsWavyStartAndStop() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando())])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Start: <glissando type="start" line-type="wavy" number="1"/>
|
|
#expect(xml.contains("<glissando type=\"start\" line-type=\"wavy\" number=\"1\"/>"))
|
|
// Stop: <glissando type="stop" number="1"/>
|
|
#expect(xml.contains("<glissando type=\"stop\" number=\"1\"/>"))
|
|
// Exactly one start and one stop.
|
|
let startCount: Int = xml.components(separatedBy: "<glissando type=\"start\"").count - 1
|
|
let stopCount: Int = xml.components(separatedBy: "<glissando type=\"stop\"").count - 1
|
|
#expect(startCount == 1)
|
|
#expect(stopCount == 1)
|
|
// Start appears before stop in document order.
|
|
let startRange: Range<String.Index>? = xml.range(of: "<glissando type=\"start\"")
|
|
let stopRange: Range<String.Index>? = xml.range(of: "<glissando 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 glissando start and stop to appear in XML")
|
|
}
|
|
}
|
|
|
|
@Test func glissandoNoAnnotationProducesByteIdenticalOutput() {
|
|
// A score without any glissando must not contain <glissando> or <slide>.
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(!xml.contains("<glissando"))
|
|
#expect(!xml.contains("<slide"))
|
|
}
|
|
|
|
// MARK: - Slide kind
|
|
|
|
@Test func slideKindEmitsSlideElement() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando(kind: .slide, lineType: .solid))])
|
|
Note(.g, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Start uses <slide>, not <glissando>.
|
|
#expect(xml.contains("<slide type=\"start\" line-type=\"solid\" number=\"1\"/>"))
|
|
// Stop also uses <slide>.
|
|
#expect(xml.contains("<slide type=\"stop\" number=\"1\"/>"))
|
|
// No <glissando> element present.
|
|
#expect(!xml.contains("<glissando"))
|
|
}
|
|
|
|
@Test func stopElementMatchesStartKind_glissando() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando(kind: .glissando))])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("<glissando type=\"stop\" number=\"1\"/>"))
|
|
#expect(!xml.contains("<slide type=\"stop\""))
|
|
}
|
|
|
|
@Test func stopElementMatchesStartKind_slide() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando(kind: .slide, lineType: .solid))])
|
|
Note(.g, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("<slide type=\"stop\" number=\"1\"/>"))
|
|
#expect(!xml.contains("<glissando type=\"stop\""))
|
|
}
|
|
|
|
// MARK: - Line types
|
|
|
|
@Test func lineTypeSolidEmitsCorrectAttribute() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando(kind: .glissando, lineType: .solid))])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("line-type=\"solid\""))
|
|
}
|
|
|
|
@Test func lineTypeDashedEmitsCorrectAttribute() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando(kind: .glissando, lineType: .dashed))])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("line-type=\"dashed\""))
|
|
}
|
|
|
|
@Test func lineTypeDottedEmitsCorrectAttribute() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando(kind: .glissando, lineType: .dotted))])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("line-type=\"dotted\""))
|
|
}
|
|
|
|
@Test func lineTypeWavyEmitsCorrectAttribute() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando(kind: .glissando, lineType: .wavy))])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("line-type=\"wavy\""))
|
|
}
|
|
|
|
// MARK: - Text label
|
|
|
|
@Test func textLabelEmitsNonSelfClosingElement() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando(text: "gliss."))])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Non-self-closing form with text content.
|
|
#expect(xml.contains("<glissando type=\"start\" line-type=\"wavy\" number=\"1\">gliss.</glissando>"))
|
|
// The self-closing form must NOT appear for the start.
|
|
let selfClosingStart: Int = xml.components(
|
|
separatedBy: "<glissando type=\"start\" line-type=\"wavy\" number=\"1\"/>"
|
|
).count - 1
|
|
#expect(selfClosingStart == 0)
|
|
}
|
|
|
|
@Test func textLabelNilEmitsSelfClosingElement() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando(text: nil))])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("<glissando type=\"start\" line-type=\"wavy\" number=\"1\"/>"))
|
|
#expect(!xml.contains(">gliss.</glissando>"))
|
|
}
|
|
|
|
@Test func textLabelWithXMLSpecialCharsIsEscaped() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando(text: "A&<B>"))])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// The escape function should have converted & → &, < → <, > → >
|
|
#expect(xml.contains("A&<B>"))
|
|
}
|
|
|
|
// MARK: - Unbalanced span handling
|
|
|
|
@Test func orphanStopDroppedAndDiagnosticPosted() async {
|
|
LyrebirdMessageCenter.shared.clear()
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
// Only an endGlissando with no matching beginGlissando.
|
|
Note(.c, 5, dur: .quarter, spanners: [.endGlissando])
|
|
Note(.e, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// No glissando elements should be emitted.
|
|
#expect(!xml.contains("<glissando"))
|
|
#expect(!xml.contains("<slide"))
|
|
// Poll until the async diagnostic Task fires (up to ~50 yields).
|
|
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
|
|
}
|
|
let warnings: [LyrebirdMessage] = LyrebirdMessageCenter.shared.messages.filter {
|
|
$0.tier == .warning
|
|
}
|
|
#expect(!warnings.isEmpty, "Expected a diagnostic warning for orphan glissando-stop")
|
|
#expect(warnings.contains { $0.text.contains("glissando") })
|
|
}
|
|
|
|
@Test func startWithNoStopDroppedAndDiagnosticPosted() async {
|
|
LyrebirdMessageCenter.shared.clear()
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando())])
|
|
// No endGlissando — orphan start.
|
|
Note(.e, 5, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// The orphan start must be dropped.
|
|
#expect(!xml.contains("<glissando type=\"start\""))
|
|
// Poll until the async diagnostic Task fires.
|
|
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
|
|
}
|
|
let warnings: [LyrebirdMessage] = LyrebirdMessageCenter.shared.messages.filter {
|
|
$0.tier == .warning
|
|
}
|
|
#expect(!warnings.isEmpty, "Expected a diagnostic warning for orphan glissando-start")
|
|
#expect(warnings.contains { $0.text.contains("glissando") })
|
|
}
|
|
|
|
// MARK: - Composition with slur
|
|
|
|
@Test func glissandoComposesWithSlurOnSameNotes() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter,
|
|
spanners: [.beginGlissando(Glissando()), .beginSlur])
|
|
Note(.e, 5, dur: .quarter,
|
|
spanners: [.endGlissando, .endSlur])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Both slur and glissando start must be present.
|
|
#expect(xml.contains("<slur number=\"1\" type=\"start\"/>"))
|
|
#expect(xml.contains("<glissando type=\"start\" line-type=\"wavy\" number=\"1\"/>"))
|
|
// Both slur and glissando stop must be present.
|
|
#expect(xml.contains("<slur number=\"1\" type=\"stop\"/>"))
|
|
#expect(xml.contains("<glissando type=\"stop\" number=\"1\"/>"))
|
|
}
|
|
|
|
// MARK: - flatten unaffected
|
|
|
|
@Test func glissandoDoesNotAffectFlatten() {
|
|
// flatten() produces the same ScoreEvent list regardless of glissando spans
|
|
// (glissando is notation-only — no effect on beat positions or durations).
|
|
let partWithGlissando: ScorePart = Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando())])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
let partPlain: ScorePart = Part("p") {
|
|
Note(.c, 5, dur: .quarter)
|
|
Note(.e, 5, dur: .quarter)
|
|
}
|
|
let eventsWithGlissando: [ScoreEvent] = partWithGlissando.flatten()
|
|
let eventsPlain: [ScoreEvent] = partPlain.flatten()
|
|
#expect(eventsWithGlissando.count == eventsPlain.count)
|
|
for (a, b): (ScoreEvent, ScoreEvent) in zip(eventsWithGlissando, eventsPlain) {
|
|
#expect(a.beat == b.beat)
|
|
#expect(a.duration == b.duration)
|
|
}
|
|
}
|
|
|
|
// MARK: - GlissandoKind
|
|
|
|
@Test func glissandoKindMusicXMLElementNames() {
|
|
#expect(GlissandoKind.glissando.musicXMLElementName == "glissando")
|
|
#expect(GlissandoKind.slide.musicXMLElementName == "slide")
|
|
}
|
|
|
|
// MARK: - GlissandoLineType
|
|
|
|
@Test func glissandoLineTypeMusicXMLValues() {
|
|
#expect(GlissandoLineType.solid.musicXMLValue == "solid")
|
|
#expect(GlissandoLineType.dashed.musicXMLValue == "dashed")
|
|
#expect(GlissandoLineType.dotted.musicXMLValue == "dotted")
|
|
#expect(GlissandoLineType.wavy.musicXMLValue == "wavy")
|
|
}
|
|
|
|
// MARK: - Glissando defaults
|
|
|
|
@Test func glissandoDefaultValues() {
|
|
let g: Glissando = Glissando()
|
|
#expect(g.kind == .glissando)
|
|
#expect(g.lineType == .wavy)
|
|
#expect(g.text == nil)
|
|
}
|
|
|
|
// MARK: - Multiple glissandos in sequence
|
|
|
|
@Test func multipleGlissandosInSequenceEmitCorrectly() {
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando())])
|
|
Note(.e, 5, dur: .quarter, spanners: [.endGlissando])
|
|
// Second glissando span
|
|
Note(.g, 5, dur: .quarter, spanners: [.beginGlissando(Glissando(kind: .slide))])
|
|
Note(.b, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// Two glissando starts: one wavy, one slide.
|
|
let glissandoStarts: Int = xml.components(separatedBy: "type=\"start\"").count - 1
|
|
// There should be 2 starts (one glissando, one slide).
|
|
#expect(glissandoStarts >= 2)
|
|
// First glissando stop uses <glissando>, second uses <slide>.
|
|
#expect(xml.contains("<glissando type=\"stop\" number=\"1\"/>"))
|
|
#expect(xml.contains("<slide type=\"stop\" number=\"1\"/>"))
|
|
}
|
|
|
|
@Test func glissandoStopSurvivesMultiVoiceBarlineSplit() {
|
|
// The stop note crosses a barline on the multi-voice encoder path, which
|
|
// reconstructs a leftover continuation atom. That reconstruction must
|
|
// carry the glissando-stop forward (regression: it previously reset to
|
|
// default, dropping the <glissando type="stop"/>).
|
|
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
|
Part("p") {
|
|
Voice(1) {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando())])
|
|
// Stop note is a whole note starting on beat 2 → crosses the
|
|
// barline (splits at the bar end).
|
|
Note(.g, 5, dur: .whole, spanners: [.endGlissando])
|
|
}
|
|
Voice(2) {
|
|
Note(.c, 4, dur: .whole)
|
|
Note(.c, 4, dur: .whole)
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(xml.contains("<glissando type=\"start\""))
|
|
#expect(xml.contains("<glissando type=\"stop\" number=\"1\"/>"),
|
|
"glissando stop must survive the multi-voice barline split")
|
|
}
|
|
|
|
@Test func singleAtomGlissandoDoesNotBreakNextSpanInVoice() {
|
|
// A degenerate single-note glissando (begin+end on one note) must clear
|
|
// the per-voice opener so a following real glissando span is not
|
|
// mis-flagged as a double-open and dropped.
|
|
let score: Score = Score(bpm: 120) {
|
|
Part("p") {
|
|
Note(.c, 5, dur: .quarter, spanners: [.beginGlissando(Glissando()), .endGlissando])
|
|
Note(.e, 5, dur: .quarter, spanners: [.beginGlissando(Glissando(kind: .slide))])
|
|
Note(.g, 5, dur: .quarter, spanners: [.endGlissando])
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
// The second (real, two-note) slide span must emit its start+stop.
|
|
#expect(xml.contains("<slide type=\"start\""),
|
|
"the span after a single-atom glissando must still open")
|
|
#expect(xml.contains("<slide type=\"stop\" number=\"1\"/>"),
|
|
"the span after a single-atom glissando must still close")
|
|
}
|
|
}
|