405 lines
15 KiB
Swift
405 lines
15 KiB
Swift
// PageLayoutTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable import LyrebirdSynth
|
|
|
|
@MainActor
|
|
@Suite("PageLayout")
|
|
struct PageLayoutTests {
|
|
|
|
// MARK: - Helpers
|
|
|
|
/// Build a minimal one-part score with optional pageLayout.
|
|
private func makeScore(pageLayout: PageLayout? = nil) -> Score {
|
|
Score(
|
|
bpm: 120,
|
|
parts: [
|
|
ScorePart(name: "Piano", elements: [Note(.c, 4, dur: .quarter)])
|
|
],
|
|
pageLayout: pageLayout
|
|
)
|
|
}
|
|
|
|
/// Parse the first occurrence of `tag` content from `xml`.
|
|
/// Returns the text between `<tag>` and `</tag>`, or nil when absent.
|
|
private func extractElement(tag: String, from xml: String) -> String? {
|
|
let open: String = "<\(tag)>"
|
|
let close: String = "</\(tag)>"
|
|
guard let startIdx: String.Index = xml.range(of: open)?.upperBound,
|
|
let endIdx: String.Index = xml.range(of: close)?.lowerBound else {
|
|
return nil
|
|
}
|
|
return String(xml[startIdx..<endIdx])
|
|
}
|
|
|
|
/// Return true when `xml` contains `<defaults>`.
|
|
private func hasDefaults(_ xml: String) -> Bool {
|
|
xml.contains("<defaults>")
|
|
}
|
|
|
|
// MARK: - nil pageLayout → no <defaults>
|
|
|
|
@Test("nil pageLayout: output contains no <defaults>")
|
|
func nilPageLayoutProducesNoDefaults() {
|
|
let withNil: Score = makeScore(pageLayout: nil)
|
|
let withoutParam: Score = makeScore()
|
|
let xmlNil: String = withNil.toMusicXMLString()
|
|
let xmlWithout: String = withoutParam.toMusicXMLString()
|
|
#expect(!hasDefaults(xmlNil))
|
|
#expect(!hasDefaults(xmlWithout))
|
|
// Byte-identical: both paths produce the same output.
|
|
#expect(xmlNil == xmlWithout)
|
|
}
|
|
|
|
// MARK: - Letter portrait
|
|
|
|
@Test("Letter portrait: <defaults> present, width < height")
|
|
func letterPortraitDimensions() {
|
|
let layout: PageLayout = PageLayout(size: .letter, orientation: .portrait)
|
|
let score: Score = makeScore(pageLayout: layout)
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
#expect(hasDefaults(xml))
|
|
|
|
// Under default scaling (7.0 mm = 40.0 tenths), tenthsPerMM = 40/7.
|
|
// Letter portrait: width = 215.9 * (40/7), height = 279.4 * (40/7).
|
|
let tpm: Double = 40.0 / 7.0
|
|
let expectedWidth: Double = 215.9 * tpm
|
|
let expectedHeight: Double = 279.4 * tpm
|
|
|
|
let widthStr: String? = extractElement(tag: "page-width", from: xml)
|
|
let heightStr: String? = extractElement(tag: "page-height", from: xml)
|
|
#expect(widthStr != nil)
|
|
#expect(heightStr != nil)
|
|
|
|
if let w: Double = widthStr.flatMap(Double.init),
|
|
let h: Double = heightStr.flatMap(Double.init) {
|
|
#expect(abs(w - expectedWidth) < 0.001)
|
|
#expect(abs(h - expectedHeight) < 0.001)
|
|
// Portrait: width < height.
|
|
#expect(w < h)
|
|
}
|
|
}
|
|
|
|
// MARK: - Letter landscape
|
|
|
|
@Test("Letter landscape: width > height (orientation swap)")
|
|
func letterLandscapeSwapsWidthHeight() {
|
|
let layout: PageLayout = PageLayout(size: .letter, orientation: .landscape)
|
|
let score: Score = makeScore(pageLayout: layout)
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
#expect(hasDefaults(xml))
|
|
|
|
let tpm: Double = 40.0 / 7.0
|
|
// Landscape: physical width = portrait height, physical height = portrait width.
|
|
let expectedWidth: Double = 279.4 * tpm
|
|
let expectedHeight: Double = 215.9 * tpm
|
|
|
|
let widthStr: String? = extractElement(tag: "page-width", from: xml)
|
|
let heightStr: String? = extractElement(tag: "page-height", from: xml)
|
|
|
|
if let w: Double = widthStr.flatMap(Double.init),
|
|
let h: Double = heightStr.flatMap(Double.init) {
|
|
#expect(abs(w - expectedWidth) < 0.001)
|
|
#expect(abs(h - expectedHeight) < 0.001)
|
|
// Landscape: width > height.
|
|
#expect(w > h)
|
|
}
|
|
}
|
|
|
|
// MARK: - A4 landscape
|
|
|
|
@Test("A4 landscape: correct tenths under default scaling")
|
|
func a4LandscapeTenths() {
|
|
let layout: PageLayout = PageLayout(size: .a4, orientation: .landscape)
|
|
let score: Score = makeScore(pageLayout: layout)
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
#expect(hasDefaults(xml))
|
|
|
|
let tpm: Double = 40.0 / 7.0
|
|
// A4: portrait width = 210 mm, height = 297 mm.
|
|
// Landscape: page-width = 297 * tpm, page-height = 210 * tpm.
|
|
let expectedWidth: Double = 297.0 * tpm // ≈ 1697.143
|
|
let expectedHeight: Double = 210.0 * tpm // = 1200.0
|
|
|
|
let widthStr: String? = extractElement(tag: "page-width", from: xml)
|
|
let heightStr: String? = extractElement(tag: "page-height", from: xml)
|
|
|
|
if let w: Double = widthStr.flatMap(Double.init),
|
|
let h: Double = heightStr.flatMap(Double.init) {
|
|
#expect(abs(w - expectedWidth) < 0.001)
|
|
#expect(abs(h - expectedHeight) < 0.001)
|
|
}
|
|
}
|
|
|
|
// MARK: - Custom page size
|
|
|
|
@Test("custom(widthMM:heightMM:) round-trips to correct tenths")
|
|
func customPageSizeTenths() {
|
|
let customWidthMM: Double = 100.0
|
|
let customHeightMM: Double = 150.0
|
|
let layout: PageLayout = PageLayout(
|
|
size: .custom(widthMillimeters: customWidthMM, heightMillimeters: customHeightMM),
|
|
orientation: .portrait
|
|
)
|
|
let score: Score = makeScore(pageLayout: layout)
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
let tpm: Double = 40.0 / 7.0
|
|
let expectedWidth: Double = customWidthMM * tpm
|
|
let expectedHeight: Double = customHeightMM * tpm
|
|
|
|
let widthStr: String? = extractElement(tag: "page-width", from: xml)
|
|
let heightStr: String? = extractElement(tag: "page-height", from: xml)
|
|
|
|
if let w: Double = widthStr.flatMap(Double.init),
|
|
let h: Double = heightStr.flatMap(Double.init) {
|
|
#expect(abs(w - expectedWidth) < 0.001)
|
|
#expect(abs(h - expectedHeight) < 0.001)
|
|
}
|
|
}
|
|
|
|
// MARK: - Symmetric margins → type="both"
|
|
|
|
@Test("symmetric margins emit single type=\"both\" page-margins block")
|
|
func symmetricMarginsEmitBoth() {
|
|
let layout: PageLayout = PageLayout(
|
|
size: .a4,
|
|
orientation: .portrait,
|
|
margins: PageLayout.Margins(uniformMillimeters: 20.0)
|
|
)
|
|
let score: Score = makeScore(pageLayout: layout)
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
#expect(xml.contains("type=\"both\""))
|
|
#expect(!xml.contains("type=\"odd\""))
|
|
#expect(!xml.contains("type=\"even\""))
|
|
}
|
|
|
|
// MARK: - Asymmetric odd/even → two page-margins blocks
|
|
|
|
@Test("asymmetric odd/even margins emit two page-margins blocks")
|
|
func asymmetricMarginsEmitOddAndEven() {
|
|
let oddMargins: PageLayout.Margins = PageLayout.Margins(
|
|
leftMillimeters: 30.0,
|
|
rightMillimeters: 15.0,
|
|
topMillimeters: 20.0,
|
|
bottomMillimeters: 20.0
|
|
)
|
|
let evenMargins: PageLayout.Margins = PageLayout.Margins(
|
|
leftMillimeters: 15.0,
|
|
rightMillimeters: 30.0,
|
|
topMillimeters: 20.0,
|
|
bottomMillimeters: 20.0
|
|
)
|
|
let layout: PageLayout = PageLayout(
|
|
size: .a4,
|
|
orientation: .portrait,
|
|
oddMargins: oddMargins,
|
|
evenMargins: evenMargins
|
|
)
|
|
let score: Score = makeScore(pageLayout: layout)
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
#expect(xml.contains("type=\"odd\""))
|
|
#expect(xml.contains("type=\"even\""))
|
|
#expect(!xml.contains("type=\"both\""))
|
|
|
|
// Verify the actual margin tenths, not just the block labels. Under the
|
|
// default scaling (40 tenths / 7 mm), a 30 mm margin → 30 * 40/7 tenths.
|
|
let tenthsPerMM: Double = 40.0 / 7.0
|
|
let expectedOddLeft: Double = 30.0 * tenthsPerMM
|
|
let expectedEvenRight: Double = 30.0 * tenthsPerMM
|
|
// The encoder emits the odd block first, then the even block.
|
|
let oddLeft: Double? = Self.firstMarginValue(in: xml, after: "type=\"odd\"", tag: "left-margin")
|
|
let evenRight: Double? = Self.firstMarginValue(in: xml, after: "type=\"even\"", tag: "right-margin")
|
|
#expect(oddLeft != nil && abs((oddLeft ?? 0) - expectedOddLeft) < 0.01)
|
|
#expect(evenRight != nil && abs((evenRight ?? 0) - expectedEvenRight) < 0.01)
|
|
}
|
|
|
|
/// Parse the first `<tag>VALUE</tag>` appearing after `marker` in `xml`.
|
|
private static func firstMarginValue(in xml: String, after marker: String, tag: String) -> Double? {
|
|
guard let markerRange: Range<String.Index> = xml.range(of: marker) else { return nil }
|
|
let tail: Substring = xml[markerRange.upperBound...]
|
|
let open: String = "<\(tag)>"
|
|
let close: String = "</\(tag)>"
|
|
guard let openRange: Range<Substring.Index> = tail.range(of: open),
|
|
let closeRange: Range<Substring.Index> = tail.range(of: close),
|
|
openRange.upperBound < closeRange.lowerBound
|
|
else { return nil }
|
|
return Double(tail[openRange.upperBound..<closeRange.lowerBound])
|
|
}
|
|
|
|
// MARK: - Scaling override
|
|
|
|
@Test("scaling override changes <millimeters>/<tenths> and scales page dims")
|
|
func scalingOverride() {
|
|
let customScaling: PageLayout.Scaling = PageLayout.Scaling(
|
|
millimeters: 5.0,
|
|
tenths: 30.0
|
|
)
|
|
let layout: PageLayout = PageLayout(
|
|
size: .a4,
|
|
orientation: .portrait,
|
|
scaling: customScaling
|
|
)
|
|
let score: Score = makeScore(pageLayout: layout)
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
#expect(hasDefaults(xml))
|
|
|
|
// Verify <millimeters> and <tenths> values.
|
|
let mmStr: String? = extractElement(tag: "millimeters", from: xml)
|
|
let tenthsStr: String? = extractElement(tag: "tenths", from: xml)
|
|
#expect(mmStr == "5")
|
|
#expect(tenthsStr == "30")
|
|
|
|
// Page height with custom scaling: A4 portrait height = 297 mm.
|
|
// tenthsPerMM = 30/5 = 6.0; height = 297 * 6.0 = 1782.0 tenths.
|
|
let tpm: Double = 30.0 / 5.0
|
|
let expectedHeight: Double = 297.0 * tpm
|
|
|
|
let heightStr: String? = extractElement(tag: "page-height", from: xml)
|
|
if let h: Double = heightStr.flatMap(Double.init) {
|
|
#expect(abs(h - expectedHeight) < 0.001)
|
|
}
|
|
}
|
|
|
|
// MARK: - Section-based score threads pageLayout through
|
|
|
|
@Test("section-based Score with pageLayout emits <defaults>")
|
|
func sectionBasedScoreEmitsDefaults() {
|
|
let layout: PageLayout = PageLayout(size: .a4, orientation: .portrait)
|
|
let score: Score = Score(
|
|
sections: [
|
|
ScoreSection(name: "Intro", parts: [
|
|
ScorePart(name: "lead", elements: [Note(.c, 4, dur: .whole)])
|
|
])
|
|
],
|
|
bpm: 120,
|
|
pageLayout: layout
|
|
)
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(hasDefaults(xml))
|
|
}
|
|
|
|
// MARK: - DSL flat init with pageLayout
|
|
|
|
@Test("DSL flat init with pageLayout emits <defaults>")
|
|
func dslFlatInitEmitsDefaults() {
|
|
let layout: PageLayout = PageLayout(size: .letter, orientation: .portrait)
|
|
let score: Score = Score(bpm: 120, pageLayout: layout) {
|
|
Part("Piano") {
|
|
Note(.c, 4, dur: .quarter)
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(hasDefaults(xml))
|
|
}
|
|
|
|
// MARK: - DSL section init with pageLayout
|
|
|
|
@Test("DSL section init with pageLayout emits <defaults>")
|
|
func dslSectionInitEmitsDefaults() {
|
|
let layout: PageLayout = PageLayout(size: .a4, orientation: .landscape)
|
|
let score: Score = Score(bpm: 120, pageLayout: layout) {
|
|
Section("A") {
|
|
Part("lead") {
|
|
Note(.c, 4, dur: .whole)
|
|
}
|
|
}
|
|
}
|
|
let xml: String = score.toMusicXMLString()
|
|
#expect(hasDefaults(xml))
|
|
}
|
|
|
|
// MARK: - <defaults> appears before <part-list>
|
|
|
|
@Test("<defaults> block appears before <part-list> in document order")
|
|
func defaultsBeforePartList() {
|
|
let layout: PageLayout = PageLayout(size: .a4, orientation: .portrait)
|
|
let score: Score = makeScore(pageLayout: layout)
|
|
let xml: String = score.toMusicXMLString()
|
|
|
|
#expect(hasDefaults(xml))
|
|
guard let defaultsRange: Range<String.Index> = xml.range(of: "<defaults>"),
|
|
let partListRange: Range<String.Index> = xml.range(of: "<part-list>") else {
|
|
Issue.record("<defaults> or <part-list> not found in output")
|
|
return
|
|
}
|
|
#expect(defaultsRange.lowerBound < partListRange.lowerBound)
|
|
}
|
|
|
|
// MARK: - PageLayout.Margins uniform init
|
|
|
|
@Test("PageLayout.Margins uniform init sets all sides equal")
|
|
func marginsUniformInit() {
|
|
let margins: PageLayout.Margins = PageLayout.Margins(uniformMillimeters: 12.0)
|
|
#expect(margins.leftMillimeters == 12.0)
|
|
#expect(margins.rightMillimeters == 12.0)
|
|
#expect(margins.topMillimeters == 12.0)
|
|
#expect(margins.bottomMillimeters == 12.0)
|
|
}
|
|
|
|
// MARK: - PageLayout.Scaling default
|
|
|
|
@Test("PageLayout.Scaling default has 7mm = 40 tenths")
|
|
func scalingDefault() {
|
|
let s: PageLayout.Scaling = .default
|
|
#expect(s.millimeters == 7.0)
|
|
#expect(s.tenths == 40.0)
|
|
}
|
|
|
|
// MARK: - Margins default
|
|
|
|
@Test("PageLayout.Margins default has 15mm all sides")
|
|
func marginsDefault() {
|
|
let m: PageLayout.Margins = .default
|
|
#expect(m.leftMillimeters == 15.0)
|
|
#expect(m.rightMillimeters == 15.0)
|
|
#expect(m.topMillimeters == 15.0)
|
|
#expect(m.bottomMillimeters == 15.0)
|
|
}
|
|
|
|
// MARK: - tenthsPerMillimeter helper
|
|
|
|
@Test("tenthsPerMillimeter computed correctly from scaling")
|
|
func tenthsPerMillimeter() {
|
|
let layout: PageLayout = PageLayout()
|
|
// Default: 40.0 / 7.0
|
|
let expected: Double = 40.0 / 7.0
|
|
#expect(abs(layout.tenthsPerMillimeter - expected) < 1e-10)
|
|
}
|
|
|
|
// MARK: - PageSize portraitMillimeters
|
|
|
|
@Test("PageSize.portraitMillimeters returns correct values for all standard sizes")
|
|
func pageSizePortraitMillimeters() {
|
|
let letterPortrait: (width: Double, height: Double) = PageLayout.PageSize.letter.portraitMillimeters
|
|
#expect(letterPortrait.width == 215.9)
|
|
#expect(letterPortrait.height == 279.4)
|
|
|
|
let a4Portrait: (width: Double, height: Double) = PageLayout.PageSize.a4.portraitMillimeters
|
|
#expect(a4Portrait.width == 210.0)
|
|
#expect(a4Portrait.height == 297.0)
|
|
|
|
let a3Portrait: (width: Double, height: Double) = PageLayout.PageSize.a3.portraitMillimeters
|
|
#expect(a3Portrait.width == 297.0)
|
|
#expect(a3Portrait.height == 420.0)
|
|
|
|
let legalPortrait: (width: Double, height: Double) = PageLayout.PageSize.legal.portraitMillimeters
|
|
#expect(legalPortrait.width == 215.9)
|
|
#expect(legalPortrait.height == 355.6)
|
|
|
|
let tabloidPortrait: (width: Double, height: Double) = PageLayout.PageSize.tabloid.portraitMillimeters
|
|
#expect(tabloidPortrait.width == 279.4)
|
|
#expect(tabloidPortrait.height == 431.8)
|
|
}
|
|
}
|