Files
Lyrebird/Tests/LyrebirdTests/_DocTheorySnippetCheckTests.swift
T
josh 560724d116 v1.3.0 release: graceful-release fix, quickstart template + demo, documentation accuracy pass
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated
  synths were hard-freed instead of released).
- New: quickstart project template + LyrebirdQuickstartDemo.
- Docs: every example typechecked or provably illustrative (426->542); ~20
  wrong-API examples fixed; root docs brought under the compile guard.
- Fixed: NRT/Score demo Package.swift build from terminal again.

See RELEASES.md for details.
2026-07-17 14:05:01 -07:00

98 lines
4.6 KiB
Swift

// _DocTheorySnippetCheckTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
/// Golden-value check for `docs/stdlib/theory.md`'s examples.
///
/// `scripts/check-doc-snippets.sh` now typechecks 17 of that doc's 21 fences, so this suite
/// is no longer the thing keeping them *compiling*. It earns its place on the other half:
/// the doc writes results as trailing comments (`Interval.majorThird.semitones // 4`) and a
/// typecheck cannot see a `// 4` silently become a lie. Same division of labour as
/// `DistributionKindGoldenValueTests`.
///
/// When an assertion here fails, fix `docs/stdlib/theory.md` and this suite together.
@Suite("doc snippet check — docs/stdlib/theory.md examples compile + match")
struct _DocTheorySnippetCheckTests {
@Test func snippetsCompileAndMatch() throws {
// Intervals
#expect(Interval.majorThird.semitones == 4)
#expect(Interval.augmentedSecond.semitones == 3)
#expect(Interval.majorThird.diatonicSteps == 2)
let majorNinth: Interval = try Interval(number: 9, quality: .major)
#expect(majorNinth.semitones == 14)
#expect(try Interval.between(.a, .bSharp) == .augmentedSecond)
let directed: (interval: Interval, direction: IntervalDirection) =
try Interval.directed(from: .named(.c, 4), to: .named(.e, 5))
#expect(directed.direction == .up)
// Transposition
#expect(try NoteName.a.transposed(by: .augmentedSecond) == .bSharp)
#expect(try Pitch.named(.b, 4).transposed(by: .perfectFourth) == .named(.e, 5))
#expect(try Pitch.named(.c, 4).transposed(by: majorNinth) == .named(.d, 5))
// Scales
#expect(try Scale.major(.d).degrees == [.d, .e, .fSharp, .g, .a, .b, .cSharp])
#expect(try Scale.wholeTone(.c).degrees == [.c, .d, .e, .fSharp, .gSharp, .aSharp])
let cMajor: Scale = try Scale.major(.c)
#expect(cMajor.degree(of: .fSharp)?.alteration == 1)
#expect(cMajor.pitch(atDegree: 2, octave: 4) == .named(.e, 4))
// Collections
let cell: PitchCollection = PitchCollection(pitches: [.named(.c, 4), .named(.e, 4), .named(.g, 4)])
#expect(try cell.transposedChromatic(by: .majorThird).pitches
== [.named(.e, 4), .named(.gSharp, 4), .named(.b, 4)])
#expect(try cell.inverted(around: .named(.c, 4)).pitches
== [.named(.c, 4), .named(.aFlat, 3), .named(.f, 3)])
// Diatonic vs chromatic
let line: PitchCollection = PitchCollection(pitches: [.named(.c, 4), .named(.d, 4), .named(.e, 4)])
#expect(try cMajor.transposeDiatonic(line, byDegrees: 2).pitches
== [.named(.e, 4), .named(.f, 4), .named(.g, 4)])
// Mapping
let triad: PitchCollection = PitchCollection(pitches: [.named(.c, 4), .named(.e, 4), .named(.g, 4)])
#expect(try triad.mapped(from: cMajor, to: try Scale.major(.d)).pitches
== [.named(.d, 4), .named(.fSharp, 4), .named(.a, 4)])
#expect(try triad.mapped(from: cMajor, to: try Scale.aeolian(.c)).pitches
== [.named(.c, 4), .named(.eFlat, 4), .named(.g, 4)])
// Pitch -> frequency, duration -> seconds
#expect(Pitch.named(.a, 4).frequency == 440.0)
let cQuarterSharpHz: Double? = Pitch.named(.cQuarterSharp, 4).frequency
let quarterToneTolerance: Double = 0.1
if let hz: Double = cQuarterSharpHz {
#expect(abs(hz - 269.29) < quarterToneTolerance)
} else {
Issue.record("expected a frequency for C quarter-sharp")
}
let bpm: Double = 120.0
let secondsPerBeat: Double = 60.0 / bpm
#expect(NoteDuration.eighth.beats * secondsPerBeat == 0.25)
#expect(NoteDuration.quarter.dotted.beats * secondsPerBeat == 0.75)
let gSharp4: Pitch = try Pitch.named(.e, 4).transposed(by: .majorThird)
#expect(gSharp4 == .named(.gSharp, 4))
#expect(gSharp4.frequency != nil)
// Notation: build a score from theory output, round-trip MusicXML
let up: PitchCollection = try PitchCollection(pitches: [.named(.c, 4), .named(.d, 4), .named(.a, 4)])
.transposedChromatic(by: .majorThird)
let score: Score = Score(bpm: 120) {
Part("theory") {
for pitch: Pitch in up.pitches {
Note(pitch, dur: .quarter)
}
}
}
let xml: String = score.toMusicXMLString()
let decoded: Score = try Score.fromMusicXML(data: Data(xml.utf8))
#expect(decoded.parts.count == 1)
}
}