668 lines
27 KiB
Swift
668 lines
27 KiB
Swift
// RepeatStructureTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("RepeatStructure and Ending")
|
||
struct RepeatStructureTests {
|
||
|
||
// MARK: - 1. Model: Ending init preconditions and value semantics
|
||
|
||
@Test func endingValidInit() {
|
||
let ending: Ending = Ending(number: 1, startMeasure: 3, stopMeasure: 5)
|
||
#expect(ending.number == 1)
|
||
#expect(ending.startMeasure == 3)
|
||
#expect(ending.stopMeasure == 5)
|
||
}
|
||
|
||
@Test func endingNumberMustBeAtLeastOne() {
|
||
// Precondition: number >= 1.
|
||
// We verify valid boundary values; the precondition traps on 0 (not tested in-process).
|
||
let e1: Ending = Ending(number: 1, startMeasure: 1, stopMeasure: 1)
|
||
#expect(e1.number == 1)
|
||
let e2: Ending = Ending(number: 2, startMeasure: 2, stopMeasure: 4)
|
||
#expect(e2.number == 2)
|
||
}
|
||
|
||
@Test func endingSingleMeasureSpan() {
|
||
// startMeasure == stopMeasure is valid (single-measure ending).
|
||
let ending: Ending = Ending(number: 1, startMeasure: 4, stopMeasure: 4)
|
||
#expect(ending.startMeasure == ending.stopMeasure)
|
||
}
|
||
|
||
@Test func endingHashableEquality() {
|
||
let a: Ending = Ending(number: 1, startMeasure: 3, stopMeasure: 3)
|
||
let b: Ending = Ending(number: 1, startMeasure: 3, stopMeasure: 3)
|
||
let c: Ending = Ending(number: 2, startMeasure: 4, stopMeasure: 4)
|
||
#expect(a == b)
|
||
#expect(a != c)
|
||
var set: Set<Ending> = Set<Ending>()
|
||
set.insert(a)
|
||
set.insert(b)
|
||
#expect(set.count == 1)
|
||
set.insert(c)
|
||
#expect(set.count == 2)
|
||
}
|
||
|
||
// MARK: - 2. Model: RepeatStructure isEmpty / .none
|
||
|
||
@Test func repeatStructureNoneIsEmpty() {
|
||
#expect(RepeatStructure.none.isEmpty)
|
||
#expect(RepeatStructure.none.forwardRepeatMeasures.isEmpty)
|
||
#expect(RepeatStructure.none.backwardRepeatMeasures.isEmpty)
|
||
#expect(RepeatStructure.none.endings.isEmpty)
|
||
}
|
||
|
||
@Test func repeatStructureDefaultInit() {
|
||
let rs: RepeatStructure = RepeatStructure()
|
||
#expect(rs.isEmpty)
|
||
}
|
||
|
||
@Test func repeatStructureNonEmptyForward() {
|
||
let rs: RepeatStructure = RepeatStructure(forwardRepeatMeasures: [1])
|
||
#expect(!rs.isEmpty)
|
||
}
|
||
|
||
@Test func repeatStructureNonEmptyBackward() {
|
||
let rs: RepeatStructure = RepeatStructure(backwardRepeatMeasures: [4])
|
||
#expect(!rs.isEmpty)
|
||
}
|
||
|
||
@Test func repeatStructureNonEmptyEndings() {
|
||
let rs: RepeatStructure = RepeatStructure(
|
||
endings: [Ending(number: 1, startMeasure: 3, stopMeasure: 3)]
|
||
)
|
||
#expect(!rs.isEmpty)
|
||
}
|
||
|
||
@Test func repeatStructureHashableEquality() {
|
||
let a: RepeatStructure = RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [4],
|
||
endings: [Ending(number: 1, startMeasure: 3, stopMeasure: 3)]
|
||
)
|
||
let b: RepeatStructure = RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [4],
|
||
endings: [Ending(number: 1, startMeasure: 3, stopMeasure: 3)]
|
||
)
|
||
#expect(a == b)
|
||
}
|
||
|
||
// MARK: - 3. Score carries repeats
|
||
|
||
@Test func scoreDefaultRepeatsIsNone() {
|
||
let score: Score = Score(bpm: 120) {
|
||
Part("p") { Note(.c, 4, dur: .quarter) }
|
||
}
|
||
#expect(score.repeats.isEmpty)
|
||
#expect(score.repeats == RepeatStructure.none)
|
||
}
|
||
|
||
@Test func scorePassthroughRepeats() {
|
||
let rs: RepeatStructure = RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [4],
|
||
endings: [
|
||
Ending(number: 1, startMeasure: 3, stopMeasure: 3),
|
||
Ending(number: 2, startMeasure: 4, stopMeasure: 4)
|
||
]
|
||
)
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: rs
|
||
) {
|
||
Part("p") { Note(.c, 4, dur: .quarter) }
|
||
}
|
||
#expect(score.repeats == rs)
|
||
}
|
||
|
||
// MARK: - 4. Encoder: .none emits zero <barline> elements
|
||
|
||
@Test func noRepeatStructureEmitsNoBarlineElements() {
|
||
// A score with .none repeats (the default) must produce byte-identical
|
||
// output with respect to barlines — specifically, ZERO <barline> elements.
|
||
let score: Score = Score(bpm: 120) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .quarter)
|
||
Note(.d, 4, dur: .quarter)
|
||
Note(.e, 4, dur: .quarter)
|
||
Note(.f, 4, dur: .quarter)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
#expect(!xml.contains("<barline"),
|
||
"score with no RepeatStructure must emit zero <barline> elements; got:\n\(xml)")
|
||
}
|
||
|
||
// MARK: - 5. Encoder: forward repeat barline
|
||
|
||
@Test func forwardRepeatAtMeasureOneEmitsLeftBarline() {
|
||
// 4/4 score, 2 measures (8 beats); forward repeat at measure 1.
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(forwardRepeatMeasures: [1])
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
#expect(xml.contains("<barline location=\"left\">"),
|
||
"forward repeat must emit a left barline; got:\n\(xml)")
|
||
#expect(xml.contains("<repeat direction=\"forward\"/>"),
|
||
"forward repeat must emit <repeat direction=\"forward\"/>; got:\n\(xml)")
|
||
#expect(xml.contains("<bar-style>heavy-light</bar-style>"),
|
||
"forward repeat left barline must carry heavy-light bar-style; got:\n\(xml)")
|
||
// No right barline for measure 1 in this test.
|
||
#expect(!xml.contains("<repeat direction=\"backward\"/>"),
|
||
"no backward repeat should be present; got:\n\(xml)")
|
||
}
|
||
|
||
@Test func forwardRepeatAtMeasureTwoEmitsCorrectMeasure() {
|
||
// Forward repeat on measure 2 must NOT put the barline in measure 1.
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(forwardRepeatMeasures: [2])
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole) // measure 1
|
||
Note(.d, 4, dur: .whole) // measure 2
|
||
Note(.e, 4, dur: .whole) // measure 3
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
#expect(xml.contains("<barline location=\"left\">"),
|
||
"forward repeat at measure 2 must emit a left barline; got:\n\(xml)")
|
||
#expect(xml.contains("<repeat direction=\"forward\"/>"),
|
||
"must emit forward repeat; got:\n\(xml)")
|
||
}
|
||
|
||
// MARK: - 6. Encoder: backward repeat barline
|
||
|
||
@Test func backwardRepeatEmitsRightBarline() {
|
||
// 4/4, 4 measures; backward repeat at measure 4.
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(backwardRepeatMeasures: [4])
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
#expect(xml.contains("<barline location=\"right\">"),
|
||
"backward repeat must emit a right barline; got:\n\(xml)")
|
||
#expect(xml.contains("<repeat direction=\"backward\"/>"),
|
||
"backward repeat must emit <repeat direction=\"backward\"/>; got:\n\(xml)")
|
||
#expect(xml.contains("<bar-style>light-heavy</bar-style>"),
|
||
"backward repeat right barline must carry light-heavy bar-style; got:\n\(xml)")
|
||
}
|
||
|
||
// MARK: - 7. Encoder: ending brackets
|
||
|
||
@Test func endingStartEmitsStartTypeOnLeftBarline() {
|
||
// 1st ending starts at measure 3 — measure 3's left barline carries
|
||
// <ending number="1" type="start"/>.
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
endings: [Ending(number: 1, startMeasure: 3, stopMeasure: 3)]
|
||
)
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
#expect(xml.contains("<ending number=\"1\" type=\"start\"/>"),
|
||
"1st ending must emit <ending number=\"1\" type=\"start\"/>; got:\n\(xml)")
|
||
}
|
||
|
||
@Test func endingStopEmitsStopTypeOnRightBarline() {
|
||
// 1st ending stops at measure 3 — measure 3's right barline carries
|
||
// <ending number="1" type="stop"/>.
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
endings: [Ending(number: 1, startMeasure: 3, stopMeasure: 3)]
|
||
)
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
#expect(xml.contains("<ending number=\"1\" type=\"stop\"/>"),
|
||
"1st ending must emit <ending number=\"1\" type=\"stop\"/>; got:\n\(xml)")
|
||
}
|
||
|
||
@Test func multiMeasureEndingEmitsContinueOnInteriorMeasures() {
|
||
// A 3-measure ending (bars 2–4): bar 2 left = start, bars 3 and 4 left
|
||
// = continue (so the volta bracket renders unbroken), bar 4 right = stop.
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
endings: [Ending(number: 1, startMeasure: 2, stopMeasure: 4)]
|
||
)
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
#expect(xml.contains("<ending number=\"1\" type=\"start\"/>"),
|
||
"bar 2 must emit ending start; got:\n\(xml)")
|
||
#expect(xml.contains("<ending number=\"1\" type=\"continue\"/>"),
|
||
"interior bars must emit ending continue; got:\n\(xml)")
|
||
#expect(xml.contains("<ending number=\"1\" type=\"stop\"/>"),
|
||
"bar 4 must emit ending stop; got:\n\(xml)")
|
||
// Two interior measures (3 and 4) each carry a continue → 2 occurrences.
|
||
let continueCount: Int = xml.components(
|
||
separatedBy: "<ending number=\"1\" type=\"continue\"/>"
|
||
).count - 1
|
||
#expect(continueCount == 2,
|
||
"expected 2 continue brackets (bars 3 + 4); got \(continueCount);\n\(xml)")
|
||
}
|
||
|
||
@Test func fullRepeatWithTwoEndingsEmitsAllBarlines() {
|
||
// Forward repeat at bar 1, backward repeat at bar 4.
|
||
// 1st ending: bar 3, 2nd ending: bar 4.
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [4],
|
||
endings: [
|
||
Ending(number: 1, startMeasure: 3, stopMeasure: 3),
|
||
Ending(number: 2, startMeasure: 4, stopMeasure: 4)
|
||
]
|
||
)
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
// Forward repeat.
|
||
#expect(xml.contains("<repeat direction=\"forward\"/>"),
|
||
"must have forward repeat; got:\n\(xml)")
|
||
// Backward repeat.
|
||
#expect(xml.contains("<repeat direction=\"backward\"/>"),
|
||
"must have backward repeat; got:\n\(xml)")
|
||
// 1st ending start and stop.
|
||
#expect(xml.contains("<ending number=\"1\" type=\"start\"/>"),
|
||
"must have 1st ending start; got:\n\(xml)")
|
||
#expect(xml.contains("<ending number=\"1\" type=\"stop\"/>"),
|
||
"must have 1st ending stop; got:\n\(xml)")
|
||
// 2nd ending start and stop.
|
||
#expect(xml.contains("<ending number=\"2\" type=\"start\"/>"),
|
||
"must have 2nd ending start; got:\n\(xml)")
|
||
#expect(xml.contains("<ending number=\"2\" type=\"stop\"/>"),
|
||
"must have 2nd ending stop; got:\n\(xml)")
|
||
}
|
||
|
||
// MARK: - 8. Encoder: barlines appear in EVERY part of a multi-part score
|
||
|
||
@Test func barlinesEmittedInEveryPart() {
|
||
// A two-part score with forward + backward repeats: both parts must carry
|
||
// the barline XML. Count occurrences of the forward repeat element.
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [2]
|
||
)
|
||
) {
|
||
Part("soprano") {
|
||
Note(.c, 5, dur: .whole)
|
||
Note(.d, 5, dur: .whole)
|
||
}
|
||
Part("alto") {
|
||
Note(.g, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
// Count occurrences — must appear once per part (2 parts).
|
||
let forwardCount: Int = xml.components(
|
||
separatedBy: "<repeat direction=\"forward\"/>"
|
||
).count - 1
|
||
#expect(forwardCount == 2,
|
||
"forward repeat must appear once per part (2); got \(forwardCount);\n\(xml)")
|
||
let backwardCount: Int = xml.components(
|
||
separatedBy: "<repeat direction=\"backward\"/>"
|
||
).count - 1
|
||
#expect(backwardCount == 2,
|
||
"backward repeat must appear once per part (2); got \(backwardCount);\n\(xml)")
|
||
}
|
||
|
||
// MARK: - 9. Encoder: multi-voice — left/right barline emitted ONCE per measure
|
||
|
||
@Test func multiVoicePartEmitsBarlineOncePerMeasure() {
|
||
// A two-voice, two-measure part with a forward repeat at measure 1.
|
||
// The left barline at measure 1 must appear exactly ONCE (not once per voice).
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(forwardRepeatMeasures: [1])
|
||
) {
|
||
Part("piano") {
|
||
Voice(1) {
|
||
Note(.e, 5, dur: .whole)
|
||
Note(.f, 5, dur: .whole)
|
||
}
|
||
Voice(2) {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
}
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
// Left barline at measure 1 must appear exactly once.
|
||
let leftBarlineCount: Int = xml.components(
|
||
separatedBy: "<barline location=\"left\">"
|
||
).count - 1
|
||
#expect(leftBarlineCount == 1,
|
||
"multi-voice part must emit the left barline exactly once; got \(leftBarlineCount);\n\(xml)")
|
||
let forwardCount: Int = xml.components(
|
||
separatedBy: "<repeat direction=\"forward\"/>"
|
||
).count - 1
|
||
#expect(forwardCount == 1,
|
||
"forward repeat must appear exactly once in a single-part multi-voice score; got \(forwardCount);\n\(xml)")
|
||
}
|
||
|
||
// MARK: - 10. Parser: barline-less document yields .none
|
||
|
||
@Test func parserProducesNoneForBarlinelessDocument() throws {
|
||
// A plain score without any <barline> elements round-trips with
|
||
// repeats == .none.
|
||
let score: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
}
|
||
}
|
||
let data: Data = score.toMusicXMLData()
|
||
let parsed: Score = try Score.fromMusicXML(data: data)
|
||
#expect(parsed.repeats.isEmpty,
|
||
"a barline-less document must parse to RepeatStructure.none; got \(parsed.repeats)")
|
||
}
|
||
|
||
// MARK: - 11. Parser: forward and backward repeats parsed correctly
|
||
|
||
@Test func parserReadsForwardRepeatMeasure() throws {
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(forwardRepeatMeasures: [1])
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
}
|
||
}
|
||
let data: Data = score.toMusicXMLData()
|
||
let parsed: Score = try Score.fromMusicXML(data: data)
|
||
#expect(parsed.repeats.forwardRepeatMeasures.contains(1),
|
||
"measure 1 must be in forwardRepeatMeasures after parse; got \(parsed.repeats)")
|
||
}
|
||
|
||
@Test func parserReadsBackwardRepeatMeasure() throws {
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(backwardRepeatMeasures: [4])
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let data: Data = score.toMusicXMLData()
|
||
let parsed: Score = try Score.fromMusicXML(data: data)
|
||
#expect(parsed.repeats.backwardRepeatMeasures.contains(4),
|
||
"measure 4 must be in backwardRepeatMeasures after parse; got \(parsed.repeats)")
|
||
}
|
||
|
||
@Test func parserReadsEndingPairs() throws {
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [4],
|
||
endings: [
|
||
Ending(number: 1, startMeasure: 3, stopMeasure: 3),
|
||
Ending(number: 2, startMeasure: 4, stopMeasure: 4)
|
||
]
|
||
)
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let data: Data = score.toMusicXMLData()
|
||
let parsed: Score = try Score.fromMusicXML(data: data)
|
||
let endings: [Ending] = parsed.repeats.endings
|
||
#expect(endings.count == 2,
|
||
"two endings must be parsed; got \(endings.count): \(endings)")
|
||
let e1: Ending? = endings.first { $0.number == 1 }
|
||
let e2: Ending? = endings.first { $0.number == 2 }
|
||
#expect(e1?.startMeasure == 3, "1st ending must start at measure 3; got \(String(describing: e1))")
|
||
#expect(e1?.stopMeasure == 3, "1st ending must stop at measure 3; got \(String(describing: e1))")
|
||
#expect(e2?.startMeasure == 4, "2nd ending must start at measure 4; got \(String(describing: e2))")
|
||
#expect(e2?.stopMeasure == 4, "2nd ending must stop at measure 4; got \(String(describing: e2))")
|
||
}
|
||
|
||
// MARK: - 12. Round-trip: full RepeatStructure encodes then parses back identically
|
||
|
||
@Test func roundTripRepeatStructure() throws {
|
||
let originalRepeats: RepeatStructure = RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [4],
|
||
endings: [
|
||
Ending(number: 1, startMeasure: 3, stopMeasure: 3),
|
||
Ending(number: 2, startMeasure: 4, stopMeasure: 4)
|
||
]
|
||
)
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: originalRepeats
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let data: Data = score.toMusicXMLData()
|
||
let parsed: Score = try Score.fromMusicXML(data: data)
|
||
#expect(parsed.repeats == originalRepeats,
|
||
"round-trip must recover the exact RepeatStructure; got \(parsed.repeats)")
|
||
}
|
||
|
||
// MARK: - 13. flatten event count unchanged with vs without repeats
|
||
|
||
@Test func flattenEventCountUnchangedWithRepeats() {
|
||
let withoutRepeats: Score = Score(bpm: 120, timeSignature: TimeSignature(4, 4)) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let withRepeats: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [4],
|
||
endings: [
|
||
Ending(number: 1, startMeasure: 3, stopMeasure: 3),
|
||
Ending(number: 2, startMeasure: 4, stopMeasure: 4)
|
||
]
|
||
)
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let eventsWithout: [ScoreEvent] = withoutRepeats.parts[0].flatten()
|
||
let eventsWith: [ScoreEvent] = withRepeats.parts[0].flatten()
|
||
#expect(eventsWithout.count == eventsWith.count,
|
||
"flatten event count must be unchanged by RepeatStructure; got \(eventsWithout.count) vs \(eventsWith.count)")
|
||
}
|
||
|
||
// MARK: - 14. Round-trip: flatten event count preserved across encode/parse
|
||
|
||
@Test func roundTripFlattenEventCountPreserved() throws {
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [4],
|
||
endings: [
|
||
Ending(number: 1, startMeasure: 3, stopMeasure: 3),
|
||
Ending(number: 2, startMeasure: 4, stopMeasure: 4)
|
||
]
|
||
)
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let originalCount: Int = score.allEvents.count
|
||
let data: Data = score.toMusicXMLData()
|
||
let parsed: Score = try Score.fromMusicXML(data: data)
|
||
let parsedCount: Int = parsed.allEvents.count
|
||
#expect(originalCount == parsedCount,
|
||
"round-trip must preserve flatten event count; got \(originalCount) → \(parsedCount)")
|
||
}
|
||
|
||
// MARK: - 15. Multi-part round-trip: barlines appear in all parts exactly once per measure
|
||
|
||
@Test func multiPartRoundTripBarlinesInAllParts() throws {
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [2]
|
||
)
|
||
) {
|
||
Part("part1") {
|
||
Note(.c, 5, dur: .whole)
|
||
Note(.d, 5, dur: .whole)
|
||
}
|
||
Part("part2") {
|
||
Note(.g, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
Part("part3") {
|
||
Note(.c, 3, dur: .whole)
|
||
Note(.d, 3, dur: .whole)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
// Three parts — each must have the forward repeat once.
|
||
let forwardCount: Int = xml.components(
|
||
separatedBy: "<repeat direction=\"forward\"/>"
|
||
).count - 1
|
||
#expect(forwardCount == 3,
|
||
"three-part score must emit forward repeat once per part; got \(forwardCount)")
|
||
// The parser reads only from the first part — round-trip correctness.
|
||
let data: Data = Data(xml.utf8)
|
||
let parsed: Score = try Score.fromMusicXML(data: data)
|
||
#expect(parsed.repeats.forwardRepeatMeasures.contains(1),
|
||
"parsed score must have forward repeat at measure 1; got \(parsed.repeats)")
|
||
#expect(parsed.repeats.backwardRepeatMeasures.contains(2),
|
||
"parsed score must have backward repeat at measure 2; got \(parsed.repeats)")
|
||
}
|
||
|
||
@Test func finalEndingWithoutBackwardRepeatEmitsDiscontinue() throws {
|
||
// Backward repeat sits at the END of the 1st ending (bar 3). The 2nd
|
||
// ending (bar 4) continues forward with no repeat on its barline, so it
|
||
// must close OPEN — MusicXML `type="discontinue"` — while the 1st ending
|
||
// (which jumps back) closes with a downward hook — `type="stop"`.
|
||
let score: Score = Score(
|
||
bpm: 120,
|
||
timeSignature: TimeSignature(4, 4),
|
||
repeats: RepeatStructure(
|
||
forwardRepeatMeasures: [1],
|
||
backwardRepeatMeasures: [3],
|
||
endings: [
|
||
Ending(number: 1, startMeasure: 3, stopMeasure: 3),
|
||
Ending(number: 2, startMeasure: 4, stopMeasure: 4)
|
||
]
|
||
)
|
||
) {
|
||
Part("p") {
|
||
Note(.c, 4, dur: .whole)
|
||
Note(.d, 4, dur: .whole)
|
||
Note(.e, 4, dur: .whole)
|
||
Note(.f, 4, dur: .whole)
|
||
}
|
||
}
|
||
let xml: String = score.toMusicXMLString()
|
||
#expect(xml.contains("<ending number=\"1\" type=\"stop\"/>"),
|
||
"1st ending (with backward repeat) closes with a hook; got:\n\(xml)")
|
||
#expect(xml.contains("<ending number=\"2\" type=\"discontinue\"/>"),
|
||
"final ending continues forward and must be open (discontinue); got:\n\(xml)")
|
||
#expect(!xml.contains("<ending number=\"2\" type=\"stop\"/>"),
|
||
"final ending must not close with a hook; got:\n\(xml)")
|
||
// Round-trip: the parser accepts `discontinue` as an ending close, so the
|
||
// 2nd ending survives and re-encodes as discontinue again.
|
||
let parsed: Score = try Score.fromMusicXML(data: score.toMusicXMLData())
|
||
let reEncoded: String = parsed.toMusicXMLString()
|
||
#expect(reEncoded.contains("<ending number=\"2\" type=\"discontinue\"/>"),
|
||
"discontinue ending must round-trip; got:\n\(reEncoded)")
|
||
}
|
||
}
|