177 lines
8.7 KiB
Swift
177 lines
8.7 KiB
Swift
// GeneratedRegistryTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import LyrebirdSynth
|
|
import LyrebirdGraphDSL
|
|
|
|
@MainActor
|
|
@Suite("GeneratedDSLRegistry")
|
|
struct GeneratedRegistryTests {
|
|
|
|
/// Floor on the generated table size — a regression that halved the registry
|
|
/// (e.g. a scanner change) would trip this rather than pass silently.
|
|
private static let minimumBuiltinCount: Int = 150
|
|
|
|
@Test func registryIsPopulatedAndUnique() {
|
|
#expect(DSLBuiltinRegistry.all.count >= GeneratedRegistryTests.minimumBuiltinCount)
|
|
// Each (name, rate) is unique — no entry collapsed in the lookup table.
|
|
let totalEntries: Int = DSLBuiltinRegistry.byName.values.reduce(0) { $0 + $1.count }
|
|
#expect(totalEntries == DSLBuiltinRegistry.all.count)
|
|
}
|
|
|
|
@Test func generatorsAvailableAtBothRates() throws {
|
|
// OscSin exists at audio and control; lookup defaults to audio.
|
|
#expect(DSLBuiltinRegistry.rates(of: "OscSin") == Set([.audio, .control]))
|
|
let audio: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin")
|
|
#expect(audio.rate == .audio)
|
|
let control: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin", rate: .control)
|
|
#expect(control.rate == .control)
|
|
}
|
|
|
|
/// A doubly-nested control-only type (`Mouse.X.Control`) is registered under
|
|
/// its joined path name (`MouseX`), not the bare inner enum (`X`), and
|
|
/// constructs via the full `Mouse.X.Control` path.
|
|
@Test func doublyNestedTypeUsesJoinedNameAndFullConstructor() throws {
|
|
#expect(DSLBuiltinRegistry.rates(of: "MouseX") == Set([.control]))
|
|
// The bare inner name must NOT leak into the flat DSL namespace.
|
|
#expect(DSLBuiltinRegistry.rates(of: "X").isEmpty)
|
|
let mouseX: DSLBuiltin = try DSLBuiltinRegistry.lookup("MouseX", rate: .control)
|
|
#expect(mouseX.rate == .control)
|
|
// Constructs without throwing (no args; reads the mouse input registry).
|
|
_ = try mouseX.make([:])
|
|
}
|
|
|
|
@Test func unavailableRateThrows() throws {
|
|
// Pan2 is audio-only (no ControlGenerator variant).
|
|
#expect(DSLBuiltinRegistry.rates(of: "Pan2") == Set([.audio]))
|
|
#expect(throws: DSLBuildError.rateUnavailable(builtin: "Pan2", rate: "control")) {
|
|
_ = try DSLBuiltinRegistry.lookup("Pan2", rate: .control)
|
|
}
|
|
}
|
|
|
|
@Test func controlRateBuiltinRenders() throws {
|
|
// A control-rate OscSin drives an audio OscSin's frequency — the kind of
|
|
// modulation nesting the DSL will produce.
|
|
let lfoControl: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin", rate: .control)
|
|
let audio: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin")
|
|
let lfo: any ValidGeneratorInput = try lfoControl.make(["freq": sig(4.0), "mul": sig(50.0), "add": sig(440.0)])
|
|
let graph: any ValidGeneratorInput = try audio.make(["freq": sig(lfo), "mul": sig(0.1)])
|
|
let engine: SynthesisEngine = makeDSLTestEngine()
|
|
try engine.start()
|
|
try engine.add(ClosureSynthesizer { graph })
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(busHasSignal(engine, bus: 0))
|
|
}
|
|
|
|
@Test func wellKnownGeneratorsArePresent() throws {
|
|
for name: String in ["OscSin", "OscSawtooth", "OscSquare", "WhiteNoise", "Pan2"] {
|
|
let builtin: DSLBuiltin = try DSLBuiltinRegistry.lookup(name)
|
|
#expect(builtin.name == name)
|
|
}
|
|
}
|
|
|
|
@Test func lookupOfUnknownNameThrows() {
|
|
#expect(throws: DSLBuildError.unknownBuiltin("NotAGenerator")) {
|
|
_ = try DSLBuiltinRegistry.lookup("NotAGenerator")
|
|
}
|
|
}
|
|
|
|
@Test func requiredParameterIsReportedAndEnforced() throws {
|
|
// OscSin's `freq` has no default, so it is required.
|
|
let osc: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin")
|
|
let freq: DSLParameter? = osc.parameters.first { $0.label == "freq" }
|
|
#expect(freq?.hasDefault == false)
|
|
#expect(throws: DSLBuildError.missingArgument(builtin: "OscSin", label: "freq")) {
|
|
_ = try osc.make([:])
|
|
}
|
|
}
|
|
|
|
/// The generated closure builds a real generator that renders audio.
|
|
@Test func generatedBuiltinRendersAudio() throws {
|
|
let osc: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin")
|
|
let graph: any ValidGeneratorInput = try osc.make(["freq": sig(440.0), "mul": sig(0.1)])
|
|
let engine: SynthesisEngine = makeDSLTestEngine()
|
|
try engine.start()
|
|
try engine.add(ClosureSynthesizer { graph })
|
|
engine.processBlock(inputSamples: [])
|
|
let anyNonZero: Bool = (0..<engine.blockSize).contains { engine.audioBuses[0][$0] != 0.0 }
|
|
#expect(anyNonZero)
|
|
}
|
|
|
|
/// A generator taking a signal input builds when handed another generator —
|
|
/// the nesting the DSL parser will produce.
|
|
@Test func nestedGeneratorArgumentBuilds() throws {
|
|
let osc: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin")
|
|
let pan: DSLBuiltin = try DSLBuiltinRegistry.lookup("Pan2")
|
|
let source: any ValidGeneratorInput = try osc.make(["freq": sig(440.0), "mul": sig(0.1)])
|
|
let graph: any ValidGeneratorInput = try pan.make(["input": sig(source), "pan": sig(0.0)])
|
|
let engine: SynthesisEngine = makeDSLTestEngine()
|
|
try engine.start()
|
|
try engine.add(ClosureSynthesizer { graph })
|
|
engine.processBlock(inputSamples: [])
|
|
// Pan2 is stereo — both buses should carry signal.
|
|
let leftNonZero: Bool = (0..<engine.blockSize).contains { engine.audioBuses[0][$0] != 0.0 }
|
|
let rightNonZero: Bool = (0..<engine.blockSize).contains { engine.audioBuses[1][$0] != 0.0 }
|
|
#expect(leftNonZero)
|
|
#expect(rightNonZero)
|
|
}
|
|
|
|
/// A parameter typed `Double` on the Swift init rejects a signal, since a
|
|
/// literal number is required there.
|
|
@Test func numericParameterRejectsSignal() throws {
|
|
let osc: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin")
|
|
let signal: any ValidGeneratorInput = try osc.make(["freq": sig(440.0)])
|
|
let delay: DSLBuiltin = try DSLBuiltinRegistry.lookup("DelayLine")
|
|
// `maxDelay` is a Double on the init — handing it a generator must throw.
|
|
#expect(throws: DSLBuildError.expectedNumber(builtin: "DelayLine", label: "maxDelay")) {
|
|
_ = try delay.make(["input": sig(signal), "maxDelay": sig(signal), "delay": sig(0.1)])
|
|
}
|
|
}
|
|
|
|
/// A generated builtin with a positional Swift argument (`Abs(_ signal:)`)
|
|
/// binds its unlabeled argument correctly through the emitted call.
|
|
@Test func positionalBuiltinBindsAndRenders() throws {
|
|
let osc: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin")
|
|
let abs: DSLBuiltin = try DSLBuiltinRegistry.lookup("Abs")
|
|
let source: any ValidGeneratorInput = try osc.make(["freq": sig(440.0), "mul": sig(0.5)])
|
|
let graph: any ValidGeneratorInput = try abs.make(["signal": sig(source)])
|
|
let engine: SynthesisEngine = makeDSLTestEngine()
|
|
try engine.start()
|
|
try engine.add(ClosureSynthesizer { graph })
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(busHasSignal(engine, bus: 0))
|
|
}
|
|
|
|
/// A parameter whose Swift default is a named internal constant
|
|
/// (`LeakDC.coef = leakDCDefaultAudioCoef`) is exposed with a default and
|
|
/// builds when omitted — proves the code-gen's constant inlining works.
|
|
@Test func namedConstantDefaultIsUsable() throws {
|
|
let leak: DSLBuiltin = try DSLBuiltinRegistry.lookup("LeakDC")
|
|
let coef: DSLParameter? = leak.parameters.first { $0.label == "coef" }
|
|
#expect(coef?.hasDefault == true)
|
|
let osc: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin")
|
|
let source: any ValidGeneratorInput = try osc.make(["freq": sig(440.0), "mul": sig(0.5)])
|
|
// `coef` omitted — must not throw a missing-argument error.
|
|
let graph: any ValidGeneratorInput = try leak.make(["input": sig(source)])
|
|
let engine: SynthesisEngine = makeDSLTestEngine()
|
|
try engine.start()
|
|
try engine.add(ClosureSynthesizer { graph })
|
|
engine.processBlock(inputSamples: [])
|
|
#expect(busHasSignal(engine, bus: 0))
|
|
}
|
|
|
|
/// Lock in which overload of a multi-init class the generator exposes.
|
|
/// `RBJBandPass` declares its `bandwidth:` init first, so that is the one
|
|
/// shipped; a future reorder that silently flipped it to `q:` trips here.
|
|
@Test func multiInitClassExposesFirstOverload() throws {
|
|
let bp: DSLBuiltin = try DSLBuiltinRegistry.lookup("RBJBandPass")
|
|
let labels: [String] = bp.parameters.map { $0.label }
|
|
#expect(labels.contains("bandwidth"))
|
|
#expect(!labels.contains("q"))
|
|
}
|
|
}
|